blob: 1426f572f972478c719e1171e3c2b7c80ad69b87 [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
yangguo@chromium.org46a2a512013-01-18 16:29:40 +000041// TODO(svenpanne) Remove me when the Chrome bindings are adapted.
42#define V8_DISABLE_DEPRECATIONS 1
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +000043// TODO(dcarney): Remove once Latin-1 transitions in WebKit has stuck.
44#define V8_ONE_BYTE_STRINGS_ENABLED 1
yangguo@chromium.org46a2a512013-01-18 16:29:40 +000045
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000046#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
48#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000049
50// Setup for Windows DLL export/import. When building the V8 DLL the
51// BUILDING_V8_SHARED needs to be defined. When building a program which uses
52// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
53// static library or building a program which uses the V8 static library neither
54// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
56#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
57 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058#endif
59
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000060#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000061#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000062#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000063#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000064#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000065#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000066#endif // BUILDING_V8_SHARED
67
68#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000069
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000071#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
72 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000073#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000075#else
76#define V8EXPORT
77#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000078#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000079#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000080#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000081
82#endif // _WIN32
83
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000084#if defined(__GNUC__) && !defined(DEBUG)
85#define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
86#elif defined(_MSC_VER) && !defined(DEBUG)
87#define V8_INLINE(declarator) __forceinline declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000088#else
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000089#define V8_INLINE(declarator) inline declarator
90#endif
91
92#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
93#define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
94#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
95#define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
96#else
97#define V8_DEPRECATED(declarator) declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000098#endif
99
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000101 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 */
103namespace v8 {
104
105class Context;
106class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000107class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108class Value;
109class Utils;
110class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000111class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112class Object;
113class Array;
114class Int32;
115class Uint32;
116class External;
117class Primitive;
118class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000119class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120class Integer;
121class Function;
122class Date;
123class ImplementationUtilities;
124class Signature;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000125class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126template <class T> class Handle;
127template <class T> class Local;
128template <class T> class Persistent;
129class FunctionTemplate;
130class ObjectTemplate;
131class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000132class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000133class StackTrace;
134class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000135class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000137namespace internal {
138
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000139class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000140class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000141class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000142class HeapObject;
143class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000144}
145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000147// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148
149
150/**
151 * A weak reference callback function.
152 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000153 * This callback should either explicitly invoke Dispose on |object| if
154 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
155 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 * \param object the weak global object to be reclaimed by the garbage collector
157 * \param parameter the value passed in when making the weak global object
158 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000159typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160 void* parameter);
161
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000162// TODO(svenpanne) Temporary definition until Chrome is in sync.
163typedef void (*NearDeathCallback)(Isolate* isolate,
164 Persistent<Value> object,
165 void* parameter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000167// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000169#define TYPE_CHECK(T, S) \
170 while (false) { \
171 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 }
173
174/**
175 * An object reference managed by the v8 garbage collector.
176 *
177 * All objects returned from v8 have to be tracked by the garbage
178 * collector so that it knows that the objects are still alive. Also,
179 * because the garbage collector may move objects, it is unsafe to
180 * point directly to an object. Instead, all objects are stored in
181 * handles which are known by the garbage collector and updated
182 * whenever an object moves. Handles should always be passed by value
183 * (except in cases like out-parameters) and they should never be
184 * allocated on the heap.
185 *
186 * There are two types of handles: local and persistent handles.
187 * Local handles are light-weight and transient and typically used in
188 * local operations. They are managed by HandleScopes. Persistent
189 * handles can be used when storing objects across several independent
190 * operations and have to be explicitly deallocated when they're no
191 * longer used.
192 *
193 * It is safe to extract the object stored in the handle by
194 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000195 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196 * behind the scenes and the same rules apply to these values as to
197 * their handles.
198 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000199template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 /**
202 * Creates an empty handle.
203 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000204 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205
206 /**
207 * Creates a new handle for the specified value.
208 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000209 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210
211 /**
212 * Creates a handle for the contents of the specified handle. This
213 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000214 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000216 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217 * between compatible handles, for instance assigning a
218 * Handle<String> to a variable declared as Handle<Value>, is legal
219 * because String is a subclass of Value.
220 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000221 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 : val_(reinterpret_cast<T*>(*that)) {
223 /**
224 * This check fails when trying to convert between incompatible
225 * handles. For example, converting from a Handle<String> to a
226 * Handle<Number>.
227 */
228 TYPE_CHECK(T, S);
229 }
230
231 /**
232 * Returns true if the handle is empty.
233 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000234 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236 /**
237 * Sets the handle to be empty. IsEmpty() will then return true.
238 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000239 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000240
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000241 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000242
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000243 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244
245 /**
246 * Checks whether two handles are the same.
247 * Returns true if both are empty, or if the objects
248 * to which they refer are identical.
249 * The handles' references are not checked.
250 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000251 template <class S> V8_INLINE(bool operator==(Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000252 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
253 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254 if (a == 0) return b == 0;
255 if (b == 0) return false;
256 return *a == *b;
257 }
258
259 /**
260 * Checks whether two handles are different.
261 * Returns true if only one of the handles is empty, or if
262 * the objects to which they refer are different.
263 * The handles' references are not checked.
264 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000265 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 return !operator==(that);
267 }
268
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000269 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000270#ifdef V8_ENABLE_CHECKS
271 // If we're going to perform the type check then we have to check
272 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000274#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 return Handle<T>(T::Cast(*that));
276 }
277
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000278 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000279 return Handle<S>::Cast(*this);
280 }
281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282 private:
283 T* val_;
284};
285
286
287/**
288 * A light-weight stack-allocated object handle. All operations
289 * that return objects from within v8 return them in local handles. They
290 * are created within HandleScopes, and all local handles allocated within a
291 * handle scope are destroyed when the handle scope is destroyed. Hence it
292 * is not necessary to explicitly deallocate local handles.
293 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000294template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000296 V8_INLINE(Local());
297 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 : Handle<T>(reinterpret_cast<T*>(*that)) {
299 /**
300 * This check fails when trying to convert between incompatible
301 * handles. For example, converting from a Handle<String> to a
302 * Handle<Number>.
303 */
304 TYPE_CHECK(T, S);
305 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000306 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
307 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000308#ifdef V8_ENABLE_CHECKS
309 // If we're going to perform the type check then we have to check
310 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000312#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 return Local<T>(T::Cast(*that));
314 }
315
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000316 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000317 return Local<S>::Cast(*this);
318 }
319
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000320 /**
321 * Create a local handle for the content of another handle.
322 * The referee is kept alive by the local handle even when
323 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000325 V8_INLINE(static Local<T> New(Handle<T> that));
326 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327};
328
329
330/**
331 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000332 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 * allocated, a Persistent handle remains valid until it is explicitly
334 * disposed.
335 *
336 * A persistent handle contains a reference to a storage cell within
337 * the v8 engine which holds an object value and which is updated by
338 * the garbage collector whenever the object is moved. A new storage
339 * cell can be created using Persistent::New and existing handles can
340 * be disposed using Persistent::Dispose. Since persistent handles
341 * are passed by value you may have many persistent handle objects
342 * that point to the same storage cell. For instance, if you pass a
343 * persistent handle as an argument to a function you will not get two
344 * different storage cells but rather two references to the same
345 * storage cell.
346 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000347template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 /**
350 * Creates an empty persistent handle that doesn't point to any
351 * storage cell.
352 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000353 V8_INLINE(Persistent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354
355 /**
356 * Creates a persistent handle for the same storage cell as the
357 * specified handle. This constructor allows you to pass persistent
358 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000359 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000361 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 * between compatible persistent handles, for instance assigning a
363 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000364 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000366 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 : Handle<T>(reinterpret_cast<T*>(*that)) {
368 /**
369 * This check fails when trying to convert between incompatible
370 * handles. For example, converting from a Handle<String> to a
371 * Handle<Number>.
372 */
373 TYPE_CHECK(T, S);
374 }
375
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000376 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000378 /**
379 * "Casts" a plain handle which is known to be a persistent handle
380 * to a persistent handle.
381 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000382 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 : Handle<T>(*that) { }
384
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000385 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000386#ifdef V8_ENABLE_CHECKS
387 // If we're going to perform the type check then we have to check
388 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000390#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391 return Persistent<T>(T::Cast(*that));
392 }
393
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000394 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000395 return Persistent<S>::Cast(*this);
396 }
397
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000398 /** Deprecated. Use Isolate version instead. */
399 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000402 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000404 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
405
406 /** Deprecated. Use Isolate version instead. */
407 V8_DEPRECATED(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408
409 /**
410 * Releases the storage cell referenced by this persistent handle.
411 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000412 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 * cell remain and IsEmpty will still return false.
414 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000415 V8_INLINE(void Dispose(Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000417 /** Deprecated. Use Isolate version instead. */
418 V8_DEPRECATED(void MakeWeak(void* parameters,
419 WeakReferenceCallback callback));
420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421 /**
422 * Make the reference to this object weak. When only weak handles
423 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000424 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 * it the object reference and the given parameters.
426 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000427 V8_INLINE(void MakeWeak(Isolate* isolate,
428 void* parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000429 NearDeathCallback callback));
430
431 /** Deprecated. Use Isolate version instead. */
432 V8_DEPRECATED(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000434 /** Clears the weak reference to this object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000435 V8_INLINE(void ClearWeak(Isolate* isolate));
436
437 /** Deprecated. Use Isolate version instead. */
438 V8_DEPRECATED(void MarkIndependent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439
440 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000441 * Marks the reference to this object independent. Garbage collector is free
442 * to ignore any object groups containing this object. Weak callback for an
443 * independent handle should not assume that it will be preceded by a global
444 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000445 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000446 V8_INLINE(void MarkIndependent(Isolate* isolate));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000447
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000448 /** Deprecated. Use Isolate version instead. */
449 V8_DEPRECATED(void MarkPartiallyDependent());
450
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000451 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000452 * Marks the reference to this object partially dependent. Partially dependent
453 * handles only depend on other partially dependent handles and these
454 * dependencies are provided through object groups. It provides a way to build
455 * smaller object groups for young objects that represent only a subset of all
456 * external dependencies. This mark is automatically cleared after each
457 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000458 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000459 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000460
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000461 /** Deprecated. Use Isolate version instead. */
462 V8_DEPRECATED(bool IsIndependent() const);
463
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000464 /** Returns true if this handle was previously marked as independent. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000465 V8_INLINE(bool IsIndependent(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000466
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000467 /** Deprecated. Use Isolate version instead. */
468 V8_DEPRECATED(bool IsNearDeath() const);
469
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000470 /** Checks if the handle holds the only reference to an object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000471 V8_INLINE(bool IsNearDeath(Isolate* isolate) const);
472
473 /** Deprecated. Use Isolate version instead. */
474 V8_DEPRECATED(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000476 /** Returns true if the handle's reference is weak. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000477 V8_INLINE(bool IsWeak(Isolate* isolate) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000479 /** Deprecated. Use Isolate version instead. */
480 V8_DEPRECATED(void SetWrapperClassId(uint16_t class_id));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000481
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000482 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000483 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
484 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000485 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000486 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id));
487
488 /** Deprecated. Use Isolate version instead. */
489 V8_DEPRECATED(uint16_t WrapperClassId() const);
490
491 /**
492 * Returns the class ID previously assigned to this handle or 0 if no class ID
493 * was previously assigned.
494 */
495 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 private:
498 friend class ImplementationUtilities;
499 friend class ObjectTemplate;
500};
501
502
v8.team.kasperl727e9952008-09-02 14:56:44 +0000503 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 * A stack-allocated class that governs a number of local handles.
505 * After a handle scope has been created, all local handles will be
506 * allocated within that handle scope until either the handle scope is
507 * deleted or another handle scope is created. If there is already a
508 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000509 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510 * new handles will again be allocated in the original handle scope.
511 *
512 * After the handle scope of a local handle has been deleted the
513 * garbage collector will no longer track the object stored in the
514 * handle and may deallocate it. The behavior of accessing a handle
515 * for which the handle scope has been deleted is undefined.
516 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000517class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000519 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
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
lrn@chromium.org303ada72010-10-27 09:33:13 +0000564 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000567 internal::Object** prev_next_;
568 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000570 // Allow for the active closing of HandleScopes which allows to pass a handle
571 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000573 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 friend class ImplementationUtilities;
576};
577
578
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000579// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580
581
582/**
583 * The superclass of values and API object templates.
584 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000585class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 private:
587 Data();
588};
589
590
591/**
592 * Pre-compilation data that can be associated with a script. This
593 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000594 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000595 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000597class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 public:
599 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000600
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000601 /**
602 * Pre-compiles the specified script (context-independent).
603 *
604 * \param input Pointer to UTF-8 script source code.
605 * \param length Length of UTF-8 script source code.
606 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000609 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000610 * Pre-compiles the specified script (context-independent).
611 *
612 * NOTE: Pre-compilation using this method cannot happen on another thread
613 * without using Lockers.
614 *
615 * \param source Script source code.
616 */
617 static ScriptData* PreCompile(Handle<String> source);
618
619 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000620 * Load previous pre-compilation data.
621 *
622 * \param data Pointer to data returned by a call to Data() of a previous
623 * ScriptData. Ownership is not transferred.
624 * \param length Length of data.
625 */
626 static ScriptData* New(const char* data, int length);
627
628 /**
629 * Returns the length of Data().
630 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000632
633 /**
634 * Returns a serialized representation of this ScriptData that can later be
635 * passed to New(). NOTE: Serialized data is platform-dependent.
636 */
637 virtual const char* Data() = 0;
638
639 /**
640 * Returns true if the source code could not be parsed.
641 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000642 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643};
644
645
646/**
647 * The origin, within a file, of a script.
648 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000649class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000651 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000652 Handle<Value> resource_name,
653 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000654 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 : resource_name_(resource_name),
656 resource_line_offset_(resource_line_offset),
657 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000658 V8_INLINE(Handle<Value> ResourceName() const);
659 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
660 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000662 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 Handle<Integer> resource_line_offset_;
664 Handle<Integer> resource_column_offset_;
665};
666
667
668/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000669 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000671class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000674 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000675 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000676 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000677 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000678 * when New() returns
679 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
680 * using pre_data speeds compilation if it's done multiple times.
681 * Owned by caller, no references are kept when New() returns.
682 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000683 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000684 * available to compile event handlers.
685 * \return Compiled script object (context independent; when run it
686 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000688 static Local<Script> New(Handle<String> source,
689 ScriptOrigin* origin = NULL,
690 ScriptData* pre_data = NULL,
691 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692
mads.s.agercbaa0602008-08-14 13:41:48 +0000693 /**
694 * Compiles the specified script using the specified file name
695 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000696 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000697 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000698 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000699 * as the script's origin.
700 * \return Compiled script object (context independent; when run it
701 * will use the currently entered context).
702 */
703 static Local<Script> New(Handle<String> source,
704 Handle<Value> file_name);
705
706 /**
707 * Compiles the specified script (bound to current context).
708 *
709 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000710 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000711 * when Compile() returns
712 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
713 * using pre_data speeds compilation if it's done multiple times.
714 * Owned by caller, no references are kept when Compile() returns.
715 * \param script_data Arbitrary data associated with script. Using
716 * this has same effect as calling SetData(), but makes data available
717 * earlier (i.e. to compile event handlers).
718 * \return Compiled script object, bound to the context that was active
719 * when this function was called. When run it will always use this
720 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000721 */
722 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000723 ScriptOrigin* origin = NULL,
724 ScriptData* pre_data = NULL,
725 Handle<String> script_data = Handle<String>());
726
727 /**
728 * Compiles the specified script using the specified file name
729 * object (typically a string) as the script's origin.
730 *
731 * \param source Script source code.
732 * \param file_name File name to use as script's origin
733 * \param script_data Arbitrary data associated with script. Using
734 * this has same effect as calling SetData(), but makes data available
735 * earlier (i.e. to compile event handlers).
736 * \return Compiled script object, bound to the context that was active
737 * when this function was called. When run it will always use this
738 * context.
739 */
740 static Local<Script> Compile(Handle<String> source,
741 Handle<Value> file_name,
742 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000743
v8.team.kasperl727e9952008-09-02 14:56:44 +0000744 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000745 * Runs the script returning the resulting value. If the script is
746 * context independent (created using ::New) it will be run in the
747 * currently entered context. If it is context specific (created
748 * using ::Compile) it will be run in the context in which it was
749 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000750 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000752
753 /**
754 * Returns the script id value.
755 */
756 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000757
758 /**
759 * Associate an additional data object with the script. This is mainly used
760 * with the debugger as this data object is only available through the
761 * debugger API.
762 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000763 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764};
765
766
767/**
768 * An error message.
769 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000770class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000772 Local<String> Get() const;
773 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000775 /**
776 * Returns the resource name for the script from where the function causing
777 * the error originates.
778 */
ager@chromium.org32912102009-01-16 10:38:43 +0000779 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000781 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000782 * Returns the resource data for the script from where the function causing
783 * the error originates.
784 */
785 Handle<Value> GetScriptData() const;
786
787 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000788 * Exception stack trace. By default stack traces are not captured for
789 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
790 * to change this option.
791 */
792 Handle<StackTrace> GetStackTrace() const;
793
794 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000795 * Returns the number, 1-based, of the line where the error occurred.
796 */
ager@chromium.org32912102009-01-16 10:38:43 +0000797 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000799 /**
800 * Returns the index within the script of the first character where
801 * the error occurred.
802 */
ager@chromium.org32912102009-01-16 10:38:43 +0000803 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000804
805 /**
806 * Returns the index within the script of the last character where
807 * the error occurred.
808 */
ager@chromium.org32912102009-01-16 10:38:43 +0000809 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000810
811 /**
812 * Returns the index within the line of the first character where
813 * the error occurred.
814 */
ager@chromium.org32912102009-01-16 10:38:43 +0000815 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000816
817 /**
818 * Returns the index within the line of the last character where
819 * the error occurred.
820 */
ager@chromium.org32912102009-01-16 10:38:43 +0000821 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000822
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823 // TODO(1245381): Print to a string instead of on a FILE.
824 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000825
826 static const int kNoLineNumberInfo = 0;
827 static const int kNoColumnInfo = 0;
828};
829
830
831/**
832 * Representation of a JavaScript stack trace. The information collected is a
833 * snapshot of the execution stack and the information remains valid after
834 * execution continues.
835 */
836class V8EXPORT StackTrace {
837 public:
838 /**
839 * Flags that determine what information is placed captured for each
840 * StackFrame when grabbing the current stack trace.
841 */
842 enum StackTraceOptions {
843 kLineNumber = 1,
844 kColumnOffset = 1 << 1 | kLineNumber,
845 kScriptName = 1 << 2,
846 kFunctionName = 1 << 3,
847 kIsEval = 1 << 4,
848 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000849 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000850 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000851 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000852 };
853
854 /**
855 * Returns a StackFrame at a particular index.
856 */
857 Local<StackFrame> GetFrame(uint32_t index) const;
858
859 /**
860 * Returns the number of StackFrames.
861 */
862 int GetFrameCount() const;
863
864 /**
865 * Returns StackTrace as a v8::Array that contains StackFrame objects.
866 */
867 Local<Array> AsArray();
868
869 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000870 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000871 *
872 * \param frame_limit The maximum number of stack frames we want to capture.
873 * \param options Enumerates the set of things we will capture for each
874 * StackFrame.
875 */
876 static Local<StackTrace> CurrentStackTrace(
877 int frame_limit,
878 StackTraceOptions options = kOverview);
879};
880
881
882/**
883 * A single JavaScript stack frame.
884 */
885class V8EXPORT StackFrame {
886 public:
887 /**
888 * Returns the number, 1-based, of the line for the associate function call.
889 * This method will return Message::kNoLineNumberInfo if it is unable to
890 * retrieve the line number, or if kLineNumber was not passed as an option
891 * when capturing the StackTrace.
892 */
893 int GetLineNumber() const;
894
895 /**
896 * Returns the 1-based column offset on the line for the associated function
897 * call.
898 * This method will return Message::kNoColumnInfo if it is unable to retrieve
899 * the column number, or if kColumnOffset was not passed as an option when
900 * capturing the StackTrace.
901 */
902 int GetColumn() const;
903
904 /**
905 * Returns the name of the resource that contains the script for the
906 * function for this StackFrame.
907 */
908 Local<String> GetScriptName() const;
909
910 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000911 * Returns the name of the resource that contains the script for the
912 * function for this StackFrame or sourceURL value if the script name
913 * is undefined and its source ends with //@ sourceURL=... string.
914 */
915 Local<String> GetScriptNameOrSourceURL() const;
916
917 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000918 * Returns the name of the function associated with this stack frame.
919 */
920 Local<String> GetFunctionName() const;
921
922 /**
923 * Returns whether or not the associated function is compiled via a call to
924 * eval().
925 */
926 bool IsEval() const;
927
928 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000929 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000930 * constructor via "new".
931 */
932 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933};
934
935
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000936// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937
938
939/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000940 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000942class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 /**
945 * Returns true if this value is the undefined value. See ECMA-262
946 * 4.3.10.
947 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000948 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949
950 /**
951 * Returns true if this value is the null value. See ECMA-262
952 * 4.3.11.
953 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000954 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955
956 /**
957 * Returns true if this value is true.
958 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000959 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960
961 /**
962 * Returns true if this value is false.
963 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000964 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965
966 /**
967 * Returns true if this value is an instance of the String type.
968 * See ECMA-262 8.4.
969 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000970 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971
972 /**
973 * Returns true if this value is a function.
974 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000975 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976
977 /**
978 * Returns true if this value is an array.
979 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000980 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981
v8.team.kasperl727e9952008-09-02 14:56:44 +0000982 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 * Returns true if this value is an object.
984 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000985 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986
v8.team.kasperl727e9952008-09-02 14:56:44 +0000987 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 * Returns true if this value is boolean.
989 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000990 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000991
v8.team.kasperl727e9952008-09-02 14:56:44 +0000992 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 * Returns true if this value is a number.
994 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000995 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996
v8.team.kasperl727e9952008-09-02 14:56:44 +0000997 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 * Returns true if this value is external.
999 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001000 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001
v8.team.kasperl727e9952008-09-02 14:56:44 +00001002 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 * Returns true if this value is a 32-bit signed integer.
1004 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001005 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001007 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001008 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001009 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001010 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001011
1012 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001013 * Returns true if this value is a Date.
1014 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001015 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001016
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001017 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001018 * Returns true if this value is a Boolean object.
1019 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001020 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001021
1022 /**
1023 * Returns true if this value is a Number object.
1024 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001025 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001026
1027 /**
1028 * Returns true if this value is a String object.
1029 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001030 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001031
1032 /**
1033 * Returns true if this value is a NativeError.
1034 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001035 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001036
1037 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001038 * Returns true if this value is a RegExp.
1039 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001040 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001041
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001042 Local<Boolean> ToBoolean() const;
1043 Local<Number> ToNumber() const;
1044 Local<String> ToString() const;
1045 Local<String> ToDetailString() const;
1046 Local<Object> ToObject() const;
1047 Local<Integer> ToInteger() const;
1048 Local<Uint32> ToUint32() const;
1049 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050
1051 /**
1052 * Attempts to convert a string to an array index.
1053 * Returns an empty handle if the conversion fails.
1054 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001055 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001057 bool BooleanValue() const;
1058 double NumberValue() const;
1059 int64_t IntegerValue() const;
1060 uint32_t Uint32Value() const;
1061 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062
1063 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001064 bool Equals(Handle<Value> that) const;
1065 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001066
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001067 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001068 V8_INLINE(bool QuickIsUndefined() const);
1069 V8_INLINE(bool QuickIsNull() const);
1070 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001071 bool FullIsUndefined() const;
1072 bool FullIsNull() const;
1073 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074};
1075
1076
1077/**
1078 * The superclass of primitive values. See ECMA-262 4.3.2.
1079 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001080class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081
1082
1083/**
1084 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1085 * or false value.
1086 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001087class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001089 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001090 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091};
1092
1093
1094/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001095 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001097class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001099 enum Encoding {
1100 UNKNOWN_ENCODING = 0x1,
1101 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001102 ASCII_ENCODING = 0x4,
1103 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001104 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001105 /**
1106 * Returns the number of characters in this string.
1107 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001108 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109
v8.team.kasperl727e9952008-09-02 14:56:44 +00001110 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001111 * Returns the number of bytes in the UTF-8 encoded
1112 * representation of this string.
1113 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001114 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001115
1116 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001117 * A fast conservative check for non-ASCII characters. May
1118 * return true even for ASCII strings, but if it returns
1119 * false you can be sure that all characters are in the range
1120 * 0-127.
1121 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001122 bool MayContainNonAscii() const;
1123
1124 /**
1125 * Returns whether this string contains only one byte data.
1126 */
1127 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001128
1129 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001130 * Write the contents of the string to an external buffer.
1131 * If no arguments are given, expects the buffer to be large
1132 * enough to hold the entire string and NULL terminator. Copies
1133 * the contents of the string and the NULL terminator into the
1134 * buffer.
1135 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001136 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1137 * before the end of the buffer.
1138 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001139 * Copies up to length characters into the output buffer.
1140 * Only null-terminates if there is enough space in the buffer.
1141 *
1142 * \param buffer The buffer into which the string will be copied.
1143 * \param start The starting position within the string at which
1144 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001145 * \param length The number of characters to copy from the string. For
1146 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001147 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001148 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001149 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001150 * \return The number of characters copied to the buffer excluding the null
1151 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001152 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001153 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001154 enum WriteOptions {
1155 NO_OPTIONS = 0,
1156 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001157 NO_NULL_TERMINATION = 2,
1158 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001159 };
1160
lrn@chromium.org34e60782011-09-15 07:25:40 +00001161 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001162 int Write(uint16_t* buffer,
1163 int start = 0,
1164 int length = -1,
1165 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001166 // ASCII characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001167 int WriteAscii(char* buffer,
1168 int start = 0,
1169 int length = -1,
1170 int options = NO_OPTIONS) const;
1171 // One byte characters.
1172 int WriteOneByte(uint8_t* buffer,
1173 int start = 0,
1174 int length = -1,
1175 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001176 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001177 int WriteUtf8(char* buffer,
1178 int length = -1,
1179 int* nchars_ref = NULL,
1180 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181
v8.team.kasperl727e9952008-09-02 14:56:44 +00001182 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001183 * A zero length string.
1184 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001185 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001186 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001187
1188 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001189 * Returns true if the string is external
1190 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001191 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192
v8.team.kasperl727e9952008-09-02 14:56:44 +00001193 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001194 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001195 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001196 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001197
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001198 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001199 public:
1200 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001201
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001202 protected:
1203 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001204
1205 /**
1206 * Internally V8 will call this Dispose method when the external string
1207 * resource is no longer needed. The default implementation will use the
1208 * delete operator. This method can be overridden in subclasses to
1209 * control how allocated external string resources are disposed.
1210 */
1211 virtual void Dispose() { delete this; }
1212
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001213 private:
1214 // Disallow copying and assigning.
1215 ExternalStringResourceBase(const ExternalStringResourceBase&);
1216 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001217
1218 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001219 };
1220
v8.team.kasperl727e9952008-09-02 14:56:44 +00001221 /**
1222 * An ExternalStringResource is a wrapper around a two-byte string
1223 * buffer that resides outside V8's heap. Implement an
1224 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001225 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001226 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001227 class V8EXPORT ExternalStringResource
1228 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229 public:
1230 /**
1231 * Override the destructor to manage the life cycle of the underlying
1232 * buffer.
1233 */
1234 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001235
1236 /**
1237 * The string data from the underlying buffer.
1238 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001240
1241 /**
1242 * The length of the string. That is, the number of two-byte characters.
1243 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001245
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 protected:
1247 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248 };
1249
1250 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001251 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001252 * string buffer that resides outside V8's heap. Implement an
1253 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001254 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001255 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001256 * UTF-8, which would require special treatment internally in the
1257 * engine and, in the case of UTF-8, do not allow efficient indexing.
1258 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001259 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001261 class V8EXPORT ExternalAsciiStringResource
1262 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 public:
1264 /**
1265 * Override the destructor to manage the life cycle of the underlying
1266 * buffer.
1267 */
1268 virtual ~ExternalAsciiStringResource() {}
1269 /** The string data from the underlying buffer.*/
1270 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001271 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272 virtual size_t length() const = 0;
1273 protected:
1274 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 };
1276
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001277 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001280 * If the string is an external string, return the ExternalStringResourceBase
1281 * regardless of the encoding, otherwise return NULL. The encoding of the
1282 * string is returned in encoding_out.
1283 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001284 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1285 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001286
1287 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001288 * Get the ExternalStringResource for an external string. Returns
1289 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001291 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292
1293 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001294 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001295 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001297 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001299 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300
1301 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001302 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001303 * The second parameter 'length' gives the buffer length. If omitted,
1304 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001306 static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307
lrn@chromium.org34e60782011-09-15 07:25:40 +00001308 /** Allocates a new string from 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001309 static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310
1311 /** Creates a symbol. Returns one if it exists already.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001312 static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313
v8.team.kasperl727e9952008-09-02 14:56:44 +00001314 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001315 * Creates a new string by concatenating the left and the right strings
1316 * passed in as parameters.
1317 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001318 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001319
1320 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001321 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001322 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001323 * resource will be disposed by calling its Dispose method. The caller of
1324 * this function should not otherwise delete or modify the resource. Neither
1325 * should the underlying buffer be deallocated or modified except through the
1326 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001327 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001328 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001329
ager@chromium.org6f10e412009-02-13 10:11:16 +00001330 /**
1331 * Associate an external string resource with this string by transforming it
1332 * in place so that existing references to this string in the JavaScript heap
1333 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001334 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001335 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001336 * The string is not modified if the operation fails. See NewExternal for
1337 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001338 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001339 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340
v8.team.kasperl727e9952008-09-02 14:56:44 +00001341 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001342 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001343 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001344 * resource will be disposed by calling its Dispose method. The caller of
1345 * this function should not otherwise delete or modify the resource. Neither
1346 * should the underlying buffer be deallocated or modified except through the
1347 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001348 */
1349 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001350
ager@chromium.org6f10e412009-02-13 10:11:16 +00001351 /**
1352 * Associate an external string resource with this string by transforming it
1353 * in place so that existing references to this string in the JavaScript heap
1354 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001355 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001356 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001357 * The string is not modified if the operation fails. See NewExternal for
1358 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001359 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001360 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001362 /**
1363 * Returns true if this string can be made external.
1364 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001365 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001366
lrn@chromium.org34e60782011-09-15 07:25:40 +00001367 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001368 static Local<String> NewUndetectable(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369
lrn@chromium.org34e60782011-09-15 07:25:40 +00001370 /** Creates an undetectable string from the supplied 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001371 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372
1373 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001374 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001375 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001376 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001377 * then the length() method returns 0 and the * operator returns
1378 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001379 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001380 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001381 public:
1382 explicit Utf8Value(Handle<v8::Value> obj);
1383 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001384 char* operator*() { return str_; }
1385 const char* operator*() const { return str_; }
1386 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001387 private:
1388 char* str_;
1389 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001390
1391 // Disallow copying and assigning.
1392 Utf8Value(const Utf8Value&);
1393 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001394 };
1395
1396 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001397 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001399 * If conversion to a string fails (eg. due to an exception in the toString()
1400 * method of the object) then the length() method returns 0 and the * operator
1401 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001402 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001403 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 public:
1405 explicit AsciiValue(Handle<v8::Value> obj);
1406 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001407 char* operator*() { return str_; }
1408 const char* operator*() const { return str_; }
1409 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 private:
1411 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001412 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001413
1414 // Disallow copying and assigning.
1415 AsciiValue(const AsciiValue&);
1416 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 };
1418
1419 /**
1420 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001421 * If conversion to a string fails (eg. due to an exception in the toString()
1422 * method of the object) then the length() method returns 0 and the * operator
1423 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001425 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 public:
1427 explicit Value(Handle<v8::Value> obj);
1428 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001429 uint16_t* operator*() { return str_; }
1430 const uint16_t* operator*() const { return str_; }
1431 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432 private:
1433 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001434 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001435
1436 // Disallow copying and assigning.
1437 Value(const Value&);
1438 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001440
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001441 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001442 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1443 Encoding encoding) const;
1444 void VerifyExternalStringResource(ExternalStringResource* val) const;
1445 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446};
1447
1448
1449/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001450 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001452class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001454 double Value() const;
1455 static Local<Number> New(double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001456 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001458 Number();
1459 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460};
1461
1462
1463/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001464 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001466class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001468 static Local<Integer> New(int32_t value);
1469 static Local<Integer> NewFromUnsigned(uint32_t value);
1470 static Local<Integer> New(int32_t value, Isolate*);
1471 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1472 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001473 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001475 Integer();
1476 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477};
1478
1479
1480/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001481 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001483class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001485 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001487 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488};
1489
1490
1491/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001492 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001493 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001494class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001496 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001498 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499};
1500
1501
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502enum PropertyAttribute {
1503 None = 0,
1504 ReadOnly = 1 << 0,
1505 DontEnum = 1 << 1,
1506 DontDelete = 1 << 2
1507};
1508
ager@chromium.org3811b432009-10-28 14:53:37 +00001509enum ExternalArrayType {
1510 kExternalByteArray = 1,
1511 kExternalUnsignedByteArray,
1512 kExternalShortArray,
1513 kExternalUnsignedShortArray,
1514 kExternalIntArray,
1515 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001516 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001517 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001518 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001519};
1520
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001521/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001522 * Accessor[Getter|Setter] are used as callback functions when
1523 * setting|getting a particular property. See Object and ObjectTemplate's
1524 * method SetAccessor.
1525 */
1526typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1527 const AccessorInfo& info);
1528
1529
1530typedef void (*AccessorSetter)(Local<String> property,
1531 Local<Value> value,
1532 const AccessorInfo& info);
1533
1534
1535/**
1536 * Access control specifications.
1537 *
1538 * Some accessors should be accessible across contexts. These
1539 * accessors have an explicit access control parameter which specifies
1540 * the kind of cross-context access that should be allowed.
1541 *
1542 * Additionally, for security, accessors can prohibit overwriting by
1543 * accessors defined in JavaScript. For objects that have such
1544 * accessors either locally or in their prototype chain it is not
1545 * possible to overwrite the accessor by using __defineGetter__ or
1546 * __defineSetter__ from JavaScript code.
1547 */
1548enum AccessControl {
1549 DEFAULT = 0,
1550 ALL_CAN_READ = 1,
1551 ALL_CAN_WRITE = 1 << 1,
1552 PROHIBITS_OVERWRITING = 1 << 2
1553};
1554
1555
1556/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001557 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001559class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001561 bool Set(Handle<Value> key,
1562 Handle<Value> value,
1563 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001564
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001565 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001566
ager@chromium.orge2902be2009-06-08 12:21:35 +00001567 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001568 // overriding accessors or read-only properties.
1569 //
1570 // Note that if the object has an interceptor the property will be set
1571 // locally, but since the interceptor takes precedence the local property
1572 // will only be returned if the interceptor doesn't return a value.
1573 //
1574 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001575 bool ForceSet(Handle<Value> key,
1576 Handle<Value> value,
1577 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001578
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001579 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001581 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001582
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001583 /**
1584 * Gets the property attributes of a property which can be None or
1585 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1586 * None when the property doesn't exist.
1587 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001588 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001589
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590 // TODO(1245389): Replace the type-specific versions of these
1591 // functions with generic ones that accept a Handle<Value> key.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001592 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001593
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001594 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001595
1596 // Delete a property on this object bypassing interceptors and
1597 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001598 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001599
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001600 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001601
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001602 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001604 bool SetAccessor(Handle<String> name,
1605 AccessorGetter getter,
1606 AccessorSetter setter = 0,
1607 Handle<Value> data = Handle<Value>(),
1608 AccessControl settings = DEFAULT,
1609 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001610
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001611 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001612 * Returns an array containing the names of the enumerable properties
1613 * of this object, including properties from prototype objects. The
1614 * array returned by this method contains the same values as would
1615 * be enumerated by a for-in statement over this object.
1616 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001617 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001618
1619 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001620 * This function has the same functionality as GetPropertyNames but
1621 * the returned array doesn't contain the names of properties from
1622 * prototype objects.
1623 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001624 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001625
1626 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 * Get the prototype object. This does not skip objects marked to
1628 * be skipped by __proto__ and it does not consult the security
1629 * handler.
1630 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001631 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632
1633 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001634 * Set 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 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001639
1640 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001641 * Finds an instance of the given function template in the prototype
1642 * chain.
1643 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001644 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001645
1646 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001647 * Call builtin Object.prototype.toString on this object.
1648 * This is different from Value::ToString() that may call
1649 * user-defined toString function. This one does not.
1650 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001651 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001653 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001654 * Returns the function invoked as a constructor for this object.
1655 * May be the null value.
1656 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001657 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001658
1659 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001660 * Returns the name of the function invoked as a constructor for this object.
1661 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001662 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001663
kasper.lund212ac232008-07-16 07:07:30 +00001664 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001665 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001666
1667 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001668 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001669
kasper.lund212ac232008-07-16 07:07:30 +00001670 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001671 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001672
1673 /**
1674 * Gets a 2-byte-aligned native pointer from an internal field. This field
1675 * must have been set by SetAlignedPointerInInternalField, everything else
1676 * leads to undefined behavior.
1677 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001678 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001679
1680 /**
1681 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1682 * a field, GetAlignedPointerFromInternalField must be used, everything else
1683 * leads to undefined behavior.
1684 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001685 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001688 bool HasOwnProperty(Handle<String> key);
1689 bool HasRealNamedProperty(Handle<String> key);
1690 bool HasRealIndexedProperty(uint32_t index);
1691 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692
1693 /**
1694 * If result.IsEmpty() no real property was located in the prototype chain.
1695 * This means interceptors in the prototype chain are not called.
1696 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001697 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001698
1699 /**
1700 * If result.IsEmpty() no real property was located on the object or
1701 * 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> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705
1706 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001707 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708
kasper.lund212ac232008-07-16 07:07:30 +00001709 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001710 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001712 /**
1713 * Turns on access check on the object if the object is an instance of
1714 * a template that has access check callbacks. If an object has no
1715 * access check info, the object cannot be accessed by anyone.
1716 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001717 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001718
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001719 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001720 * Returns the identity hash for this object. The current implementation
1721 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001722 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001723 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001724 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001725 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001726 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001727
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001728 /**
1729 * Access hidden properties on JavaScript objects. These properties are
1730 * hidden from the executing JavaScript and only accessible through the V8
1731 * C++ API. Hidden properties introduced by V8 internally (for example the
1732 * identity hash) are prefixed with "v8::".
1733 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001734 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1735 Local<Value> GetHiddenValue(Handle<String> key);
1736 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001737
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001738 /**
1739 * Returns true if this is an instance of an api function (one
1740 * created from a function created from a function template) and has
1741 * been modified since it was created. Note that this method is
1742 * conservative and may return true for objects that haven't actually
1743 * been modified.
1744 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001745 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001746
1747 /**
1748 * Clone this object with a fast but shallow copy. Values will point
1749 * to the same values as the original object.
1750 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001751 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001753 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001754 * Returns the context in which the object was created.
1755 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001756 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001757
1758 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001759 * Set the backing store of the indexed properties to be managed by the
1760 * embedding layer. Access to the indexed properties will follow the rules
1761 * spelled out in CanvasPixelArray.
1762 * Note: The embedding program still owns the data and needs to ensure that
1763 * the backing store is preserved while V8 has a reference.
1764 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001765 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1766 bool HasIndexedPropertiesInPixelData();
1767 uint8_t* GetIndexedPropertiesPixelData();
1768 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001769
ager@chromium.org3811b432009-10-28 14:53:37 +00001770 /**
1771 * Set the backing store of the indexed properties to be managed by the
1772 * embedding layer. Access to the indexed properties will follow the rules
1773 * spelled out for the CanvasArray subtypes in the WebGL specification.
1774 * Note: The embedding program still owns the data and needs to ensure that
1775 * the backing store is preserved while V8 has a reference.
1776 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001777 void SetIndexedPropertiesToExternalArrayData(void* data,
1778 ExternalArrayType array_type,
1779 int number_of_elements);
1780 bool HasIndexedPropertiesInExternalArrayData();
1781 void* GetIndexedPropertiesExternalArrayData();
1782 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1783 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001784
lrn@chromium.org1c092762011-05-09 09:42:16 +00001785 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001786 * Checks whether a callback is set by the
1787 * ObjectTemplate::SetCallAsFunctionHandler method.
1788 * When an Object is callable this method returns true.
1789 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001790 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001791
1792 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001793 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001794 * ObjectTemplate::SetCallAsFunctionHandler method.
1795 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001796 Local<Value> CallAsFunction(Handle<Object> recv,
1797 int argc,
1798 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001799
1800 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001801 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001802 * ObjectTemplate::SetCallAsFunctionHandler method.
1803 * Note: This method behaves like the Function::NewInstance method.
1804 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001805 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001806
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001807 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001808 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001809
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001811 Object();
1812 static void CheckCast(Value* obj);
1813 Local<Value> SlowGetInternalField(int index);
1814 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815};
1816
1817
1818/**
1819 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1820 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001821class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001823 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824
ager@chromium.org3e875802009-06-29 08:26:34 +00001825 /**
1826 * Clones an element at index |index|. Returns an empty
1827 * handle if cloning fails (for any reason).
1828 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001829 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001830
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001831 /**
1832 * Creates a JavaScript array with the given length. If the length
1833 * is negative the returned array will have length 0.
1834 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001835 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001836
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001837 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001839 Array();
1840 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841};
1842
1843
1844/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001845 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001847class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001849 Local<Object> NewInstance() const;
1850 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1851 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1852 void SetName(Handle<String> name);
1853 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001854
1855 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001856 * Name inferred from variable or property assignment of this function.
1857 * Used to facilitate debugging and profiling of JavaScript code written
1858 * in an OO style, where many functions are anonymous but are assigned
1859 * to object properties.
1860 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001861 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001862
1863 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001864 * Returns zero based line number of function body and
1865 * kLineOffsetNotFound if no information available.
1866 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001867 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001868 /**
1869 * Returns zero based column number of function body and
1870 * kLineOffsetNotFound if no information available.
1871 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001872 int GetScriptColumnNumber() const;
1873 Handle<Value> GetScriptId() const;
1874 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001875 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001876 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001877
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001879 Function();
1880 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881};
1882
1883
1884/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001885 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1886 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001887class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001888 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001889 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001890
1891 /**
1892 * A specialization of Value::NumberValue that is more efficient
1893 * because we know the structure of this object.
1894 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001895 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001896
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001897 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001898
1899 /**
1900 * Notification that the embedder has changed the time zone,
1901 * daylight savings time, or other date / time configuration
1902 * parameters. V8 keeps a cache of various values used for
1903 * date / time computation. This notification will reset
1904 * those cached values for the current context so that date /
1905 * time configuration changes would be reflected in the Date
1906 * object.
1907 *
1908 * This API should not be called more than needed as it will
1909 * negatively impact the performance of date operations.
1910 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001911 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001912
1913 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001914 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001915};
1916
1917
1918/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001919 * A Number object (ECMA-262, 4.3.21).
1920 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001921class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001922 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001923 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001924
1925 /**
1926 * Returns the Number held by the object.
1927 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001928 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001929
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001930 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001931
1932 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001933 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001934};
1935
1936
1937/**
1938 * A Boolean object (ECMA-262, 4.3.15).
1939 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001940class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001941 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001942 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001943
1944 /**
1945 * Returns the Boolean held by the object.
1946 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001947 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001948
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001949 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001950
1951 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001952 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001953};
1954
1955
1956/**
1957 * A String object (ECMA-262, 4.3.18).
1958 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001959class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001960 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001961 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001962
1963 /**
1964 * Returns the String held by the object.
1965 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001966 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001967
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001968 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001969
1970 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001971 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001972};
1973
1974
1975/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001976 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1977 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001978class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001979 public:
1980 /**
1981 * Regular expression flag bits. They can be or'ed to enable a set
1982 * of flags.
1983 */
1984 enum Flags {
1985 kNone = 0,
1986 kGlobal = 1,
1987 kIgnoreCase = 2,
1988 kMultiline = 4
1989 };
1990
1991 /**
1992 * Creates a regular expression from the given pattern string and
1993 * the flags bit field. May throw a JavaScript exception as
1994 * described in ECMA-262, 15.10.4.1.
1995 *
1996 * For example,
1997 * RegExp::New(v8::String::New("foo"),
1998 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1999 * is equivalent to evaluating "/foo/gm".
2000 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002001 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002002
2003 /**
2004 * Returns the value of the source property: a string representing
2005 * the regular expression.
2006 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002007 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002008
2009 /**
2010 * Returns the flags bit field.
2011 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002012 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002013
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002014 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002015
2016 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002017 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002018};
2019
2020
2021/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002022 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2023 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002025class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002027 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002028 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002029 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002031 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032};
2033
2034
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002035// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036
2037
2038/**
2039 * The superclass of object and function templates.
2040 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002041class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 public:
2043 /** Adds a property to each instance created by this template.*/
2044 void Set(Handle<String> name, Handle<Data> value,
2045 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002046 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 private:
2048 Template();
2049
2050 friend class ObjectTemplate;
2051 friend class FunctionTemplate;
2052};
2053
2054
2055/**
2056 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002057 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058 * including the receiver, the number and values of arguments, and
2059 * the holder of the function.
2060 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002061class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002063 V8_INLINE(int Length() const);
2064 V8_INLINE(Local<Value> operator[](int i) const);
2065 V8_INLINE(Local<Function> Callee() const);
2066 V8_INLINE(Local<Object> This() const);
2067 V8_INLINE(Local<Object> Holder() const);
2068 V8_INLINE(bool IsConstructCall() const);
2069 V8_INLINE(Local<Value> Data() const);
2070 V8_INLINE(Isolate* GetIsolate() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002073 static const int kIsolateIndex = 0;
2074 static const int kDataIndex = -1;
2075 static const int kCalleeIndex = -2;
2076 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002077
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 friend class ImplementationUtilities;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002079 V8_INLINE(Arguments(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002080 internal::Object** values,
2081 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002082 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002083 internal::Object** implicit_args_;
2084 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002086 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087};
2088
2089
2090/**
2091 * The information passed to an accessor callback about the context
2092 * of the property access.
2093 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002094class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002096 V8_INLINE(AccessorInfo(internal::Object** args))
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002097 : args_(args) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002098 V8_INLINE(Isolate* GetIsolate() const);
2099 V8_INLINE(Local<Value> Data() const);
2100 V8_INLINE(Local<Object> This() const);
2101 V8_INLINE(Local<Object> Holder() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002104 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105};
2106
2107
2108typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 * NamedProperty[Getter|Setter] are used as interceptors on object.
2112 * See ObjectTemplate::SetNamedPropertyHandler.
2113 */
2114typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2115 const AccessorInfo& info);
2116
2117
2118/**
2119 * Returns the value if the setter intercepts the request.
2120 * Otherwise, returns an empty handle.
2121 */
2122typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2123 Local<Value> value,
2124 const AccessorInfo& info);
2125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126/**
2127 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002128 * The result is an integer encoding property attributes (like v8::None,
2129 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002131typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2132 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133
2134
2135/**
2136 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002137 * The return value is true if the property could be deleted and false
2138 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139 */
2140typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2141 const AccessorInfo& info);
2142
2143/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002144 * Returns an array containing the names of the properties the named
2145 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 */
2147typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2148
v8.team.kasperl727e9952008-09-02 14:56:44 +00002149
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002151 * Returns the value of the property if the getter intercepts the
2152 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 */
2154typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2155 const AccessorInfo& info);
2156
2157
2158/**
2159 * Returns the value if the setter intercepts the request.
2160 * Otherwise, returns an empty handle.
2161 */
2162typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2163 Local<Value> value,
2164 const AccessorInfo& info);
2165
2166
2167/**
2168 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002169 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002171typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2172 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173
2174/**
2175 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002176 * The return value is true if the property could be deleted and false
2177 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 */
2179typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2180 const AccessorInfo& info);
2181
v8.team.kasperl727e9952008-09-02 14:56:44 +00002182/**
2183 * Returns an array containing the indices of the properties the
2184 * indexed property getter intercepts.
2185 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2187
2188
2189/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002190 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 */
2192enum AccessType {
2193 ACCESS_GET,
2194 ACCESS_SET,
2195 ACCESS_HAS,
2196 ACCESS_DELETE,
2197 ACCESS_KEYS
2198};
2199
v8.team.kasperl727e9952008-09-02 14:56:44 +00002200
2201/**
2202 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002203 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002204 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002205typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206 Local<Value> key,
2207 AccessType type,
2208 Local<Value> data);
2209
v8.team.kasperl727e9952008-09-02 14:56:44 +00002210
2211/**
2212 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002213 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002214 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002215typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216 uint32_t index,
2217 AccessType type,
2218 Local<Value> data);
2219
2220
2221/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002222 * A FunctionTemplate is used to create functions at runtime. There
2223 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002224 * context. The lifetime of the created function is equal to the
2225 * lifetime of the context. So in case the embedder needs to create
2226 * temporary functions that can be collected using Scripts is
2227 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 *
2229 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002230 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002232 * A FunctionTemplate has a corresponding instance template which is
2233 * used to create object instances when the function is used as a
2234 * constructor. Properties added to the instance template are added to
2235 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 *
2237 * A FunctionTemplate can have a prototype template. The prototype template
2238 * is used to create the prototype object of the function.
2239 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002240 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002242 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2244 * t->Set("func_property", v8::Number::New(1));
2245 *
2246 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2247 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2248 * proto_t->Set("proto_const", v8::Number::New(2));
2249 *
2250 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2251 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2252 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2253 * instance_t->Set("instance_property", Number::New(3));
2254 *
2255 * v8::Local<v8::Function> function = t->GetFunction();
2256 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002257 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258 *
2259 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002260 * and "instance" for the instance object created above. The function
2261 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002263 * \code
2264 * func_property in function == true;
2265 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002267 * function.prototype.proto_method() invokes 'InvokeCallback'
2268 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002270 * instance instanceof function == true;
2271 * instance.instance_accessor calls 'InstanceAccessorCallback'
2272 * instance.instance_property == 3;
2273 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002275 * A FunctionTemplate can inherit from another one by calling the
2276 * FunctionTemplate::Inherit method. The following graph illustrates
2277 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002278 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002279 * \code
2280 * FunctionTemplate Parent -> Parent() . prototype -> { }
2281 * ^ ^
2282 * | Inherit(Parent) | .__proto__
2283 * | |
2284 * FunctionTemplate Child -> Child() . prototype -> { }
2285 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002287 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2288 * object of the Child() function has __proto__ pointing to the
2289 * Parent() function's prototype object. An instance of the Child
2290 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002292 * Let Parent be the FunctionTemplate initialized in the previous
2293 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002295 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 * Local<FunctionTemplate> parent = t;
2297 * Local<FunctionTemplate> child = FunctionTemplate::New();
2298 * child->Inherit(parent);
2299 *
2300 * Local<Function> child_function = child->GetFunction();
2301 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002302 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002304 * The Child function and Child instance will have the following
2305 * properties:
2306 *
2307 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002309 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002311 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002313class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314 public:
2315 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002316 static Local<FunctionTemplate> New(
2317 InvocationCallback callback = 0,
2318 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002319 Handle<Signature> signature = Handle<Signature>(),
2320 int length = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 /** Returns the unique function instance in the current execution context.*/
2322 Local<Function> GetFunction();
2323
v8.team.kasperl727e9952008-09-02 14:56:44 +00002324 /**
2325 * Set the call-handler callback for a FunctionTemplate. This
2326 * callback is called whenever the function created from this
2327 * FunctionTemplate is called.
2328 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329 void SetCallHandler(InvocationCallback callback,
2330 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002332 /** Set the predefined length property for the FunctionTemplate. */
2333 void SetLength(int length);
2334
v8.team.kasperl727e9952008-09-02 14:56:44 +00002335 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 Local<ObjectTemplate> InstanceTemplate();
2337
2338 /** Causes the function template to inherit from a parent function template.*/
2339 void Inherit(Handle<FunctionTemplate> parent);
2340
2341 /**
2342 * A PrototypeTemplate is the template used to create the prototype object
2343 * of the function created by this template.
2344 */
2345 Local<ObjectTemplate> PrototypeTemplate();
2346
v8.team.kasperl727e9952008-09-02 14:56:44 +00002347 /**
2348 * Set the class name of the FunctionTemplate. This is used for
2349 * printing objects created with the function created from the
2350 * FunctionTemplate as its constructor.
2351 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 void SetClassName(Handle<String> name);
2353
2354 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002355 * Determines whether the __proto__ accessor ignores instances of
2356 * the function template. If instances of the function template are
2357 * ignored, __proto__ skips all instances and instead returns the
2358 * next object in the prototype chain.
2359 *
2360 * Call with a value of true to make the __proto__ accessor ignore
2361 * instances of the function template. Call with a value of false
2362 * to make the __proto__ accessor not ignore instances of the
2363 * function template. By default, instances of a function template
2364 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365 */
2366 void SetHiddenPrototype(bool value);
2367
2368 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002369 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2370 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002371 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002372 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002373
2374 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002375 * Returns true if the given object is an instance of this function
2376 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377 */
2378 bool HasInstance(Handle<Value> object);
2379
2380 private:
2381 FunctionTemplate();
2382 void AddInstancePropertyAccessor(Handle<String> name,
2383 AccessorGetter getter,
2384 AccessorSetter setter,
2385 Handle<Value> data,
2386 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002387 PropertyAttribute attributes,
2388 Handle<AccessorSignature> signature);
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
2461 /**
2462 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002463 *
2464 * Whenever a named property is accessed on objects created from
2465 * this object template, the provided callback is invoked instead of
2466 * accessing the property directly on the JavaScript object.
2467 *
2468 * \param getter The callback to invoke when getting a property.
2469 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002470 * \param query The callback to invoke to check if a property is present,
2471 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002472 * \param deleter The callback to invoke when deleting a property.
2473 * \param enumerator The callback to invoke to enumerate all the named
2474 * properties of an object.
2475 * \param data A piece of data that will be passed to the callbacks
2476 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477 */
2478 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2479 NamedPropertySetter setter = 0,
2480 NamedPropertyQuery query = 0,
2481 NamedPropertyDeleter deleter = 0,
2482 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002483 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002484
2485 /**
2486 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002487 *
2488 * Whenever an indexed property is accessed on objects created from
2489 * this object template, the provided callback is invoked instead of
2490 * accessing the property directly on the JavaScript object.
2491 *
2492 * \param getter The callback to invoke when getting a property.
2493 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002494 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002495 * \param deleter The callback to invoke when deleting a property.
2496 * \param enumerator The callback to invoke to enumerate all the indexed
2497 * properties of an object.
2498 * \param data A piece of data that will be passed to the callbacks
2499 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500 */
2501 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2502 IndexedPropertySetter setter = 0,
2503 IndexedPropertyQuery query = 0,
2504 IndexedPropertyDeleter deleter = 0,
2505 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002506 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002507
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 /**
2509 * Sets the callback to be used when calling instances created from
2510 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002511 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512 * function.
2513 */
2514 void SetCallAsFunctionHandler(InvocationCallback callback,
2515 Handle<Value> data = Handle<Value>());
2516
v8.team.kasperl727e9952008-09-02 14:56:44 +00002517 /**
2518 * Mark object instances of the template as undetectable.
2519 *
2520 * In many ways, undetectable objects behave as though they are not
2521 * there. They behave like 'undefined' in conditionals and when
2522 * printed. However, properties can be accessed and called as on
2523 * normal objects.
2524 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 void MarkAsUndetectable();
2526
v8.team.kasperl727e9952008-09-02 14:56:44 +00002527 /**
2528 * Sets access check callbacks on the object template.
2529 *
2530 * When accessing properties on instances of this object template,
2531 * the access check callback will be called to determine whether or
2532 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002533 * The last parameter specifies whether access checks are turned
2534 * on by default on instances. If access checks are off by default,
2535 * they can be turned on on individual instances by calling
2536 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002537 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2539 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002540 Handle<Value> data = Handle<Value>(),
2541 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002542
kasper.lund212ac232008-07-16 07:07:30 +00002543 /**
2544 * Gets the number of internal fields for objects generated from
2545 * this template.
2546 */
2547 int InternalFieldCount();
2548
2549 /**
2550 * Sets the number of internal fields for objects generated from
2551 * this template.
2552 */
2553 void SetInternalFieldCount(int value);
2554
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002555 private:
2556 ObjectTemplate();
2557 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2558 friend class FunctionTemplate;
2559};
2560
2561
2562/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002563 * A Signature specifies which receivers and arguments are valid
2564 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002566class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567 public:
2568 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2569 Handle<FunctionTemplate>(),
2570 int argc = 0,
2571 Handle<FunctionTemplate> argv[] = 0);
2572 private:
2573 Signature();
2574};
2575
2576
2577/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002578 * An AccessorSignature specifies which receivers are valid parameters
2579 * to an accessor callback.
2580 */
2581class V8EXPORT AccessorSignature : public Data {
2582 public:
2583 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2584 Handle<FunctionTemplate>());
2585 private:
2586 AccessorSignature();
2587};
2588
2589
2590/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002591 * A utility for determining the type of objects based on the template
2592 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002594class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595 public:
2596 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2597 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2598 int match(Handle<Value> value);
2599 private:
2600 TypeSwitch();
2601};
2602
2603
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002604// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002605
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002606class V8EXPORT ExternalAsciiStringResourceImpl
2607 : public String::ExternalAsciiStringResource {
2608 public:
2609 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2610 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2611 : data_(data), length_(length) {}
2612 const char* data() const { return data_; }
2613 size_t length() const { return length_; }
2614
2615 private:
2616 const char* data_;
2617 size_t length_;
2618};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619
2620/**
2621 * Ignore
2622 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002623class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002625 // Note that the strings passed into this constructor must live as long
2626 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002628 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002630 const char** deps = 0,
2631 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632 virtual ~Extension() { }
2633 virtual v8::Handle<v8::FunctionTemplate>
2634 GetNativeFunction(v8::Handle<v8::String> name) {
2635 return v8::Handle<v8::FunctionTemplate>();
2636 }
2637
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002638 const char* name() const { return name_; }
2639 size_t source_length() const { return source_length_; }
2640 const String::ExternalAsciiStringResource* source() const {
2641 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642 int dependency_count() { return dep_count_; }
2643 const char** dependencies() { return deps_; }
2644 void set_auto_enable(bool value) { auto_enable_ = value; }
2645 bool auto_enable() { return auto_enable_; }
2646
2647 private:
2648 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002649 size_t source_length_; // expected to initialize before source_
2650 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 int dep_count_;
2652 const char** deps_;
2653 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002654
2655 // Disallow copying and assigning.
2656 Extension(const Extension&);
2657 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658};
2659
2660
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002661void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662
2663
2664/**
2665 * Ignore
2666 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002667class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002669 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670 RegisterExtension(extension);
2671 }
2672};
2673
2674
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002675// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676
2677
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002678Handle<Primitive> V8EXPORT Undefined();
2679Handle<Primitive> V8EXPORT Null();
2680Handle<Boolean> V8EXPORT True();
2681Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002683V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
2684V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
2685V8_INLINE(Handle<Boolean> True(Isolate* isolate));
2686V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002687
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688
2689/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002690 * A set of constraints that specifies the limits of the runtime's memory use.
2691 * You must set the heap size before initializing the VM - the size cannot be
2692 * adjusted after the VM is initialized.
2693 *
2694 * If you are using threads then you should hold the V8::Locker lock while
2695 * setting the stack limit and you must set a non-default stack limit separately
2696 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002698class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 public:
2700 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002701 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002703 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002705 int max_executable_size() { return max_executable_size_; }
2706 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002707 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002708 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002709 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2710 private:
2711 int max_young_space_size_;
2712 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002713 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714 uint32_t* stack_limit_;
2715};
2716
2717
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002718bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002719
2720
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002721// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002722
2723
2724typedef void (*FatalErrorCallback)(const char* location, const char* message);
2725
2726
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002727typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728
2729
2730/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002731 * Schedules an exception to be thrown when returning to JavaScript. When an
2732 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002734 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002736Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737
2738/**
2739 * Create new error objects by calling the corresponding error object
2740 * constructor with the message.
2741 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002742class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743 public:
2744 static Local<Value> RangeError(Handle<String> message);
2745 static Local<Value> ReferenceError(Handle<String> message);
2746 static Local<Value> SyntaxError(Handle<String> message);
2747 static Local<Value> TypeError(Handle<String> message);
2748 static Local<Value> Error(Handle<String> message);
2749};
2750
2751
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002752// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002754typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002755
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002756typedef void* (*CreateHistogramCallback)(const char* name,
2757 int min,
2758 int max,
2759 size_t buckets);
2760
2761typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2762
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002763// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002764 enum ObjectSpace {
2765 kObjectSpaceNewSpace = 1 << 0,
2766 kObjectSpaceOldPointerSpace = 1 << 1,
2767 kObjectSpaceOldDataSpace = 1 << 2,
2768 kObjectSpaceCodeSpace = 1 << 3,
2769 kObjectSpaceMapSpace = 1 << 4,
2770 kObjectSpaceLoSpace = 1 << 5,
2771
2772 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2773 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2774 kObjectSpaceLoSpace
2775 };
2776
2777 enum AllocationAction {
2778 kAllocationActionAllocate = 1 << 0,
2779 kAllocationActionFree = 1 << 1,
2780 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2781 };
2782
2783typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2784 AllocationAction action,
2785 int size);
2786
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002787// --- Leave Script Callback ---
2788typedef void (*CallCompletedCallback)();
2789
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002790// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2792 AccessType type,
2793 Local<Value> data);
2794
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002795// --- AllowCodeGenerationFromStrings callbacks ---
2796
2797/**
2798 * Callback to check if code generation from strings is allowed. See
2799 * Context::AllowCodeGenerationFromStrings.
2800 */
2801typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2802
2803// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002804
2805/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002806 * Applications can register callback functions which will be called
2807 * before and after a garbage collection. Allocations are not
2808 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002809 * objects (set or delete properties for example) since it is possible
2810 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002812enum GCType {
2813 kGCTypeScavenge = 1 << 0,
2814 kGCTypeMarkSweepCompact = 1 << 1,
2815 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2816};
2817
2818enum GCCallbackFlags {
2819 kNoGCCallbackFlags = 0,
2820 kGCCallbackFlagCompacted = 1 << 0
2821};
2822
2823typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2824typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2825
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002826typedef void (*GCCallback)();
2827
2828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002830 * Collection of V8 heap information.
2831 *
2832 * Instances of this class can be passed to v8::V8::HeapStatistics to
2833 * get heap statistics from V8.
2834 */
2835class V8EXPORT HeapStatistics {
2836 public:
2837 HeapStatistics();
2838 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002839 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00002840 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002841 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002842 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002843
2844 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00002845 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002846 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00002847 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002848 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002849 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002850
2851 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002852 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00002853};
2854
2855
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002856class RetainedObjectInfo;
2857
ager@chromium.org3811b432009-10-28 14:53:37 +00002858/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002859 * Isolate represents an isolated instance of the V8 engine. V8
2860 * isolates have completely separate states. Objects from one isolate
2861 * must not be used in other isolates. When V8 is initialized a
2862 * default isolate is implicitly created and entered. The embedder
2863 * can create additional isolates and use them in parallel in multiple
2864 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002865 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002866 */
2867class V8EXPORT Isolate {
2868 public:
2869 /**
2870 * Stack-allocated class which sets the isolate for all operations
2871 * executed within a local scope.
2872 */
2873 class V8EXPORT Scope {
2874 public:
2875 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2876 isolate->Enter();
2877 }
2878
2879 ~Scope() { isolate_->Exit(); }
2880
2881 private:
2882 Isolate* const isolate_;
2883
2884 // Prevent copying of Scope objects.
2885 Scope(const Scope&);
2886 Scope& operator=(const Scope&);
2887 };
2888
2889 /**
2890 * Creates a new isolate. Does not change the currently entered
2891 * isolate.
2892 *
2893 * When an isolate is no longer used its resources should be freed
2894 * by calling Dispose(). Using the delete operator is not allowed.
2895 */
2896 static Isolate* New();
2897
2898 /**
2899 * Returns the entered isolate for the current thread or NULL in
2900 * case there is no current isolate.
2901 */
2902 static Isolate* GetCurrent();
2903
2904 /**
2905 * Methods below this point require holding a lock (using Locker) in
2906 * a multi-threaded environment.
2907 */
2908
2909 /**
2910 * Sets this isolate as the entered one for the current thread.
2911 * Saves the previously entered one (if any), so that it can be
2912 * restored when exiting. Re-entering an isolate is allowed.
2913 */
2914 void Enter();
2915
2916 /**
2917 * Exits this isolate by restoring the previously entered one in the
2918 * current thread. The isolate may still stay the same, if it was
2919 * entered more than once.
2920 *
2921 * Requires: this == Isolate::GetCurrent().
2922 */
2923 void Exit();
2924
2925 /**
2926 * Disposes the isolate. The isolate must not be entered by any
2927 * thread to be disposable.
2928 */
2929 void Dispose();
2930
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002931 /**
2932 * Associate embedder-specific data with the isolate
2933 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002934 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002935
2936 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002937 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002938 * Returns NULL if SetData has never been called.
2939 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002940 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002941
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002942 /**
2943 * Get statistics about the heap memory usage.
2944 */
2945 void GetHeapStatistics(HeapStatistics* heap_statistics);
2946
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002947 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002948 Isolate();
2949 Isolate(const Isolate&);
2950 ~Isolate();
2951 Isolate& operator=(const Isolate&);
2952 void* operator new(size_t size);
2953 void operator delete(void*, size_t);
2954};
2955
2956
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002957class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002958 public:
2959 enum CompressionAlgorithm {
2960 kUncompressed,
2961 kBZip2
2962 };
2963
2964 const char* data;
2965 int compressed_size;
2966 int raw_size;
2967};
2968
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002969
2970/**
2971 * A helper class for driving V8 startup data decompression. It is based on
2972 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2973 * for an embedder to use this class, instead, API functions can be used
2974 * directly.
2975 *
2976 * For an example of the class usage, see the "shell.cc" sample application.
2977 */
2978class V8EXPORT StartupDataDecompressor { // NOLINT
2979 public:
2980 StartupDataDecompressor();
2981 virtual ~StartupDataDecompressor();
2982 int Decompress();
2983
2984 protected:
2985 virtual int DecompressData(char* raw_data,
2986 int* raw_data_size,
2987 const char* compressed_data,
2988 int compressed_data_size) = 0;
2989
2990 private:
2991 char** raw_data;
2992};
2993
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002994
2995/**
2996 * EntropySource is used as a callback function when v8 needs a source
2997 * of entropy.
2998 */
2999typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3000
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003001
3002/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003003 * ReturnAddressLocationResolver is used as a callback function when v8 is
3004 * resolving the location of a return address on the stack. Profilers that
3005 * change the return address on the stack can use this to resolve the stack
3006 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003007 *
3008 * \param return_addr_location points to a location on stack where a machine
3009 * return address resides.
3010 * \returns either return_addr_location, or else a pointer to the profiler's
3011 * copy of the original return address.
3012 *
3013 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003014 */
3015typedef uintptr_t (*ReturnAddressLocationResolver)(
3016 uintptr_t return_addr_location);
3017
3018
3019/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003020 * FunctionEntryHook is the type of the profile entry hook called at entry to
3021 * any generated function when function-level profiling is enabled.
3022 *
3023 * \param function the address of the function that's being entered.
3024 * \param return_addr_location points to a location on stack where the machine
3025 * return address resides. This can be used to identify the caller of
3026 * \p function, and/or modified to divert execution when \p function exits.
3027 *
3028 * \note the entry hook must not cause garbage collection.
3029 */
3030typedef void (*FunctionEntryHook)(uintptr_t function,
3031 uintptr_t return_addr_location);
3032
3033
3034/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003035 * A JIT code event is issued each time code is added, moved or removed.
3036 *
3037 * \note removal events are not currently issued.
3038 */
3039struct JitCodeEvent {
3040 enum EventType {
3041 CODE_ADDED,
3042 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003043 CODE_REMOVED,
3044 CODE_ADD_LINE_POS_INFO,
3045 CODE_START_LINE_INFO_RECORDING,
3046 CODE_END_LINE_INFO_RECORDING
3047 };
3048 // Definition of the code position type. The "POSITION" type means the place
3049 // in the source code which are of interest when making stack traces to
3050 // pin-point the source location of a stack frame as close as possible.
3051 // The "STATEMENT_POSITION" means the place at the beginning of each
3052 // statement, and is used to indicate possible break locations.
3053 enum PositionType {
3054 POSITION,
3055 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003056 };
3057
3058 // Type of event.
3059 EventType type;
3060 // Start of the instructions.
3061 void* code_start;
3062 // Size of the instructions.
3063 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003064 // Script info for CODE_ADDED event.
3065 Handle<Script> script;
3066 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
3067 // code line information which is returned from the
3068 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
3069 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
3070 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003071
3072 union {
3073 // Only valid for CODE_ADDED.
3074 struct {
3075 // Name of the object associated with the code, note that the string is
3076 // not zero-terminated.
3077 const char* str;
3078 // Number of chars in str.
3079 size_t len;
3080 } name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003081
3082 // Only valid for CODE_ADD_LINE_POS_INFO
3083 struct {
3084 // PC offset
3085 size_t offset;
3086 // Code postion
3087 size_t pos;
3088 // The position type.
3089 PositionType position_type;
3090 } line_info;
3091
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003092 // New location of instructions. Only valid for CODE_MOVED.
3093 void* new_code_start;
3094 };
3095};
3096
3097/**
3098 * Option flags passed to the SetJitCodeEventHandler function.
3099 */
3100enum JitCodeEventOptions {
3101 kJitCodeEventDefault = 0,
3102 // Generate callbacks for already existent code.
3103 kJitCodeEventEnumExisting = 1
3104};
3105
3106
3107/**
3108 * Callback function passed to SetJitCodeEventHandler.
3109 *
3110 * \param event code add, move or removal event.
3111 */
3112typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3113
3114
3115/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003116 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003117 */
3118class V8EXPORT ExternalResourceVisitor { // NOLINT
3119 public:
3120 virtual ~ExternalResourceVisitor() {}
3121 virtual void VisitExternalString(Handle<String> string) {}
3122};
3123
3124
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003125/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003126 * Interface for iterating through all the persistent handles in the heap.
3127 */
3128class V8EXPORT PersistentHandleVisitor { // NOLINT
3129 public:
3130 virtual ~PersistentHandleVisitor() {}
3131 virtual void VisitPersistentHandle(Persistent<Value> value,
3132 uint16_t class_id) {}
3133};
3134
3135
3136/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003137 * Container class for static utility functions.
3138 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003139class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003140 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003141 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142 static void SetFatalErrorHandler(FatalErrorCallback that);
3143
v8.team.kasperl727e9952008-09-02 14:56:44 +00003144 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003145 * Set the callback to invoke to check if code generation from
3146 * strings should be allowed.
3147 */
3148 static void SetAllowCodeGenerationFromStringsCallback(
3149 AllowCodeGenerationFromStringsCallback that);
3150
3151 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003152 * Ignore out-of-memory exceptions.
3153 *
3154 * V8 running out of memory is treated as a fatal error by default.
3155 * This means that the fatal error handler is called and that V8 is
3156 * terminated.
3157 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003158 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003159 * out-of-memory situation as a fatal error. This way, the contexts
3160 * that did not cause the out of memory problem might be able to
3161 * continue execution.
3162 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003163 static void IgnoreOutOfMemoryException();
3164
v8.team.kasperl727e9952008-09-02 14:56:44 +00003165 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003166 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003167 * fatal errors such as out-of-memory situations.
3168 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169 static bool IsDead();
3170
3171 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003172 * The following 4 functions are to be used when V8 is built with
3173 * the 'compress_startup_data' flag enabled. In this case, the
3174 * embedder must decompress startup data prior to initializing V8.
3175 *
3176 * This is how interaction with V8 should look like:
3177 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3178 * v8::StartupData* compressed_data =
3179 * new v8::StartupData[compressed_data_count];
3180 * v8::V8::GetCompressedStartupData(compressed_data);
3181 * ... decompress data (compressed_data can be updated in-place) ...
3182 * v8::V8::SetDecompressedStartupData(compressed_data);
3183 * ... now V8 can be initialized
3184 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003185 *
3186 * A helper class StartupDataDecompressor is provided. It implements
3187 * the protocol of the interaction described above, and can be used in
3188 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003189 */
3190 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3191 static int GetCompressedStartupDataCount();
3192 static void GetCompressedStartupData(StartupData* compressed_data);
3193 static void SetDecompressedStartupData(StartupData* decompressed_data);
3194
3195 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003196 * Adds a message listener.
3197 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003198 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003199 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003200 *
3201 * If data is specified, it will be passed to the callback when it is called.
3202 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003203 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003204 static bool AddMessageListener(MessageCallback that,
3205 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003206
3207 /**
3208 * Remove all message listeners from the specified callback function.
3209 */
3210 static void RemoveMessageListeners(MessageCallback that);
3211
3212 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003213 * Tells V8 to capture current stack trace when uncaught exception occurs
3214 * and report it to the message listeners. The option is off by default.
3215 */
3216 static void SetCaptureStackTraceForUncaughtExceptions(
3217 bool capture,
3218 int frame_limit = 10,
3219 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3220
3221 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003222 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003223 */
3224 static void SetFlagsFromString(const char* str, int length);
3225
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003226 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003227 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003228 */
3229 static void SetFlagsFromCommandLine(int* argc,
3230 char** argv,
3231 bool remove_flags);
3232
kasper.lund7276f142008-07-30 08:49:36 +00003233 /** Get the version string. */
3234 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003235
3236 /**
3237 * Enables the host application to provide a mechanism for recording
3238 * statistics counters.
3239 */
3240 static void SetCounterFunction(CounterLookupCallback);
3241
3242 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003243 * Enables the host application to provide a mechanism for recording
3244 * histograms. The CreateHistogram function returns a
3245 * histogram which will later be passed to the AddHistogramSample
3246 * function.
3247 */
3248 static void SetCreateHistogramFunction(CreateHistogramCallback);
3249 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3250
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003251 /** Callback function for reporting failed access checks.*/
3252 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3253
3254 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003255 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003256 * garbage collection. Allocations are not allowed in the
3257 * callback function, you therefore cannot manipulate objects (set
3258 * or delete properties for example) since it is possible such
3259 * operations will result in the allocation of objects. It is possible
3260 * to specify the GCType filter for your callback. But it is not possible to
3261 * register the same callback function two times with different
3262 * GCType filters.
3263 */
3264 static void AddGCPrologueCallback(
3265 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3266
3267 /**
3268 * This function removes callback which was installed by
3269 * AddGCPrologueCallback function.
3270 */
3271 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3272
3273 /**
3274 * The function is deprecated. Please use AddGCPrologueCallback instead.
3275 * Enables the host application to receive a notification before a
3276 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003277 * callback function, you therefore cannot manipulate objects (set
3278 * or delete properties for example) since it is possible such
3279 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003281 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003282
3283 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003284 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003285 * garbage collection. Allocations are not allowed in the
3286 * callback function, you therefore cannot manipulate objects (set
3287 * or delete properties for example) since it is possible such
3288 * operations will result in the allocation of objects. It is possible
3289 * to specify the GCType filter for your callback. But it is not possible to
3290 * register the same callback function two times with different
3291 * GCType filters.
3292 */
3293 static void AddGCEpilogueCallback(
3294 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3295
3296 /**
3297 * This function removes callback which was installed by
3298 * AddGCEpilogueCallback function.
3299 */
3300 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3301
3302 /**
3303 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3304 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003305 * major garbage collection. Allocations are not allowed in the
3306 * callback function, you therefore cannot manipulate objects (set
3307 * or delete properties for example) since it is possible such
3308 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003309 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003310 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003311
3312 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003313 * Enables the host application to provide a mechanism to be notified
3314 * and perform custom logging when V8 Allocates Executable Memory.
3315 */
3316 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3317 ObjectSpace space,
3318 AllocationAction action);
3319
3320 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003321 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003322 */
3323 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3324
3325 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003326 * Adds a callback to notify the host application when a script finished
3327 * running. If a script re-enters the runtime during executing, the
3328 * CallCompletedCallback is only invoked when the outer-most script
3329 * execution ends. Executing scripts inside the callback do not trigger
3330 * further callbacks.
3331 */
3332 static void AddCallCompletedCallback(CallCompletedCallback callback);
3333
3334 /**
3335 * Removes callback that was installed by AddCallCompletedCallback.
3336 */
3337 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3338
3339 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003340 * Allows the host application to group objects together. If one
3341 * object in the group is alive, all objects in the group are alive.
3342 * After each garbage collection, object groups are removed. It is
3343 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003344 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003345 * wrapper objects. Object groups for all dependent handles need to
3346 * be provided for kGCTypeMarkSweepCompact collections, for all other
3347 * garbage collection types it is sufficient to provide object groups
3348 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003349 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003350 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003351 static void AddObjectGroup(Persistent<Value>* objects,
3352 size_t length,
3353 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003354 static void AddObjectGroup(Isolate* isolate,
3355 Persistent<Value>* objects,
3356 size_t length,
3357 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003358
3359 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003360 * Allows the host application to declare implicit references between
3361 * the objects: if |parent| is alive, all |children| are alive too.
3362 * After each garbage collection, all implicit references
3363 * are removed. It is intended to be used in the before-garbage-collection
3364 * callback function.
3365 */
3366 static void AddImplicitReferences(Persistent<Object> parent,
3367 Persistent<Value>* children,
3368 size_t length);
3369
3370 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003371 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003372 * initialize from scratch. This function is called implicitly if
3373 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003374 */
3375 static bool Initialize();
3376
kasper.lund7276f142008-07-30 08:49:36 +00003377 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003378 * Allows the host application to provide a callback which can be used
3379 * as a source of entropy for random number generators.
3380 */
3381 static void SetEntropySource(EntropySource source);
3382
3383 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003384 * Allows the host application to provide a callback that allows v8 to
3385 * cooperate with a profiler that rewrites return addresses on stack.
3386 */
3387 static void SetReturnAddressLocationResolver(
3388 ReturnAddressLocationResolver return_address_resolver);
3389
3390 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003391 * Allows the host application to provide the address of a function that's
3392 * invoked on entry to every V8-generated function.
3393 * Note that \p entry_hook is invoked at the very start of each
3394 * generated function.
3395 *
3396 * \param entry_hook a function that will be invoked on entry to every
3397 * V8-generated function.
3398 * \returns true on success on supported platforms, false on failure.
3399 * \note Setting a new entry hook function when one is already active will
3400 * fail.
3401 */
3402 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3403
3404 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003405 * Allows the host application to provide the address of a function that is
3406 * notified each time code is added, moved or removed.
3407 *
3408 * \param options options for the JIT code event handler.
3409 * \param event_handler the JIT code event handler, which will be invoked
3410 * each time code is added, moved or removed.
3411 * \note \p event_handler won't get notified of existent code.
3412 * \note since code removal notifications are not currently issued, the
3413 * \p event_handler may get notifications of code that overlaps earlier
3414 * code notifications. This happens when code areas are reused, and the
3415 * earlier overlapping code areas should therefore be discarded.
3416 * \note the events passed to \p event_handler and the strings they point to
3417 * are not guaranteed to live past each call. The \p event_handler must
3418 * copy strings and other parameters it needs to keep around.
3419 * \note the set of events declared in JitCodeEvent::EventType is expected to
3420 * grow over time, and the JitCodeEvent structure is expected to accrue
3421 * new members. The \p event_handler function must ignore event codes
3422 * it does not recognize to maintain future compatibility.
3423 */
3424 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3425 JitCodeEventHandler event_handler);
3426
3427 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003428 * Adjusts the amount of registered external memory. Used to give
3429 * V8 an indication of the amount of externally allocated memory
3430 * that is kept alive by JavaScript objects. V8 uses this to decide
3431 * when to perform global garbage collections. Registering
3432 * externally allocated memory will trigger global garbage
3433 * collections more often than otherwise in an attempt to garbage
3434 * collect the JavaScript objects keeping the externally allocated
3435 * memory alive.
3436 *
3437 * \param change_in_bytes the change in externally allocated memory
3438 * that is kept alive by JavaScript objects.
3439 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003440 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003441 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3442 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003443
iposva@chromium.org245aa852009-02-10 00:49:54 +00003444 /**
3445 * Suspends recording of tick samples in the profiler.
3446 * When the V8 profiling mode is enabled (usually via command line
3447 * switches) this function suspends recording of tick samples.
3448 * Profiling ticks are discarded until ResumeProfiler() is called.
3449 *
3450 * See also the --prof and --prof_auto command line switches to
3451 * enable V8 profiling.
3452 */
3453 static void PauseProfiler();
3454
3455 /**
3456 * Resumes recording of tick samples in the profiler.
3457 * See also PauseProfiler().
3458 */
3459 static void ResumeProfiler();
3460
ager@chromium.org41826e72009-03-30 13:30:57 +00003461 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003462 * Return whether profiler is currently paused.
3463 */
3464 static bool IsProfilerPaused();
3465
3466 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003467 * Retrieve the V8 thread id of the calling thread.
3468 *
3469 * The thread id for a thread should only be retrieved after the V8
3470 * lock has been acquired with a Locker object with that thread.
3471 */
3472 static int GetCurrentThreadId();
3473
3474 /**
3475 * Forcefully terminate execution of a JavaScript thread. This can
3476 * be used to terminate long-running scripts.
3477 *
3478 * TerminateExecution should only be called when then V8 lock has
3479 * been acquired with a Locker object. Therefore, in order to be
3480 * able to terminate long-running threads, preemption must be
3481 * enabled to allow the user of TerminateExecution to acquire the
3482 * lock.
3483 *
3484 * The termination is achieved by throwing an exception that is
3485 * uncatchable by JavaScript exception handlers. Termination
3486 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003487 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003488 * exception handler that catches an exception should check if that
3489 * exception is a termination exception and immediately return if
3490 * that is the case. Returning immediately in that case will
3491 * continue the propagation of the termination exception if needed.
3492 *
3493 * The thread id passed to TerminateExecution must have been
3494 * obtained by calling GetCurrentThreadId on the thread in question.
3495 *
3496 * \param thread_id The thread id of the thread to terminate.
3497 */
3498 static void TerminateExecution(int thread_id);
3499
3500 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003501 * Forcefully terminate the current thread of JavaScript execution
3502 * in the given isolate. If no isolate is provided, the default
3503 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003504 *
3505 * This method can be used by any thread even if that thread has not
3506 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003507 *
3508 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003509 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003510 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003511
3512 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003513 * Is V8 terminating JavaScript execution.
3514 *
3515 * Returns true if JavaScript execution is currently terminating
3516 * because of a call to TerminateExecution. In that case there are
3517 * still JavaScript frames on the stack and the termination
3518 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003519 *
3520 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003521 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003522 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003523
3524 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003525 * Releases any resources used by v8 and stops any utility threads
3526 * that may be running. Note that disposing v8 is permanent, it
3527 * cannot be reinitialized.
3528 *
3529 * It should generally not be necessary to dispose v8 before exiting
3530 * a process, this should happen automatically. It is only necessary
3531 * to use if the process needs the resources taken up by v8.
3532 */
3533 static bool Dispose();
3534
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003535 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
3536 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003537
3538 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003539 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003540 * heap. GC is not invoked prior to iterating, therefore there is no
3541 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003542 */
3543 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3544
3545 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003546 * Iterates through all the persistent handles in the current isolate's heap
3547 * that have class_ids.
3548 */
3549 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3550
3551 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003552 * Iterates through all the persistent handles in the current isolate's heap
3553 * that have class_ids and are candidates to be marked as partially dependent
3554 * handles. This will visit handles to young objects created since the last
3555 * garbage collection but is free to visit an arbitrary superset of these
3556 * objects.
3557 */
3558 static void VisitHandlesForPartialDependence(
3559 Isolate* isolate, PersistentHandleVisitor* visitor);
3560
3561 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003562 * Optional notification that the embedder is idle.
3563 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003564 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003565 * Returns true if the embedder should stop calling IdleNotification
3566 * until real work has been done. This indicates that V8 has done
3567 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003568 *
3569 * The hint argument specifies the amount of work to be done in the function
3570 * on scale from 1 to 1000. There is no guarantee that the actual work will
3571 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003572 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003573 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003574
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003575 /**
3576 * Optional notification that the system is running low on memory.
3577 * V8 uses these notifications to attempt to free memory.
3578 */
3579 static void LowMemoryNotification();
3580
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003581 /**
3582 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003583 * these notifications to guide the GC heuristic. Returns the number
3584 * of context disposals - including this one - since the last time
3585 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003586 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003587 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003588
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003589 private:
3590 V8();
3591
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003592 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
3593 internal::Object** handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003594 static void DisposeGlobal(internal::Isolate* isolate,
3595 internal::Object** global_handle);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003596 static void MakeWeak(internal::Isolate* isolate,
3597 internal::Object** global_handle,
3598 void* data,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003599 WeakReferenceCallback weak_reference_callback,
3600 NearDeathCallback near_death_callback);
3601 static void ClearWeak(internal::Isolate* isolate,
3602 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603
3604 template <class T> friend class Handle;
3605 template <class T> friend class Local;
3606 template <class T> friend class Persistent;
3607 friend class Context;
3608};
3609
3610
3611/**
3612 * An external exception handler.
3613 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003614class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003616 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003617 * Creates a new try/catch block and registers it with v8. Note that
3618 * all TryCatch blocks should be stack allocated because the memory
3619 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003620 */
3621 TryCatch();
3622
3623 /**
3624 * Unregisters and deletes this try/catch block.
3625 */
3626 ~TryCatch();
3627
3628 /**
3629 * Returns true if an exception has been caught by this try/catch block.
3630 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003631 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003632
3633 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003634 * For certain types of exceptions, it makes no sense to continue
3635 * execution.
3636 *
3637 * Currently, the only type of exception that can be caught by a
3638 * TryCatch handler and for which it does not make sense to continue
3639 * is termination exception. Such exceptions are thrown when the
3640 * TerminateExecution methods are called to terminate a long-running
3641 * script.
3642 *
3643 * If CanContinue returns false, the correct action is to perform
3644 * any C++ cleanup needed and then return.
3645 */
3646 bool CanContinue() const;
3647
3648 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003649 * Throws the exception caught by this TryCatch in a way that avoids
3650 * it being caught again by this same TryCatch. As with ThrowException
3651 * it is illegal to execute any JavaScript operations after calling
3652 * ReThrow; the caller must return immediately to where the exception
3653 * is caught.
3654 */
3655 Handle<Value> ReThrow();
3656
3657 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003658 * Returns the exception caught by this try/catch block. If no exception has
3659 * been caught an empty handle is returned.
3660 *
3661 * The returned handle is valid until this TryCatch block has been destroyed.
3662 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003663 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003664
3665 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003666 * Returns the .stack property of the thrown object. If no .stack
3667 * property is present an empty handle is returned.
3668 */
3669 Local<Value> StackTrace() const;
3670
3671 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003672 * Returns the message associated with this exception. If there is
3673 * no message associated an empty handle is returned.
3674 *
3675 * The returned handle is valid until this TryCatch block has been
3676 * destroyed.
3677 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003678 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003679
3680 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003681 * Clears any exceptions that may have been caught by this try/catch block.
3682 * After this method has been called, HasCaught() will return false.
3683 *
3684 * It is not necessary to clear a try/catch block before using it again; if
3685 * another exception is thrown the previously caught exception will just be
3686 * overwritten. However, it is often a good idea since it makes it easier
3687 * to determine which operation threw a given exception.
3688 */
3689 void Reset();
3690
v8.team.kasperl727e9952008-09-02 14:56:44 +00003691 /**
3692 * Set verbosity of the external exception handler.
3693 *
3694 * By default, exceptions that are caught by an external exception
3695 * handler are not reported. Call SetVerbose with true on an
3696 * external exception handler to have exceptions caught by the
3697 * handler reported as if they were not caught.
3698 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003699 void SetVerbose(bool value);
3700
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003701 /**
3702 * Set whether or not this TryCatch should capture a Message object
3703 * which holds source information about where the exception
3704 * occurred. True by default.
3705 */
3706 void SetCaptureMessage(bool value);
3707
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003708 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003709 // Make it hard to create heap-allocated TryCatch blocks.
3710 TryCatch(const TryCatch&);
3711 void operator=(const TryCatch&);
3712 void* operator new(size_t size);
3713 void operator delete(void*, size_t);
3714
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003715 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003716 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003717 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003718 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003719 bool is_verbose_ : 1;
3720 bool can_continue_ : 1;
3721 bool capture_message_ : 1;
3722 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003723
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003724 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003725};
3726
3727
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003728// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003729
3730
3731/**
3732 * Ignore
3733 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003734class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003735 public:
3736 ExtensionConfiguration(int name_count, const char* names[])
3737 : name_count_(name_count), names_(names) { }
3738 private:
3739 friend class ImplementationUtilities;
3740 int name_count_;
3741 const char** names_;
3742};
3743
3744
3745/**
3746 * A sandboxed execution context with its own set of built-in objects
3747 * and functions.
3748 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003749class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003750 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003751 /**
3752 * Returns the global proxy object or global object itself for
3753 * detached contexts.
3754 *
3755 * Global proxy object is a thin wrapper whose prototype points to
3756 * actual context's global object with the properties like Object, etc.
3757 * This is done that way for security reasons (for more details see
3758 * https://wiki.mozilla.org/Gecko:SplitWindow).
3759 *
3760 * Please note that changes to global proxy object prototype most probably
3761 * would break VM---v8 expects only global object as a prototype of
3762 * global proxy object.
3763 *
3764 * If DetachGlobal() has been invoked, Global() would return actual global
3765 * object until global is reattached with ReattachGlobal().
3766 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003767 Local<Object> Global();
3768
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003769 /**
3770 * Detaches the global object from its context before
3771 * the global object can be reused to create a new context.
3772 */
3773 void DetachGlobal();
3774
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003775 /**
3776 * Reattaches a global object to a context. This can be used to
3777 * restore the connection between a global object and a context
3778 * after DetachGlobal has been called.
3779 *
3780 * \param global_object The global object to reattach to the
3781 * context. For this to work, the global object must be the global
3782 * object that was associated with this context before a call to
3783 * DetachGlobal.
3784 */
3785 void ReattachGlobal(Handle<Object> global_object);
3786
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003787 /** Creates a new context.
3788 *
3789 * Returns a persistent handle to the newly allocated context. This
3790 * persistent handle has to be disposed when the context is no
3791 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003792 *
3793 * \param extensions An optional extension configuration containing
3794 * the extensions to be installed in the newly created context.
3795 *
3796 * \param global_template An optional object template from which the
3797 * global object for the newly created context will be created.
3798 *
3799 * \param global_object An optional global object to be reused for
3800 * the newly created context. This global object must have been
3801 * created by a previous call to Context::New with the same global
3802 * template. The state of the global object will be completely reset
3803 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003804 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003805 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003806 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003807 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3808 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809
kasper.lund44510672008-07-25 07:37:58 +00003810 /** Returns the last entered context. */
3811 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003812
kasper.lund44510672008-07-25 07:37:58 +00003813 /** Returns the context that is on the top of the stack. */
3814 static Local<Context> GetCurrent();
3815
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003817 * Returns the context of the calling JavaScript code. That is the
3818 * context of the top-most JavaScript frame. If there are no
3819 * JavaScript frames an empty handle is returned.
3820 */
3821 static Local<Context> GetCalling();
3822
3823 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003824 * Sets the security token for the context. To access an object in
3825 * another context, the security tokens must match.
3826 */
3827 void SetSecurityToken(Handle<Value> token);
3828
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003829 /** Restores the security token to the default value. */
3830 void UseDefaultSecurityToken();
3831
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003832 /** Returns the security token of this context.*/
3833 Handle<Value> GetSecurityToken();
3834
v8.team.kasperl727e9952008-09-02 14:56:44 +00003835 /**
3836 * Enter this context. After entering a context, all code compiled
3837 * and run is compiled and run in this context. If another context
3838 * is already entered, this old context is saved so it can be
3839 * restored when the new context is exited.
3840 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003841 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003842
3843 /**
3844 * Exit this context. Exiting the current context restores the
3845 * context that was in place when entering the current context.
3846 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003847 void Exit();
3848
v8.team.kasperl727e9952008-09-02 14:56:44 +00003849 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003850 bool HasOutOfMemoryException();
3851
v8.team.kasperl727e9952008-09-02 14:56:44 +00003852 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003853 static bool InContext();
3854
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003855 /** Returns an isolate associated with a current context. */
3856 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00003857
3858 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003859 * Gets the embedder data with the given index, which must have been set by a
3860 * previous call to SetEmbedderData with the same index. Note that index 0
3861 * currently has a special meaning for Chrome's debugger.
3862 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003863 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003864
3865 /**
3866 * Sets the embedder data with the given index, growing the data as
3867 * needed. Note that index 0 currently has a special meaning for Chrome's
3868 * debugger.
3869 */
3870 void SetEmbedderData(int index, Handle<Value> value);
3871
3872 /**
3873 * Gets a 2-byte-aligned native pointer from the embedder data with the given
3874 * index, which must have bees set by a previous call to
3875 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
3876 * currently has a special meaning for Chrome's debugger.
3877 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003878 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003879
3880 /**
3881 * Sets a 2-byte-aligned native pointer in the embedder data with the given
3882 * index, growing the data as needed. Note that index 0 currently has a
3883 * special meaning for Chrome's debugger.
3884 */
3885 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00003886
3887 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003888 * Control whether code generation from strings is allowed. Calling
3889 * this method with false will disable 'eval' and the 'Function'
3890 * constructor for code running in this context. If 'eval' or the
3891 * 'Function' constructor are used an exception will be thrown.
3892 *
3893 * If code generation from strings is not allowed the
3894 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3895 * set before blocking the call to 'eval' or the 'Function'
3896 * constructor. If that callback returns true, the call will be
3897 * allowed, otherwise an exception will be thrown. If no callback is
3898 * set an exception will be thrown.
3899 */
3900 void AllowCodeGenerationFromStrings(bool allow);
3901
3902 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003903 * Returns true if code generation from strings is allowed for the context.
3904 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3905 */
3906 bool IsCodeGenerationFromStringsAllowed();
3907
3908 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003909 * Sets the error description for the exception that is thrown when
3910 * code generation from strings is not allowed and 'eval' or the 'Function'
3911 * constructor are called.
3912 */
3913 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3914
3915 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003916 * Stack-allocated class which sets the execution context for all
3917 * operations executed within a local scope.
3918 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003919 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003921 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003922 context_->Enter();
3923 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003924 V8_INLINE(~Scope()) { context_->Exit(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003925 private:
3926 Handle<Context> context_;
3927 };
3928
3929 private:
3930 friend class Value;
3931 friend class Script;
3932 friend class Object;
3933 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003934
3935 Local<Value> SlowGetEmbedderData(int index);
3936 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003937};
3938
3939
3940/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003941 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
3942 * to use any given V8 isolate, see the comments in the Isolate class. The
3943 * definition of 'using a V8 isolate' includes accessing handles or holding onto
3944 * object pointers obtained from V8 handles while in the particular V8 isolate.
3945 * It is up to the user of V8 to ensure, perhaps with locking, that this
3946 * constraint is not violated. In addition to any other synchronization
3947 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
3948 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003949 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003950 * v8::Locker is a scoped lock object. While it's active, i.e. between its
3951 * construction and destruction, the current thread is allowed to use the locked
3952 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
3953 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003954 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003955 * Sample usage:
3956* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957 * ...
3958 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003959 * v8::Locker locker(isolate);
3960 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003961 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003962 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003963 * ...
3964 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003965 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003966 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003967 * If you wish to stop using V8 in a thread A you can do this either by
3968 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
3969 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003970 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003971 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003972 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003973 * isolate->Exit();
3974 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003975 * ...
3976 * // Code not using V8 goes here while V8 can run in another thread.
3977 * ...
3978 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003979 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003980 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003981 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003982 * The Unlocker object is intended for use in a long-running callback from V8,
3983 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003985 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
3986 * given thread. This can be useful if you have code that can be called either
3987 * from code that holds the lock or from code that does not. The Unlocker is
3988 * not recursive so you can not have several Unlockers on the stack at once, and
3989 * 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 +00003990 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003991 * An unlocker will unlock several lockers if it has to and reinstate the
3992 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003993 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003994 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003995 * // V8 not locked.
3996 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003997 * v8::Locker locker(isolate);
3998 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003999 * // V8 locked.
4000 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004001 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004002 * // V8 still locked (2 levels).
4003 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004004 * isolate->Exit();
4005 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004006 * // V8 not locked.
4007 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004008 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004009 * // V8 locked again (2 levels).
4010 * }
4011 * // V8 still locked (1 level).
4012 * }
4013 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004014 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004015 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004016class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004017 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004018 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004019 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004020 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004021 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
4022
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004023 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004024 V8_DEPRECATED(Unlocker());
4025
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004026 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00004027 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004028 void Initialize(Isolate* isolate);
4029
lrn@chromium.org1c092762011-05-09 09:42:16 +00004030 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004031};
4032
4033
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004034class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004035 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004036 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004037 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004038 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004039 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
4040
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004041 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004042 V8_DEPRECATED(Locker());
4043
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004044 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004045
4046 /**
4047 * Start preemption.
4048 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004049 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004050 * that will switch between multiple threads that are in contention
4051 * for the V8 lock.
4052 */
4053 static void StartPreemption(int every_n_ms);
4054
4055 /**
4056 * Stop preemption.
4057 */
4058 static void StopPreemption();
4059
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004060 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004061 * Returns whether or not the locker for a given isolate, is locked by the
4062 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004063 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004064 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004065
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004066 /**
4067 * Returns whether v8::Locker is being used by this V8 instance.
4068 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004069 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004070
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004072 void Initialize(Isolate* isolate);
4073
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004074 bool has_lock_;
4075 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004076 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004077
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004078 static bool active_;
4079
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004080 // Disallow copying and assigning.
4081 Locker(const Locker&);
4082 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004083};
4084
4085
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004086/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004087 * A struct for exporting HeapStats data from V8, using "push" model.
4088 */
4089struct HeapStatsUpdate;
4090
4091
4092/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004093 * An interface for exporting data from V8, using "push" model.
4094 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004095class V8EXPORT OutputStream { // NOLINT
4096 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004097 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004098 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004099 };
4100 enum WriteResult {
4101 kContinue = 0,
4102 kAbort = 1
4103 };
4104 virtual ~OutputStream() {}
4105 /** Notify about the end of stream. */
4106 virtual void EndOfStream() = 0;
4107 /** Get preferred output chunk size. Called only once. */
4108 virtual int GetChunkSize() { return 1024; }
4109 /** Get preferred output encoding. Called only once. */
4110 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4111 /**
4112 * Writes the next chunk of snapshot data into the stream. Writing
4113 * can be stopped by returning kAbort as function result. EndOfStream
4114 * will not be called in case writing was aborted.
4115 */
4116 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004117 /**
4118 * Writes the next chunk of heap stats data into the stream. Writing
4119 * can be stopped by returning kAbort as function result. EndOfStream
4120 * will not be called in case writing was aborted.
4121 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004122 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004123 return kAbort;
4124 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004125};
4126
4127
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004128/**
4129 * An interface for reporting progress and controlling long-running
4130 * activities.
4131 */
4132class V8EXPORT ActivityControl { // NOLINT
4133 public:
4134 enum ControlOption {
4135 kContinue = 0,
4136 kAbort = 1
4137 };
4138 virtual ~ActivityControl() {}
4139 /**
4140 * Notify about current progress. The activity can be stopped by
4141 * returning kAbort as the callback result.
4142 */
4143 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4144};
4145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004147// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004148
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004149
4150namespace internal {
4151
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004152const int kApiPointerSize = sizeof(void*); // NOLINT
4153const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004154
4155// Tag information for HeapObject.
4156const int kHeapObjectTag = 1;
4157const int kHeapObjectTagSize = 2;
4158const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4159
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004160// Tag information for Smi.
4161const int kSmiTag = 0;
4162const int kSmiTagSize = 1;
4163const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4164
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004165template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004166
4167// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004168template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004169 static const int kSmiShiftSize = 0;
4170 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004171 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004172 int shift_bits = kSmiTagSize + kSmiShiftSize;
4173 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4174 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4175 }
4176};
4177
4178// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004179template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004180 static const int kSmiShiftSize = 31;
4181 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004182 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004183 int shift_bits = kSmiTagSize + kSmiShiftSize;
4184 // Shift down and throw away top 32 bits.
4185 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4186 }
4187};
4188
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004189typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4190const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4191const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004192
4193/**
4194 * This class exports constants and functionality from within v8 that
4195 * is necessary to implement inline functions in the v8 api. Don't
4196 * depend on functions and constants defined here.
4197 */
4198class Internals {
4199 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004200 // These values match non-compiler-dependent values defined within
4201 // the implementation of v8.
4202 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004203 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004204 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004205
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004206 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004207 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004208 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004209 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4210 static const int kContextHeaderSize = 2 * kApiPointerSize;
4211 static const int kContextEmbedderDataIndex = 54;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004212 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004213 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004214 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004215 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004216
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004217 static const int kIsolateStateOffset = 0;
4218 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4219 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4220 static const int kUndefinedValueRootIndex = 5;
4221 static const int kNullValueRootIndex = 7;
4222 static const int kTrueValueRootIndex = 8;
4223 static const int kFalseValueRootIndex = 9;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004224 static const int kEmptyStringRootIndex = 119;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004225
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004226 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
4227 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
4228 static const int kNodeStateMask = 0xf;
4229 static const int kNodeStateIsWeakValue = 2;
4230 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004231 static const int kNodeIsIndependentShift = 4;
4232 static const int kNodeIsPartiallyDependentShift = 5;
4233
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004234 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004235 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004236 static const int kOddballType = 0x83;
4237 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004238
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004239 static const int kUndefinedOddballKind = 5;
4240 static const int kNullOddballKind = 3;
4241
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004242 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004243 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4244 kHeapObjectTag);
4245 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004246
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004247 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004248 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004249 }
4250
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004251 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004252 typedef internal::Object O;
4253 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4254 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4255 }
4256
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004257 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004258 typedef internal::Object O;
4259 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4260 }
4261
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004262 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004263 int representation = (instance_type & kFullStringRepresentationMask);
4264 return representation == kExternalTwoByteRepresentationTag;
4265 }
4266
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004267 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004268 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4269 return *reinterpret_cast<int*>(addr) == 1;
4270 }
4271
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004272 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
4273 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4274 return *addr & (1 << shift);
4275 }
4276
4277 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
4278 bool value, int shift)) {
4279 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4280 uint8_t mask = 1 << shift;
4281 *addr = (*addr & ~mask) | (value << shift);
4282 }
4283
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004284 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
4285 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4286 return *addr & kNodeStateMask;
4287 }
4288
4289 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
4290 uint8_t value)) {
4291 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4292 *addr = (*addr & ~kNodeStateMask) | value;
4293 }
4294
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004295 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004296 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4297 kIsolateEmbedderDataOffset;
4298 *reinterpret_cast<void**>(addr) = data;
4299 }
4300
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004301 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004302 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4303 kIsolateEmbedderDataOffset;
4304 return *reinterpret_cast<void**>(addr);
4305 }
4306
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004307 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
4308 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004309 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4310 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4311 }
4312
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004313 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004314 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004315 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4316 return *reinterpret_cast<T*>(addr);
4317 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004318
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004319 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004320 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004321 typedef internal::Object O;
4322 typedef internal::Internals I;
4323 O* ctx = *reinterpret_cast<O**>(context);
4324 int embedder_data_offset = I::kContextHeaderSize +
4325 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4326 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4327 int value_offset =
4328 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4329 return I::ReadField<T>(embedder_data, value_offset);
4330 }
4331
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004332 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
4333 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
4334 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
4335 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
4336 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
4337 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
4338 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004339};
4340
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004341} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004342
4343
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004344template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004345Local<T>::Local() : Handle<T>() { }
4346
4347
4348template <class T>
4349Local<T> Local<T>::New(Handle<T> that) {
4350 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004351 T* that_ptr = *that;
4352 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4353 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4354 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4355 reinterpret_cast<internal::HeapObject*>(*p))));
4356 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004357 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4358}
4359
4360
4361template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004362Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004363 if (that.IsEmpty()) return Local<T>();
4364 T* that_ptr = *that;
4365 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4366 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4367 reinterpret_cast<internal::Isolate*>(isolate), *p)));
4368}
4369
4370
4371template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004372Persistent<T> Persistent<T>::New(Handle<T> that) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004373 return New(Isolate::GetCurrent(), that);
4374}
4375
4376
4377template <class T>
4378Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004379 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004380 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004381 return Persistent<T>(reinterpret_cast<T*>(
4382 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
4383 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004384}
4385
4386
4387template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004388bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004389 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004390}
4391
4392
4393template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004394bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004395 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004396 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004397 if (!I::IsInitialized(isolate)) return false;
4398 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(**this),
4399 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004400}
4401
4402
4403template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004404bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004405 return IsNearDeath(Isolate::GetCurrent());
4406}
4407
4408
4409template <class T>
4410bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
4411 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004412 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004413 if (!I::IsInitialized(isolate)) return false;
4414 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4415 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004416}
4417
4418
4419template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004420bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004421 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004422}
4423
4424
4425template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004426bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004427 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004428 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004429 if (!I::IsInitialized(isolate)) return false;
4430 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4431 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004432}
4433
4434
4435template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004436void Persistent<T>::Dispose() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004437 Dispose(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004438}
4439
4440
4441template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004442void Persistent<T>::Dispose(Isolate* isolate) {
4443 if (this->IsEmpty()) return;
4444 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4445 reinterpret_cast<internal::Object**>(**this));
4446}
4447
4448
4449template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004450Persistent<T>::Persistent() : Handle<T>() { }
4451
4452template <class T>
4453void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004454 Isolate* isolate = Isolate::GetCurrent();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004455 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4456 reinterpret_cast<internal::Object**>(**this),
4457 parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004458 callback,
4459 NULL);
4460}
4461
4462template <class T>
4463void Persistent<T>::MakeWeak(Isolate* isolate,
4464 void* parameters,
4465 NearDeathCallback callback) {
4466 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4467 reinterpret_cast<internal::Object**>(**this),
4468 parameters,
4469 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004470 callback);
4471}
4472
4473template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004474void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004475 ClearWeak(Isolate::GetCurrent());
4476}
4477
4478template <class T>
4479void Persistent<T>::ClearWeak(Isolate* isolate) {
4480 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
4481 reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004482}
4483
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004484template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004485void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004486 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004487}
4488
4489template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004490void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004491 typedef internal::Internals I;
4492 if (this->IsEmpty()) return;
4493 if (!I::IsInitialized(isolate)) return;
4494 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004495 true,
4496 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004497}
4498
4499template <class T>
4500void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004501 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004502}
4503
4504template <class T>
4505void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004506 typedef internal::Internals I;
4507 if (this->IsEmpty()) return;
4508 if (!I::IsInitialized(isolate)) return;
4509 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004510 true,
4511 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004512}
4513
4514template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004515void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004516 SetWrapperClassId(Isolate::GetCurrent(), class_id);
4517}
4518
4519template <class T>
4520void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004521 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004522 if (this->IsEmpty()) return;
4523 if (!I::IsInitialized(isolate)) return;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004524 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4525 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4526 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004527}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004528
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004529template <class T>
4530uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004531 return WrapperClassId(Isolate::GetCurrent());
4532}
4533
4534template <class T>
4535uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004536 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004537 if (this->IsEmpty()) return 0;
4538 if (!I::IsInitialized(isolate)) return 0;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004539 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4540 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4541 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004542}
4543
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004544Arguments::Arguments(internal::Object** implicit_args,
4545 internal::Object** values, int length,
4546 bool is_construct_call)
4547 : implicit_args_(implicit_args),
4548 values_(values),
4549 length_(length),
4550 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004551
4552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004553Local<Value> Arguments::operator[](int i) const {
4554 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4555 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4556}
4557
4558
4559Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004560 return Local<Function>(reinterpret_cast<Function*>(
4561 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004562}
4563
4564
4565Local<Object> Arguments::This() const {
4566 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4567}
4568
4569
4570Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004571 return Local<Object>(reinterpret_cast<Object*>(
4572 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004573}
4574
4575
4576Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004577 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004578}
4579
4580
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004581Isolate* Arguments::GetIsolate() const {
4582 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4583}
4584
4585
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004586bool Arguments::IsConstructCall() const {
4587 return is_construct_call_;
4588}
4589
4590
4591int Arguments::Length() const {
4592 return length_;
4593}
4594
4595
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004596template <class T>
4597Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004598 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4599 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004600 return Local<T>(reinterpret_cast<T*>(after));
4601}
4602
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004603Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004604 return resource_name_;
4605}
4606
4607
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004608Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004609 return resource_line_offset_;
4610}
4611
4612
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004613Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004614 return resource_column_offset_;
4615}
4616
4617
4618Handle<Boolean> Boolean::New(bool value) {
4619 return value ? True() : False();
4620}
4621
4622
4623void Template::Set(const char* name, v8::Handle<Data> value) {
4624 Set(v8::String::New(name), value);
4625}
4626
4627
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004628Local<Value> Object::GetInternalField(int index) {
4629#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004630 typedef internal::Object O;
4631 typedef internal::Internals I;
4632 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004633 // Fast path: If the object is a plain JSObject, which is the common case, we
4634 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004635 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004636 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004637 O* value = I::ReadField<O*>(obj, offset);
4638 O** result = HandleScope::CreateHandle(value);
4639 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004640 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004641#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004642 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004643}
4644
4645
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004646void* Object::GetAlignedPointerFromInternalField(int index) {
4647#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004648 typedef internal::Object O;
4649 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004650 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004651 // Fast path: If the object is a plain JSObject, which is the common case, we
4652 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004653 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004654 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004655 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004656 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004657#endif
4658 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004659}
4660
4661
4662String* String::Cast(v8::Value* value) {
4663#ifdef V8_ENABLE_CHECKS
4664 CheckCast(value);
4665#endif
4666 return static_cast<String*>(value);
4667}
4668
4669
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004670Local<String> String::Empty(Isolate* isolate) {
4671 typedef internal::Object* S;
4672 typedef internal::Internals I;
4673 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004674 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004675 return Local<String>(reinterpret_cast<String*>(slot));
4676}
4677
4678
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004679String::ExternalStringResource* String::GetExternalStringResource() const {
4680 typedef internal::Object O;
4681 typedef internal::Internals I;
4682 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004683 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004684 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004685 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4686 result = reinterpret_cast<String::ExternalStringResource*>(value);
4687 } else {
4688 result = NULL;
4689 }
4690#ifdef V8_ENABLE_CHECKS
4691 VerifyExternalStringResource(result);
4692#endif
4693 return result;
4694}
4695
4696
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004697String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4698 String::Encoding* encoding_out) const {
4699 typedef internal::Object O;
4700 typedef internal::Internals I;
4701 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4702 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4703 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4704 ExternalStringResourceBase* resource = NULL;
4705 if (type == I::kExternalAsciiRepresentationTag ||
4706 type == I::kExternalTwoByteRepresentationTag) {
4707 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4708 resource = static_cast<ExternalStringResourceBase*>(value);
4709 }
4710#ifdef V8_ENABLE_CHECKS
4711 VerifyExternalStringResourceBase(resource, *encoding_out);
4712#endif
4713 return resource;
4714}
4715
4716
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004717bool Value::IsUndefined() const {
4718#ifdef V8_ENABLE_CHECKS
4719 return FullIsUndefined();
4720#else
4721 return QuickIsUndefined();
4722#endif
4723}
4724
4725bool Value::QuickIsUndefined() const {
4726 typedef internal::Object O;
4727 typedef internal::Internals I;
4728 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4729 if (!I::HasHeapObjectTag(obj)) return false;
4730 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4731 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4732}
4733
4734
4735bool Value::IsNull() const {
4736#ifdef V8_ENABLE_CHECKS
4737 return FullIsNull();
4738#else
4739 return QuickIsNull();
4740#endif
4741}
4742
4743bool Value::QuickIsNull() const {
4744 typedef internal::Object O;
4745 typedef internal::Internals I;
4746 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4747 if (!I::HasHeapObjectTag(obj)) return false;
4748 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4749 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4750}
4751
4752
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004753bool Value::IsString() const {
4754#ifdef V8_ENABLE_CHECKS
4755 return FullIsString();
4756#else
4757 return QuickIsString();
4758#endif
4759}
4760
4761bool Value::QuickIsString() const {
4762 typedef internal::Object O;
4763 typedef internal::Internals I;
4764 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4765 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004766 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004767}
4768
4769
4770Number* Number::Cast(v8::Value* value) {
4771#ifdef V8_ENABLE_CHECKS
4772 CheckCast(value);
4773#endif
4774 return static_cast<Number*>(value);
4775}
4776
4777
4778Integer* Integer::Cast(v8::Value* value) {
4779#ifdef V8_ENABLE_CHECKS
4780 CheckCast(value);
4781#endif
4782 return static_cast<Integer*>(value);
4783}
4784
4785
4786Date* Date::Cast(v8::Value* value) {
4787#ifdef V8_ENABLE_CHECKS
4788 CheckCast(value);
4789#endif
4790 return static_cast<Date*>(value);
4791}
4792
4793
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004794StringObject* StringObject::Cast(v8::Value* value) {
4795#ifdef V8_ENABLE_CHECKS
4796 CheckCast(value);
4797#endif
4798 return static_cast<StringObject*>(value);
4799}
4800
4801
4802NumberObject* NumberObject::Cast(v8::Value* value) {
4803#ifdef V8_ENABLE_CHECKS
4804 CheckCast(value);
4805#endif
4806 return static_cast<NumberObject*>(value);
4807}
4808
4809
4810BooleanObject* BooleanObject::Cast(v8::Value* value) {
4811#ifdef V8_ENABLE_CHECKS
4812 CheckCast(value);
4813#endif
4814 return static_cast<BooleanObject*>(value);
4815}
4816
4817
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004818RegExp* RegExp::Cast(v8::Value* value) {
4819#ifdef V8_ENABLE_CHECKS
4820 CheckCast(value);
4821#endif
4822 return static_cast<RegExp*>(value);
4823}
4824
4825
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004826Object* Object::Cast(v8::Value* value) {
4827#ifdef V8_ENABLE_CHECKS
4828 CheckCast(value);
4829#endif
4830 return static_cast<Object*>(value);
4831}
4832
4833
4834Array* Array::Cast(v8::Value* value) {
4835#ifdef V8_ENABLE_CHECKS
4836 CheckCast(value);
4837#endif
4838 return static_cast<Array*>(value);
4839}
4840
4841
4842Function* Function::Cast(v8::Value* value) {
4843#ifdef V8_ENABLE_CHECKS
4844 CheckCast(value);
4845#endif
4846 return static_cast<Function*>(value);
4847}
4848
4849
4850External* External::Cast(v8::Value* value) {
4851#ifdef V8_ENABLE_CHECKS
4852 CheckCast(value);
4853#endif
4854 return static_cast<External*>(value);
4855}
4856
4857
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004858Isolate* AccessorInfo::GetIsolate() const {
4859 return *reinterpret_cast<Isolate**>(&args_[-3]);
4860}
4861
4862
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004863Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004864 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004865}
4866
4867
4868Local<Object> AccessorInfo::This() const {
4869 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4870}
4871
4872
4873Local<Object> AccessorInfo::Holder() const {
4874 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4875}
4876
4877
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004878Handle<Primitive> Undefined(Isolate* isolate) {
4879 typedef internal::Object* S;
4880 typedef internal::Internals I;
4881 if (!I::IsInitialized(isolate)) return Undefined();
4882 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4883 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4884}
4885
4886
4887Handle<Primitive> Null(Isolate* isolate) {
4888 typedef internal::Object* S;
4889 typedef internal::Internals I;
4890 if (!I::IsInitialized(isolate)) return Null();
4891 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4892 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4893}
4894
4895
4896Handle<Boolean> True(Isolate* isolate) {
4897 typedef internal::Object* S;
4898 typedef internal::Internals I;
4899 if (!I::IsInitialized(isolate)) return True();
4900 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4901 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4902}
4903
4904
4905Handle<Boolean> False(Isolate* isolate) {
4906 typedef internal::Object* S;
4907 typedef internal::Internals I;
4908 if (!I::IsInitialized(isolate)) return False();
4909 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4910 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4911}
4912
4913
4914void Isolate::SetData(void* data) {
4915 typedef internal::Internals I;
4916 I::SetEmbedderData(this, data);
4917}
4918
4919
4920void* Isolate::GetData() {
4921 typedef internal::Internals I;
4922 return I::GetEmbedderData(this);
4923}
4924
4925
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004926Local<Value> Context::GetEmbedderData(int index) {
4927#ifndef V8_ENABLE_CHECKS
4928 typedef internal::Object O;
4929 typedef internal::Internals I;
4930 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
4931 return Local<Value>(reinterpret_cast<Value*>(result));
4932#else
4933 return SlowGetEmbedderData(index);
4934#endif
4935}
4936
4937
4938void* Context::GetAlignedPointerFromEmbedderData(int index) {
4939#ifndef V8_ENABLE_CHECKS
4940 typedef internal::Internals I;
4941 return I::ReadEmbedderData<void*>(this, index);
4942#else
4943 return SlowGetAlignedPointerFromEmbedderData(index);
4944#endif
4945}
4946
4947
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004948/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004949 * \example shell.cc
4950 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004951 * command-line and executes them.
4952 */
4953
4954
4955/**
4956 * \example process.cc
4957 */
4958
4959
4960} // namespace v8
4961
4962
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004963#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004964#undef TYPE_CHECK
4965
4966
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004967#endif // V8_H_