blob: 329576fa9496581ef465d567f1c7dc3099e920fe [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
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000079#if defined(__GNUC__) && !defined(DEBUG)
80#define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
81#elif defined(_MSC_VER) && !defined(DEBUG)
82#define V8_INLINE(declarator) __forceinline declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000083#else
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000084#define V8_INLINE(declarator) inline declarator
85#endif
86
87#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
88#define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
89#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
90#define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
91#else
92#define V8_DEPRECATED(declarator) declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000093#endif
94
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000096 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 */
98namespace v8 {
99
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000100class AccessorInfo;
101class AccessorSignature;
102class Array;
103class Boolean;
104class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105class Context;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000106class Data;
107class Date;
108class DeclaredAccessorDescriptor;
109class External;
110class Function;
111class FunctionTemplate;
112class ImplementationUtilities;
113class Int32;
114class Integer;
115class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000117class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118class Object;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000119class ObjectOperationDescriptor;
120class ObjectTemplate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121class Primitive;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000122class RawOperationDescriptor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123class Signature;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000124class StackFrame;
125class StackTrace;
126class String;
127class StringObject;
128class Uint32;
129class Utils;
130class Value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131template <class T> class Handle;
132template <class T> class Local;
133template <class T> class Persistent;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000135namespace internal {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000136class Arguments;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000137class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000138class HeapObject;
139class Isolate;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000140class Object;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000141}
142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000144// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145
146
147/**
148 * A weak reference callback function.
149 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000150 * This callback should either explicitly invoke Dispose on |object| if
151 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
152 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153 * \param object the weak global object to be reclaimed by the garbage collector
154 * \param parameter the value passed in when making the weak global object
155 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000156typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 void* parameter);
158
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000159// TODO(svenpanne) Temporary definition until Chrome is in sync.
160typedef void (*NearDeathCallback)(Isolate* isolate,
161 Persistent<Value> object,
162 void* parameter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000164// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000166#define TYPE_CHECK(T, S) \
167 while (false) { \
168 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 }
170
171/**
172 * An object reference managed by the v8 garbage collector.
173 *
174 * All objects returned from v8 have to be tracked by the garbage
175 * collector so that it knows that the objects are still alive. Also,
176 * because the garbage collector may move objects, it is unsafe to
177 * point directly to an object. Instead, all objects are stored in
178 * handles which are known by the garbage collector and updated
179 * whenever an object moves. Handles should always be passed by value
180 * (except in cases like out-parameters) and they should never be
181 * allocated on the heap.
182 *
183 * There are two types of handles: local and persistent handles.
184 * Local handles are light-weight and transient and typically used in
185 * local operations. They are managed by HandleScopes. Persistent
186 * handles can be used when storing objects across several independent
187 * operations and have to be explicitly deallocated when they're no
188 * longer used.
189 *
190 * It is safe to extract the object stored in the handle by
191 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000192 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 * behind the scenes and the same rules apply to these values as to
194 * their handles.
195 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000196template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198 /**
199 * Creates an empty handle.
200 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000201 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202
203 /**
204 * Creates a new handle for the specified value.
205 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000206 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207
208 /**
209 * Creates a handle for the contents of the specified handle. This
210 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000211 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000213 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214 * between compatible handles, for instance assigning a
215 * Handle<String> to a variable declared as Handle<Value>, is legal
216 * because String is a subclass of Value.
217 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000218 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 : val_(reinterpret_cast<T*>(*that)) {
220 /**
221 * This check fails when trying to convert between incompatible
222 * handles. For example, converting from a Handle<String> to a
223 * Handle<Number>.
224 */
225 TYPE_CHECK(T, S);
226 }
227
228 /**
229 * Returns true if the handle is empty.
230 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000231 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 /**
234 * Sets the handle to be empty. IsEmpty() will then return true.
235 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000236 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000237
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000238 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000239
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000240 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241
242 /**
243 * Checks whether two handles are the same.
244 * Returns true if both are empty, or if the objects
245 * to which they refer are identical.
246 * The handles' references are not checked.
247 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000248 template <class S> V8_INLINE(bool operator==(Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000249 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
250 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 if (a == 0) return b == 0;
252 if (b == 0) return false;
253 return *a == *b;
254 }
255
256 /**
257 * Checks whether two handles are different.
258 * Returns true if only one of the handles is empty, or if
259 * the objects to which they refer are different.
260 * The handles' references are not checked.
261 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000262 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 return !operator==(that);
264 }
265
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000266 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000267#ifdef V8_ENABLE_CHECKS
268 // If we're going to perform the type check then we have to check
269 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000271#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 return Handle<T>(T::Cast(*that));
273 }
274
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000275 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000276 return Handle<S>::Cast(*this);
277 }
278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 private:
280 T* val_;
281};
282
283
284/**
285 * A light-weight stack-allocated object handle. All operations
286 * that return objects from within v8 return them in local handles. They
287 * are created within HandleScopes, and all local handles allocated within a
288 * handle scope are destroyed when the handle scope is destroyed. Hence it
289 * is not necessary to explicitly deallocate local handles.
290 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000291template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000293 V8_INLINE(Local());
294 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 : Handle<T>(reinterpret_cast<T*>(*that)) {
296 /**
297 * This check fails when trying to convert between incompatible
298 * handles. For example, converting from a Handle<String> to a
299 * Handle<Number>.
300 */
301 TYPE_CHECK(T, S);
302 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000303 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
304 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000305#ifdef V8_ENABLE_CHECKS
306 // If we're going to perform the type check then we have to check
307 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000309#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 return Local<T>(T::Cast(*that));
311 }
312
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000313 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000314 return Local<S>::Cast(*this);
315 }
316
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000317 /**
318 * Create a local handle for the content of another handle.
319 * The referee is kept alive by the local handle even when
320 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000322 V8_INLINE(static Local<T> New(Handle<T> that));
323 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324};
325
326
327/**
328 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000329 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 * allocated, a Persistent handle remains valid until it is explicitly
331 * disposed.
332 *
333 * A persistent handle contains a reference to a storage cell within
334 * the v8 engine which holds an object value and which is updated by
335 * the garbage collector whenever the object is moved. A new storage
336 * cell can be created using Persistent::New and existing handles can
337 * be disposed using Persistent::Dispose. Since persistent handles
338 * are passed by value you may have many persistent handle objects
339 * that point to the same storage cell. For instance, if you pass a
340 * persistent handle as an argument to a function you will not get two
341 * different storage cells but rather two references to the same
342 * storage cell.
343 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000344template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 /**
347 * Creates an empty persistent handle that doesn't point to any
348 * storage cell.
349 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000350 V8_INLINE(Persistent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351
352 /**
353 * Creates a persistent handle for the same storage cell as the
354 * specified handle. This constructor allows you to pass persistent
355 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000356 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000358 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 * between compatible persistent handles, for instance assigning a
360 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000361 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000363 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 : Handle<T>(reinterpret_cast<T*>(*that)) {
365 /**
366 * This check fails when trying to convert between incompatible
367 * handles. For example, converting from a Handle<String> to a
368 * Handle<Number>.
369 */
370 TYPE_CHECK(T, S);
371 }
372
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000373 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000375 /**
376 * "Casts" a plain handle which is known to be a persistent handle
377 * to a persistent handle.
378 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000379 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 : Handle<T>(*that) { }
381
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000382 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000383#ifdef V8_ENABLE_CHECKS
384 // If we're going to perform the type check then we have to check
385 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000387#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388 return Persistent<T>(T::Cast(*that));
389 }
390
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000391 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000392 return Persistent<S>::Cast(*this);
393 }
394
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000395 /** Deprecated. Use Isolate version instead. */
396 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
397
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000399 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000401 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
402
403 /** Deprecated. Use Isolate version instead. */
404 V8_DEPRECATED(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405
406 /**
407 * Releases the storage cell referenced by this persistent handle.
408 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000409 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410 * cell remain and IsEmpty will still return false.
411 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000412 V8_INLINE(void Dispose(Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000414 /** Deprecated. Use Isolate version instead. */
415 V8_DEPRECATED(void MakeWeak(void* parameters,
416 WeakReferenceCallback callback));
417
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418 /**
419 * Make the reference to this object weak. When only weak handles
420 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000421 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422 * it the object reference and the given parameters.
423 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000424 V8_INLINE(void MakeWeak(Isolate* isolate,
425 void* parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000426 NearDeathCallback callback));
427
428 /** Deprecated. Use Isolate version instead. */
429 V8_DEPRECATED(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000431 /** Clears the weak reference to this object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000432 V8_INLINE(void ClearWeak(Isolate* isolate));
433
434 /** Deprecated. Use Isolate version instead. */
435 V8_DEPRECATED(void MarkIndependent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436
437 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000438 * Marks the reference to this object independent. Garbage collector is free
439 * to ignore any object groups containing this object. Weak callback for an
440 * independent handle should not assume that it will be preceded by a global
441 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000442 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000443 V8_INLINE(void MarkIndependent(Isolate* isolate));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000444
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000445 /** Deprecated. Use Isolate version instead. */
446 V8_DEPRECATED(void MarkPartiallyDependent());
447
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000448 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000449 * Marks the reference to this object partially dependent. Partially dependent
450 * handles only depend on other partially dependent handles and these
451 * dependencies are provided through object groups. It provides a way to build
452 * smaller object groups for young objects that represent only a subset of all
453 * external dependencies. This mark is automatically cleared after each
454 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000455 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000456 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000457
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000458 /** Deprecated. Use Isolate version instead. */
459 V8_DEPRECATED(bool IsIndependent() const);
460
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000461 /** Returns true if this handle was previously marked as independent. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000462 V8_INLINE(bool IsIndependent(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000463
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000464 /** Deprecated. Use Isolate version instead. */
465 V8_DEPRECATED(bool IsNearDeath() const);
466
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000467 /** Checks if the handle holds the only reference to an object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000468 V8_INLINE(bool IsNearDeath(Isolate* isolate) const);
469
470 /** Deprecated. Use Isolate version instead. */
471 V8_DEPRECATED(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000473 /** Returns true if the handle's reference is weak. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000474 V8_INLINE(bool IsWeak(Isolate* isolate) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000476 /** Deprecated. Use Isolate version instead. */
477 V8_DEPRECATED(void SetWrapperClassId(uint16_t class_id));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000478
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000479 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000480 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
481 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000482 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000483 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id));
484
485 /** Deprecated. Use Isolate version instead. */
486 V8_DEPRECATED(uint16_t WrapperClassId() const);
487
488 /**
489 * Returns the class ID previously assigned to this handle or 0 if no class ID
490 * was previously assigned.
491 */
492 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000493
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 private:
495 friend class ImplementationUtilities;
496 friend class ObjectTemplate;
497};
498
499
v8.team.kasperl727e9952008-09-02 14:56:44 +0000500 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 * A stack-allocated class that governs a number of local handles.
502 * After a handle scope has been created, all local handles will be
503 * allocated within that handle scope until either the handle scope is
504 * deleted or another handle scope is created. If there is already a
505 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000506 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507 * new handles will again be allocated in the original handle scope.
508 *
509 * After the handle scope of a local handle has been deleted the
510 * garbage collector will no longer track the object stored in the
511 * handle and may deallocate it. The behavior of accessing a handle
512 * for which the handle scope has been deleted is undefined.
513 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000514class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000516 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000517 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000519 HandleScope(Isolate* isolate);
520
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000521 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522
523 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 * Closes the handle scope and returns the value as a handle in the
525 * previous scope, which is the new current scope after the call.
526 */
527 template <class T> Local<T> Close(Handle<T> value);
528
529 /**
530 * Counts the number of allocated handles.
531 */
532 static int NumberOfHandles();
533
534 /**
535 * Creates a new handle with the given value.
536 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000537 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000538 static internal::Object** CreateHandle(internal::Isolate* isolate,
539 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000540 // Faster version, uses HeapObject to obtain the current Isolate.
541 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542
543 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000544 // Make it hard to create heap-allocated or illegal handle scopes by
545 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 HandleScope(const HandleScope&);
547 void operator=(const HandleScope&);
548 void* operator new(size_t size);
549 void operator delete(void*, size_t);
550
ager@chromium.org3811b432009-10-28 14:53:37 +0000551 // This Data class is accessible internally as HandleScopeData through a
552 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000553 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000555 internal::Object** next;
556 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000557 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000558 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000560 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561 }
562 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000563
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000564 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000565 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000567 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000568 internal::Object** prev_next_;
569 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000571 // Allow for the active closing of HandleScopes which allows to pass a handle
572 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000574 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 friend class ImplementationUtilities;
577};
578
579
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000580// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581
582
583/**
584 * The superclass of values and API object templates.
585 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000586class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 private:
588 Data();
589};
590
591
592/**
593 * Pre-compilation data that can be associated with a script. This
594 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000595 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000596 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000598class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 public:
600 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000601
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000602 /**
603 * Pre-compiles the specified script (context-independent).
604 *
605 * \param input Pointer to UTF-8 script source code.
606 * \param length Length of UTF-8 script source code.
607 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000610 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000611 * Pre-compiles the specified script (context-independent).
612 *
613 * NOTE: Pre-compilation using this method cannot happen on another thread
614 * without using Lockers.
615 *
616 * \param source Script source code.
617 */
618 static ScriptData* PreCompile(Handle<String> source);
619
620 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000621 * Load previous pre-compilation data.
622 *
623 * \param data Pointer to data returned by a call to Data() of a previous
624 * ScriptData. Ownership is not transferred.
625 * \param length Length of data.
626 */
627 static ScriptData* New(const char* data, int length);
628
629 /**
630 * Returns the length of Data().
631 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000633
634 /**
635 * Returns a serialized representation of this ScriptData that can later be
636 * passed to New(). NOTE: Serialized data is platform-dependent.
637 */
638 virtual const char* Data() = 0;
639
640 /**
641 * Returns true if the source code could not be parsed.
642 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000643 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644};
645
646
647/**
648 * The origin, within a file, of a script.
649 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000650class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000652 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000653 Handle<Value> resource_name,
654 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000655 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 : resource_name_(resource_name),
657 resource_line_offset_(resource_line_offset),
658 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000659 V8_INLINE(Handle<Value> ResourceName() const);
660 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
661 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000663 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 Handle<Integer> resource_line_offset_;
665 Handle<Integer> resource_column_offset_;
666};
667
668
669/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000670 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000672class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000675 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000676 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000677 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000678 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000679 * when New() returns
680 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
681 * using pre_data speeds compilation if it's done multiple times.
682 * Owned by caller, no references are kept when New() returns.
683 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000684 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000685 * available to compile event handlers.
686 * \return Compiled script object (context independent; when run it
687 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000689 static Local<Script> New(Handle<String> source,
690 ScriptOrigin* origin = NULL,
691 ScriptData* pre_data = NULL,
692 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693
mads.s.agercbaa0602008-08-14 13:41:48 +0000694 /**
695 * Compiles the specified script using the specified file name
696 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000697 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000698 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000699 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000700 * as the script's origin.
701 * \return Compiled script object (context independent; when run it
702 * will use the currently entered context).
703 */
704 static Local<Script> New(Handle<String> source,
705 Handle<Value> file_name);
706
707 /**
708 * Compiles the specified script (bound to current context).
709 *
710 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000711 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000712 * when Compile() returns
713 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
714 * using pre_data speeds compilation if it's done multiple times.
715 * Owned by caller, no references are kept when Compile() returns.
716 * \param script_data Arbitrary data associated with script. Using
717 * this has same effect as calling SetData(), but makes data available
718 * earlier (i.e. to compile event handlers).
719 * \return Compiled script object, bound to the context that was active
720 * when this function was called. When run it will always use this
721 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000722 */
723 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000724 ScriptOrigin* origin = NULL,
725 ScriptData* pre_data = NULL,
726 Handle<String> script_data = Handle<String>());
727
728 /**
729 * Compiles the specified script using the specified file name
730 * object (typically a string) as the script's origin.
731 *
732 * \param source Script source code.
733 * \param file_name File name to use as script's origin
734 * \param script_data Arbitrary data associated with script. Using
735 * this has same effect as calling SetData(), but makes data available
736 * earlier (i.e. to compile event handlers).
737 * \return Compiled script object, bound to the context that was active
738 * when this function was called. When run it will always use this
739 * context.
740 */
741 static Local<Script> Compile(Handle<String> source,
742 Handle<Value> file_name,
743 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000744
v8.team.kasperl727e9952008-09-02 14:56:44 +0000745 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000746 * Runs the script returning the resulting value. If the script is
747 * context independent (created using ::New) it will be run in the
748 * currently entered context. If it is context specific (created
749 * using ::Compile) it will be run in the context in which it was
750 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000751 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000753
754 /**
755 * Returns the script id value.
756 */
757 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000758
759 /**
760 * Associate an additional data object with the script. This is mainly used
761 * with the debugger as this data object is only available through the
762 * debugger API.
763 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000764 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765};
766
767
768/**
769 * An error message.
770 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000771class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000773 Local<String> Get() const;
774 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000776 /**
777 * Returns the resource name for the script from where the function causing
778 * the error originates.
779 */
ager@chromium.org32912102009-01-16 10:38:43 +0000780 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000782 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000783 * Returns the resource data for the script from where the function causing
784 * the error originates.
785 */
786 Handle<Value> GetScriptData() const;
787
788 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000789 * Exception stack trace. By default stack traces are not captured for
790 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
791 * to change this option.
792 */
793 Handle<StackTrace> GetStackTrace() const;
794
795 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000796 * Returns the number, 1-based, of the line where the error occurred.
797 */
ager@chromium.org32912102009-01-16 10:38:43 +0000798 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000800 /**
801 * Returns the index within the script of the first character where
802 * the error occurred.
803 */
ager@chromium.org32912102009-01-16 10:38:43 +0000804 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000805
806 /**
807 * Returns the index within the script of the last character where
808 * the error occurred.
809 */
ager@chromium.org32912102009-01-16 10:38:43 +0000810 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000811
812 /**
813 * Returns the index within the line of the first character where
814 * the error occurred.
815 */
ager@chromium.org32912102009-01-16 10:38:43 +0000816 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000817
818 /**
819 * Returns the index within the line of the last character where
820 * the error occurred.
821 */
ager@chromium.org32912102009-01-16 10:38:43 +0000822 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000823
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000824 // TODO(1245381): Print to a string instead of on a FILE.
825 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000826
827 static const int kNoLineNumberInfo = 0;
828 static const int kNoColumnInfo = 0;
829};
830
831
832/**
833 * Representation of a JavaScript stack trace. The information collected is a
834 * snapshot of the execution stack and the information remains valid after
835 * execution continues.
836 */
837class V8EXPORT StackTrace {
838 public:
839 /**
840 * Flags that determine what information is placed captured for each
841 * StackFrame when grabbing the current stack trace.
842 */
843 enum StackTraceOptions {
844 kLineNumber = 1,
845 kColumnOffset = 1 << 1 | kLineNumber,
846 kScriptName = 1 << 2,
847 kFunctionName = 1 << 3,
848 kIsEval = 1 << 4,
849 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000850 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000851 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000852 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000853 };
854
855 /**
856 * Returns a StackFrame at a particular index.
857 */
858 Local<StackFrame> GetFrame(uint32_t index) const;
859
860 /**
861 * Returns the number of StackFrames.
862 */
863 int GetFrameCount() const;
864
865 /**
866 * Returns StackTrace as a v8::Array that contains StackFrame objects.
867 */
868 Local<Array> AsArray();
869
870 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000871 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000872 *
873 * \param frame_limit The maximum number of stack frames we want to capture.
874 * \param options Enumerates the set of things we will capture for each
875 * StackFrame.
876 */
877 static Local<StackTrace> CurrentStackTrace(
878 int frame_limit,
879 StackTraceOptions options = kOverview);
880};
881
882
883/**
884 * A single JavaScript stack frame.
885 */
886class V8EXPORT StackFrame {
887 public:
888 /**
889 * Returns the number, 1-based, of the line for the associate function call.
890 * This method will return Message::kNoLineNumberInfo if it is unable to
891 * retrieve the line number, or if kLineNumber was not passed as an option
892 * when capturing the StackTrace.
893 */
894 int GetLineNumber() const;
895
896 /**
897 * Returns the 1-based column offset on the line for the associated function
898 * call.
899 * This method will return Message::kNoColumnInfo if it is unable to retrieve
900 * the column number, or if kColumnOffset was not passed as an option when
901 * capturing the StackTrace.
902 */
903 int GetColumn() const;
904
905 /**
906 * Returns the name of the resource that contains the script for the
907 * function for this StackFrame.
908 */
909 Local<String> GetScriptName() const;
910
911 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000912 * Returns the name of the resource that contains the script for the
913 * function for this StackFrame or sourceURL value if the script name
914 * is undefined and its source ends with //@ sourceURL=... string.
915 */
916 Local<String> GetScriptNameOrSourceURL() const;
917
918 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000919 * Returns the name of the function associated with this stack frame.
920 */
921 Local<String> GetFunctionName() const;
922
923 /**
924 * Returns whether or not the associated function is compiled via a call to
925 * eval().
926 */
927 bool IsEval() const;
928
929 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000930 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000931 * constructor via "new".
932 */
933 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934};
935
936
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000937// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938
939
940/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000941 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000943class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 /**
946 * Returns true if this value is the undefined value. See ECMA-262
947 * 4.3.10.
948 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000949 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950
951 /**
952 * Returns true if this value is the null value. See ECMA-262
953 * 4.3.11.
954 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000955 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956
957 /**
958 * Returns true if this value is true.
959 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000960 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961
962 /**
963 * Returns true if this value is false.
964 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000965 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966
967 /**
968 * Returns true if this value is an instance of the String type.
969 * See ECMA-262 8.4.
970 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000971 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972
973 /**
974 * Returns true if this value is a function.
975 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000976 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977
978 /**
979 * Returns true if this value is an array.
980 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000981 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982
v8.team.kasperl727e9952008-09-02 14:56:44 +0000983 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 * Returns true if this value is an object.
985 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000986 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987
v8.team.kasperl727e9952008-09-02 14:56:44 +0000988 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989 * Returns true if this value is boolean.
990 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000991 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000992
v8.team.kasperl727e9952008-09-02 14:56:44 +0000993 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994 * Returns true if this value is a number.
995 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000996 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997
v8.team.kasperl727e9952008-09-02 14:56:44 +0000998 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999 * Returns true if this value is external.
1000 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001001 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002
v8.team.kasperl727e9952008-09-02 14:56:44 +00001003 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004 * Returns true if this value is a 32-bit signed integer.
1005 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001006 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001008 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001009 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001010 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001011 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001012
1013 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001014 * Returns true if this value is a Date.
1015 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001016 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001017
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001018 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001019 * Returns true if this value is a Boolean object.
1020 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001021 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001022
1023 /**
1024 * Returns true if this value is a Number object.
1025 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001026 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001027
1028 /**
1029 * Returns true if this value is a String object.
1030 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001031 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001032
1033 /**
1034 * Returns true if this value is a NativeError.
1035 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001036 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001037
1038 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001039 * Returns true if this value is a RegExp.
1040 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001041 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001042
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001043 Local<Boolean> ToBoolean() const;
1044 Local<Number> ToNumber() const;
1045 Local<String> ToString() const;
1046 Local<String> ToDetailString() const;
1047 Local<Object> ToObject() const;
1048 Local<Integer> ToInteger() const;
1049 Local<Uint32> ToUint32() const;
1050 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051
1052 /**
1053 * Attempts to convert a string to an array index.
1054 * Returns an empty handle if the conversion fails.
1055 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001056 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001058 bool BooleanValue() const;
1059 double NumberValue() const;
1060 int64_t IntegerValue() const;
1061 uint32_t Uint32Value() const;
1062 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063
1064 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001065 bool Equals(Handle<Value> that) const;
1066 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001067
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001068 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001069 V8_INLINE(bool QuickIsUndefined() const);
1070 V8_INLINE(bool QuickIsNull() const);
1071 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001072 bool FullIsUndefined() const;
1073 bool FullIsNull() const;
1074 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075};
1076
1077
1078/**
1079 * The superclass of primitive values. See ECMA-262 4.3.2.
1080 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001081class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082
1083
1084/**
1085 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1086 * or false value.
1087 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001088class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001090 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001091 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092};
1093
1094
1095/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001096 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001098class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001100 enum Encoding {
1101 UNKNOWN_ENCODING = 0x1,
1102 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001103 ASCII_ENCODING = 0x4,
1104 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001105 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001106 /**
1107 * Returns the number of characters in this string.
1108 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001109 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110
v8.team.kasperl727e9952008-09-02 14:56:44 +00001111 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001112 * Returns the number of bytes in the UTF-8 encoded
1113 * representation of this string.
1114 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001115 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001116
1117 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001118 * A fast conservative check for non-ASCII characters. May
1119 * return true even for ASCII strings, but if it returns
1120 * false you can be sure that all characters are in the range
1121 * 0-127.
1122 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001123 bool MayContainNonAscii() const;
1124
1125 /**
1126 * Returns whether this string contains only one byte data.
1127 */
1128 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001129
1130 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001131 * Write the contents of the string to an external buffer.
1132 * If no arguments are given, expects the buffer to be large
1133 * enough to hold the entire string and NULL terminator. Copies
1134 * the contents of the string and the NULL terminator into the
1135 * buffer.
1136 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001137 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1138 * before the end of the buffer.
1139 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001140 * Copies up to length characters into the output buffer.
1141 * Only null-terminates if there is enough space in the buffer.
1142 *
1143 * \param buffer The buffer into which the string will be copied.
1144 * \param start The starting position within the string at which
1145 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001146 * \param length The number of characters to copy from the string. For
1147 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001148 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001149 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001150 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001151 * \return The number of characters copied to the buffer excluding the null
1152 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001153 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001154 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001155 enum WriteOptions {
1156 NO_OPTIONS = 0,
1157 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001158 NO_NULL_TERMINATION = 2,
1159 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001160 };
1161
lrn@chromium.org34e60782011-09-15 07:25:40 +00001162 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001163 int Write(uint16_t* buffer,
1164 int start = 0,
1165 int length = -1,
1166 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001167 // ASCII characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001168 int WriteAscii(char* buffer,
1169 int start = 0,
1170 int length = -1,
1171 int options = NO_OPTIONS) const;
1172 // One byte characters.
1173 int WriteOneByte(uint8_t* buffer,
1174 int start = 0,
1175 int length = -1,
1176 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001177 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001178 int WriteUtf8(char* buffer,
1179 int length = -1,
1180 int* nchars_ref = NULL,
1181 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182
v8.team.kasperl727e9952008-09-02 14:56:44 +00001183 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001184 * A zero length string.
1185 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001186 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001187 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001188
1189 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001190 * Returns true if the string is external
1191 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001192 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193
v8.team.kasperl727e9952008-09-02 14:56:44 +00001194 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001195 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001196 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001197 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001198
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001199 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001200 public:
1201 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001202
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001203 protected:
1204 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001205
1206 /**
1207 * Internally V8 will call this Dispose method when the external string
1208 * resource is no longer needed. The default implementation will use the
1209 * delete operator. This method can be overridden in subclasses to
1210 * control how allocated external string resources are disposed.
1211 */
1212 virtual void Dispose() { delete this; }
1213
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001214 private:
1215 // Disallow copying and assigning.
1216 ExternalStringResourceBase(const ExternalStringResourceBase&);
1217 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001218
1219 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001220 };
1221
v8.team.kasperl727e9952008-09-02 14:56:44 +00001222 /**
1223 * An ExternalStringResource is a wrapper around a two-byte string
1224 * buffer that resides outside V8's heap. Implement an
1225 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001226 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001227 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001228 class V8EXPORT ExternalStringResource
1229 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230 public:
1231 /**
1232 * Override the destructor to manage the life cycle of the underlying
1233 * buffer.
1234 */
1235 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001236
1237 /**
1238 * The string data from the underlying buffer.
1239 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001241
1242 /**
1243 * The length of the string. That is, the number of two-byte characters.
1244 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 protected:
1248 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 };
1250
1251 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001252 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001253 * string buffer that resides outside V8's heap. Implement an
1254 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001255 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001256 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001257 * UTF-8, which would require special treatment internally in the
1258 * engine and, in the case of UTF-8, do not allow efficient indexing.
1259 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001260 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001262 class V8EXPORT ExternalAsciiStringResource
1263 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 public:
1265 /**
1266 * Override the destructor to manage the life cycle of the underlying
1267 * buffer.
1268 */
1269 virtual ~ExternalAsciiStringResource() {}
1270 /** The string data from the underlying buffer.*/
1271 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001272 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 virtual size_t length() const = 0;
1274 protected:
1275 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 };
1277
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001278 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1279
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001281 * If the string is an external string, return the ExternalStringResourceBase
1282 * regardless of the encoding, otherwise return NULL. The encoding of the
1283 * string is returned in encoding_out.
1284 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001285 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1286 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001287
1288 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001289 * Get the ExternalStringResource for an external string. Returns
1290 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001292 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293
1294 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001295 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001296 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001298 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001300 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301
1302 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001303 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001304 * The second parameter 'length' gives the buffer length. If omitted,
1305 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001307 static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308
lrn@chromium.org34e60782011-09-15 07:25:40 +00001309 /** Allocates a new string from 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001310 static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311
1312 /** Creates a symbol. Returns one if it exists already.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001313 static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314
v8.team.kasperl727e9952008-09-02 14:56:44 +00001315 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001316 * Creates a new string by concatenating the left and the right strings
1317 * passed in as parameters.
1318 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001319 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001320
1321 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001322 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001323 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001324 * resource will be disposed by calling its Dispose method. The caller of
1325 * this function should not otherwise delete or modify the resource. Neither
1326 * should the underlying buffer be deallocated or modified except through the
1327 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001328 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001329 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001330
ager@chromium.org6f10e412009-02-13 10:11:16 +00001331 /**
1332 * Associate an external string resource with this string by transforming it
1333 * in place so that existing references to this string in the JavaScript heap
1334 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001335 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001336 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001337 * The string is not modified if the operation fails. See NewExternal for
1338 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001339 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001340 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341
v8.team.kasperl727e9952008-09-02 14:56:44 +00001342 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001343 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001344 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001345 * resource will be disposed by calling its Dispose method. The caller of
1346 * this function should not otherwise delete or modify the resource. Neither
1347 * should the underlying buffer be deallocated or modified except through the
1348 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001349 */
1350 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001351
ager@chromium.org6f10e412009-02-13 10:11:16 +00001352 /**
1353 * Associate an external string resource with this string by transforming it
1354 * in place so that existing references to this string in the JavaScript heap
1355 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001356 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001357 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001358 * The string is not modified if the operation fails. See NewExternal for
1359 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001360 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001361 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001363 /**
1364 * Returns true if this string can be made external.
1365 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001366 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001367
lrn@chromium.org34e60782011-09-15 07:25:40 +00001368 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001369 static Local<String> NewUndetectable(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370
lrn@chromium.org34e60782011-09-15 07:25:40 +00001371 /** Creates an undetectable string from the supplied 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001372 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373
1374 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001375 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001376 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001377 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001378 * then the length() method returns 0 and the * operator returns
1379 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001380 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001381 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001382 public:
1383 explicit Utf8Value(Handle<v8::Value> obj);
1384 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001385 char* operator*() { return str_; }
1386 const char* operator*() const { return str_; }
1387 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001388 private:
1389 char* str_;
1390 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001391
1392 // Disallow copying and assigning.
1393 Utf8Value(const Utf8Value&);
1394 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001395 };
1396
1397 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001398 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001400 * If conversion to a string fails (eg. due to an exception in the toString()
1401 * method of the object) then the length() method returns 0 and the * operator
1402 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001404 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 public:
1406 explicit AsciiValue(Handle<v8::Value> obj);
1407 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001408 char* operator*() { return str_; }
1409 const char* operator*() const { return str_; }
1410 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 private:
1412 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001413 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001414
1415 // Disallow copying and assigning.
1416 AsciiValue(const AsciiValue&);
1417 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 };
1419
1420 /**
1421 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001422 * If conversion to a string fails (eg. due to an exception in the toString()
1423 * method of the object) then the length() method returns 0 and the * operator
1424 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001426 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427 public:
1428 explicit Value(Handle<v8::Value> obj);
1429 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001430 uint16_t* operator*() { return str_; }
1431 const uint16_t* operator*() const { return str_; }
1432 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 private:
1434 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001435 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001436
1437 // Disallow copying and assigning.
1438 Value(const Value&);
1439 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001441
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001442 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001443 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1444 Encoding encoding) const;
1445 void VerifyExternalStringResource(ExternalStringResource* val) const;
1446 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447};
1448
1449
1450/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001451 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001453class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001455 double Value() const;
1456 static Local<Number> New(double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001457 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001459 Number();
1460 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461};
1462
1463
1464/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001465 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001467class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001469 static Local<Integer> New(int32_t value);
1470 static Local<Integer> NewFromUnsigned(uint32_t value);
1471 static Local<Integer> New(int32_t value, Isolate*);
1472 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1473 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001474 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001476 Integer();
1477 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478};
1479
1480
1481/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001482 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001484class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001486 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001487 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001488 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001489};
1490
1491
1492/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001493 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001495class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001497 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001499 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500};
1501
1502
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503enum PropertyAttribute {
1504 None = 0,
1505 ReadOnly = 1 << 0,
1506 DontEnum = 1 << 1,
1507 DontDelete = 1 << 2
1508};
1509
ager@chromium.org3811b432009-10-28 14:53:37 +00001510enum ExternalArrayType {
1511 kExternalByteArray = 1,
1512 kExternalUnsignedByteArray,
1513 kExternalShortArray,
1514 kExternalUnsignedShortArray,
1515 kExternalIntArray,
1516 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001517 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001518 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001519 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001520};
1521
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001523 * Accessor[Getter|Setter] are used as callback functions when
1524 * setting|getting a particular property. See Object and ObjectTemplate's
1525 * method SetAccessor.
1526 */
1527typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1528 const AccessorInfo& info);
1529
1530
1531typedef void (*AccessorSetter)(Local<String> property,
1532 Local<Value> value,
1533 const AccessorInfo& info);
1534
1535
1536/**
1537 * Access control specifications.
1538 *
1539 * Some accessors should be accessible across contexts. These
1540 * accessors have an explicit access control parameter which specifies
1541 * the kind of cross-context access that should be allowed.
1542 *
1543 * Additionally, for security, accessors can prohibit overwriting by
1544 * accessors defined in JavaScript. For objects that have such
1545 * accessors either locally or in their prototype chain it is not
1546 * possible to overwrite the accessor by using __defineGetter__ or
1547 * __defineSetter__ from JavaScript code.
1548 */
1549enum AccessControl {
1550 DEFAULT = 0,
1551 ALL_CAN_READ = 1,
1552 ALL_CAN_WRITE = 1 << 1,
1553 PROHIBITS_OVERWRITING = 1 << 2
1554};
1555
1556
1557/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001558 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001560class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001561 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001562 bool Set(Handle<Value> key,
1563 Handle<Value> value,
1564 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001565
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001566 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001567
ager@chromium.orge2902be2009-06-08 12:21:35 +00001568 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001569 // overriding accessors or read-only properties.
1570 //
1571 // Note that if the object has an interceptor the property will be set
1572 // locally, but since the interceptor takes precedence the local property
1573 // will only be returned if the interceptor doesn't return a value.
1574 //
1575 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001576 bool ForceSet(Handle<Value> key,
1577 Handle<Value> value,
1578 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001579
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001580 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001582 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001583
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001584 /**
1585 * Gets the property attributes of a property which can be None or
1586 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1587 * None when the property doesn't exist.
1588 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001589 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001590
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 // TODO(1245389): Replace the type-specific versions of these
1592 // functions with generic ones that accept a Handle<Value> key.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001593 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001594
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001595 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001596
1597 // Delete a property on this object bypassing interceptors and
1598 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001599 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001600
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001601 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001602
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001603 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001605 bool SetAccessor(Handle<String> name,
1606 AccessorGetter getter,
1607 AccessorSetter setter = 0,
1608 Handle<Value> data = Handle<Value>(),
1609 AccessControl settings = DEFAULT,
1610 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001611
ulan@chromium.org750145a2013-03-07 15:14:13 +00001612 // This function is not yet stable and should not be used at this time.
1613 bool SetAccessor(Handle<String> name,
1614 Handle<DeclaredAccessorDescriptor> descriptor,
1615 AccessControl settings = DEFAULT,
1616 PropertyAttribute attribute = None);
1617
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001619 * Returns an array containing the names of the enumerable properties
1620 * of this object, including properties from prototype objects. The
1621 * array returned by this method contains the same values as would
1622 * be enumerated by a for-in statement over this object.
1623 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001624 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001625
1626 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001627 * This function has the same functionality as GetPropertyNames but
1628 * the returned array doesn't contain the names of properties from
1629 * prototype objects.
1630 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001631 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001632
1633 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 * Get the prototype object. This does not skip objects marked to
1635 * be skipped by __proto__ and it does not consult the security
1636 * handler.
1637 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001638 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639
1640 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001641 * Set the prototype object. This does not skip objects marked to
1642 * be skipped by __proto__ and it does not consult the security
1643 * handler.
1644 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001645 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001646
1647 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001648 * Finds an instance of the given function template in the prototype
1649 * chain.
1650 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001651 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001652
1653 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 * Call builtin Object.prototype.toString on this object.
1655 * This is different from Value::ToString() that may call
1656 * user-defined toString function. This one does not.
1657 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001658 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001660 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001661 * Returns the function invoked as a constructor for this object.
1662 * May be the null value.
1663 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001664 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001665
1666 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001667 * Returns the name of the function invoked as a constructor for this object.
1668 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001669 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001670
kasper.lund212ac232008-07-16 07:07:30 +00001671 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001672 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001673
1674 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001675 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001676
kasper.lund212ac232008-07-16 07:07:30 +00001677 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001678 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001679
1680 /**
1681 * Gets a 2-byte-aligned native pointer from an internal field. This field
1682 * must have been set by SetAlignedPointerInInternalField, everything else
1683 * leads to undefined behavior.
1684 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001685 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001686
1687 /**
1688 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1689 * a field, GetAlignedPointerFromInternalField must be used, everything else
1690 * leads to undefined behavior.
1691 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001692 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001693
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001695 bool HasOwnProperty(Handle<String> key);
1696 bool HasRealNamedProperty(Handle<String> key);
1697 bool HasRealIndexedProperty(uint32_t index);
1698 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699
1700 /**
1701 * If result.IsEmpty() no real property was located in the prototype chain.
1702 * This means interceptors in the prototype chain are not called.
1703 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001704 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001705
1706 /**
1707 * If result.IsEmpty() no real property was located on the object or
1708 * in the prototype chain.
1709 * This means interceptors in the prototype chain are not called.
1710 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001711 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712
1713 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001714 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715
kasper.lund212ac232008-07-16 07:07:30 +00001716 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001717 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001719 /**
1720 * Turns on access check on the object if the object is an instance of
1721 * a template that has access check callbacks. If an object has no
1722 * access check info, the object cannot be accessed by anyone.
1723 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001724 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001725
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001726 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001727 * Returns the identity hash for this object. The current implementation
1728 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001729 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001730 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001731 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001732 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001733 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001734
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001735 /**
1736 * Access hidden properties on JavaScript objects. These properties are
1737 * hidden from the executing JavaScript and only accessible through the V8
1738 * C++ API. Hidden properties introduced by V8 internally (for example the
1739 * identity hash) are prefixed with "v8::".
1740 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001741 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1742 Local<Value> GetHiddenValue(Handle<String> key);
1743 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001744
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001745 /**
1746 * Returns true if this is an instance of an api function (one
1747 * created from a function created from a function template) and has
1748 * been modified since it was created. Note that this method is
1749 * conservative and may return true for objects that haven't actually
1750 * been modified.
1751 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001752 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001753
1754 /**
1755 * Clone this object with a fast but shallow copy. Values will point
1756 * to the same values as the original object.
1757 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001758 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001760 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001761 * Returns the context in which the object was created.
1762 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001763 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001764
1765 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001766 * Set the backing store of the indexed properties to be managed by the
1767 * embedding layer. Access to the indexed properties will follow the rules
1768 * spelled out in CanvasPixelArray.
1769 * Note: The embedding program still owns the data and needs to ensure that
1770 * the backing store is preserved while V8 has a reference.
1771 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001772 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1773 bool HasIndexedPropertiesInPixelData();
1774 uint8_t* GetIndexedPropertiesPixelData();
1775 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001776
ager@chromium.org3811b432009-10-28 14:53:37 +00001777 /**
1778 * Set the backing store of the indexed properties to be managed by the
1779 * embedding layer. Access to the indexed properties will follow the rules
1780 * spelled out for the CanvasArray subtypes in the WebGL specification.
1781 * Note: The embedding program still owns the data and needs to ensure that
1782 * the backing store is preserved while V8 has a reference.
1783 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001784 void SetIndexedPropertiesToExternalArrayData(void* data,
1785 ExternalArrayType array_type,
1786 int number_of_elements);
1787 bool HasIndexedPropertiesInExternalArrayData();
1788 void* GetIndexedPropertiesExternalArrayData();
1789 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1790 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001791
lrn@chromium.org1c092762011-05-09 09:42:16 +00001792 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001793 * Checks whether a callback is set by the
1794 * ObjectTemplate::SetCallAsFunctionHandler method.
1795 * When an Object is callable this method returns true.
1796 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001797 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001798
1799 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001800 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001801 * ObjectTemplate::SetCallAsFunctionHandler method.
1802 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001803 Local<Value> CallAsFunction(Handle<Object> recv,
1804 int argc,
1805 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001806
1807 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001808 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001809 * ObjectTemplate::SetCallAsFunctionHandler method.
1810 * Note: This method behaves like the Function::NewInstance method.
1811 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001812 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001813
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001814 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001815 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001816
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001818 Object();
1819 static void CheckCast(Value* obj);
1820 Local<Value> SlowGetInternalField(int index);
1821 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822};
1823
1824
1825/**
1826 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1827 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001828class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001830 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831
ager@chromium.org3e875802009-06-29 08:26:34 +00001832 /**
1833 * Clones an element at index |index|. Returns an empty
1834 * handle if cloning fails (for any reason).
1835 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001836 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001837
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001838 /**
1839 * Creates a JavaScript array with the given length. If the length
1840 * is negative the returned array will have length 0.
1841 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001842 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001843
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001844 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001846 Array();
1847 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848};
1849
1850
1851/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001852 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001854class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001856 Local<Object> NewInstance() const;
1857 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1858 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1859 void SetName(Handle<String> name);
1860 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001861
1862 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001863 * Name inferred from variable or property assignment of this function.
1864 * Used to facilitate debugging and profiling of JavaScript code written
1865 * in an OO style, where many functions are anonymous but are assigned
1866 * to object properties.
1867 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001868 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001869
1870 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001871 * Returns zero based line number of function body and
1872 * kLineOffsetNotFound if no information available.
1873 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001874 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001875 /**
1876 * Returns zero based column number of function body and
1877 * kLineOffsetNotFound if no information available.
1878 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001879 int GetScriptColumnNumber() const;
1880 Handle<Value> GetScriptId() const;
1881 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001882 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001883 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001884
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001886 Function();
1887 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888};
1889
1890
1891/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001892 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1893 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001894class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001895 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001896 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001897
1898 /**
1899 * A specialization of Value::NumberValue that is more efficient
1900 * because we know the structure of this object.
1901 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001902 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001903
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001904 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001905
1906 /**
1907 * Notification that the embedder has changed the time zone,
1908 * daylight savings time, or other date / time configuration
1909 * parameters. V8 keeps a cache of various values used for
1910 * date / time computation. This notification will reset
1911 * those cached values for the current context so that date /
1912 * time configuration changes would be reflected in the Date
1913 * object.
1914 *
1915 * This API should not be called more than needed as it will
1916 * negatively impact the performance of date operations.
1917 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001918 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001919
1920 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001921 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001922};
1923
1924
1925/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001926 * A Number object (ECMA-262, 4.3.21).
1927 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001928class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001929 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001930 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001931
1932 /**
1933 * Returns the Number held by the object.
1934 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001935 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001936
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001937 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001938
1939 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001940 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001941};
1942
1943
1944/**
1945 * A Boolean object (ECMA-262, 4.3.15).
1946 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001947class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001948 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001949 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001950
1951 /**
1952 * Returns the Boolean held by the object.
1953 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001954 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001955
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001956 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001957
1958 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001959 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001960};
1961
1962
1963/**
1964 * A String object (ECMA-262, 4.3.18).
1965 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001966class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001967 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001968 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001969
1970 /**
1971 * Returns the String held by the object.
1972 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001973 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001974
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001975 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001976
1977 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001978 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001979};
1980
1981
1982/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001983 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1984 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001985class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001986 public:
1987 /**
1988 * Regular expression flag bits. They can be or'ed to enable a set
1989 * of flags.
1990 */
1991 enum Flags {
1992 kNone = 0,
1993 kGlobal = 1,
1994 kIgnoreCase = 2,
1995 kMultiline = 4
1996 };
1997
1998 /**
1999 * Creates a regular expression from the given pattern string and
2000 * the flags bit field. May throw a JavaScript exception as
2001 * described in ECMA-262, 15.10.4.1.
2002 *
2003 * For example,
2004 * RegExp::New(v8::String::New("foo"),
2005 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2006 * is equivalent to evaluating "/foo/gm".
2007 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002008 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002009
2010 /**
2011 * Returns the value of the source property: a string representing
2012 * the regular expression.
2013 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002014 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002015
2016 /**
2017 * Returns the flags bit field.
2018 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002019 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002020
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002021 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002022
2023 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002024 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002025};
2026
2027
2028/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002029 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2030 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002032class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002034 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002035 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002036 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002038 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039};
2040
2041
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002042// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043
2044
2045/**
2046 * The superclass of object and function templates.
2047 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002048class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049 public:
2050 /** Adds a property to each instance created by this template.*/
2051 void Set(Handle<String> name, Handle<Data> value,
2052 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002053 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 private:
2055 Template();
2056
2057 friend class ObjectTemplate;
2058 friend class FunctionTemplate;
2059};
2060
2061
2062/**
2063 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002064 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 * including the receiver, the number and values of arguments, and
2066 * the holder of the function.
2067 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002068class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002070 V8_INLINE(int Length() const);
2071 V8_INLINE(Local<Value> operator[](int i) const);
2072 V8_INLINE(Local<Function> Callee() const);
2073 V8_INLINE(Local<Object> This() const);
2074 V8_INLINE(Local<Object> Holder() const);
2075 V8_INLINE(bool IsConstructCall() const);
2076 V8_INLINE(Local<Value> Data() const);
2077 V8_INLINE(Isolate* GetIsolate() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002078
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002080 static const int kIsolateIndex = 0;
2081 static const int kDataIndex = -1;
2082 static const int kCalleeIndex = -2;
2083 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002084
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 friend class ImplementationUtilities;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002086 V8_INLINE(Arguments(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002087 internal::Object** values,
2088 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002089 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002090 internal::Object** implicit_args_;
2091 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002093 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094};
2095
2096
2097/**
2098 * The information passed to an accessor callback about the context
2099 * of the property access.
2100 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002101class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002103 V8_INLINE(AccessorInfo(internal::Object** args))
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002104 : args_(args) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002105 V8_INLINE(Isolate* GetIsolate() const);
2106 V8_INLINE(Local<Value> Data() const);
2107 V8_INLINE(Local<Object> This() const);
2108 V8_INLINE(Local<Object> Holder() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002111 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112};
2113
2114
2115typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118 * NamedProperty[Getter|Setter] are used as interceptors on object.
2119 * See ObjectTemplate::SetNamedPropertyHandler.
2120 */
2121typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2122 const AccessorInfo& info);
2123
2124
2125/**
2126 * Returns the value if the setter intercepts the request.
2127 * Otherwise, returns an empty handle.
2128 */
2129typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2130 Local<Value> value,
2131 const AccessorInfo& info);
2132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133/**
2134 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002135 * The result is an integer encoding property attributes (like v8::None,
2136 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002138typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2139 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140
2141
2142/**
2143 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002144 * The return value is true if the property could be deleted and false
2145 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 */
2147typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2148 const AccessorInfo& info);
2149
2150/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002151 * Returns an array containing the names of the properties the named
2152 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 */
2154typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2155
v8.team.kasperl727e9952008-09-02 14:56:44 +00002156
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002158 * Returns the value of the property if the getter intercepts the
2159 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160 */
2161typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2162 const AccessorInfo& info);
2163
2164
2165/**
2166 * Returns the value if the setter intercepts the request.
2167 * Otherwise, returns an empty handle.
2168 */
2169typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2170 Local<Value> value,
2171 const AccessorInfo& info);
2172
2173
2174/**
2175 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002176 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002178typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2179 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180
2181/**
2182 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002183 * The return value is true if the property could be deleted and false
2184 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 */
2186typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2187 const AccessorInfo& info);
2188
v8.team.kasperl727e9952008-09-02 14:56:44 +00002189/**
2190 * Returns an array containing the indices of the properties the
2191 * indexed property getter intercepts.
2192 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2194
2195
2196/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002197 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198 */
2199enum AccessType {
2200 ACCESS_GET,
2201 ACCESS_SET,
2202 ACCESS_HAS,
2203 ACCESS_DELETE,
2204 ACCESS_KEYS
2205};
2206
v8.team.kasperl727e9952008-09-02 14:56:44 +00002207
2208/**
2209 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002210 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002211 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002212typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 Local<Value> key,
2214 AccessType type,
2215 Local<Value> data);
2216
v8.team.kasperl727e9952008-09-02 14:56:44 +00002217
2218/**
2219 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002220 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002221 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002222typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 uint32_t index,
2224 AccessType type,
2225 Local<Value> data);
2226
2227
2228/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002229 * A FunctionTemplate is used to create functions at runtime. There
2230 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002231 * context. The lifetime of the created function is equal to the
2232 * lifetime of the context. So in case the embedder needs to create
2233 * temporary functions that can be collected using Scripts is
2234 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 *
2236 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002237 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002239 * A FunctionTemplate has a corresponding instance template which is
2240 * used to create object instances when the function is used as a
2241 * constructor. Properties added to the instance template are added to
2242 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 *
2244 * A FunctionTemplate can have a prototype template. The prototype template
2245 * is used to create the prototype object of the function.
2246 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002247 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002249 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2251 * t->Set("func_property", v8::Number::New(1));
2252 *
2253 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2254 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2255 * proto_t->Set("proto_const", v8::Number::New(2));
2256 *
2257 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2258 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2259 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2260 * instance_t->Set("instance_property", Number::New(3));
2261 *
2262 * v8::Local<v8::Function> function = t->GetFunction();
2263 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002264 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 *
2266 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002267 * and "instance" for the instance object created above. The function
2268 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002270 * \code
2271 * func_property in function == true;
2272 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002274 * function.prototype.proto_method() invokes 'InvokeCallback'
2275 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002277 * instance instanceof function == true;
2278 * instance.instance_accessor calls 'InstanceAccessorCallback'
2279 * instance.instance_property == 3;
2280 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002282 * A FunctionTemplate can inherit from another one by calling the
2283 * FunctionTemplate::Inherit method. The following graph illustrates
2284 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002286 * \code
2287 * FunctionTemplate Parent -> Parent() . prototype -> { }
2288 * ^ ^
2289 * | Inherit(Parent) | .__proto__
2290 * | |
2291 * FunctionTemplate Child -> Child() . prototype -> { }
2292 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002294 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2295 * object of the Child() function has __proto__ pointing to the
2296 * Parent() function's prototype object. An instance of the Child
2297 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002299 * Let Parent be the FunctionTemplate initialized in the previous
2300 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002302 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 * Local<FunctionTemplate> parent = t;
2304 * Local<FunctionTemplate> child = FunctionTemplate::New();
2305 * child->Inherit(parent);
2306 *
2307 * Local<Function> child_function = child->GetFunction();
2308 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002309 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002311 * The Child function and Child instance will have the following
2312 * properties:
2313 *
2314 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002316 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002318 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002320class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 public:
2322 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002323 static Local<FunctionTemplate> New(
2324 InvocationCallback callback = 0,
2325 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002326 Handle<Signature> signature = Handle<Signature>(),
2327 int length = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 /** Returns the unique function instance in the current execution context.*/
2329 Local<Function> GetFunction();
2330
v8.team.kasperl727e9952008-09-02 14:56:44 +00002331 /**
2332 * Set the call-handler callback for a FunctionTemplate. This
2333 * callback is called whenever the function created from this
2334 * FunctionTemplate is called.
2335 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 void SetCallHandler(InvocationCallback callback,
2337 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002339 /** Set the predefined length property for the FunctionTemplate. */
2340 void SetLength(int length);
2341
v8.team.kasperl727e9952008-09-02 14:56:44 +00002342 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 Local<ObjectTemplate> InstanceTemplate();
2344
2345 /** Causes the function template to inherit from a parent function template.*/
2346 void Inherit(Handle<FunctionTemplate> parent);
2347
2348 /**
2349 * A PrototypeTemplate is the template used to create the prototype object
2350 * of the function created by this template.
2351 */
2352 Local<ObjectTemplate> PrototypeTemplate();
2353
v8.team.kasperl727e9952008-09-02 14:56:44 +00002354 /**
2355 * Set the class name of the FunctionTemplate. This is used for
2356 * printing objects created with the function created from the
2357 * FunctionTemplate as its constructor.
2358 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359 void SetClassName(Handle<String> name);
2360
2361 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002362 * Determines whether the __proto__ accessor ignores instances of
2363 * the function template. If instances of the function template are
2364 * ignored, __proto__ skips all instances and instead returns the
2365 * next object in the prototype chain.
2366 *
2367 * Call with a value of true to make the __proto__ accessor ignore
2368 * instances of the function template. Call with a value of false
2369 * to make the __proto__ accessor not ignore instances of the
2370 * function template. By default, instances of a function template
2371 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372 */
2373 void SetHiddenPrototype(bool value);
2374
2375 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002376 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2377 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002378 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002379 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002380
2381 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002382 * Returns true if the given object is an instance of this function
2383 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384 */
2385 bool HasInstance(Handle<Value> object);
2386
2387 private:
2388 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2390 NamedPropertySetter setter,
2391 NamedPropertyQuery query,
2392 NamedPropertyDeleter remover,
2393 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002394 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2396 IndexedPropertySetter setter,
2397 IndexedPropertyQuery query,
2398 IndexedPropertyDeleter remover,
2399 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002400 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2402 Handle<Value> data);
2403
2404 friend class Context;
2405 friend class ObjectTemplate;
2406};
2407
2408
2409/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002410 * An ObjectTemplate is used to create objects at runtime.
2411 *
2412 * Properties added to an ObjectTemplate are added to each object
2413 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002415class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002416 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002417 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002419
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420 /** Creates a new instance of this template.*/
2421 Local<Object> NewInstance();
2422
2423 /**
2424 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002425 *
2426 * Whenever the property with the given name is accessed on objects
2427 * created from this ObjectTemplate the getter and setter callbacks
2428 * are called instead of getting and setting the property directly
2429 * on the JavaScript object.
2430 *
2431 * \param name The name of the property for which an accessor is added.
2432 * \param getter The callback to invoke when getting the property.
2433 * \param setter The callback to invoke when setting the property.
2434 * \param data A piece of data that will be passed to the getter and setter
2435 * callbacks whenever they are invoked.
2436 * \param settings Access control settings for the accessor. This is a bit
2437 * field consisting of one of more of
2438 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2439 * The default is to not allow cross-context access.
2440 * ALL_CAN_READ means that all cross-context reads are allowed.
2441 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2442 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2443 * cross-context access.
2444 * \param attribute The attributes of the property for which an accessor
2445 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002446 * \param signature The signature describes valid receivers for the accessor
2447 * and is used to perform implicit instance checks against them. If the
2448 * receiver is incompatible (i.e. is not an instance of the constructor as
2449 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2450 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002451 */
2452 void SetAccessor(Handle<String> name,
2453 AccessorGetter getter,
2454 AccessorSetter setter = 0,
2455 Handle<Value> data = Handle<Value>(),
2456 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002457 PropertyAttribute attribute = None,
2458 Handle<AccessorSignature> signature =
2459 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460
ulan@chromium.org750145a2013-03-07 15:14:13 +00002461 // This function is not yet stable and should not be used at this time.
2462 bool SetAccessor(Handle<String> name,
2463 Handle<DeclaredAccessorDescriptor> descriptor,
2464 AccessControl settings = DEFAULT,
2465 PropertyAttribute attribute = None,
2466 Handle<AccessorSignature> signature =
2467 Handle<AccessorSignature>());
2468
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002469 /**
2470 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002471 *
2472 * Whenever a named property is accessed on objects created from
2473 * this object template, the provided callback is invoked instead of
2474 * accessing the property directly on the JavaScript object.
2475 *
2476 * \param getter The callback to invoke when getting a property.
2477 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002478 * \param query The callback to invoke to check if a property is present,
2479 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002480 * \param deleter The callback to invoke when deleting a property.
2481 * \param enumerator The callback to invoke to enumerate all the named
2482 * properties of an object.
2483 * \param data A piece of data that will be passed to the callbacks
2484 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485 */
2486 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2487 NamedPropertySetter setter = 0,
2488 NamedPropertyQuery query = 0,
2489 NamedPropertyDeleter deleter = 0,
2490 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002491 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492
2493 /**
2494 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002495 *
2496 * Whenever an indexed property is accessed on objects created from
2497 * this object template, the provided callback is invoked instead of
2498 * accessing the property directly on the JavaScript object.
2499 *
2500 * \param getter The callback to invoke when getting a property.
2501 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002502 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002503 * \param deleter The callback to invoke when deleting a property.
2504 * \param enumerator The callback to invoke to enumerate all the indexed
2505 * properties of an object.
2506 * \param data A piece of data that will be passed to the callbacks
2507 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 */
2509 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2510 IndexedPropertySetter setter = 0,
2511 IndexedPropertyQuery query = 0,
2512 IndexedPropertyDeleter deleter = 0,
2513 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002514 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002515
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 /**
2517 * Sets the callback to be used when calling instances created from
2518 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002519 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002520 * function.
2521 */
2522 void SetCallAsFunctionHandler(InvocationCallback callback,
2523 Handle<Value> data = Handle<Value>());
2524
v8.team.kasperl727e9952008-09-02 14:56:44 +00002525 /**
2526 * Mark object instances of the template as undetectable.
2527 *
2528 * In many ways, undetectable objects behave as though they are not
2529 * there. They behave like 'undefined' in conditionals and when
2530 * printed. However, properties can be accessed and called as on
2531 * normal objects.
2532 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533 void MarkAsUndetectable();
2534
v8.team.kasperl727e9952008-09-02 14:56:44 +00002535 /**
2536 * Sets access check callbacks on the object template.
2537 *
2538 * When accessing properties on instances of this object template,
2539 * the access check callback will be called to determine whether or
2540 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002541 * The last parameter specifies whether access checks are turned
2542 * on by default on instances. If access checks are off by default,
2543 * they can be turned on on individual instances by calling
2544 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002545 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2547 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002548 Handle<Value> data = Handle<Value>(),
2549 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550
kasper.lund212ac232008-07-16 07:07:30 +00002551 /**
2552 * Gets the number of internal fields for objects generated from
2553 * this template.
2554 */
2555 int InternalFieldCount();
2556
2557 /**
2558 * Sets the number of internal fields for objects generated from
2559 * this template.
2560 */
2561 void SetInternalFieldCount(int value);
2562
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 private:
2564 ObjectTemplate();
2565 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2566 friend class FunctionTemplate;
2567};
2568
2569
2570/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002571 * A Signature specifies which receivers and arguments are valid
2572 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002574class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575 public:
2576 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2577 Handle<FunctionTemplate>(),
2578 int argc = 0,
2579 Handle<FunctionTemplate> argv[] = 0);
2580 private:
2581 Signature();
2582};
2583
2584
2585/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002586 * An AccessorSignature specifies which receivers are valid parameters
2587 * to an accessor callback.
2588 */
2589class V8EXPORT AccessorSignature : public Data {
2590 public:
2591 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2592 Handle<FunctionTemplate>());
2593 private:
2594 AccessorSignature();
2595};
2596
2597
ulan@chromium.org750145a2013-03-07 15:14:13 +00002598class V8EXPORT DeclaredAccessorDescriptor : public Data {
2599 private:
2600 DeclaredAccessorDescriptor();
2601};
2602
2603
2604class V8EXPORT ObjectOperationDescriptor : public Data {
2605 public:
2606 // This function is not yet stable and should not be used at this time.
2607 static Local<RawOperationDescriptor> NewInternalFieldDereference(
2608 Isolate* isolate,
2609 int internal_field);
2610 private:
2611 ObjectOperationDescriptor();
2612};
2613
2614
2615enum DeclaredAccessorDescriptorDataType {
2616 kDescriptorBoolType,
2617 kDescriptorInt8Type, kDescriptorUint8Type,
2618 kDescriptorInt16Type, kDescriptorUint16Type,
2619 kDescriptorInt32Type, kDescriptorUint32Type,
2620 kDescriptorFloatType, kDescriptorDoubleType
2621};
2622
2623
2624class V8EXPORT RawOperationDescriptor : public Data {
2625 public:
2626 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
2627 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
2628 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
2629 int16_t byte_offset);
2630 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
2631 void* compare_value);
2632 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
2633 Isolate* isolate,
2634 DeclaredAccessorDescriptorDataType data_type,
2635 uint8_t bool_offset = 0);
2636 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
2637 uint8_t bitmask,
2638 uint8_t compare_value);
2639 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
2640 Isolate* isolate,
2641 uint16_t bitmask,
2642 uint16_t compare_value);
2643 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
2644 Isolate* isolate,
2645 uint32_t bitmask,
2646 uint32_t compare_value);
2647
2648 private:
2649 RawOperationDescriptor();
2650};
2651
2652
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002653/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002654 * A utility for determining the type of objects based on the template
2655 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002657class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 public:
2659 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2660 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2661 int match(Handle<Value> value);
2662 private:
2663 TypeSwitch();
2664};
2665
2666
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002667// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002669class V8EXPORT ExternalAsciiStringResourceImpl
2670 : public String::ExternalAsciiStringResource {
2671 public:
2672 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2673 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2674 : data_(data), length_(length) {}
2675 const char* data() const { return data_; }
2676 size_t length() const { return length_; }
2677
2678 private:
2679 const char* data_;
2680 size_t length_;
2681};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682
2683/**
2684 * Ignore
2685 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002686class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002688 // Note that the strings passed into this constructor must live as long
2689 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002691 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002693 const char** deps = 0,
2694 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 virtual ~Extension() { }
2696 virtual v8::Handle<v8::FunctionTemplate>
2697 GetNativeFunction(v8::Handle<v8::String> name) {
2698 return v8::Handle<v8::FunctionTemplate>();
2699 }
2700
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002701 const char* name() const { return name_; }
2702 size_t source_length() const { return source_length_; }
2703 const String::ExternalAsciiStringResource* source() const {
2704 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705 int dependency_count() { return dep_count_; }
2706 const char** dependencies() { return deps_; }
2707 void set_auto_enable(bool value) { auto_enable_ = value; }
2708 bool auto_enable() { return auto_enable_; }
2709
2710 private:
2711 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002712 size_t source_length_; // expected to initialize before source_
2713 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714 int dep_count_;
2715 const char** deps_;
2716 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002717
2718 // Disallow copying and assigning.
2719 Extension(const Extension&);
2720 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721};
2722
2723
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002724void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725
2726
2727/**
2728 * Ignore
2729 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002730class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002732 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733 RegisterExtension(extension);
2734 }
2735};
2736
2737
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002738// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002739
2740
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002741Handle<Primitive> V8EXPORT Undefined();
2742Handle<Primitive> V8EXPORT Null();
2743Handle<Boolean> V8EXPORT True();
2744Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002745
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002746V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
2747V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
2748V8_INLINE(Handle<Boolean> True(Isolate* isolate));
2749V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002750
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751
2752/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002753 * A set of constraints that specifies the limits of the runtime's memory use.
2754 * You must set the heap size before initializing the VM - the size cannot be
2755 * adjusted after the VM is initialized.
2756 *
2757 * If you are using threads then you should hold the V8::Locker lock while
2758 * setting the stack limit and you must set a non-default stack limit separately
2759 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002761class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762 public:
2763 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002764 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002766 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002768 int max_executable_size() { return max_executable_size_; }
2769 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002770 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002771 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002772 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2773 private:
2774 int max_young_space_size_;
2775 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002776 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002777 uint32_t* stack_limit_;
2778};
2779
2780
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002781bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002782
2783
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002784// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785
2786
2787typedef void (*FatalErrorCallback)(const char* location, const char* message);
2788
2789
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002790typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791
2792
2793/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002794 * Schedules an exception to be thrown when returning to JavaScript. When an
2795 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002796 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002797 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002799Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800
2801/**
2802 * Create new error objects by calling the corresponding error object
2803 * constructor with the message.
2804 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002805class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 public:
2807 static Local<Value> RangeError(Handle<String> message);
2808 static Local<Value> ReferenceError(Handle<String> message);
2809 static Local<Value> SyntaxError(Handle<String> message);
2810 static Local<Value> TypeError(Handle<String> message);
2811 static Local<Value> Error(Handle<String> message);
2812};
2813
2814
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002815// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002817typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002819typedef void* (*CreateHistogramCallback)(const char* name,
2820 int min,
2821 int max,
2822 size_t buckets);
2823
2824typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2825
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002826// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002827 enum ObjectSpace {
2828 kObjectSpaceNewSpace = 1 << 0,
2829 kObjectSpaceOldPointerSpace = 1 << 1,
2830 kObjectSpaceOldDataSpace = 1 << 2,
2831 kObjectSpaceCodeSpace = 1 << 3,
2832 kObjectSpaceMapSpace = 1 << 4,
2833 kObjectSpaceLoSpace = 1 << 5,
2834
2835 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2836 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2837 kObjectSpaceLoSpace
2838 };
2839
2840 enum AllocationAction {
2841 kAllocationActionAllocate = 1 << 0,
2842 kAllocationActionFree = 1 << 1,
2843 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2844 };
2845
2846typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2847 AllocationAction action,
2848 int size);
2849
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002850// --- Leave Script Callback ---
2851typedef void (*CallCompletedCallback)();
2852
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002853// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002854typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2855 AccessType type,
2856 Local<Value> data);
2857
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002858// --- AllowCodeGenerationFromStrings callbacks ---
2859
2860/**
2861 * Callback to check if code generation from strings is allowed. See
2862 * Context::AllowCodeGenerationFromStrings.
2863 */
2864typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2865
2866// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002867
2868/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002869 * Applications can register callback functions which will be called
2870 * before and after a garbage collection. Allocations are not
2871 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002872 * objects (set or delete properties for example) since it is possible
2873 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002875enum GCType {
2876 kGCTypeScavenge = 1 << 0,
2877 kGCTypeMarkSweepCompact = 1 << 1,
2878 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2879};
2880
2881enum GCCallbackFlags {
2882 kNoGCCallbackFlags = 0,
2883 kGCCallbackFlagCompacted = 1 << 0
2884};
2885
2886typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2887typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002889typedef void (*GCCallback)();
2890
2891
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002893 * Collection of V8 heap information.
2894 *
2895 * Instances of this class can be passed to v8::V8::HeapStatistics to
2896 * get heap statistics from V8.
2897 */
2898class V8EXPORT HeapStatistics {
2899 public:
2900 HeapStatistics();
2901 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002902 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00002903 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002904 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002905 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002906
2907 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00002908 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002909 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00002910 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002911 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002912 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002913
2914 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002915 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00002916};
2917
2918
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002919class RetainedObjectInfo;
2920
ager@chromium.org3811b432009-10-28 14:53:37 +00002921/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002922 * Isolate represents an isolated instance of the V8 engine. V8
2923 * isolates have completely separate states. Objects from one isolate
2924 * must not be used in other isolates. When V8 is initialized a
2925 * default isolate is implicitly created and entered. The embedder
2926 * can create additional isolates and use them in parallel in multiple
2927 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002928 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002929 */
2930class V8EXPORT Isolate {
2931 public:
2932 /**
2933 * Stack-allocated class which sets the isolate for all operations
2934 * executed within a local scope.
2935 */
2936 class V8EXPORT Scope {
2937 public:
2938 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2939 isolate->Enter();
2940 }
2941
2942 ~Scope() { isolate_->Exit(); }
2943
2944 private:
2945 Isolate* const isolate_;
2946
2947 // Prevent copying of Scope objects.
2948 Scope(const Scope&);
2949 Scope& operator=(const Scope&);
2950 };
2951
2952 /**
2953 * Creates a new isolate. Does not change the currently entered
2954 * isolate.
2955 *
2956 * When an isolate is no longer used its resources should be freed
2957 * by calling Dispose(). Using the delete operator is not allowed.
2958 */
2959 static Isolate* New();
2960
2961 /**
2962 * Returns the entered isolate for the current thread or NULL in
2963 * case there is no current isolate.
2964 */
2965 static Isolate* GetCurrent();
2966
2967 /**
2968 * Methods below this point require holding a lock (using Locker) in
2969 * a multi-threaded environment.
2970 */
2971
2972 /**
2973 * Sets this isolate as the entered one for the current thread.
2974 * Saves the previously entered one (if any), so that it can be
2975 * restored when exiting. Re-entering an isolate is allowed.
2976 */
2977 void Enter();
2978
2979 /**
2980 * Exits this isolate by restoring the previously entered one in the
2981 * current thread. The isolate may still stay the same, if it was
2982 * entered more than once.
2983 *
2984 * Requires: this == Isolate::GetCurrent().
2985 */
2986 void Exit();
2987
2988 /**
2989 * Disposes the isolate. The isolate must not be entered by any
2990 * thread to be disposable.
2991 */
2992 void Dispose();
2993
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002994 /**
2995 * Associate embedder-specific data with the isolate
2996 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002997 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002998
2999 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003000 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003001 * Returns NULL if SetData has never been called.
3002 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003003 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003004
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003005 /**
3006 * Get statistics about the heap memory usage.
3007 */
3008 void GetHeapStatistics(HeapStatistics* heap_statistics);
3009
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003010 /**
3011 * Adjusts the amount of registered external memory. Used to give V8 an
3012 * indication of the amount of externally allocated memory that is kept alive
3013 * by JavaScript objects. V8 uses this to decide when to perform global
3014 * garbage collections. Registering externally allocated memory will trigger
3015 * global garbage collections more often than it would otherwise in an attempt
3016 * to garbage collect the JavaScript objects that keep the externally
3017 * allocated memory alive.
3018 *
3019 * \param change_in_bytes the change in externally allocated memory that is
3020 * kept alive by JavaScript objects.
3021 * \returns the adjusted value.
3022 */
3023 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3024
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003025 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003026 Isolate();
3027 Isolate(const Isolate&);
3028 ~Isolate();
3029 Isolate& operator=(const Isolate&);
3030 void* operator new(size_t size);
3031 void operator delete(void*, size_t);
3032};
3033
3034
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003035class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003036 public:
3037 enum CompressionAlgorithm {
3038 kUncompressed,
3039 kBZip2
3040 };
3041
3042 const char* data;
3043 int compressed_size;
3044 int raw_size;
3045};
3046
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003047
3048/**
3049 * A helper class for driving V8 startup data decompression. It is based on
3050 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3051 * for an embedder to use this class, instead, API functions can be used
3052 * directly.
3053 *
3054 * For an example of the class usage, see the "shell.cc" sample application.
3055 */
3056class V8EXPORT StartupDataDecompressor { // NOLINT
3057 public:
3058 StartupDataDecompressor();
3059 virtual ~StartupDataDecompressor();
3060 int Decompress();
3061
3062 protected:
3063 virtual int DecompressData(char* raw_data,
3064 int* raw_data_size,
3065 const char* compressed_data,
3066 int compressed_data_size) = 0;
3067
3068 private:
3069 char** raw_data;
3070};
3071
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003072
3073/**
3074 * EntropySource is used as a callback function when v8 needs a source
3075 * of entropy.
3076 */
3077typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3078
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003079
3080/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003081 * ReturnAddressLocationResolver is used as a callback function when v8 is
3082 * resolving the location of a return address on the stack. Profilers that
3083 * change the return address on the stack can use this to resolve the stack
3084 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003085 *
3086 * \param return_addr_location points to a location on stack where a machine
3087 * return address resides.
3088 * \returns either return_addr_location, or else a pointer to the profiler's
3089 * copy of the original return address.
3090 *
3091 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003092 */
3093typedef uintptr_t (*ReturnAddressLocationResolver)(
3094 uintptr_t return_addr_location);
3095
3096
3097/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003098 * FunctionEntryHook is the type of the profile entry hook called at entry to
3099 * any generated function when function-level profiling is enabled.
3100 *
3101 * \param function the address of the function that's being entered.
3102 * \param return_addr_location points to a location on stack where the machine
3103 * return address resides. This can be used to identify the caller of
3104 * \p function, and/or modified to divert execution when \p function exits.
3105 *
3106 * \note the entry hook must not cause garbage collection.
3107 */
3108typedef void (*FunctionEntryHook)(uintptr_t function,
3109 uintptr_t return_addr_location);
3110
3111
3112/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003113 * A JIT code event is issued each time code is added, moved or removed.
3114 *
3115 * \note removal events are not currently issued.
3116 */
3117struct JitCodeEvent {
3118 enum EventType {
3119 CODE_ADDED,
3120 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003121 CODE_REMOVED,
3122 CODE_ADD_LINE_POS_INFO,
3123 CODE_START_LINE_INFO_RECORDING,
3124 CODE_END_LINE_INFO_RECORDING
3125 };
3126 // Definition of the code position type. The "POSITION" type means the place
3127 // in the source code which are of interest when making stack traces to
3128 // pin-point the source location of a stack frame as close as possible.
3129 // The "STATEMENT_POSITION" means the place at the beginning of each
3130 // statement, and is used to indicate possible break locations.
3131 enum PositionType {
3132 POSITION,
3133 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003134 };
3135
3136 // Type of event.
3137 EventType type;
3138 // Start of the instructions.
3139 void* code_start;
3140 // Size of the instructions.
3141 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003142 // Script info for CODE_ADDED event.
3143 Handle<Script> script;
3144 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
3145 // code line information which is returned from the
3146 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
3147 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
3148 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003149
3150 union {
3151 // Only valid for CODE_ADDED.
3152 struct {
3153 // Name of the object associated with the code, note that the string is
3154 // not zero-terminated.
3155 const char* str;
3156 // Number of chars in str.
3157 size_t len;
3158 } name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003159
3160 // Only valid for CODE_ADD_LINE_POS_INFO
3161 struct {
3162 // PC offset
3163 size_t offset;
3164 // Code postion
3165 size_t pos;
3166 // The position type.
3167 PositionType position_type;
3168 } line_info;
3169
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003170 // New location of instructions. Only valid for CODE_MOVED.
3171 void* new_code_start;
3172 };
3173};
3174
3175/**
3176 * Option flags passed to the SetJitCodeEventHandler function.
3177 */
3178enum JitCodeEventOptions {
3179 kJitCodeEventDefault = 0,
3180 // Generate callbacks for already existent code.
3181 kJitCodeEventEnumExisting = 1
3182};
3183
3184
3185/**
3186 * Callback function passed to SetJitCodeEventHandler.
3187 *
3188 * \param event code add, move or removal event.
3189 */
3190typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3191
3192
3193/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003194 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003195 */
3196class V8EXPORT ExternalResourceVisitor { // NOLINT
3197 public:
3198 virtual ~ExternalResourceVisitor() {}
3199 virtual void VisitExternalString(Handle<String> string) {}
3200};
3201
3202
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003203/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003204 * Interface for iterating through all the persistent handles in the heap.
3205 */
3206class V8EXPORT PersistentHandleVisitor { // NOLINT
3207 public:
3208 virtual ~PersistentHandleVisitor() {}
3209 virtual void VisitPersistentHandle(Persistent<Value> value,
3210 uint16_t class_id) {}
3211};
3212
3213
3214/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003215 * Container class for static utility functions.
3216 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003217class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003219 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220 static void SetFatalErrorHandler(FatalErrorCallback that);
3221
v8.team.kasperl727e9952008-09-02 14:56:44 +00003222 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003223 * Set the callback to invoke to check if code generation from
3224 * strings should be allowed.
3225 */
3226 static void SetAllowCodeGenerationFromStringsCallback(
3227 AllowCodeGenerationFromStringsCallback that);
3228
3229 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003230 * Ignore out-of-memory exceptions.
3231 *
3232 * V8 running out of memory is treated as a fatal error by default.
3233 * This means that the fatal error handler is called and that V8 is
3234 * terminated.
3235 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003236 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003237 * out-of-memory situation as a fatal error. This way, the contexts
3238 * that did not cause the out of memory problem might be able to
3239 * continue execution.
3240 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241 static void IgnoreOutOfMemoryException();
3242
v8.team.kasperl727e9952008-09-02 14:56:44 +00003243 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003244 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003245 * fatal errors such as out-of-memory situations.
3246 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003247 static bool IsDead();
3248
3249 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003250 * The following 4 functions are to be used when V8 is built with
3251 * the 'compress_startup_data' flag enabled. In this case, the
3252 * embedder must decompress startup data prior to initializing V8.
3253 *
3254 * This is how interaction with V8 should look like:
3255 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3256 * v8::StartupData* compressed_data =
3257 * new v8::StartupData[compressed_data_count];
3258 * v8::V8::GetCompressedStartupData(compressed_data);
3259 * ... decompress data (compressed_data can be updated in-place) ...
3260 * v8::V8::SetDecompressedStartupData(compressed_data);
3261 * ... now V8 can be initialized
3262 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003263 *
3264 * A helper class StartupDataDecompressor is provided. It implements
3265 * the protocol of the interaction described above, and can be used in
3266 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003267 */
3268 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3269 static int GetCompressedStartupDataCount();
3270 static void GetCompressedStartupData(StartupData* compressed_data);
3271 static void SetDecompressedStartupData(StartupData* decompressed_data);
3272
3273 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003274 * Adds a message listener.
3275 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003276 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003277 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003278 *
3279 * If data is specified, it will be passed to the callback when it is called.
3280 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003282 static bool AddMessageListener(MessageCallback that,
3283 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003284
3285 /**
3286 * Remove all message listeners from the specified callback function.
3287 */
3288 static void RemoveMessageListeners(MessageCallback that);
3289
3290 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003291 * Tells V8 to capture current stack trace when uncaught exception occurs
3292 * and report it to the message listeners. The option is off by default.
3293 */
3294 static void SetCaptureStackTraceForUncaughtExceptions(
3295 bool capture,
3296 int frame_limit = 10,
3297 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3298
3299 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003300 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003301 */
3302 static void SetFlagsFromString(const char* str, int length);
3303
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003304 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003305 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003306 */
3307 static void SetFlagsFromCommandLine(int* argc,
3308 char** argv,
3309 bool remove_flags);
3310
kasper.lund7276f142008-07-30 08:49:36 +00003311 /** Get the version string. */
3312 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003313
3314 /**
3315 * Enables the host application to provide a mechanism for recording
3316 * statistics counters.
3317 */
3318 static void SetCounterFunction(CounterLookupCallback);
3319
3320 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003321 * Enables the host application to provide a mechanism for recording
3322 * histograms. The CreateHistogram function returns a
3323 * histogram which will later be passed to the AddHistogramSample
3324 * function.
3325 */
3326 static void SetCreateHistogramFunction(CreateHistogramCallback);
3327 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3328
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329 /** Callback function for reporting failed access checks.*/
3330 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3331
3332 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003333 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003334 * garbage collection. Allocations are not allowed in the
3335 * callback function, you therefore cannot manipulate objects (set
3336 * or delete properties for example) since it is possible such
3337 * operations will result in the allocation of objects. It is possible
3338 * to specify the GCType filter for your callback. But it is not possible to
3339 * register the same callback function two times with different
3340 * GCType filters.
3341 */
3342 static void AddGCPrologueCallback(
3343 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3344
3345 /**
3346 * This function removes callback which was installed by
3347 * AddGCPrologueCallback function.
3348 */
3349 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3350
3351 /**
3352 * The function is deprecated. Please use AddGCPrologueCallback instead.
3353 * Enables the host application to receive a notification before a
3354 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003355 * callback function, you therefore cannot manipulate objects (set
3356 * or delete properties for example) since it is possible such
3357 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003358 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003359 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003360
3361 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003362 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003363 * garbage collection. Allocations are not allowed in the
3364 * callback function, you therefore cannot manipulate objects (set
3365 * or delete properties for example) since it is possible such
3366 * operations will result in the allocation of objects. It is possible
3367 * to specify the GCType filter for your callback. But it is not possible to
3368 * register the same callback function two times with different
3369 * GCType filters.
3370 */
3371 static void AddGCEpilogueCallback(
3372 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3373
3374 /**
3375 * This function removes callback which was installed by
3376 * AddGCEpilogueCallback function.
3377 */
3378 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3379
3380 /**
3381 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3382 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003383 * major garbage collection. Allocations are not allowed in the
3384 * callback function, you therefore cannot manipulate objects (set
3385 * or delete properties for example) since it is possible such
3386 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003387 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003388 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003389
3390 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003391 * Enables the host application to provide a mechanism to be notified
3392 * and perform custom logging when V8 Allocates Executable Memory.
3393 */
3394 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3395 ObjectSpace space,
3396 AllocationAction action);
3397
3398 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003399 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003400 */
3401 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3402
3403 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003404 * Adds a callback to notify the host application when a script finished
3405 * running. If a script re-enters the runtime during executing, the
3406 * CallCompletedCallback is only invoked when the outer-most script
3407 * execution ends. Executing scripts inside the callback do not trigger
3408 * further callbacks.
3409 */
3410 static void AddCallCompletedCallback(CallCompletedCallback callback);
3411
3412 /**
3413 * Removes callback that was installed by AddCallCompletedCallback.
3414 */
3415 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3416
3417 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003418 * Allows the host application to group objects together. If one
3419 * object in the group is alive, all objects in the group are alive.
3420 * After each garbage collection, object groups are removed. It is
3421 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003422 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003423 * wrapper objects. Object groups for all dependent handles need to
3424 * be provided for kGCTypeMarkSweepCompact collections, for all other
3425 * garbage collection types it is sufficient to provide object groups
3426 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003427 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003428 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003429 static void AddObjectGroup(Persistent<Value>* objects,
3430 size_t length,
3431 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003432 static void AddObjectGroup(Isolate* isolate,
3433 Persistent<Value>* objects,
3434 size_t length,
3435 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003436
3437 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003438 * Allows the host application to declare implicit references between
3439 * the objects: if |parent| is alive, all |children| are alive too.
3440 * After each garbage collection, all implicit references
3441 * are removed. It is intended to be used in the before-garbage-collection
3442 * callback function.
3443 */
3444 static void AddImplicitReferences(Persistent<Object> parent,
3445 Persistent<Value>* children,
3446 size_t length);
3447
3448 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003449 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003450 * initialize from scratch. This function is called implicitly if
3451 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003452 */
3453 static bool Initialize();
3454
kasper.lund7276f142008-07-30 08:49:36 +00003455 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003456 * Allows the host application to provide a callback which can be used
3457 * as a source of entropy for random number generators.
3458 */
3459 static void SetEntropySource(EntropySource source);
3460
3461 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003462 * Allows the host application to provide a callback that allows v8 to
3463 * cooperate with a profiler that rewrites return addresses on stack.
3464 */
3465 static void SetReturnAddressLocationResolver(
3466 ReturnAddressLocationResolver return_address_resolver);
3467
3468 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003469 * Allows the host application to provide the address of a function that's
3470 * invoked on entry to every V8-generated function.
3471 * Note that \p entry_hook is invoked at the very start of each
3472 * generated function.
3473 *
3474 * \param entry_hook a function that will be invoked on entry to every
3475 * V8-generated function.
3476 * \returns true on success on supported platforms, false on failure.
3477 * \note Setting a new entry hook function when one is already active will
3478 * fail.
3479 */
3480 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3481
3482 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003483 * Allows the host application to provide the address of a function that is
3484 * notified each time code is added, moved or removed.
3485 *
3486 * \param options options for the JIT code event handler.
3487 * \param event_handler the JIT code event handler, which will be invoked
3488 * each time code is added, moved or removed.
3489 * \note \p event_handler won't get notified of existent code.
3490 * \note since code removal notifications are not currently issued, the
3491 * \p event_handler may get notifications of code that overlaps earlier
3492 * code notifications. This happens when code areas are reused, and the
3493 * earlier overlapping code areas should therefore be discarded.
3494 * \note the events passed to \p event_handler and the strings they point to
3495 * are not guaranteed to live past each call. The \p event_handler must
3496 * copy strings and other parameters it needs to keep around.
3497 * \note the set of events declared in JitCodeEvent::EventType is expected to
3498 * grow over time, and the JitCodeEvent structure is expected to accrue
3499 * new members. The \p event_handler function must ignore event codes
3500 * it does not recognize to maintain future compatibility.
3501 */
3502 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3503 JitCodeEventHandler event_handler);
3504
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003505 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
3506 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003507 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3508 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003509
iposva@chromium.org245aa852009-02-10 00:49:54 +00003510 /**
3511 * Suspends recording of tick samples in the profiler.
3512 * When the V8 profiling mode is enabled (usually via command line
3513 * switches) this function suspends recording of tick samples.
3514 * Profiling ticks are discarded until ResumeProfiler() is called.
3515 *
3516 * See also the --prof and --prof_auto command line switches to
3517 * enable V8 profiling.
3518 */
3519 static void PauseProfiler();
3520
3521 /**
3522 * Resumes recording of tick samples in the profiler.
3523 * See also PauseProfiler().
3524 */
3525 static void ResumeProfiler();
3526
ager@chromium.org41826e72009-03-30 13:30:57 +00003527 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003528 * Return whether profiler is currently paused.
3529 */
3530 static bool IsProfilerPaused();
3531
3532 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003533 * Retrieve the V8 thread id of the calling thread.
3534 *
3535 * The thread id for a thread should only be retrieved after the V8
3536 * lock has been acquired with a Locker object with that thread.
3537 */
3538 static int GetCurrentThreadId();
3539
3540 /**
3541 * Forcefully terminate execution of a JavaScript thread. This can
3542 * be used to terminate long-running scripts.
3543 *
3544 * TerminateExecution should only be called when then V8 lock has
3545 * been acquired with a Locker object. Therefore, in order to be
3546 * able to terminate long-running threads, preemption must be
3547 * enabled to allow the user of TerminateExecution to acquire the
3548 * lock.
3549 *
3550 * The termination is achieved by throwing an exception that is
3551 * uncatchable by JavaScript exception handlers. Termination
3552 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003553 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003554 * exception handler that catches an exception should check if that
3555 * exception is a termination exception and immediately return if
3556 * that is the case. Returning immediately in that case will
3557 * continue the propagation of the termination exception if needed.
3558 *
3559 * The thread id passed to TerminateExecution must have been
3560 * obtained by calling GetCurrentThreadId on the thread in question.
3561 *
3562 * \param thread_id The thread id of the thread to terminate.
3563 */
3564 static void TerminateExecution(int thread_id);
3565
3566 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003567 * Forcefully terminate the current thread of JavaScript execution
3568 * in the given isolate. If no isolate is provided, the default
3569 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003570 *
3571 * This method can be used by any thread even if that thread has not
3572 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003573 *
3574 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003575 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003576 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003577
3578 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003579 * Is V8 terminating JavaScript execution.
3580 *
3581 * Returns true if JavaScript execution is currently terminating
3582 * because of a call to TerminateExecution. In that case there are
3583 * still JavaScript frames on the stack and the termination
3584 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003585 *
3586 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003587 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003588 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003589
3590 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003591 * Releases any resources used by v8 and stops any utility threads
3592 * that may be running. Note that disposing v8 is permanent, it
3593 * cannot be reinitialized.
3594 *
3595 * It should generally not be necessary to dispose v8 before exiting
3596 * a process, this should happen automatically. It is only necessary
3597 * to use if the process needs the resources taken up by v8.
3598 */
3599 static bool Dispose();
3600
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003601 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
3602 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003603
3604 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003605 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003606 * heap. GC is not invoked prior to iterating, therefore there is no
3607 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003608 */
3609 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3610
3611 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003612 * Iterates through all the persistent handles in the current isolate's heap
3613 * that have class_ids.
3614 */
3615 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3616
3617 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003618 * Iterates through all the persistent handles in the current isolate's heap
3619 * that have class_ids and are candidates to be marked as partially dependent
3620 * handles. This will visit handles to young objects created since the last
3621 * garbage collection but is free to visit an arbitrary superset of these
3622 * objects.
3623 */
3624 static void VisitHandlesForPartialDependence(
3625 Isolate* isolate, PersistentHandleVisitor* visitor);
3626
3627 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003628 * Optional notification that the embedder is idle.
3629 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003630 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003631 * Returns true if the embedder should stop calling IdleNotification
3632 * until real work has been done. This indicates that V8 has done
3633 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003634 *
3635 * The hint argument specifies the amount of work to be done in the function
3636 * on scale from 1 to 1000. There is no guarantee that the actual work will
3637 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003638 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003639 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003640
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003641 /**
3642 * Optional notification that the system is running low on memory.
3643 * V8 uses these notifications to attempt to free memory.
3644 */
3645 static void LowMemoryNotification();
3646
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003647 /**
3648 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003649 * these notifications to guide the GC heuristic. Returns the number
3650 * of context disposals - including this one - since the last time
3651 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003652 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003653 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003654
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003655 private:
3656 V8();
3657
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003658 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
3659 internal::Object** handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003660 static void DisposeGlobal(internal::Isolate* isolate,
3661 internal::Object** global_handle);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003662 static void MakeWeak(internal::Isolate* isolate,
3663 internal::Object** global_handle,
3664 void* data,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003665 WeakReferenceCallback weak_reference_callback,
3666 NearDeathCallback near_death_callback);
3667 static void ClearWeak(internal::Isolate* isolate,
3668 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003669
3670 template <class T> friend class Handle;
3671 template <class T> friend class Local;
3672 template <class T> friend class Persistent;
3673 friend class Context;
3674};
3675
3676
3677/**
3678 * An external exception handler.
3679 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003680class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003681 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003682 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003683 * Creates a new try/catch block and registers it with v8. Note that
3684 * all TryCatch blocks should be stack allocated because the memory
3685 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003686 */
3687 TryCatch();
3688
3689 /**
3690 * Unregisters and deletes this try/catch block.
3691 */
3692 ~TryCatch();
3693
3694 /**
3695 * Returns true if an exception has been caught by this try/catch block.
3696 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003697 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003698
3699 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003700 * For certain types of exceptions, it makes no sense to continue
3701 * execution.
3702 *
3703 * Currently, the only type of exception that can be caught by a
3704 * TryCatch handler and for which it does not make sense to continue
3705 * is termination exception. Such exceptions are thrown when the
3706 * TerminateExecution methods are called to terminate a long-running
3707 * script.
3708 *
3709 * If CanContinue returns false, the correct action is to perform
3710 * any C++ cleanup needed and then return.
3711 */
3712 bool CanContinue() const;
3713
3714 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003715 * Throws the exception caught by this TryCatch in a way that avoids
3716 * it being caught again by this same TryCatch. As with ThrowException
3717 * it is illegal to execute any JavaScript operations after calling
3718 * ReThrow; the caller must return immediately to where the exception
3719 * is caught.
3720 */
3721 Handle<Value> ReThrow();
3722
3723 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724 * Returns the exception caught by this try/catch block. If no exception has
3725 * been caught an empty handle is returned.
3726 *
3727 * The returned handle is valid until this TryCatch block has been destroyed.
3728 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003729 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003730
3731 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003732 * Returns the .stack property of the thrown object. If no .stack
3733 * property is present an empty handle is returned.
3734 */
3735 Local<Value> StackTrace() const;
3736
3737 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003738 * Returns the message associated with this exception. If there is
3739 * no message associated an empty handle is returned.
3740 *
3741 * The returned handle is valid until this TryCatch block has been
3742 * destroyed.
3743 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003744 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003745
3746 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003747 * Clears any exceptions that may have been caught by this try/catch block.
3748 * After this method has been called, HasCaught() will return false.
3749 *
3750 * It is not necessary to clear a try/catch block before using it again; if
3751 * another exception is thrown the previously caught exception will just be
3752 * overwritten. However, it is often a good idea since it makes it easier
3753 * to determine which operation threw a given exception.
3754 */
3755 void Reset();
3756
v8.team.kasperl727e9952008-09-02 14:56:44 +00003757 /**
3758 * Set verbosity of the external exception handler.
3759 *
3760 * By default, exceptions that are caught by an external exception
3761 * handler are not reported. Call SetVerbose with true on an
3762 * external exception handler to have exceptions caught by the
3763 * handler reported as if they were not caught.
3764 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003765 void SetVerbose(bool value);
3766
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003767 /**
3768 * Set whether or not this TryCatch should capture a Message object
3769 * which holds source information about where the exception
3770 * occurred. True by default.
3771 */
3772 void SetCaptureMessage(bool value);
3773
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003774 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003775 // Make it hard to create heap-allocated TryCatch blocks.
3776 TryCatch(const TryCatch&);
3777 void operator=(const TryCatch&);
3778 void* operator new(size_t size);
3779 void operator delete(void*, size_t);
3780
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003781 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003782 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003783 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003784 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003785 bool is_verbose_ : 1;
3786 bool can_continue_ : 1;
3787 bool capture_message_ : 1;
3788 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003789
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003790 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003791};
3792
3793
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003794// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795
3796
3797/**
3798 * Ignore
3799 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003800class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003801 public:
3802 ExtensionConfiguration(int name_count, const char* names[])
3803 : name_count_(name_count), names_(names) { }
3804 private:
3805 friend class ImplementationUtilities;
3806 int name_count_;
3807 const char** names_;
3808};
3809
3810
3811/**
3812 * A sandboxed execution context with its own set of built-in objects
3813 * and functions.
3814 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003815class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003817 /**
3818 * Returns the global proxy object or global object itself for
3819 * detached contexts.
3820 *
3821 * Global proxy object is a thin wrapper whose prototype points to
3822 * actual context's global object with the properties like Object, etc.
3823 * This is done that way for security reasons (for more details see
3824 * https://wiki.mozilla.org/Gecko:SplitWindow).
3825 *
3826 * Please note that changes to global proxy object prototype most probably
3827 * would break VM---v8 expects only global object as a prototype of
3828 * global proxy object.
3829 *
3830 * If DetachGlobal() has been invoked, Global() would return actual global
3831 * object until global is reattached with ReattachGlobal().
3832 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833 Local<Object> Global();
3834
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003835 /**
3836 * Detaches the global object from its context before
3837 * the global object can be reused to create a new context.
3838 */
3839 void DetachGlobal();
3840
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003841 /**
3842 * Reattaches a global object to a context. This can be used to
3843 * restore the connection between a global object and a context
3844 * after DetachGlobal has been called.
3845 *
3846 * \param global_object The global object to reattach to the
3847 * context. For this to work, the global object must be the global
3848 * object that was associated with this context before a call to
3849 * DetachGlobal.
3850 */
3851 void ReattachGlobal(Handle<Object> global_object);
3852
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003853 /** Creates a new context.
3854 *
3855 * Returns a persistent handle to the newly allocated context. This
3856 * persistent handle has to be disposed when the context is no
3857 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003858 *
3859 * \param extensions An optional extension configuration containing
3860 * the extensions to be installed in the newly created context.
3861 *
3862 * \param global_template An optional object template from which the
3863 * global object for the newly created context will be created.
3864 *
3865 * \param global_object An optional global object to be reused for
3866 * the newly created context. This global object must have been
3867 * created by a previous call to Context::New with the same global
3868 * template. The state of the global object will be completely reset
3869 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003870 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003871 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003872 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003873 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3874 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875
kasper.lund44510672008-07-25 07:37:58 +00003876 /** Returns the last entered context. */
3877 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003878
kasper.lund44510672008-07-25 07:37:58 +00003879 /** Returns the context that is on the top of the stack. */
3880 static Local<Context> GetCurrent();
3881
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003883 * Returns the context of the calling JavaScript code. That is the
3884 * context of the top-most JavaScript frame. If there are no
3885 * JavaScript frames an empty handle is returned.
3886 */
3887 static Local<Context> GetCalling();
3888
3889 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003890 * Sets the security token for the context. To access an object in
3891 * another context, the security tokens must match.
3892 */
3893 void SetSecurityToken(Handle<Value> token);
3894
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003895 /** Restores the security token to the default value. */
3896 void UseDefaultSecurityToken();
3897
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003898 /** Returns the security token of this context.*/
3899 Handle<Value> GetSecurityToken();
3900
v8.team.kasperl727e9952008-09-02 14:56:44 +00003901 /**
3902 * Enter this context. After entering a context, all code compiled
3903 * and run is compiled and run in this context. If another context
3904 * is already entered, this old context is saved so it can be
3905 * restored when the new context is exited.
3906 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003908
3909 /**
3910 * Exit this context. Exiting the current context restores the
3911 * context that was in place when entering the current context.
3912 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003913 void Exit();
3914
v8.team.kasperl727e9952008-09-02 14:56:44 +00003915 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003916 bool HasOutOfMemoryException();
3917
v8.team.kasperl727e9952008-09-02 14:56:44 +00003918 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919 static bool InContext();
3920
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003921 /** Returns an isolate associated with a current context. */
3922 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00003923
3924 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003925 * Gets the embedder data with the given index, which must have been set by a
3926 * previous call to SetEmbedderData with the same index. Note that index 0
3927 * currently has a special meaning for Chrome's debugger.
3928 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003929 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003930
3931 /**
3932 * Sets the embedder data with the given index, growing the data as
3933 * needed. Note that index 0 currently has a special meaning for Chrome's
3934 * debugger.
3935 */
3936 void SetEmbedderData(int index, Handle<Value> value);
3937
3938 /**
3939 * Gets a 2-byte-aligned native pointer from the embedder data with the given
3940 * index, which must have bees set by a previous call to
3941 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
3942 * currently has a special meaning for Chrome's debugger.
3943 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003944 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003945
3946 /**
3947 * Sets a 2-byte-aligned native pointer in the embedder data with the given
3948 * index, growing the data as needed. Note that index 0 currently has a
3949 * special meaning for Chrome's debugger.
3950 */
3951 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00003952
3953 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003954 * Control whether code generation from strings is allowed. Calling
3955 * this method with false will disable 'eval' and the 'Function'
3956 * constructor for code running in this context. If 'eval' or the
3957 * 'Function' constructor are used an exception will be thrown.
3958 *
3959 * If code generation from strings is not allowed the
3960 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3961 * set before blocking the call to 'eval' or the 'Function'
3962 * constructor. If that callback returns true, the call will be
3963 * allowed, otherwise an exception will be thrown. If no callback is
3964 * set an exception will be thrown.
3965 */
3966 void AllowCodeGenerationFromStrings(bool allow);
3967
3968 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003969 * Returns true if code generation from strings is allowed for the context.
3970 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3971 */
3972 bool IsCodeGenerationFromStringsAllowed();
3973
3974 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003975 * Sets the error description for the exception that is thrown when
3976 * code generation from strings is not allowed and 'eval' or the 'Function'
3977 * constructor are called.
3978 */
3979 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3980
3981 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982 * Stack-allocated class which sets the execution context for all
3983 * operations executed within a local scope.
3984 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003985 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003986 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003987 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003988 context_->Enter();
3989 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003990 V8_INLINE(~Scope()) { context_->Exit(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991 private:
3992 Handle<Context> context_;
3993 };
3994
3995 private:
3996 friend class Value;
3997 friend class Script;
3998 friend class Object;
3999 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004000
4001 Local<Value> SlowGetEmbedderData(int index);
4002 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003};
4004
4005
4006/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004007 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4008 * to use any given V8 isolate, see the comments in the Isolate class. The
4009 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4010 * object pointers obtained from V8 handles while in the particular V8 isolate.
4011 * It is up to the user of V8 to ensure, perhaps with locking, that this
4012 * constraint is not violated. In addition to any other synchronization
4013 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4014 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004015 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004016 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4017 * construction and destruction, the current thread is allowed to use the locked
4018 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4019 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004020 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004021 * Sample usage:
4022* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004023 * ...
4024 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004025 * v8::Locker locker(isolate);
4026 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004027 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004028 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004029 * ...
4030 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00004031 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004033 * If you wish to stop using V8 in a thread A you can do this either by
4034 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
4035 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004036 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004037 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004038 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004039 * isolate->Exit();
4040 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004041 * ...
4042 * // Code not using V8 goes here while V8 can run in another thread.
4043 * ...
4044 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004045 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004046 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004047 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004048 * The Unlocker object is intended for use in a long-running callback from V8,
4049 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004050 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004051 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
4052 * given thread. This can be useful if you have code that can be called either
4053 * from code that holds the lock or from code that does not. The Unlocker is
4054 * not recursive so you can not have several Unlockers on the stack at once, and
4055 * you can not use an Unlocker in a thread that is not inside a Locker's scope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004056 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004057 * An unlocker will unlock several lockers if it has to and reinstate the
4058 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004059 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004060 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004061 * // V8 not locked.
4062 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004063 * v8::Locker locker(isolate);
4064 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004065 * // V8 locked.
4066 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004067 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004068 * // V8 still locked (2 levels).
4069 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004070 * isolate->Exit();
4071 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004072 * // V8 not locked.
4073 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004074 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004075 * // V8 locked again (2 levels).
4076 * }
4077 * // V8 still locked (1 level).
4078 * }
4079 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004080 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004081 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004082class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004083 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004084 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004085 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004086 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004087 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
4088
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004089 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004090 V8_DEPRECATED(Unlocker());
4091
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004092 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00004093 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004094 void Initialize(Isolate* isolate);
4095
lrn@chromium.org1c092762011-05-09 09:42:16 +00004096 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004097};
4098
4099
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004100class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004101 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004102 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004103 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004104 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004105 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
4106
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004107 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004108 V8_DEPRECATED(Locker());
4109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004110 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004111
4112 /**
4113 * Start preemption.
4114 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004115 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004116 * that will switch between multiple threads that are in contention
4117 * for the V8 lock.
4118 */
4119 static void StartPreemption(int every_n_ms);
4120
4121 /**
4122 * Stop preemption.
4123 */
4124 static void StopPreemption();
4125
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004126 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004127 * Returns whether or not the locker for a given isolate, is locked by the
4128 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004129 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004130 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004131
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004132 /**
4133 * Returns whether v8::Locker is being used by this V8 instance.
4134 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004135 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004137 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004138 void Initialize(Isolate* isolate);
4139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004140 bool has_lock_;
4141 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004142 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004143
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004144 static bool active_;
4145
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004146 // Disallow copying and assigning.
4147 Locker(const Locker&);
4148 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004149};
4150
4151
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004152/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004153 * A struct for exporting HeapStats data from V8, using "push" model.
4154 */
4155struct HeapStatsUpdate;
4156
4157
4158/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004159 * An interface for exporting data from V8, using "push" model.
4160 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004161class V8EXPORT OutputStream { // NOLINT
4162 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004163 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004164 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004165 };
4166 enum WriteResult {
4167 kContinue = 0,
4168 kAbort = 1
4169 };
4170 virtual ~OutputStream() {}
4171 /** Notify about the end of stream. */
4172 virtual void EndOfStream() = 0;
4173 /** Get preferred output chunk size. Called only once. */
4174 virtual int GetChunkSize() { return 1024; }
4175 /** Get preferred output encoding. Called only once. */
4176 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4177 /**
4178 * Writes the next chunk of snapshot data into the stream. Writing
4179 * can be stopped by returning kAbort as function result. EndOfStream
4180 * will not be called in case writing was aborted.
4181 */
4182 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004183 /**
4184 * Writes the next chunk of heap stats data into the stream. Writing
4185 * can be stopped by returning kAbort as function result. EndOfStream
4186 * will not be called in case writing was aborted.
4187 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004188 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004189 return kAbort;
4190 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004191};
4192
4193
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004194/**
4195 * An interface for reporting progress and controlling long-running
4196 * activities.
4197 */
4198class V8EXPORT ActivityControl { // NOLINT
4199 public:
4200 enum ControlOption {
4201 kContinue = 0,
4202 kAbort = 1
4203 };
4204 virtual ~ActivityControl() {}
4205 /**
4206 * Notify about current progress. The activity can be stopped by
4207 * returning kAbort as the callback result.
4208 */
4209 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4210};
4211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004212
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004213// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004214
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004215
4216namespace internal {
4217
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004218const int kApiPointerSize = sizeof(void*); // NOLINT
4219const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004220
4221// Tag information for HeapObject.
4222const int kHeapObjectTag = 1;
4223const int kHeapObjectTagSize = 2;
4224const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4225
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004226// Tag information for Smi.
4227const int kSmiTag = 0;
4228const int kSmiTagSize = 1;
4229const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4230
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004231template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004232
4233// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004234template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004235 static const int kSmiShiftSize = 0;
4236 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004237 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004238 int shift_bits = kSmiTagSize + kSmiShiftSize;
4239 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4240 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4241 }
4242};
4243
4244// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004245template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004246 static const int kSmiShiftSize = 31;
4247 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004248 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004249 int shift_bits = kSmiTagSize + kSmiShiftSize;
4250 // Shift down and throw away top 32 bits.
4251 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4252 }
4253};
4254
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004255typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4256const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4257const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004258
4259/**
4260 * This class exports constants and functionality from within v8 that
4261 * is necessary to implement inline functions in the v8 api. Don't
4262 * depend on functions and constants defined here.
4263 */
4264class Internals {
4265 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004266 // These values match non-compiler-dependent values defined within
4267 // the implementation of v8.
4268 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004269 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004270 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004271
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004272 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004273 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004274 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004275 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4276 static const int kContextHeaderSize = 2 * kApiPointerSize;
4277 static const int kContextEmbedderDataIndex = 54;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004278 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004279 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004280 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004281 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004282
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004283 static const int kIsolateStateOffset = 0;
4284 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4285 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4286 static const int kUndefinedValueRootIndex = 5;
4287 static const int kNullValueRootIndex = 7;
4288 static const int kTrueValueRootIndex = 8;
4289 static const int kFalseValueRootIndex = 9;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004290 static const int kEmptyStringRootIndex = 119;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004291
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004292 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
4293 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
4294 static const int kNodeStateMask = 0xf;
4295 static const int kNodeStateIsWeakValue = 2;
4296 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004297 static const int kNodeIsIndependentShift = 4;
4298 static const int kNodeIsPartiallyDependentShift = 5;
4299
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004300 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004301 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004302 static const int kOddballType = 0x83;
4303 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004304
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004305 static const int kUndefinedOddballKind = 5;
4306 static const int kNullOddballKind = 3;
4307
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004308 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004309 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4310 kHeapObjectTag);
4311 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004312
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004313 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004314 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004315 }
4316
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004317 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004318 typedef internal::Object O;
4319 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4320 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4321 }
4322
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004323 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004324 typedef internal::Object O;
4325 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4326 }
4327
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004328 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004329 int representation = (instance_type & kFullStringRepresentationMask);
4330 return representation == kExternalTwoByteRepresentationTag;
4331 }
4332
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004333 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004334 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4335 return *reinterpret_cast<int*>(addr) == 1;
4336 }
4337
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004338 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
4339 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4340 return *addr & (1 << shift);
4341 }
4342
4343 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
4344 bool value, int shift)) {
4345 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4346 uint8_t mask = 1 << shift;
4347 *addr = (*addr & ~mask) | (value << shift);
4348 }
4349
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004350 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
4351 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4352 return *addr & kNodeStateMask;
4353 }
4354
4355 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
4356 uint8_t value)) {
4357 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4358 *addr = (*addr & ~kNodeStateMask) | value;
4359 }
4360
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004361 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004362 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4363 kIsolateEmbedderDataOffset;
4364 *reinterpret_cast<void**>(addr) = data;
4365 }
4366
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004367 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004368 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4369 kIsolateEmbedderDataOffset;
4370 return *reinterpret_cast<void**>(addr);
4371 }
4372
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004373 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
4374 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004375 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4376 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4377 }
4378
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004379 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004380 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004381 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4382 return *reinterpret_cast<T*>(addr);
4383 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004384
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004385 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004386 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004387 typedef internal::Object O;
4388 typedef internal::Internals I;
4389 O* ctx = *reinterpret_cast<O**>(context);
4390 int embedder_data_offset = I::kContextHeaderSize +
4391 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4392 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4393 int value_offset =
4394 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4395 return I::ReadField<T>(embedder_data, value_offset);
4396 }
4397
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004398 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
4399 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
4400 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
4401 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
4402 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
4403 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
4404 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004405};
4406
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004407} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004408
4409
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004410template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004411Local<T>::Local() : Handle<T>() { }
4412
4413
4414template <class T>
4415Local<T> Local<T>::New(Handle<T> that) {
4416 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004417 T* that_ptr = *that;
4418 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4419 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4420 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4421 reinterpret_cast<internal::HeapObject*>(*p))));
4422 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004423 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4424}
4425
4426
4427template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004428Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004429 if (that.IsEmpty()) return Local<T>();
4430 T* that_ptr = *that;
4431 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4432 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4433 reinterpret_cast<internal::Isolate*>(isolate), *p)));
4434}
4435
4436
4437template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004438Persistent<T> Persistent<T>::New(Handle<T> that) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004439 return New(Isolate::GetCurrent(), that);
4440}
4441
4442
4443template <class T>
4444Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004445 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004446 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004447 return Persistent<T>(reinterpret_cast<T*>(
4448 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
4449 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004450}
4451
4452
4453template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004454bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004455 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004456}
4457
4458
4459template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004460bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004461 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004462 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004463 if (!I::IsInitialized(isolate)) return false;
4464 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(**this),
4465 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004466}
4467
4468
4469template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004470bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004471 return IsNearDeath(Isolate::GetCurrent());
4472}
4473
4474
4475template <class T>
4476bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
4477 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004478 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004479 if (!I::IsInitialized(isolate)) return false;
4480 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4481 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004482}
4483
4484
4485template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004486bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004487 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004488}
4489
4490
4491template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004492bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004493 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004494 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004495 if (!I::IsInitialized(isolate)) return false;
4496 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4497 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004498}
4499
4500
4501template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004502void Persistent<T>::Dispose() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004503 Dispose(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004504}
4505
4506
4507template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004508void Persistent<T>::Dispose(Isolate* isolate) {
4509 if (this->IsEmpty()) return;
4510 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4511 reinterpret_cast<internal::Object**>(**this));
4512}
4513
4514
4515template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004516Persistent<T>::Persistent() : Handle<T>() { }
4517
4518template <class T>
4519void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004520 Isolate* isolate = Isolate::GetCurrent();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004521 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4522 reinterpret_cast<internal::Object**>(**this),
4523 parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004524 callback,
4525 NULL);
4526}
4527
4528template <class T>
4529void Persistent<T>::MakeWeak(Isolate* isolate,
4530 void* parameters,
4531 NearDeathCallback callback) {
4532 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4533 reinterpret_cast<internal::Object**>(**this),
4534 parameters,
4535 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004536 callback);
4537}
4538
4539template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004540void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004541 ClearWeak(Isolate::GetCurrent());
4542}
4543
4544template <class T>
4545void Persistent<T>::ClearWeak(Isolate* isolate) {
4546 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
4547 reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004548}
4549
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004550template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004551void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004552 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004553}
4554
4555template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004556void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004557 typedef internal::Internals I;
4558 if (this->IsEmpty()) return;
4559 if (!I::IsInitialized(isolate)) return;
4560 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004561 true,
4562 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004563}
4564
4565template <class T>
4566void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004567 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004568}
4569
4570template <class T>
4571void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004572 typedef internal::Internals I;
4573 if (this->IsEmpty()) return;
4574 if (!I::IsInitialized(isolate)) return;
4575 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004576 true,
4577 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004578}
4579
4580template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004581void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004582 SetWrapperClassId(Isolate::GetCurrent(), class_id);
4583}
4584
4585template <class T>
4586void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004587 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004588 if (this->IsEmpty()) return;
4589 if (!I::IsInitialized(isolate)) return;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004590 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4591 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4592 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004593}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004594
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004595template <class T>
4596uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004597 return WrapperClassId(Isolate::GetCurrent());
4598}
4599
4600template <class T>
4601uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004602 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004603 if (this->IsEmpty()) return 0;
4604 if (!I::IsInitialized(isolate)) return 0;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004605 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4606 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4607 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004608}
4609
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004610Arguments::Arguments(internal::Object** implicit_args,
4611 internal::Object** values, int length,
4612 bool is_construct_call)
4613 : implicit_args_(implicit_args),
4614 values_(values),
4615 length_(length),
4616 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004617
4618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004619Local<Value> Arguments::operator[](int i) const {
4620 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4621 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4622}
4623
4624
4625Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004626 return Local<Function>(reinterpret_cast<Function*>(
4627 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004628}
4629
4630
4631Local<Object> Arguments::This() const {
4632 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4633}
4634
4635
4636Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004637 return Local<Object>(reinterpret_cast<Object*>(
4638 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004639}
4640
4641
4642Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004643 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004644}
4645
4646
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004647Isolate* Arguments::GetIsolate() const {
4648 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4649}
4650
4651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004652bool Arguments::IsConstructCall() const {
4653 return is_construct_call_;
4654}
4655
4656
4657int Arguments::Length() const {
4658 return length_;
4659}
4660
4661
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004662template <class T>
4663Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004664 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4665 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004666 return Local<T>(reinterpret_cast<T*>(after));
4667}
4668
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004669Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004670 return resource_name_;
4671}
4672
4673
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004674Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004675 return resource_line_offset_;
4676}
4677
4678
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004679Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004680 return resource_column_offset_;
4681}
4682
4683
4684Handle<Boolean> Boolean::New(bool value) {
4685 return value ? True() : False();
4686}
4687
4688
4689void Template::Set(const char* name, v8::Handle<Data> value) {
4690 Set(v8::String::New(name), value);
4691}
4692
4693
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004694Local<Value> Object::GetInternalField(int index) {
4695#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004696 typedef internal::Object O;
4697 typedef internal::Internals I;
4698 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004699 // Fast path: If the object is a plain JSObject, which is the common case, we
4700 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004701 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004702 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004703 O* value = I::ReadField<O*>(obj, offset);
4704 O** result = HandleScope::CreateHandle(value);
4705 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004706 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004707#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004708 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004709}
4710
4711
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004712void* Object::GetAlignedPointerFromInternalField(int index) {
4713#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004714 typedef internal::Object O;
4715 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004716 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004717 // Fast path: If the object is a plain JSObject, which is the common case, we
4718 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004719 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004720 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004721 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004722 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004723#endif
4724 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004725}
4726
4727
4728String* String::Cast(v8::Value* value) {
4729#ifdef V8_ENABLE_CHECKS
4730 CheckCast(value);
4731#endif
4732 return static_cast<String*>(value);
4733}
4734
4735
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004736Local<String> String::Empty(Isolate* isolate) {
4737 typedef internal::Object* S;
4738 typedef internal::Internals I;
4739 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004740 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004741 return Local<String>(reinterpret_cast<String*>(slot));
4742}
4743
4744
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004745String::ExternalStringResource* String::GetExternalStringResource() const {
4746 typedef internal::Object O;
4747 typedef internal::Internals I;
4748 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004749 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004750 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004751 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4752 result = reinterpret_cast<String::ExternalStringResource*>(value);
4753 } else {
4754 result = NULL;
4755 }
4756#ifdef V8_ENABLE_CHECKS
4757 VerifyExternalStringResource(result);
4758#endif
4759 return result;
4760}
4761
4762
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004763String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4764 String::Encoding* encoding_out) const {
4765 typedef internal::Object O;
4766 typedef internal::Internals I;
4767 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4768 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4769 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4770 ExternalStringResourceBase* resource = NULL;
4771 if (type == I::kExternalAsciiRepresentationTag ||
4772 type == I::kExternalTwoByteRepresentationTag) {
4773 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4774 resource = static_cast<ExternalStringResourceBase*>(value);
4775 }
4776#ifdef V8_ENABLE_CHECKS
4777 VerifyExternalStringResourceBase(resource, *encoding_out);
4778#endif
4779 return resource;
4780}
4781
4782
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004783bool Value::IsUndefined() const {
4784#ifdef V8_ENABLE_CHECKS
4785 return FullIsUndefined();
4786#else
4787 return QuickIsUndefined();
4788#endif
4789}
4790
4791bool Value::QuickIsUndefined() const {
4792 typedef internal::Object O;
4793 typedef internal::Internals I;
4794 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4795 if (!I::HasHeapObjectTag(obj)) return false;
4796 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4797 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4798}
4799
4800
4801bool Value::IsNull() const {
4802#ifdef V8_ENABLE_CHECKS
4803 return FullIsNull();
4804#else
4805 return QuickIsNull();
4806#endif
4807}
4808
4809bool Value::QuickIsNull() const {
4810 typedef internal::Object O;
4811 typedef internal::Internals I;
4812 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4813 if (!I::HasHeapObjectTag(obj)) return false;
4814 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4815 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4816}
4817
4818
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004819bool Value::IsString() const {
4820#ifdef V8_ENABLE_CHECKS
4821 return FullIsString();
4822#else
4823 return QuickIsString();
4824#endif
4825}
4826
4827bool Value::QuickIsString() const {
4828 typedef internal::Object O;
4829 typedef internal::Internals I;
4830 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4831 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004832 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004833}
4834
4835
4836Number* Number::Cast(v8::Value* value) {
4837#ifdef V8_ENABLE_CHECKS
4838 CheckCast(value);
4839#endif
4840 return static_cast<Number*>(value);
4841}
4842
4843
4844Integer* Integer::Cast(v8::Value* value) {
4845#ifdef V8_ENABLE_CHECKS
4846 CheckCast(value);
4847#endif
4848 return static_cast<Integer*>(value);
4849}
4850
4851
4852Date* Date::Cast(v8::Value* value) {
4853#ifdef V8_ENABLE_CHECKS
4854 CheckCast(value);
4855#endif
4856 return static_cast<Date*>(value);
4857}
4858
4859
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004860StringObject* StringObject::Cast(v8::Value* value) {
4861#ifdef V8_ENABLE_CHECKS
4862 CheckCast(value);
4863#endif
4864 return static_cast<StringObject*>(value);
4865}
4866
4867
4868NumberObject* NumberObject::Cast(v8::Value* value) {
4869#ifdef V8_ENABLE_CHECKS
4870 CheckCast(value);
4871#endif
4872 return static_cast<NumberObject*>(value);
4873}
4874
4875
4876BooleanObject* BooleanObject::Cast(v8::Value* value) {
4877#ifdef V8_ENABLE_CHECKS
4878 CheckCast(value);
4879#endif
4880 return static_cast<BooleanObject*>(value);
4881}
4882
4883
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004884RegExp* RegExp::Cast(v8::Value* value) {
4885#ifdef V8_ENABLE_CHECKS
4886 CheckCast(value);
4887#endif
4888 return static_cast<RegExp*>(value);
4889}
4890
4891
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004892Object* Object::Cast(v8::Value* value) {
4893#ifdef V8_ENABLE_CHECKS
4894 CheckCast(value);
4895#endif
4896 return static_cast<Object*>(value);
4897}
4898
4899
4900Array* Array::Cast(v8::Value* value) {
4901#ifdef V8_ENABLE_CHECKS
4902 CheckCast(value);
4903#endif
4904 return static_cast<Array*>(value);
4905}
4906
4907
4908Function* Function::Cast(v8::Value* value) {
4909#ifdef V8_ENABLE_CHECKS
4910 CheckCast(value);
4911#endif
4912 return static_cast<Function*>(value);
4913}
4914
4915
4916External* External::Cast(v8::Value* value) {
4917#ifdef V8_ENABLE_CHECKS
4918 CheckCast(value);
4919#endif
4920 return static_cast<External*>(value);
4921}
4922
4923
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004924Isolate* AccessorInfo::GetIsolate() const {
4925 return *reinterpret_cast<Isolate**>(&args_[-3]);
4926}
4927
4928
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004929Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004930 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004931}
4932
4933
4934Local<Object> AccessorInfo::This() const {
4935 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4936}
4937
4938
4939Local<Object> AccessorInfo::Holder() const {
4940 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4941}
4942
4943
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004944Handle<Primitive> Undefined(Isolate* isolate) {
4945 typedef internal::Object* S;
4946 typedef internal::Internals I;
4947 if (!I::IsInitialized(isolate)) return Undefined();
4948 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4949 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4950}
4951
4952
4953Handle<Primitive> Null(Isolate* isolate) {
4954 typedef internal::Object* S;
4955 typedef internal::Internals I;
4956 if (!I::IsInitialized(isolate)) return Null();
4957 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4958 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4959}
4960
4961
4962Handle<Boolean> True(Isolate* isolate) {
4963 typedef internal::Object* S;
4964 typedef internal::Internals I;
4965 if (!I::IsInitialized(isolate)) return True();
4966 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4967 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4968}
4969
4970
4971Handle<Boolean> False(Isolate* isolate) {
4972 typedef internal::Object* S;
4973 typedef internal::Internals I;
4974 if (!I::IsInitialized(isolate)) return False();
4975 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4976 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4977}
4978
4979
4980void Isolate::SetData(void* data) {
4981 typedef internal::Internals I;
4982 I::SetEmbedderData(this, data);
4983}
4984
4985
4986void* Isolate::GetData() {
4987 typedef internal::Internals I;
4988 return I::GetEmbedderData(this);
4989}
4990
4991
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004992Local<Value> Context::GetEmbedderData(int index) {
4993#ifndef V8_ENABLE_CHECKS
4994 typedef internal::Object O;
4995 typedef internal::Internals I;
4996 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
4997 return Local<Value>(reinterpret_cast<Value*>(result));
4998#else
4999 return SlowGetEmbedderData(index);
5000#endif
5001}
5002
5003
5004void* Context::GetAlignedPointerFromEmbedderData(int index) {
5005#ifndef V8_ENABLE_CHECKS
5006 typedef internal::Internals I;
5007 return I::ReadEmbedderData<void*>(this, index);
5008#else
5009 return SlowGetAlignedPointerFromEmbedderData(index);
5010#endif
5011}
5012
5013
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005014/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00005015 * \example shell.cc
5016 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005017 * command-line and executes them.
5018 */
5019
5020
5021/**
5022 * \example process.cc
5023 */
5024
5025
5026} // namespace v8
5027
5028
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005029#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005030#undef TYPE_CHECK
5031
5032
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005033#endif // V8_H_