blob: 5f3e2ed98feeb4a190ab9897601ba8aafeb390ef [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
43
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000044#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045
46#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000047
48// Setup for Windows DLL export/import. When building the V8 DLL the
49// BUILDING_V8_SHARED needs to be defined. When building a program which uses
50// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
51// static library or building a program which uses the V8 static library neither
52// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000053#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
54#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
55 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056#endif
57
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000059#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000060#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000061#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000062#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000063#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000064#endif // BUILDING_V8_SHARED
65
66#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000067
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000069#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
70 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000071#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000072#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000073#else
74#define V8EXPORT
75#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000076#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000077#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000078#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079
80#endif // _WIN32
81
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000082#if defined(__GNUC__) && !defined(DEBUG)
83#define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
84#elif defined(_MSC_VER) && !defined(DEBUG)
85#define V8_INLINE(declarator) __forceinline declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000086#else
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000087#define V8_INLINE(declarator) inline declarator
88#endif
89
90#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
91#define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
92#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
93#define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
94#else
95#define V8_DEPRECATED(declarator) declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000096#endif
97
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000099 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 */
101namespace v8 {
102
103class Context;
104class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000105class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106class Value;
107class Utils;
108class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000109class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110class Object;
111class Array;
112class Int32;
113class Uint32;
114class External;
115class Primitive;
116class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000117class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118class Integer;
119class Function;
120class Date;
121class ImplementationUtilities;
122class Signature;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000123class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124template <class T> class Handle;
125template <class T> class Local;
126template <class T> class Persistent;
127class FunctionTemplate;
128class ObjectTemplate;
129class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000130class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000131class StackTrace;
132class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000133class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000135namespace internal {
136
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000137class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000138class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000139class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000140class HeapObject;
141class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000142}
143
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000145// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146
147
148/**
149 * A weak reference callback function.
150 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000151 * This callback should either explicitly invoke Dispose on |object| if
152 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
153 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 * \param object the weak global object to be reclaimed by the garbage collector
155 * \param parameter the value passed in when making the weak global object
156 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000157typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 void* parameter);
159
160
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000161// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000163#define TYPE_CHECK(T, S) \
164 while (false) { \
165 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 }
167
168/**
169 * An object reference managed by the v8 garbage collector.
170 *
171 * All objects returned from v8 have to be tracked by the garbage
172 * collector so that it knows that the objects are still alive. Also,
173 * because the garbage collector may move objects, it is unsafe to
174 * point directly to an object. Instead, all objects are stored in
175 * handles which are known by the garbage collector and updated
176 * whenever an object moves. Handles should always be passed by value
177 * (except in cases like out-parameters) and they should never be
178 * allocated on the heap.
179 *
180 * There are two types of handles: local and persistent handles.
181 * Local handles are light-weight and transient and typically used in
182 * local operations. They are managed by HandleScopes. Persistent
183 * handles can be used when storing objects across several independent
184 * operations and have to be explicitly deallocated when they're no
185 * longer used.
186 *
187 * It is safe to extract the object stored in the handle by
188 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000189 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 * behind the scenes and the same rules apply to these values as to
191 * their handles.
192 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000193template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 /**
196 * Creates an empty handle.
197 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000198 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199
200 /**
201 * Creates a new handle for the specified value.
202 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000203 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
205 /**
206 * Creates a handle for the contents of the specified handle. This
207 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000208 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000210 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 * between compatible handles, for instance assigning a
212 * Handle<String> to a variable declared as Handle<Value>, is legal
213 * because String is a subclass of Value.
214 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000215 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 : val_(reinterpret_cast<T*>(*that)) {
217 /**
218 * This check fails when trying to convert between incompatible
219 * handles. For example, converting from a Handle<String> to a
220 * Handle<Number>.
221 */
222 TYPE_CHECK(T, S);
223 }
224
225 /**
226 * Returns true if the handle is empty.
227 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000228 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 /**
231 * Sets the handle to be empty. IsEmpty() will then return true.
232 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000233 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000234
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000235 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000236
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000237 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238
239 /**
240 * Checks whether two handles are the same.
241 * Returns true if both are empty, or if the objects
242 * to which they refer are identical.
243 * The handles' references are not checked.
244 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000245 template <class S> V8_INLINE(bool operator==(Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000246 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
247 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 if (a == 0) return b == 0;
249 if (b == 0) return false;
250 return *a == *b;
251 }
252
253 /**
254 * Checks whether two handles are different.
255 * Returns true if only one of the handles is empty, or if
256 * the objects to which they refer are different.
257 * The handles' references are not checked.
258 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000259 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 return !operator==(that);
261 }
262
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000263 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000264#ifdef V8_ENABLE_CHECKS
265 // If we're going to perform the type check then we have to check
266 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000268#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 return Handle<T>(T::Cast(*that));
270 }
271
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000272 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000273 return Handle<S>::Cast(*this);
274 }
275
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 private:
277 T* val_;
278};
279
280
281/**
282 * A light-weight stack-allocated object handle. All operations
283 * that return objects from within v8 return them in local handles. They
284 * are created within HandleScopes, and all local handles allocated within a
285 * handle scope are destroyed when the handle scope is destroyed. Hence it
286 * is not necessary to explicitly deallocate local handles.
287 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000288template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000290 V8_INLINE(Local());
291 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 : Handle<T>(reinterpret_cast<T*>(*that)) {
293 /**
294 * This check fails when trying to convert between incompatible
295 * handles. For example, converting from a Handle<String> to a
296 * Handle<Number>.
297 */
298 TYPE_CHECK(T, S);
299 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000300 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
301 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000302#ifdef V8_ENABLE_CHECKS
303 // If we're going to perform the type check then we have to check
304 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000306#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 return Local<T>(T::Cast(*that));
308 }
309
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000310 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000311 return Local<S>::Cast(*this);
312 }
313
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000314 /**
315 * Create a local handle for the content of another handle.
316 * The referee is kept alive by the local handle even when
317 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000319 V8_INLINE(static Local<T> New(Handle<T> that));
320 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321};
322
323
324/**
325 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000326 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 * allocated, a Persistent handle remains valid until it is explicitly
328 * disposed.
329 *
330 * A persistent handle contains a reference to a storage cell within
331 * the v8 engine which holds an object value and which is updated by
332 * the garbage collector whenever the object is moved. A new storage
333 * cell can be created using Persistent::New and existing handles can
334 * be disposed using Persistent::Dispose. Since persistent handles
335 * are passed by value you may have many persistent handle objects
336 * that point to the same storage cell. For instance, if you pass a
337 * persistent handle as an argument to a function you will not get two
338 * different storage cells but rather two references to the same
339 * storage cell.
340 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000341template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343 /**
344 * Creates an empty persistent handle that doesn't point to any
345 * storage cell.
346 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000347 V8_INLINE(Persistent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348
349 /**
350 * Creates a persistent handle for the same storage cell as the
351 * specified handle. This constructor allows you to pass persistent
352 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000353 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000355 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356 * between compatible persistent handles, for instance assigning a
357 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000358 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000360 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361 : Handle<T>(reinterpret_cast<T*>(*that)) {
362 /**
363 * This check fails when trying to convert between incompatible
364 * handles. For example, converting from a Handle<String> to a
365 * Handle<Number>.
366 */
367 TYPE_CHECK(T, S);
368 }
369
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000370 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000372 /**
373 * "Casts" a plain handle which is known to be a persistent handle
374 * to a persistent handle.
375 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000376 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377 : Handle<T>(*that) { }
378
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000379 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000380#ifdef V8_ENABLE_CHECKS
381 // If we're going to perform the type check then we have to check
382 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000384#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385 return Persistent<T>(T::Cast(*that));
386 }
387
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000388 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000389 return Persistent<S>::Cast(*this);
390 }
391
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000393 * Creates a new persistent handle for an existing local or
394 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000396 V8_INLINE(static Persistent<T> New(Handle<T> that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397
398 /**
399 * Releases the storage cell referenced by this persistent handle.
400 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000401 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 * cell remain and IsEmpty will still return false.
403 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000404 V8_INLINE(void Dispose());
405 V8_INLINE(void Dispose(Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406
407 /**
408 * Make the reference to this object weak. When only weak handles
409 * refer to the object, the garbage collector will perform a
410 * callback to the given V8::WeakReferenceCallback function, passing
411 * it the object reference and the given parameters.
412 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000413 V8_INLINE(void MakeWeak(void* parameters, WeakReferenceCallback callback));
414 V8_INLINE(void MakeWeak(Isolate* isolate,
415 void* parameters,
416 WeakReferenceCallback callback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000418 /** Clears the weak reference to this object. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000419 V8_INLINE(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420
421 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000422 * Marks the reference to this object independent. Garbage collector
423 * is free to ignore any object groups containing this object.
424 * Weak callback for an independent handle should not
425 * assume that it will be preceded by a global GC prologue callback
426 * or followed by a global GC epilogue callback.
427 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000428 V8_INLINE(void MarkIndependent());
429 V8_INLINE(void MarkIndependent(Isolate* isolate));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000430
431 /**
432 * Marks the reference to this object partially dependent. Partially
433 * dependent handles only depend on other partially dependent handles and
434 * these dependencies are provided through object groups. It provides a way
435 * to build smaller object groups for young objects that represent only a
436 * subset of all external dependencies. This mark is automatically cleared
437 * after each garbage collection.
438 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000439 V8_INLINE(void MarkPartiallyDependent());
440 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000441
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000442 /** Returns true if this handle was previously marked as independent. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000443 V8_INLINE(bool IsIndependent() const);
444 V8_INLINE(bool IsIndependent(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000445
446 /** Checks if the handle holds the only reference to an object. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000447 V8_INLINE(bool IsNearDeath() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000449 /** Returns true if the handle's reference is weak. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000450 V8_INLINE(bool IsWeak() const);
451 V8_INLINE(bool IsWeak(Isolate* isolate) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000453 /**
454 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
455 * interface description in v8-profiler.h for details.
456 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000457 V8_INLINE(void SetWrapperClassId(uint16_t class_id));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000458
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000459 /**
460 * Returns the class ID previously assigned to this handle or 0 if no class
461 * ID was previously assigned.
462 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000463 V8_INLINE(uint16_t WrapperClassId() const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465 private:
466 friend class ImplementationUtilities;
467 friend class ObjectTemplate;
468};
469
470
v8.team.kasperl727e9952008-09-02 14:56:44 +0000471 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 * A stack-allocated class that governs a number of local handles.
473 * After a handle scope has been created, all local handles will be
474 * allocated within that handle scope until either the handle scope is
475 * deleted or another handle scope is created. If there is already a
476 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000477 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 * new handles will again be allocated in the original handle scope.
479 *
480 * After the handle scope of a local handle has been deleted the
481 * garbage collector will no longer track the object stored in the
482 * handle and may deallocate it. The behavior of accessing a handle
483 * for which the handle scope has been deleted is undefined.
484 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000485class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000487 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000489 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490
491 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 * Closes the handle scope and returns the value as a handle in the
493 * previous scope, which is the new current scope after the call.
494 */
495 template <class T> Local<T> Close(Handle<T> value);
496
497 /**
498 * Counts the number of allocated handles.
499 */
500 static int NumberOfHandles();
501
502 /**
503 * Creates a new handle with the given value.
504 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000505 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000506 static internal::Object** CreateHandle(internal::Isolate* isolate,
507 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000508 // Faster version, uses HeapObject to obtain the current Isolate.
509 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510
511 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000512 // Make it hard to create heap-allocated or illegal handle scopes by
513 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 HandleScope(const HandleScope&);
515 void operator=(const HandleScope&);
516 void* operator new(size_t size);
517 void operator delete(void*, size_t);
518
ager@chromium.org3811b432009-10-28 14:53:37 +0000519 // This Data class is accessible internally as HandleScopeData through a
520 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000521 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000523 internal::Object** next;
524 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000525 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000526 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000528 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 }
530 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000531
lrn@chromium.org303ada72010-10-27 09:33:13 +0000532 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000534 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000535 internal::Object** prev_next_;
536 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000538 // Allow for the active closing of HandleScopes which allows to pass a handle
539 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000541 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 friend class ImplementationUtilities;
544};
545
546
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000547// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548
549
550/**
551 * The superclass of values and API object templates.
552 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000553class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 private:
555 Data();
556};
557
558
559/**
560 * Pre-compilation data that can be associated with a script. This
561 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000562 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000563 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000565class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 public:
567 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000568
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000569 /**
570 * Pre-compiles the specified script (context-independent).
571 *
572 * \param input Pointer to UTF-8 script source code.
573 * \param length Length of UTF-8 script source code.
574 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000577 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000578 * Pre-compiles the specified script (context-independent).
579 *
580 * NOTE: Pre-compilation using this method cannot happen on another thread
581 * without using Lockers.
582 *
583 * \param source Script source code.
584 */
585 static ScriptData* PreCompile(Handle<String> source);
586
587 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000588 * Load previous pre-compilation data.
589 *
590 * \param data Pointer to data returned by a call to Data() of a previous
591 * ScriptData. Ownership is not transferred.
592 * \param length Length of data.
593 */
594 static ScriptData* New(const char* data, int length);
595
596 /**
597 * Returns the length of Data().
598 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000600
601 /**
602 * Returns a serialized representation of this ScriptData that can later be
603 * passed to New(). NOTE: Serialized data is platform-dependent.
604 */
605 virtual const char* Data() = 0;
606
607 /**
608 * Returns true if the source code could not be parsed.
609 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000610 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611};
612
613
614/**
615 * The origin, within a file, of a script.
616 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000617class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000619 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000620 Handle<Value> resource_name,
621 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000622 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623 : resource_name_(resource_name),
624 resource_line_offset_(resource_line_offset),
625 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000626 V8_INLINE(Handle<Value> ResourceName() const);
627 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
628 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000630 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631 Handle<Integer> resource_line_offset_;
632 Handle<Integer> resource_column_offset_;
633};
634
635
636/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000637 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000639class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000642 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000643 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000644 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000645 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000646 * when New() returns
647 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
648 * using pre_data speeds compilation if it's done multiple times.
649 * Owned by caller, no references are kept when New() returns.
650 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000651 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000652 * available to compile event handlers.
653 * \return Compiled script object (context independent; when run it
654 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000656 static Local<Script> New(Handle<String> source,
657 ScriptOrigin* origin = NULL,
658 ScriptData* pre_data = NULL,
659 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660
mads.s.agercbaa0602008-08-14 13:41:48 +0000661 /**
662 * Compiles the specified script using the specified file name
663 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000664 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000665 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000666 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000667 * as the script's origin.
668 * \return Compiled script object (context independent; when run it
669 * will use the currently entered context).
670 */
671 static Local<Script> New(Handle<String> source,
672 Handle<Value> file_name);
673
674 /**
675 * Compiles the specified script (bound to current context).
676 *
677 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000678 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000679 * when Compile() returns
680 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
681 * using pre_data speeds compilation if it's done multiple times.
682 * Owned by caller, no references are kept when Compile() returns.
683 * \param script_data Arbitrary data associated with script. Using
684 * this has same effect as calling SetData(), but makes data available
685 * earlier (i.e. to compile event handlers).
686 * \return Compiled script object, bound to the context that was active
687 * when this function was called. When run it will always use this
688 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000689 */
690 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000691 ScriptOrigin* origin = NULL,
692 ScriptData* pre_data = NULL,
693 Handle<String> script_data = Handle<String>());
694
695 /**
696 * Compiles the specified script using the specified file name
697 * object (typically a string) as the script's origin.
698 *
699 * \param source Script source code.
700 * \param file_name File name to use as script's origin
701 * \param script_data Arbitrary data associated with script. Using
702 * this has same effect as calling SetData(), but makes data available
703 * earlier (i.e. to compile event handlers).
704 * \return Compiled script object, bound to the context that was active
705 * when this function was called. When run it will always use this
706 * context.
707 */
708 static Local<Script> Compile(Handle<String> source,
709 Handle<Value> file_name,
710 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000711
v8.team.kasperl727e9952008-09-02 14:56:44 +0000712 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000713 * Runs the script returning the resulting value. If the script is
714 * context independent (created using ::New) it will be run in the
715 * currently entered context. If it is context specific (created
716 * using ::Compile) it will be run in the context in which it was
717 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000718 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000720
721 /**
722 * Returns the script id value.
723 */
724 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000725
726 /**
727 * Associate an additional data object with the script. This is mainly used
728 * with the debugger as this data object is only available through the
729 * debugger API.
730 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000731 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732};
733
734
735/**
736 * An error message.
737 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000738class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000740 Local<String> Get() const;
741 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000743 /**
744 * Returns the resource name for the script from where the function causing
745 * the error originates.
746 */
ager@chromium.org32912102009-01-16 10:38:43 +0000747 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000749 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000750 * Returns the resource data for the script from where the function causing
751 * the error originates.
752 */
753 Handle<Value> GetScriptData() const;
754
755 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000756 * Exception stack trace. By default stack traces are not captured for
757 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
758 * to change this option.
759 */
760 Handle<StackTrace> GetStackTrace() const;
761
762 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000763 * Returns the number, 1-based, of the line where the error occurred.
764 */
ager@chromium.org32912102009-01-16 10:38:43 +0000765 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000767 /**
768 * Returns the index within the script of the first character where
769 * the error occurred.
770 */
ager@chromium.org32912102009-01-16 10:38:43 +0000771 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000772
773 /**
774 * Returns the index within the script of the last character where
775 * the error occurred.
776 */
ager@chromium.org32912102009-01-16 10:38:43 +0000777 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000778
779 /**
780 * Returns the index within the line of the first character where
781 * the error occurred.
782 */
ager@chromium.org32912102009-01-16 10:38:43 +0000783 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000784
785 /**
786 * Returns the index within the line of the last character where
787 * the error occurred.
788 */
ager@chromium.org32912102009-01-16 10:38:43 +0000789 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000790
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791 // TODO(1245381): Print to a string instead of on a FILE.
792 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000793
794 static const int kNoLineNumberInfo = 0;
795 static const int kNoColumnInfo = 0;
796};
797
798
799/**
800 * Representation of a JavaScript stack trace. The information collected is a
801 * snapshot of the execution stack and the information remains valid after
802 * execution continues.
803 */
804class V8EXPORT StackTrace {
805 public:
806 /**
807 * Flags that determine what information is placed captured for each
808 * StackFrame when grabbing the current stack trace.
809 */
810 enum StackTraceOptions {
811 kLineNumber = 1,
812 kColumnOffset = 1 << 1 | kLineNumber,
813 kScriptName = 1 << 2,
814 kFunctionName = 1 << 3,
815 kIsEval = 1 << 4,
816 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000817 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000818 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000819 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000820 };
821
822 /**
823 * Returns a StackFrame at a particular index.
824 */
825 Local<StackFrame> GetFrame(uint32_t index) const;
826
827 /**
828 * Returns the number of StackFrames.
829 */
830 int GetFrameCount() const;
831
832 /**
833 * Returns StackTrace as a v8::Array that contains StackFrame objects.
834 */
835 Local<Array> AsArray();
836
837 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000838 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000839 *
840 * \param frame_limit The maximum number of stack frames we want to capture.
841 * \param options Enumerates the set of things we will capture for each
842 * StackFrame.
843 */
844 static Local<StackTrace> CurrentStackTrace(
845 int frame_limit,
846 StackTraceOptions options = kOverview);
847};
848
849
850/**
851 * A single JavaScript stack frame.
852 */
853class V8EXPORT StackFrame {
854 public:
855 /**
856 * Returns the number, 1-based, of the line for the associate function call.
857 * This method will return Message::kNoLineNumberInfo if it is unable to
858 * retrieve the line number, or if kLineNumber was not passed as an option
859 * when capturing the StackTrace.
860 */
861 int GetLineNumber() const;
862
863 /**
864 * Returns the 1-based column offset on the line for the associated function
865 * call.
866 * This method will return Message::kNoColumnInfo if it is unable to retrieve
867 * the column number, or if kColumnOffset was not passed as an option when
868 * capturing the StackTrace.
869 */
870 int GetColumn() const;
871
872 /**
873 * Returns the name of the resource that contains the script for the
874 * function for this StackFrame.
875 */
876 Local<String> GetScriptName() const;
877
878 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000879 * Returns the name of the resource that contains the script for the
880 * function for this StackFrame or sourceURL value if the script name
881 * is undefined and its source ends with //@ sourceURL=... string.
882 */
883 Local<String> GetScriptNameOrSourceURL() const;
884
885 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000886 * Returns the name of the function associated with this stack frame.
887 */
888 Local<String> GetFunctionName() const;
889
890 /**
891 * Returns whether or not the associated function is compiled via a call to
892 * eval().
893 */
894 bool IsEval() const;
895
896 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000897 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000898 * constructor via "new".
899 */
900 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901};
902
903
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000904// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905
906
907/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000908 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000910class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 /**
913 * Returns true if this value is the undefined value. See ECMA-262
914 * 4.3.10.
915 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000916 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
918 /**
919 * Returns true if this value is the null value. See ECMA-262
920 * 4.3.11.
921 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000922 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923
924 /**
925 * Returns true if this value is true.
926 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000927 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
929 /**
930 * Returns true if this value is false.
931 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000932 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933
934 /**
935 * Returns true if this value is an instance of the String type.
936 * See ECMA-262 8.4.
937 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000938 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939
940 /**
941 * Returns true if this value is a function.
942 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000943 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944
945 /**
946 * Returns true if this value is an array.
947 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000948 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949
v8.team.kasperl727e9952008-09-02 14:56:44 +0000950 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 * Returns true if this value is an object.
952 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000953 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954
v8.team.kasperl727e9952008-09-02 14:56:44 +0000955 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 * Returns true if this value is boolean.
957 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000958 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000959
v8.team.kasperl727e9952008-09-02 14:56:44 +0000960 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 * Returns true if this value is a number.
962 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000963 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964
v8.team.kasperl727e9952008-09-02 14:56:44 +0000965 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 * Returns true if this value is external.
967 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000968 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969
v8.team.kasperl727e9952008-09-02 14:56:44 +0000970 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 * Returns true if this value is a 32-bit signed integer.
972 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000973 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000975 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000976 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000977 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000978 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000979
980 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000981 * Returns true if this value is a Date.
982 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000983 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000984
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000985 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000986 * Returns true if this value is a Boolean object.
987 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000988 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000989
990 /**
991 * Returns true if this value is a Number object.
992 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000993 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000994
995 /**
996 * Returns true if this value is a String object.
997 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000998 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000999
1000 /**
1001 * Returns true if this value is a NativeError.
1002 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001003 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001004
1005 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001006 * Returns true if this value is a RegExp.
1007 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001008 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001009
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001010 Local<Boolean> ToBoolean() const;
1011 Local<Number> ToNumber() const;
1012 Local<String> ToString() const;
1013 Local<String> ToDetailString() const;
1014 Local<Object> ToObject() const;
1015 Local<Integer> ToInteger() const;
1016 Local<Uint32> ToUint32() const;
1017 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018
1019 /**
1020 * Attempts to convert a string to an array index.
1021 * Returns an empty handle if the conversion fails.
1022 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001023 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001025 bool BooleanValue() const;
1026 double NumberValue() const;
1027 int64_t IntegerValue() const;
1028 uint32_t Uint32Value() const;
1029 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030
1031 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001032 bool Equals(Handle<Value> that) const;
1033 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001034
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001035 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001036 V8_INLINE(bool QuickIsUndefined() const);
1037 V8_INLINE(bool QuickIsNull() const);
1038 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001039 bool FullIsUndefined() const;
1040 bool FullIsNull() const;
1041 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042};
1043
1044
1045/**
1046 * The superclass of primitive values. See ECMA-262 4.3.2.
1047 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001048class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049
1050
1051/**
1052 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1053 * or false value.
1054 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001055class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001057 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001058 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059};
1060
1061
1062/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001063 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001065class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001067 enum Encoding {
1068 UNKNOWN_ENCODING = 0x1,
1069 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001070 ASCII_ENCODING = 0x4,
1071 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001072 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001073 /**
1074 * Returns the number of characters in this string.
1075 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001076 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077
v8.team.kasperl727e9952008-09-02 14:56:44 +00001078 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001079 * Returns the number of bytes in the UTF-8 encoded
1080 * representation of this string.
1081 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001082 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001083
1084 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001085 * A fast conservative check for non-ASCII characters. May
1086 * return true even for ASCII strings, but if it returns
1087 * false you can be sure that all characters are in the range
1088 * 0-127.
1089 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001090 bool MayContainNonAscii() const;
1091
1092 /**
1093 * Returns whether this string contains only one byte data.
1094 */
1095 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001096
1097 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001098 * Write the contents of the string to an external buffer.
1099 * If no arguments are given, expects the buffer to be large
1100 * enough to hold the entire string and NULL terminator. Copies
1101 * the contents of the string and the NULL terminator into the
1102 * buffer.
1103 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001104 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1105 * before the end of the buffer.
1106 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001107 * Copies up to length characters into the output buffer.
1108 * Only null-terminates if there is enough space in the buffer.
1109 *
1110 * \param buffer The buffer into which the string will be copied.
1111 * \param start The starting position within the string at which
1112 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001113 * \param length The number of characters to copy from the string. For
1114 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001115 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001116 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001117 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001118 * \return The number of characters copied to the buffer excluding the null
1119 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001120 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001121 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001122 enum WriteOptions {
1123 NO_OPTIONS = 0,
1124 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001125 NO_NULL_TERMINATION = 2,
1126 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001127 };
1128
lrn@chromium.org34e60782011-09-15 07:25:40 +00001129 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001130 int Write(uint16_t* buffer,
1131 int start = 0,
1132 int length = -1,
1133 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001134 // ASCII characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001135 int WriteAscii(char* buffer,
1136 int start = 0,
1137 int length = -1,
1138 int options = NO_OPTIONS) const;
1139 // One byte characters.
1140 int WriteOneByte(uint8_t* buffer,
1141 int start = 0,
1142 int length = -1,
1143 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001144 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001145 int WriteUtf8(char* buffer,
1146 int length = -1,
1147 int* nchars_ref = NULL,
1148 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149
v8.team.kasperl727e9952008-09-02 14:56:44 +00001150 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001151 * A zero length string.
1152 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001153 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001154 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001155
1156 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001157 * Returns true if the string is external
1158 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001159 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160
v8.team.kasperl727e9952008-09-02 14:56:44 +00001161 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001162 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001163 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001164 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001165
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001166 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001167 public:
1168 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001169
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001170 protected:
1171 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001172
1173 /**
1174 * Internally V8 will call this Dispose method when the external string
1175 * resource is no longer needed. The default implementation will use the
1176 * delete operator. This method can be overridden in subclasses to
1177 * control how allocated external string resources are disposed.
1178 */
1179 virtual void Dispose() { delete this; }
1180
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001181 private:
1182 // Disallow copying and assigning.
1183 ExternalStringResourceBase(const ExternalStringResourceBase&);
1184 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001185
1186 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001187 };
1188
v8.team.kasperl727e9952008-09-02 14:56:44 +00001189 /**
1190 * An ExternalStringResource is a wrapper around a two-byte string
1191 * buffer that resides outside V8's heap. Implement an
1192 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001193 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001194 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001195 class V8EXPORT ExternalStringResource
1196 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197 public:
1198 /**
1199 * Override the destructor to manage the life cycle of the underlying
1200 * buffer.
1201 */
1202 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001203
1204 /**
1205 * The string data from the underlying buffer.
1206 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001208
1209 /**
1210 * The length of the string. That is, the number of two-byte characters.
1211 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001212 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001213
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214 protected:
1215 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 };
1217
1218 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001219 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001220 * string buffer that resides outside V8's heap. Implement an
1221 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001222 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001223 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001224 * UTF-8, which would require special treatment internally in the
1225 * engine and, in the case of UTF-8, do not allow efficient indexing.
1226 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001227 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001229 class V8EXPORT ExternalAsciiStringResource
1230 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 public:
1232 /**
1233 * Override the destructor to manage the life cycle of the underlying
1234 * buffer.
1235 */
1236 virtual ~ExternalAsciiStringResource() {}
1237 /** The string data from the underlying buffer.*/
1238 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001239 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 virtual size_t length() const = 0;
1241 protected:
1242 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243 };
1244
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001245 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001248 * If the string is an external string, return the ExternalStringResourceBase
1249 * regardless of the encoding, otherwise return NULL. The encoding of the
1250 * string is returned in encoding_out.
1251 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001252 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1253 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001254
1255 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001256 * Get the ExternalStringResource for an external string. Returns
1257 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001259 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260
1261 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001262 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001263 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001265 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001267 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268
1269 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001270 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001271 * The second parameter 'length' gives the buffer length. If omitted,
1272 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001274 static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275
lrn@chromium.org34e60782011-09-15 07:25:40 +00001276 /** Allocates a new string from 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001277 static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278
1279 /** Creates a symbol. Returns one if it exists already.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001280 static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281
v8.team.kasperl727e9952008-09-02 14:56:44 +00001282 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001283 * Creates a new string by concatenating the left and the right strings
1284 * passed in as parameters.
1285 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001286 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001287
1288 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001289 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001290 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001291 * resource will be disposed by calling its Dispose method. The caller of
1292 * this function should not otherwise delete or modify the resource. Neither
1293 * should the underlying buffer be deallocated or modified except through the
1294 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001295 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001296 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001297
ager@chromium.org6f10e412009-02-13 10:11:16 +00001298 /**
1299 * Associate an external string resource with this string by transforming it
1300 * in place so that existing references to this string in the JavaScript heap
1301 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001302 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001303 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001304 * The string is not modified if the operation fails. See NewExternal for
1305 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001306 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001307 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308
v8.team.kasperl727e9952008-09-02 14:56:44 +00001309 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001310 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001311 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001312 * resource will be disposed by calling its Dispose method. The caller of
1313 * this function should not otherwise delete or modify the resource. Neither
1314 * should the underlying buffer be deallocated or modified except through the
1315 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001316 */
1317 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001318
ager@chromium.org6f10e412009-02-13 10:11:16 +00001319 /**
1320 * Associate an external string resource with this string by transforming it
1321 * in place so that existing references to this string in the JavaScript heap
1322 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001323 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001324 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001325 * The string is not modified if the operation fails. See NewExternal for
1326 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001327 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001328 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001330 /**
1331 * Returns true if this string can be made external.
1332 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001333 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001334
lrn@chromium.org34e60782011-09-15 07:25:40 +00001335 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001336 static Local<String> NewUndetectable(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337
lrn@chromium.org34e60782011-09-15 07:25:40 +00001338 /** Creates an undetectable string from the supplied 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001339 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340
1341 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001342 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001343 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001344 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001345 * then the length() method returns 0 and the * operator returns
1346 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001347 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001348 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001349 public:
1350 explicit Utf8Value(Handle<v8::Value> obj);
1351 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001352 char* operator*() { return str_; }
1353 const char* operator*() const { return str_; }
1354 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001355 private:
1356 char* str_;
1357 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001358
1359 // Disallow copying and assigning.
1360 Utf8Value(const Utf8Value&);
1361 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001362 };
1363
1364 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001365 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001367 * If conversion to a string fails (eg. due to an exception in the toString()
1368 * method of the object) then the length() method returns 0 and the * operator
1369 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001371 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372 public:
1373 explicit AsciiValue(Handle<v8::Value> obj);
1374 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001375 char* operator*() { return str_; }
1376 const char* operator*() const { return str_; }
1377 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 private:
1379 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001380 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001381
1382 // Disallow copying and assigning.
1383 AsciiValue(const AsciiValue&);
1384 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385 };
1386
1387 /**
1388 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001389 * If conversion to a string fails (eg. due to an exception in the toString()
1390 * method of the object) then the length() method returns 0 and the * operator
1391 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001393 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 public:
1395 explicit Value(Handle<v8::Value> obj);
1396 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001397 uint16_t* operator*() { return str_; }
1398 const uint16_t* operator*() const { return str_; }
1399 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400 private:
1401 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001402 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001403
1404 // Disallow copying and assigning.
1405 Value(const Value&);
1406 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001408
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001409 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001410 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1411 Encoding encoding) const;
1412 void VerifyExternalStringResource(ExternalStringResource* val) const;
1413 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414};
1415
1416
1417/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001418 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001420class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001422 double Value() const;
1423 static Local<Number> New(double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001424 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001426 Number();
1427 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428};
1429
1430
1431/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001432 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001434class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001436 static Local<Integer> New(int32_t value);
1437 static Local<Integer> NewFromUnsigned(uint32_t value);
1438 static Local<Integer> New(int32_t value, Isolate*);
1439 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1440 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001441 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001443 Integer();
1444 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001445};
1446
1447
1448/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001449 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001451class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001453 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001455 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456};
1457
1458
1459/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001460 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001462class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001464 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001466 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467};
1468
1469
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470enum PropertyAttribute {
1471 None = 0,
1472 ReadOnly = 1 << 0,
1473 DontEnum = 1 << 1,
1474 DontDelete = 1 << 2
1475};
1476
ager@chromium.org3811b432009-10-28 14:53:37 +00001477enum ExternalArrayType {
1478 kExternalByteArray = 1,
1479 kExternalUnsignedByteArray,
1480 kExternalShortArray,
1481 kExternalUnsignedShortArray,
1482 kExternalIntArray,
1483 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001484 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001485 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001486 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001487};
1488
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001489/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001490 * Accessor[Getter|Setter] are used as callback functions when
1491 * setting|getting a particular property. See Object and ObjectTemplate's
1492 * method SetAccessor.
1493 */
1494typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1495 const AccessorInfo& info);
1496
1497
1498typedef void (*AccessorSetter)(Local<String> property,
1499 Local<Value> value,
1500 const AccessorInfo& info);
1501
1502
1503/**
1504 * Access control specifications.
1505 *
1506 * Some accessors should be accessible across contexts. These
1507 * accessors have an explicit access control parameter which specifies
1508 * the kind of cross-context access that should be allowed.
1509 *
1510 * Additionally, for security, accessors can prohibit overwriting by
1511 * accessors defined in JavaScript. For objects that have such
1512 * accessors either locally or in their prototype chain it is not
1513 * possible to overwrite the accessor by using __defineGetter__ or
1514 * __defineSetter__ from JavaScript code.
1515 */
1516enum AccessControl {
1517 DEFAULT = 0,
1518 ALL_CAN_READ = 1,
1519 ALL_CAN_WRITE = 1 << 1,
1520 PROHIBITS_OVERWRITING = 1 << 2
1521};
1522
1523
1524/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001525 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001527class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001529 bool Set(Handle<Value> key,
1530 Handle<Value> value,
1531 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001532
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001533 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001534
ager@chromium.orge2902be2009-06-08 12:21:35 +00001535 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001536 // overriding accessors or read-only properties.
1537 //
1538 // Note that if the object has an interceptor the property will be set
1539 // locally, but since the interceptor takes precedence the local property
1540 // will only be returned if the interceptor doesn't return a value.
1541 //
1542 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001543 bool ForceSet(Handle<Value> key,
1544 Handle<Value> value,
1545 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001546
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001547 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001549 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001550
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001551 /**
1552 * Gets the property attributes of a property which can be None or
1553 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1554 * None when the property doesn't exist.
1555 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001556 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 // TODO(1245389): Replace the type-specific versions of these
1559 // functions with generic ones that accept a Handle<Value> key.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001560 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001561
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001562 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001563
1564 // Delete a property on this object bypassing interceptors and
1565 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001566 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001567
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001568 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001569
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001570 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001572 bool SetAccessor(Handle<String> name,
1573 AccessorGetter getter,
1574 AccessorSetter setter = 0,
1575 Handle<Value> data = Handle<Value>(),
1576 AccessControl settings = DEFAULT,
1577 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001578
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001580 * Returns an array containing the names of the enumerable properties
1581 * of this object, including properties from prototype objects. The
1582 * array returned by this method contains the same values as would
1583 * be enumerated by a for-in statement over this object.
1584 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001585 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001586
1587 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001588 * This function has the same functionality as GetPropertyNames but
1589 * the returned array doesn't contain the names of properties from
1590 * prototype objects.
1591 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001592 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001593
1594 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001595 * Get the prototype object. This does not skip objects marked to
1596 * be skipped by __proto__ and it does not consult the security
1597 * handler.
1598 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001599 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600
1601 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001602 * Set the prototype object. This does not skip objects marked to
1603 * be skipped by __proto__ and it does not consult the security
1604 * handler.
1605 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001606 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001607
1608 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001609 * Finds an instance of the given function template in the prototype
1610 * chain.
1611 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001612 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001613
1614 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615 * Call builtin Object.prototype.toString on this object.
1616 * This is different from Value::ToString() that may call
1617 * user-defined toString function. This one does not.
1618 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001619 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001621 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001622 * Returns the function invoked as a constructor for this object.
1623 * May be the null value.
1624 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001625 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001626
1627 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001628 * Returns the name of the function invoked as a constructor for this object.
1629 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001630 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001631
kasper.lund212ac232008-07-16 07:07:30 +00001632 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001633 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001634
1635 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001636 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001637
kasper.lund212ac232008-07-16 07:07:30 +00001638 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001639 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001640
1641 /**
1642 * Gets a 2-byte-aligned native pointer from an internal field. This field
1643 * must have been set by SetAlignedPointerInInternalField, everything else
1644 * leads to undefined behavior.
1645 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001646 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001647
1648 /**
1649 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1650 * a field, GetAlignedPointerFromInternalField must be used, everything else
1651 * leads to undefined behavior.
1652 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001653 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001654
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001656 bool HasOwnProperty(Handle<String> key);
1657 bool HasRealNamedProperty(Handle<String> key);
1658 bool HasRealIndexedProperty(uint32_t index);
1659 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660
1661 /**
1662 * If result.IsEmpty() no real property was located in the prototype chain.
1663 * This means interceptors in the prototype chain are not called.
1664 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001665 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001666
1667 /**
1668 * If result.IsEmpty() no real property was located on the object or
1669 * in the prototype chain.
1670 * This means interceptors in the prototype chain are not called.
1671 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001672 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673
1674 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001675 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676
kasper.lund212ac232008-07-16 07:07:30 +00001677 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001678 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001680 /**
1681 * Turns on access check on the object if the object is an instance of
1682 * a template that has access check callbacks. If an object has no
1683 * access check info, the object cannot be accessed by anyone.
1684 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001685 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001686
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001687 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001688 * Returns the identity hash for this object. The current implementation
1689 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001690 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001691 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001692 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001693 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001694 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001695
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001696 /**
1697 * Access hidden properties on JavaScript objects. These properties are
1698 * hidden from the executing JavaScript and only accessible through the V8
1699 * C++ API. Hidden properties introduced by V8 internally (for example the
1700 * identity hash) are prefixed with "v8::".
1701 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001702 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1703 Local<Value> GetHiddenValue(Handle<String> key);
1704 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001705
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001706 /**
1707 * Returns true if this is an instance of an api function (one
1708 * created from a function created from a function template) and has
1709 * been modified since it was created. Note that this method is
1710 * conservative and may return true for objects that haven't actually
1711 * been modified.
1712 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001713 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001714
1715 /**
1716 * Clone this object with a fast but shallow copy. Values will point
1717 * to the same values as the original object.
1718 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001719 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001721 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001722 * Returns the context in which the object was created.
1723 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001724 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001725
1726 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001727 * Set the backing store of the indexed properties to be managed by the
1728 * embedding layer. Access to the indexed properties will follow the rules
1729 * spelled out in CanvasPixelArray.
1730 * Note: The embedding program still owns the data and needs to ensure that
1731 * the backing store is preserved while V8 has a reference.
1732 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001733 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1734 bool HasIndexedPropertiesInPixelData();
1735 uint8_t* GetIndexedPropertiesPixelData();
1736 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001737
ager@chromium.org3811b432009-10-28 14:53:37 +00001738 /**
1739 * Set the backing store of the indexed properties to be managed by the
1740 * embedding layer. Access to the indexed properties will follow the rules
1741 * spelled out for the CanvasArray subtypes in the WebGL specification.
1742 * Note: The embedding program still owns the data and needs to ensure that
1743 * the backing store is preserved while V8 has a reference.
1744 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001745 void SetIndexedPropertiesToExternalArrayData(void* data,
1746 ExternalArrayType array_type,
1747 int number_of_elements);
1748 bool HasIndexedPropertiesInExternalArrayData();
1749 void* GetIndexedPropertiesExternalArrayData();
1750 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1751 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001752
lrn@chromium.org1c092762011-05-09 09:42:16 +00001753 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001754 * Checks whether a callback is set by the
1755 * ObjectTemplate::SetCallAsFunctionHandler method.
1756 * When an Object is callable this method returns true.
1757 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001758 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001759
1760 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001761 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001762 * ObjectTemplate::SetCallAsFunctionHandler method.
1763 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001764 Local<Value> CallAsFunction(Handle<Object> recv,
1765 int argc,
1766 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001767
1768 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001769 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001770 * ObjectTemplate::SetCallAsFunctionHandler method.
1771 * Note: This method behaves like the Function::NewInstance method.
1772 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001773 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001774
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001775 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001776 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001777
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001779 Object();
1780 static void CheckCast(Value* obj);
1781 Local<Value> SlowGetInternalField(int index);
1782 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783};
1784
1785
1786/**
1787 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1788 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001789class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001791 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792
ager@chromium.org3e875802009-06-29 08:26:34 +00001793 /**
1794 * Clones an element at index |index|. Returns an empty
1795 * handle if cloning fails (for any reason).
1796 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001797 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001798
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001799 /**
1800 * Creates a JavaScript array with the given length. If the length
1801 * is negative the returned array will have length 0.
1802 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001803 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001804
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001805 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001807 Array();
1808 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809};
1810
1811
1812/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001813 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001815class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001817 Local<Object> NewInstance() const;
1818 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1819 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1820 void SetName(Handle<String> name);
1821 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001822
1823 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001824 * Name inferred from variable or property assignment of this function.
1825 * Used to facilitate debugging and profiling of JavaScript code written
1826 * in an OO style, where many functions are anonymous but are assigned
1827 * to object properties.
1828 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001829 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001830
1831 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001832 * Returns zero based line number of function body and
1833 * kLineOffsetNotFound if no information available.
1834 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001835 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001836 /**
1837 * Returns zero based column number of function body and
1838 * kLineOffsetNotFound if no information available.
1839 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001840 int GetScriptColumnNumber() const;
1841 Handle<Value> GetScriptId() const;
1842 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001843 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001844 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001845
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001847 Function();
1848 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849};
1850
1851
1852/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001853 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1854 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001855class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001856 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001857 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001858
1859 /**
1860 * A specialization of Value::NumberValue that is more efficient
1861 * because we know the structure of this object.
1862 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001863 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001864
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001865 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001866
1867 /**
1868 * Notification that the embedder has changed the time zone,
1869 * daylight savings time, or other date / time configuration
1870 * parameters. V8 keeps a cache of various values used for
1871 * date / time computation. This notification will reset
1872 * those cached values for the current context so that date /
1873 * time configuration changes would be reflected in the Date
1874 * object.
1875 *
1876 * This API should not be called more than needed as it will
1877 * negatively impact the performance of date operations.
1878 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001879 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001880
1881 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001882 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001883};
1884
1885
1886/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001887 * A Number object (ECMA-262, 4.3.21).
1888 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001889class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001890 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001891 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001892
1893 /**
1894 * Returns the Number held by the object.
1895 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001896 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001897
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001898 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001899
1900 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001901 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001902};
1903
1904
1905/**
1906 * A Boolean object (ECMA-262, 4.3.15).
1907 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001908class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001909 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001910 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001911
1912 /**
1913 * Returns the Boolean held by the object.
1914 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001915 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001916
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001917 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001918
1919 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001920 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001921};
1922
1923
1924/**
1925 * A String object (ECMA-262, 4.3.18).
1926 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001927class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001928 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001929 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001930
1931 /**
1932 * Returns the String held by the object.
1933 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001934 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001935
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001936 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001937
1938 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001939 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001940};
1941
1942
1943/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001944 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1945 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001946class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001947 public:
1948 /**
1949 * Regular expression flag bits. They can be or'ed to enable a set
1950 * of flags.
1951 */
1952 enum Flags {
1953 kNone = 0,
1954 kGlobal = 1,
1955 kIgnoreCase = 2,
1956 kMultiline = 4
1957 };
1958
1959 /**
1960 * Creates a regular expression from the given pattern string and
1961 * the flags bit field. May throw a JavaScript exception as
1962 * described in ECMA-262, 15.10.4.1.
1963 *
1964 * For example,
1965 * RegExp::New(v8::String::New("foo"),
1966 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1967 * is equivalent to evaluating "/foo/gm".
1968 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001969 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001970
1971 /**
1972 * Returns the value of the source property: a string representing
1973 * the regular expression.
1974 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001975 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001976
1977 /**
1978 * Returns the flags bit field.
1979 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001980 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001981
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001982 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001983
1984 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001985 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001986};
1987
1988
1989/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001990 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
1991 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001993class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001995 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001996 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001997 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001999 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000};
2001
2002
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002003// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004
2005
2006/**
2007 * The superclass of object and function templates.
2008 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002009class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 public:
2011 /** Adds a property to each instance created by this template.*/
2012 void Set(Handle<String> name, Handle<Data> value,
2013 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002014 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015 private:
2016 Template();
2017
2018 friend class ObjectTemplate;
2019 friend class FunctionTemplate;
2020};
2021
2022
2023/**
2024 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002025 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 * including the receiver, the number and values of arguments, and
2027 * the holder of the function.
2028 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002029class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002031 V8_INLINE(int Length() const);
2032 V8_INLINE(Local<Value> operator[](int i) const);
2033 V8_INLINE(Local<Function> Callee() const);
2034 V8_INLINE(Local<Object> This() const);
2035 V8_INLINE(Local<Object> Holder() const);
2036 V8_INLINE(bool IsConstructCall() const);
2037 V8_INLINE(Local<Value> Data() const);
2038 V8_INLINE(Isolate* GetIsolate() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002039
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002041 static const int kIsolateIndex = 0;
2042 static const int kDataIndex = -1;
2043 static const int kCalleeIndex = -2;
2044 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002045
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 friend class ImplementationUtilities;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002047 V8_INLINE(Arguments(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002048 internal::Object** values,
2049 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002050 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002051 internal::Object** implicit_args_;
2052 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002054 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055};
2056
2057
2058/**
2059 * The information passed to an accessor callback about the context
2060 * of the property access.
2061 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002062class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002064 V8_INLINE(AccessorInfo(internal::Object** args))
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002065 : args_(args) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002066 V8_INLINE(Isolate* GetIsolate() const);
2067 V8_INLINE(Local<Value> Data() const);
2068 V8_INLINE(Local<Object> This() const);
2069 V8_INLINE(Local<Object> Holder() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002070
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002072 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073};
2074
2075
2076typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2077
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 * NamedProperty[Getter|Setter] are used as interceptors on object.
2080 * See ObjectTemplate::SetNamedPropertyHandler.
2081 */
2082typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2083 const AccessorInfo& info);
2084
2085
2086/**
2087 * Returns the value if the setter intercepts the request.
2088 * Otherwise, returns an empty handle.
2089 */
2090typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2091 Local<Value> value,
2092 const AccessorInfo& info);
2093
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094/**
2095 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002096 * The result is an integer encoding property attributes (like v8::None,
2097 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002099typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2100 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101
2102
2103/**
2104 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002105 * The return value is true if the property could be deleted and false
2106 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 */
2108typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2109 const AccessorInfo& info);
2110
2111/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002112 * Returns an array containing the names of the properties the named
2113 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114 */
2115typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2116
v8.team.kasperl727e9952008-09-02 14:56:44 +00002117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002119 * Returns the value of the property if the getter intercepts the
2120 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 */
2122typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2123 const AccessorInfo& info);
2124
2125
2126/**
2127 * Returns the value if the setter intercepts the request.
2128 * Otherwise, returns an empty handle.
2129 */
2130typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2131 Local<Value> value,
2132 const AccessorInfo& info);
2133
2134
2135/**
2136 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002137 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002139typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2140 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141
2142/**
2143 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002144 * The return value is true if the property could be deleted and false
2145 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 */
2147typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2148 const AccessorInfo& info);
2149
v8.team.kasperl727e9952008-09-02 14:56:44 +00002150/**
2151 * Returns an array containing the indices of the properties the
2152 * indexed property getter intercepts.
2153 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2155
2156
2157/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002158 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159 */
2160enum AccessType {
2161 ACCESS_GET,
2162 ACCESS_SET,
2163 ACCESS_HAS,
2164 ACCESS_DELETE,
2165 ACCESS_KEYS
2166};
2167
v8.team.kasperl727e9952008-09-02 14:56:44 +00002168
2169/**
2170 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002171 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002173typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 Local<Value> key,
2175 AccessType type,
2176 Local<Value> data);
2177
v8.team.kasperl727e9952008-09-02 14:56:44 +00002178
2179/**
2180 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002181 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002182 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002183typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 uint32_t index,
2185 AccessType type,
2186 Local<Value> data);
2187
2188
2189/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002190 * A FunctionTemplate is used to create functions at runtime. There
2191 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002192 * context. The lifetime of the created function is equal to the
2193 * lifetime of the context. So in case the embedder needs to create
2194 * temporary functions that can be collected using Scripts is
2195 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 *
2197 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002198 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002200 * A FunctionTemplate has a corresponding instance template which is
2201 * used to create object instances when the function is used as a
2202 * constructor. Properties added to the instance template are added to
2203 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 *
2205 * A FunctionTemplate can have a prototype template. The prototype template
2206 * is used to create the prototype object of the function.
2207 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002208 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002210 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2212 * t->Set("func_property", v8::Number::New(1));
2213 *
2214 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2215 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2216 * proto_t->Set("proto_const", v8::Number::New(2));
2217 *
2218 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2219 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2220 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2221 * instance_t->Set("instance_property", Number::New(3));
2222 *
2223 * v8::Local<v8::Function> function = t->GetFunction();
2224 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002225 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 *
2227 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002228 * and "instance" for the instance object created above. The function
2229 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002231 * \code
2232 * func_property in function == true;
2233 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002235 * function.prototype.proto_method() invokes 'InvokeCallback'
2236 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002238 * instance instanceof function == true;
2239 * instance.instance_accessor calls 'InstanceAccessorCallback'
2240 * instance.instance_property == 3;
2241 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002243 * A FunctionTemplate can inherit from another one by calling the
2244 * FunctionTemplate::Inherit method. The following graph illustrates
2245 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002247 * \code
2248 * FunctionTemplate Parent -> Parent() . prototype -> { }
2249 * ^ ^
2250 * | Inherit(Parent) | .__proto__
2251 * | |
2252 * FunctionTemplate Child -> Child() . prototype -> { }
2253 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002255 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2256 * object of the Child() function has __proto__ pointing to the
2257 * Parent() function's prototype object. An instance of the Child
2258 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002260 * Let Parent be the FunctionTemplate initialized in the previous
2261 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002263 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 * Local<FunctionTemplate> parent = t;
2265 * Local<FunctionTemplate> child = FunctionTemplate::New();
2266 * child->Inherit(parent);
2267 *
2268 * Local<Function> child_function = child->GetFunction();
2269 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002270 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002272 * The Child function and Child instance will have the following
2273 * properties:
2274 *
2275 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002277 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002278 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002279 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002280 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002281class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282 public:
2283 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002284 static Local<FunctionTemplate> New(
2285 InvocationCallback callback = 0,
2286 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002287 Handle<Signature> signature = Handle<Signature>(),
2288 int length = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 /** Returns the unique function instance in the current execution context.*/
2290 Local<Function> GetFunction();
2291
v8.team.kasperl727e9952008-09-02 14:56:44 +00002292 /**
2293 * Set the call-handler callback for a FunctionTemplate. This
2294 * callback is called whenever the function created from this
2295 * FunctionTemplate is called.
2296 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297 void SetCallHandler(InvocationCallback callback,
2298 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002300 /** Set the predefined length property for the FunctionTemplate. */
2301 void SetLength(int length);
2302
v8.team.kasperl727e9952008-09-02 14:56:44 +00002303 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304 Local<ObjectTemplate> InstanceTemplate();
2305
2306 /** Causes the function template to inherit from a parent function template.*/
2307 void Inherit(Handle<FunctionTemplate> parent);
2308
2309 /**
2310 * A PrototypeTemplate is the template used to create the prototype object
2311 * of the function created by this template.
2312 */
2313 Local<ObjectTemplate> PrototypeTemplate();
2314
v8.team.kasperl727e9952008-09-02 14:56:44 +00002315 /**
2316 * Set the class name of the FunctionTemplate. This is used for
2317 * printing objects created with the function created from the
2318 * FunctionTemplate as its constructor.
2319 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320 void SetClassName(Handle<String> name);
2321
2322 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002323 * Determines whether the __proto__ accessor ignores instances of
2324 * the function template. If instances of the function template are
2325 * ignored, __proto__ skips all instances and instead returns the
2326 * next object in the prototype chain.
2327 *
2328 * Call with a value of true to make the __proto__ accessor ignore
2329 * instances of the function template. Call with a value of false
2330 * to make the __proto__ accessor not ignore instances of the
2331 * function template. By default, instances of a function template
2332 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002333 */
2334 void SetHiddenPrototype(bool value);
2335
2336 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002337 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2338 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002339 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002340 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002341
2342 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002343 * Returns true if the given object is an instance of this function
2344 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345 */
2346 bool HasInstance(Handle<Value> object);
2347
2348 private:
2349 FunctionTemplate();
2350 void AddInstancePropertyAccessor(Handle<String> name,
2351 AccessorGetter getter,
2352 AccessorSetter setter,
2353 Handle<Value> data,
2354 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002355 PropertyAttribute attributes,
2356 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2358 NamedPropertySetter setter,
2359 NamedPropertyQuery query,
2360 NamedPropertyDeleter remover,
2361 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002362 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002363 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2364 IndexedPropertySetter setter,
2365 IndexedPropertyQuery query,
2366 IndexedPropertyDeleter remover,
2367 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002368 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2370 Handle<Value> data);
2371
2372 friend class Context;
2373 friend class ObjectTemplate;
2374};
2375
2376
2377/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002378 * An ObjectTemplate is used to create objects at runtime.
2379 *
2380 * Properties added to an ObjectTemplate are added to each object
2381 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002383class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002385 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388 /** Creates a new instance of this template.*/
2389 Local<Object> NewInstance();
2390
2391 /**
2392 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002393 *
2394 * Whenever the property with the given name is accessed on objects
2395 * created from this ObjectTemplate the getter and setter callbacks
2396 * are called instead of getting and setting the property directly
2397 * on the JavaScript object.
2398 *
2399 * \param name The name of the property for which an accessor is added.
2400 * \param getter The callback to invoke when getting the property.
2401 * \param setter The callback to invoke when setting the property.
2402 * \param data A piece of data that will be passed to the getter and setter
2403 * callbacks whenever they are invoked.
2404 * \param settings Access control settings for the accessor. This is a bit
2405 * field consisting of one of more of
2406 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2407 * The default is to not allow cross-context access.
2408 * ALL_CAN_READ means that all cross-context reads are allowed.
2409 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2410 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2411 * cross-context access.
2412 * \param attribute The attributes of the property for which an accessor
2413 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002414 * \param signature The signature describes valid receivers for the accessor
2415 * and is used to perform implicit instance checks against them. If the
2416 * receiver is incompatible (i.e. is not an instance of the constructor as
2417 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2418 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419 */
2420 void SetAccessor(Handle<String> name,
2421 AccessorGetter getter,
2422 AccessorSetter setter = 0,
2423 Handle<Value> data = Handle<Value>(),
2424 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002425 PropertyAttribute attribute = None,
2426 Handle<AccessorSignature> signature =
2427 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428
2429 /**
2430 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002431 *
2432 * Whenever a named property is accessed on objects created from
2433 * this object template, the provided callback is invoked instead of
2434 * accessing the property directly on the JavaScript object.
2435 *
2436 * \param getter The callback to invoke when getting a property.
2437 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002438 * \param query The callback to invoke to check if a property is present,
2439 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002440 * \param deleter The callback to invoke when deleting a property.
2441 * \param enumerator The callback to invoke to enumerate all the named
2442 * properties of an object.
2443 * \param data A piece of data that will be passed to the callbacks
2444 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002445 */
2446 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2447 NamedPropertySetter setter = 0,
2448 NamedPropertyQuery query = 0,
2449 NamedPropertyDeleter deleter = 0,
2450 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002451 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452
2453 /**
2454 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002455 *
2456 * Whenever an indexed property is accessed on objects created from
2457 * this object template, the provided callback is invoked instead of
2458 * accessing the property directly on the JavaScript object.
2459 *
2460 * \param getter The callback to invoke when getting a property.
2461 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002462 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002463 * \param deleter The callback to invoke when deleting a property.
2464 * \param enumerator The callback to invoke to enumerate all the indexed
2465 * properties of an object.
2466 * \param data A piece of data that will be passed to the callbacks
2467 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 */
2469 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2470 IndexedPropertySetter setter = 0,
2471 IndexedPropertyQuery query = 0,
2472 IndexedPropertyDeleter deleter = 0,
2473 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002474 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002475
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476 /**
2477 * Sets the callback to be used when calling instances created from
2478 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002479 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 * function.
2481 */
2482 void SetCallAsFunctionHandler(InvocationCallback callback,
2483 Handle<Value> data = Handle<Value>());
2484
v8.team.kasperl727e9952008-09-02 14:56:44 +00002485 /**
2486 * Mark object instances of the template as undetectable.
2487 *
2488 * In many ways, undetectable objects behave as though they are not
2489 * there. They behave like 'undefined' in conditionals and when
2490 * printed. However, properties can be accessed and called as on
2491 * normal objects.
2492 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002493 void MarkAsUndetectable();
2494
v8.team.kasperl727e9952008-09-02 14:56:44 +00002495 /**
2496 * Sets access check callbacks on the object template.
2497 *
2498 * When accessing properties on instances of this object template,
2499 * the access check callback will be called to determine whether or
2500 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002501 * The last parameter specifies whether access checks are turned
2502 * on by default on instances. If access checks are off by default,
2503 * they can be turned on on individual instances by calling
2504 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002505 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2507 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002508 Handle<Value> data = Handle<Value>(),
2509 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510
kasper.lund212ac232008-07-16 07:07:30 +00002511 /**
2512 * Gets the number of internal fields for objects generated from
2513 * this template.
2514 */
2515 int InternalFieldCount();
2516
2517 /**
2518 * Sets the number of internal fields for objects generated from
2519 * this template.
2520 */
2521 void SetInternalFieldCount(int value);
2522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523 private:
2524 ObjectTemplate();
2525 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2526 friend class FunctionTemplate;
2527};
2528
2529
2530/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002531 * A Signature specifies which receivers and arguments are valid
2532 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002534class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 public:
2536 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2537 Handle<FunctionTemplate>(),
2538 int argc = 0,
2539 Handle<FunctionTemplate> argv[] = 0);
2540 private:
2541 Signature();
2542};
2543
2544
2545/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002546 * An AccessorSignature specifies which receivers are valid parameters
2547 * to an accessor callback.
2548 */
2549class V8EXPORT AccessorSignature : public Data {
2550 public:
2551 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2552 Handle<FunctionTemplate>());
2553 private:
2554 AccessorSignature();
2555};
2556
2557
2558/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002559 * A utility for determining the type of objects based on the template
2560 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002562class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 public:
2564 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2565 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2566 int match(Handle<Value> value);
2567 private:
2568 TypeSwitch();
2569};
2570
2571
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002572// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002574class V8EXPORT ExternalAsciiStringResourceImpl
2575 : public String::ExternalAsciiStringResource {
2576 public:
2577 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2578 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2579 : data_(data), length_(length) {}
2580 const char* data() const { return data_; }
2581 size_t length() const { return length_; }
2582
2583 private:
2584 const char* data_;
2585 size_t length_;
2586};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587
2588/**
2589 * Ignore
2590 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002591class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002593 // Note that the strings passed into this constructor must live as long
2594 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002596 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002598 const char** deps = 0,
2599 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600 virtual ~Extension() { }
2601 virtual v8::Handle<v8::FunctionTemplate>
2602 GetNativeFunction(v8::Handle<v8::String> name) {
2603 return v8::Handle<v8::FunctionTemplate>();
2604 }
2605
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002606 const char* name() const { return name_; }
2607 size_t source_length() const { return source_length_; }
2608 const String::ExternalAsciiStringResource* source() const {
2609 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610 int dependency_count() { return dep_count_; }
2611 const char** dependencies() { return deps_; }
2612 void set_auto_enable(bool value) { auto_enable_ = value; }
2613 bool auto_enable() { return auto_enable_; }
2614
2615 private:
2616 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002617 size_t source_length_; // expected to initialize before source_
2618 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 int dep_count_;
2620 const char** deps_;
2621 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002622
2623 // Disallow copying and assigning.
2624 Extension(const Extension&);
2625 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626};
2627
2628
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002629void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630
2631
2632/**
2633 * Ignore
2634 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002635class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002637 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 RegisterExtension(extension);
2639 }
2640};
2641
2642
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002643// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644
2645
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002646Handle<Primitive> V8EXPORT Undefined();
2647Handle<Primitive> V8EXPORT Null();
2648Handle<Boolean> V8EXPORT True();
2649Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002651V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
2652V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
2653V8_INLINE(Handle<Boolean> True(Isolate* isolate));
2654V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002655
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656
2657/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002658 * A set of constraints that specifies the limits of the runtime's memory use.
2659 * You must set the heap size before initializing the VM - the size cannot be
2660 * adjusted after the VM is initialized.
2661 *
2662 * If you are using threads then you should hold the V8::Locker lock while
2663 * setting the stack limit and you must set a non-default stack limit separately
2664 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002666class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667 public:
2668 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002669 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002671 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002673 int max_executable_size() { return max_executable_size_; }
2674 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002675 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002676 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2678 private:
2679 int max_young_space_size_;
2680 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002681 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 uint32_t* stack_limit_;
2683};
2684
2685
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002686bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687
2688
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002689// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690
2691
2692typedef void (*FatalErrorCallback)(const char* location, const char* message);
2693
2694
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002695typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696
2697
2698/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002699 * Schedules an exception to be thrown when returning to JavaScript. When an
2700 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002702 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002704Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705
2706/**
2707 * Create new error objects by calling the corresponding error object
2708 * constructor with the message.
2709 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002710class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711 public:
2712 static Local<Value> RangeError(Handle<String> message);
2713 static Local<Value> ReferenceError(Handle<String> message);
2714 static Local<Value> SyntaxError(Handle<String> message);
2715 static Local<Value> TypeError(Handle<String> message);
2716 static Local<Value> Error(Handle<String> message);
2717};
2718
2719
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002720// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002722typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002724typedef void* (*CreateHistogramCallback)(const char* name,
2725 int min,
2726 int max,
2727 size_t buckets);
2728
2729typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2730
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002731// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002732 enum ObjectSpace {
2733 kObjectSpaceNewSpace = 1 << 0,
2734 kObjectSpaceOldPointerSpace = 1 << 1,
2735 kObjectSpaceOldDataSpace = 1 << 2,
2736 kObjectSpaceCodeSpace = 1 << 3,
2737 kObjectSpaceMapSpace = 1 << 4,
2738 kObjectSpaceLoSpace = 1 << 5,
2739
2740 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2741 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2742 kObjectSpaceLoSpace
2743 };
2744
2745 enum AllocationAction {
2746 kAllocationActionAllocate = 1 << 0,
2747 kAllocationActionFree = 1 << 1,
2748 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2749 };
2750
2751typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2752 AllocationAction action,
2753 int size);
2754
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002755// --- Leave Script Callback ---
2756typedef void (*CallCompletedCallback)();
2757
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002758// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002759typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2760 AccessType type,
2761 Local<Value> data);
2762
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002763// --- AllowCodeGenerationFromStrings callbacks ---
2764
2765/**
2766 * Callback to check if code generation from strings is allowed. See
2767 * Context::AllowCodeGenerationFromStrings.
2768 */
2769typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2770
2771// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002772
2773/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002774 * Applications can register callback functions which will be called
2775 * before and after a garbage collection. Allocations are not
2776 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002777 * objects (set or delete properties for example) since it is possible
2778 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002779 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002780enum GCType {
2781 kGCTypeScavenge = 1 << 0,
2782 kGCTypeMarkSweepCompact = 1 << 1,
2783 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2784};
2785
2786enum GCCallbackFlags {
2787 kNoGCCallbackFlags = 0,
2788 kGCCallbackFlagCompacted = 1 << 0
2789};
2790
2791typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2792typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2793
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002794typedef void (*GCCallback)();
2795
2796
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002798 * Collection of V8 heap information.
2799 *
2800 * Instances of this class can be passed to v8::V8::HeapStatistics to
2801 * get heap statistics from V8.
2802 */
2803class V8EXPORT HeapStatistics {
2804 public:
2805 HeapStatistics();
2806 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002807 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00002808 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002809 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002810 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002811
2812 private:
2813 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002814 void set_total_heap_size_executable(size_t size) {
2815 total_heap_size_executable_ = size;
2816 }
danno@chromium.org72204d52012-10-31 10:02:10 +00002817 void set_total_physical_size(size_t size) {
2818 total_physical_size_ = size;
2819 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002820 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002821 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002822
2823 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002824 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00002825 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002826 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002827 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002828
2829 friend class V8;
2830};
2831
2832
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002833class RetainedObjectInfo;
2834
ager@chromium.org3811b432009-10-28 14:53:37 +00002835/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002836 * Isolate represents an isolated instance of the V8 engine. V8
2837 * isolates have completely separate states. Objects from one isolate
2838 * must not be used in other isolates. When V8 is initialized a
2839 * default isolate is implicitly created and entered. The embedder
2840 * can create additional isolates and use them in parallel in multiple
2841 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002842 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002843 */
2844class V8EXPORT Isolate {
2845 public:
2846 /**
2847 * Stack-allocated class which sets the isolate for all operations
2848 * executed within a local scope.
2849 */
2850 class V8EXPORT Scope {
2851 public:
2852 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2853 isolate->Enter();
2854 }
2855
2856 ~Scope() { isolate_->Exit(); }
2857
2858 private:
2859 Isolate* const isolate_;
2860
2861 // Prevent copying of Scope objects.
2862 Scope(const Scope&);
2863 Scope& operator=(const Scope&);
2864 };
2865
2866 /**
2867 * Creates a new isolate. Does not change the currently entered
2868 * isolate.
2869 *
2870 * When an isolate is no longer used its resources should be freed
2871 * by calling Dispose(). Using the delete operator is not allowed.
2872 */
2873 static Isolate* New();
2874
2875 /**
2876 * Returns the entered isolate for the current thread or NULL in
2877 * case there is no current isolate.
2878 */
2879 static Isolate* GetCurrent();
2880
2881 /**
2882 * Methods below this point require holding a lock (using Locker) in
2883 * a multi-threaded environment.
2884 */
2885
2886 /**
2887 * Sets this isolate as the entered one for the current thread.
2888 * Saves the previously entered one (if any), so that it can be
2889 * restored when exiting. Re-entering an isolate is allowed.
2890 */
2891 void Enter();
2892
2893 /**
2894 * Exits this isolate by restoring the previously entered one in the
2895 * current thread. The isolate may still stay the same, if it was
2896 * entered more than once.
2897 *
2898 * Requires: this == Isolate::GetCurrent().
2899 */
2900 void Exit();
2901
2902 /**
2903 * Disposes the isolate. The isolate must not be entered by any
2904 * thread to be disposable.
2905 */
2906 void Dispose();
2907
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002908 /**
2909 * Associate embedder-specific data with the isolate
2910 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002911 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002912
2913 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002914 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002915 * Returns NULL if SetData has never been called.
2916 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002917 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002918
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002919 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002920 Isolate();
2921 Isolate(const Isolate&);
2922 ~Isolate();
2923 Isolate& operator=(const Isolate&);
2924 void* operator new(size_t size);
2925 void operator delete(void*, size_t);
2926};
2927
2928
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002929class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002930 public:
2931 enum CompressionAlgorithm {
2932 kUncompressed,
2933 kBZip2
2934 };
2935
2936 const char* data;
2937 int compressed_size;
2938 int raw_size;
2939};
2940
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002941
2942/**
2943 * A helper class for driving V8 startup data decompression. It is based on
2944 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2945 * for an embedder to use this class, instead, API functions can be used
2946 * directly.
2947 *
2948 * For an example of the class usage, see the "shell.cc" sample application.
2949 */
2950class V8EXPORT StartupDataDecompressor { // NOLINT
2951 public:
2952 StartupDataDecompressor();
2953 virtual ~StartupDataDecompressor();
2954 int Decompress();
2955
2956 protected:
2957 virtual int DecompressData(char* raw_data,
2958 int* raw_data_size,
2959 const char* compressed_data,
2960 int compressed_data_size) = 0;
2961
2962 private:
2963 char** raw_data;
2964};
2965
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002966
2967/**
2968 * EntropySource is used as a callback function when v8 needs a source
2969 * of entropy.
2970 */
2971typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2972
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002973
2974/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002975 * ReturnAddressLocationResolver is used as a callback function when v8 is
2976 * resolving the location of a return address on the stack. Profilers that
2977 * change the return address on the stack can use this to resolve the stack
2978 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002979 *
2980 * \param return_addr_location points to a location on stack where a machine
2981 * return address resides.
2982 * \returns either return_addr_location, or else a pointer to the profiler's
2983 * copy of the original return address.
2984 *
2985 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00002986 */
2987typedef uintptr_t (*ReturnAddressLocationResolver)(
2988 uintptr_t return_addr_location);
2989
2990
2991/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002992 * FunctionEntryHook is the type of the profile entry hook called at entry to
2993 * any generated function when function-level profiling is enabled.
2994 *
2995 * \param function the address of the function that's being entered.
2996 * \param return_addr_location points to a location on stack where the machine
2997 * return address resides. This can be used to identify the caller of
2998 * \p function, and/or modified to divert execution when \p function exits.
2999 *
3000 * \note the entry hook must not cause garbage collection.
3001 */
3002typedef void (*FunctionEntryHook)(uintptr_t function,
3003 uintptr_t return_addr_location);
3004
3005
3006/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003007 * A JIT code event is issued each time code is added, moved or removed.
3008 *
3009 * \note removal events are not currently issued.
3010 */
3011struct JitCodeEvent {
3012 enum EventType {
3013 CODE_ADDED,
3014 CODE_MOVED,
3015 CODE_REMOVED
3016 };
3017
3018 // Type of event.
3019 EventType type;
3020 // Start of the instructions.
3021 void* code_start;
3022 // Size of the instructions.
3023 size_t code_len;
3024
3025 union {
3026 // Only valid for CODE_ADDED.
3027 struct {
3028 // Name of the object associated with the code, note that the string is
3029 // not zero-terminated.
3030 const char* str;
3031 // Number of chars in str.
3032 size_t len;
3033 } name;
3034 // New location of instructions. Only valid for CODE_MOVED.
3035 void* new_code_start;
3036 };
3037};
3038
3039/**
3040 * Option flags passed to the SetJitCodeEventHandler function.
3041 */
3042enum JitCodeEventOptions {
3043 kJitCodeEventDefault = 0,
3044 // Generate callbacks for already existent code.
3045 kJitCodeEventEnumExisting = 1
3046};
3047
3048
3049/**
3050 * Callback function passed to SetJitCodeEventHandler.
3051 *
3052 * \param event code add, move or removal event.
3053 */
3054typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3055
3056
3057/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003058 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003059 */
3060class V8EXPORT ExternalResourceVisitor { // NOLINT
3061 public:
3062 virtual ~ExternalResourceVisitor() {}
3063 virtual void VisitExternalString(Handle<String> string) {}
3064};
3065
3066
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003067/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003068 * Interface for iterating through all the persistent handles in the heap.
3069 */
3070class V8EXPORT PersistentHandleVisitor { // NOLINT
3071 public:
3072 virtual ~PersistentHandleVisitor() {}
3073 virtual void VisitPersistentHandle(Persistent<Value> value,
3074 uint16_t class_id) {}
3075};
3076
3077
3078/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003079 * Container class for static utility functions.
3080 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003081class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003082 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003083 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084 static void SetFatalErrorHandler(FatalErrorCallback that);
3085
v8.team.kasperl727e9952008-09-02 14:56:44 +00003086 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003087 * Set the callback to invoke to check if code generation from
3088 * strings should be allowed.
3089 */
3090 static void SetAllowCodeGenerationFromStringsCallback(
3091 AllowCodeGenerationFromStringsCallback that);
3092
3093 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003094 * Ignore out-of-memory exceptions.
3095 *
3096 * V8 running out of memory is treated as a fatal error by default.
3097 * This means that the fatal error handler is called and that V8 is
3098 * terminated.
3099 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003100 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003101 * out-of-memory situation as a fatal error. This way, the contexts
3102 * that did not cause the out of memory problem might be able to
3103 * continue execution.
3104 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003105 static void IgnoreOutOfMemoryException();
3106
v8.team.kasperl727e9952008-09-02 14:56:44 +00003107 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003108 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003109 * fatal errors such as out-of-memory situations.
3110 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003111 static bool IsDead();
3112
3113 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003114 * The following 4 functions are to be used when V8 is built with
3115 * the 'compress_startup_data' flag enabled. In this case, the
3116 * embedder must decompress startup data prior to initializing V8.
3117 *
3118 * This is how interaction with V8 should look like:
3119 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3120 * v8::StartupData* compressed_data =
3121 * new v8::StartupData[compressed_data_count];
3122 * v8::V8::GetCompressedStartupData(compressed_data);
3123 * ... decompress data (compressed_data can be updated in-place) ...
3124 * v8::V8::SetDecompressedStartupData(compressed_data);
3125 * ... now V8 can be initialized
3126 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003127 *
3128 * A helper class StartupDataDecompressor is provided. It implements
3129 * the protocol of the interaction described above, and can be used in
3130 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003131 */
3132 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3133 static int GetCompressedStartupDataCount();
3134 static void GetCompressedStartupData(StartupData* compressed_data);
3135 static void SetDecompressedStartupData(StartupData* decompressed_data);
3136
3137 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003138 * Adds a message listener.
3139 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003140 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003141 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142 */
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003143 static bool AddMessageListener(MessageCallback that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144
3145 /**
3146 * Remove all message listeners from the specified callback function.
3147 */
3148 static void RemoveMessageListeners(MessageCallback that);
3149
3150 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003151 * Tells V8 to capture current stack trace when uncaught exception occurs
3152 * and report it to the message listeners. The option is off by default.
3153 */
3154 static void SetCaptureStackTraceForUncaughtExceptions(
3155 bool capture,
3156 int frame_limit = 10,
3157 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3158
3159 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003160 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003161 */
3162 static void SetFlagsFromString(const char* str, int length);
3163
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003164 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003165 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003166 */
3167 static void SetFlagsFromCommandLine(int* argc,
3168 char** argv,
3169 bool remove_flags);
3170
kasper.lund7276f142008-07-30 08:49:36 +00003171 /** Get the version string. */
3172 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003173
3174 /**
3175 * Enables the host application to provide a mechanism for recording
3176 * statistics counters.
3177 */
3178 static void SetCounterFunction(CounterLookupCallback);
3179
3180 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003181 * Enables the host application to provide a mechanism for recording
3182 * histograms. The CreateHistogram function returns a
3183 * histogram which will later be passed to the AddHistogramSample
3184 * function.
3185 */
3186 static void SetCreateHistogramFunction(CreateHistogramCallback);
3187 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3188
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003189 /** Callback function for reporting failed access checks.*/
3190 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3191
3192 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003193 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003194 * garbage collection. Allocations are not allowed in the
3195 * callback function, you therefore cannot manipulate objects (set
3196 * or delete properties for example) since it is possible such
3197 * operations will result in the allocation of objects. It is possible
3198 * to specify the GCType filter for your callback. But it is not possible to
3199 * register the same callback function two times with different
3200 * GCType filters.
3201 */
3202 static void AddGCPrologueCallback(
3203 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3204
3205 /**
3206 * This function removes callback which was installed by
3207 * AddGCPrologueCallback function.
3208 */
3209 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3210
3211 /**
3212 * The function is deprecated. Please use AddGCPrologueCallback instead.
3213 * Enables the host application to receive a notification before a
3214 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003215 * callback function, you therefore cannot manipulate objects (set
3216 * or delete properties for example) since it is possible such
3217 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003219 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220
3221 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003222 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003223 * garbage collection. Allocations are not allowed in the
3224 * callback function, you therefore cannot manipulate objects (set
3225 * or delete properties for example) since it is possible such
3226 * operations will result in the allocation of objects. It is possible
3227 * to specify the GCType filter for your callback. But it is not possible to
3228 * register the same callback function two times with different
3229 * GCType filters.
3230 */
3231 static void AddGCEpilogueCallback(
3232 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3233
3234 /**
3235 * This function removes callback which was installed by
3236 * AddGCEpilogueCallback function.
3237 */
3238 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3239
3240 /**
3241 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3242 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003243 * major garbage collection. Allocations are not allowed in the
3244 * callback function, you therefore cannot manipulate objects (set
3245 * or delete properties for example) since it is possible such
3246 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003247 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003248 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003249
3250 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003251 * Enables the host application to provide a mechanism to be notified
3252 * and perform custom logging when V8 Allocates Executable Memory.
3253 */
3254 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3255 ObjectSpace space,
3256 AllocationAction action);
3257
3258 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003259 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003260 */
3261 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3262
3263 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003264 * Adds a callback to notify the host application when a script finished
3265 * running. If a script re-enters the runtime during executing, the
3266 * CallCompletedCallback is only invoked when the outer-most script
3267 * execution ends. Executing scripts inside the callback do not trigger
3268 * further callbacks.
3269 */
3270 static void AddCallCompletedCallback(CallCompletedCallback callback);
3271
3272 /**
3273 * Removes callback that was installed by AddCallCompletedCallback.
3274 */
3275 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3276
3277 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003278 * Allows the host application to group objects together. If one
3279 * object in the group is alive, all objects in the group are alive.
3280 * After each garbage collection, object groups are removed. It is
3281 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003282 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003283 * wrapper objects. Object groups for all dependent handles need to
3284 * be provided for kGCTypeMarkSweepCompact collections, for all other
3285 * garbage collection types it is sufficient to provide object groups
3286 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003287 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003289 static void AddObjectGroup(Persistent<Value>* objects,
3290 size_t length,
3291 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003292 static void AddObjectGroup(Isolate* isolate,
3293 Persistent<Value>* objects,
3294 size_t length,
3295 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003296
3297 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003298 * Allows the host application to declare implicit references between
3299 * the objects: if |parent| is alive, all |children| are alive too.
3300 * After each garbage collection, all implicit references
3301 * are removed. It is intended to be used in the before-garbage-collection
3302 * callback function.
3303 */
3304 static void AddImplicitReferences(Persistent<Object> parent,
3305 Persistent<Value>* children,
3306 size_t length);
3307
3308 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003309 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003310 * initialize from scratch. This function is called implicitly if
3311 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003312 */
3313 static bool Initialize();
3314
kasper.lund7276f142008-07-30 08:49:36 +00003315 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003316 * Allows the host application to provide a callback which can be used
3317 * as a source of entropy for random number generators.
3318 */
3319 static void SetEntropySource(EntropySource source);
3320
3321 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003322 * Allows the host application to provide a callback that allows v8 to
3323 * cooperate with a profiler that rewrites return addresses on stack.
3324 */
3325 static void SetReturnAddressLocationResolver(
3326 ReturnAddressLocationResolver return_address_resolver);
3327
3328 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003329 * Allows the host application to provide the address of a function that's
3330 * invoked on entry to every V8-generated function.
3331 * Note that \p entry_hook is invoked at the very start of each
3332 * generated function.
3333 *
3334 * \param entry_hook a function that will be invoked on entry to every
3335 * V8-generated function.
3336 * \returns true on success on supported platforms, false on failure.
3337 * \note Setting a new entry hook function when one is already active will
3338 * fail.
3339 */
3340 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3341
3342 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003343 * Allows the host application to provide the address of a function that is
3344 * notified each time code is added, moved or removed.
3345 *
3346 * \param options options for the JIT code event handler.
3347 * \param event_handler the JIT code event handler, which will be invoked
3348 * each time code is added, moved or removed.
3349 * \note \p event_handler won't get notified of existent code.
3350 * \note since code removal notifications are not currently issued, the
3351 * \p event_handler may get notifications of code that overlaps earlier
3352 * code notifications. This happens when code areas are reused, and the
3353 * earlier overlapping code areas should therefore be discarded.
3354 * \note the events passed to \p event_handler and the strings they point to
3355 * are not guaranteed to live past each call. The \p event_handler must
3356 * copy strings and other parameters it needs to keep around.
3357 * \note the set of events declared in JitCodeEvent::EventType is expected to
3358 * grow over time, and the JitCodeEvent structure is expected to accrue
3359 * new members. The \p event_handler function must ignore event codes
3360 * it does not recognize to maintain future compatibility.
3361 */
3362 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3363 JitCodeEventHandler event_handler);
3364
3365 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003366 * Adjusts the amount of registered external memory. Used to give
3367 * V8 an indication of the amount of externally allocated memory
3368 * that is kept alive by JavaScript objects. V8 uses this to decide
3369 * when to perform global garbage collections. Registering
3370 * externally allocated memory will trigger global garbage
3371 * collections more often than otherwise in an attempt to garbage
3372 * collect the JavaScript objects keeping the externally allocated
3373 * memory alive.
3374 *
3375 * \param change_in_bytes the change in externally allocated memory
3376 * that is kept alive by JavaScript objects.
3377 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003378 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003379 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3380 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003381
iposva@chromium.org245aa852009-02-10 00:49:54 +00003382 /**
3383 * Suspends recording of tick samples in the profiler.
3384 * When the V8 profiling mode is enabled (usually via command line
3385 * switches) this function suspends recording of tick samples.
3386 * Profiling ticks are discarded until ResumeProfiler() is called.
3387 *
3388 * See also the --prof and --prof_auto command line switches to
3389 * enable V8 profiling.
3390 */
3391 static void PauseProfiler();
3392
3393 /**
3394 * Resumes recording of tick samples in the profiler.
3395 * See also PauseProfiler().
3396 */
3397 static void ResumeProfiler();
3398
ager@chromium.org41826e72009-03-30 13:30:57 +00003399 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003400 * Return whether profiler is currently paused.
3401 */
3402 static bool IsProfilerPaused();
3403
3404 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003405 * Retrieve the V8 thread id of the calling thread.
3406 *
3407 * The thread id for a thread should only be retrieved after the V8
3408 * lock has been acquired with a Locker object with that thread.
3409 */
3410 static int GetCurrentThreadId();
3411
3412 /**
3413 * Forcefully terminate execution of a JavaScript thread. This can
3414 * be used to terminate long-running scripts.
3415 *
3416 * TerminateExecution should only be called when then V8 lock has
3417 * been acquired with a Locker object. Therefore, in order to be
3418 * able to terminate long-running threads, preemption must be
3419 * enabled to allow the user of TerminateExecution to acquire the
3420 * lock.
3421 *
3422 * The termination is achieved by throwing an exception that is
3423 * uncatchable by JavaScript exception handlers. Termination
3424 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003425 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003426 * exception handler that catches an exception should check if that
3427 * exception is a termination exception and immediately return if
3428 * that is the case. Returning immediately in that case will
3429 * continue the propagation of the termination exception if needed.
3430 *
3431 * The thread id passed to TerminateExecution must have been
3432 * obtained by calling GetCurrentThreadId on the thread in question.
3433 *
3434 * \param thread_id The thread id of the thread to terminate.
3435 */
3436 static void TerminateExecution(int thread_id);
3437
3438 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003439 * Forcefully terminate the current thread of JavaScript execution
3440 * in the given isolate. If no isolate is provided, the default
3441 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003442 *
3443 * This method can be used by any thread even if that thread has not
3444 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003445 *
3446 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003447 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003448 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003449
3450 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003451 * Is V8 terminating JavaScript execution.
3452 *
3453 * Returns true if JavaScript execution is currently terminating
3454 * because of a call to TerminateExecution. In that case there are
3455 * still JavaScript frames on the stack and the termination
3456 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003457 *
3458 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003459 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003460 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003461
3462 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003463 * Releases any resources used by v8 and stops any utility threads
3464 * that may be running. Note that disposing v8 is permanent, it
3465 * cannot be reinitialized.
3466 *
3467 * It should generally not be necessary to dispose v8 before exiting
3468 * a process, this should happen automatically. It is only necessary
3469 * to use if the process needs the resources taken up by v8.
3470 */
3471 static bool Dispose();
3472
ager@chromium.org3811b432009-10-28 14:53:37 +00003473 /**
3474 * Get statistics about the heap memory usage.
3475 */
3476 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003477
3478 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003479 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003480 * heap. GC is not invoked prior to iterating, therefore there is no
3481 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003482 */
3483 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3484
3485 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003486 * Iterates through all the persistent handles in the current isolate's heap
3487 * that have class_ids.
3488 */
3489 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3490
3491 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003492 * Iterates through all the persistent handles in the current isolate's heap
3493 * that have class_ids and are candidates to be marked as partially dependent
3494 * handles. This will visit handles to young objects created since the last
3495 * garbage collection but is free to visit an arbitrary superset of these
3496 * objects.
3497 */
3498 static void VisitHandlesForPartialDependence(
3499 Isolate* isolate, PersistentHandleVisitor* visitor);
3500
3501 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003502 * Optional notification that the embedder is idle.
3503 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003504 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003505 * Returns true if the embedder should stop calling IdleNotification
3506 * until real work has been done. This indicates that V8 has done
3507 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003508 *
3509 * The hint argument specifies the amount of work to be done in the function
3510 * on scale from 1 to 1000. There is no guarantee that the actual work will
3511 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003512 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003513 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003514
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003515 /**
3516 * Optional notification that the system is running low on memory.
3517 * V8 uses these notifications to attempt to free memory.
3518 */
3519 static void LowMemoryNotification();
3520
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003521 /**
3522 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003523 * these notifications to guide the GC heuristic. Returns the number
3524 * of context disposals - including this one - since the last time
3525 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003526 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003527 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003528
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003529 private:
3530 V8();
3531
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003532 static internal::Object** GlobalizeReference(internal::Object** handle);
3533 static void DisposeGlobal(internal::Object** global_handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003534 static void DisposeGlobal(internal::Isolate* isolate,
3535 internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003536 static void MakeWeak(internal::Object** global_handle,
3537 void* data,
3538 WeakReferenceCallback);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003539 static void MakeWeak(internal::Isolate* isolate,
3540 internal::Object** global_handle,
3541 void* data,
3542 WeakReferenceCallback);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003543 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003544 static void MarkIndependent(internal::Object** global_handle);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003545 static void MarkIndependent(internal::Isolate* isolate,
3546 internal::Object** global_handle);
3547 static void MarkPartiallyDependent(internal::Object** global_handle);
3548 static void MarkPartiallyDependent(internal::Isolate* isolate,
3549 internal::Object** global_handle);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003550 static bool IsGlobalIndependent(internal::Object** global_handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003551 static bool IsGlobalIndependent(internal::Isolate* isolate,
3552 internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003553 static bool IsGlobalNearDeath(internal::Object** global_handle);
3554 static bool IsGlobalWeak(internal::Object** global_handle);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003555 static bool IsGlobalWeak(internal::Isolate* isolate,
3556 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003557
3558 template <class T> friend class Handle;
3559 template <class T> friend class Local;
3560 template <class T> friend class Persistent;
3561 friend class Context;
3562};
3563
3564
3565/**
3566 * An external exception handler.
3567 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003568class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003569 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003571 * Creates a new try/catch block and registers it with v8. Note that
3572 * all TryCatch blocks should be stack allocated because the memory
3573 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003574 */
3575 TryCatch();
3576
3577 /**
3578 * Unregisters and deletes this try/catch block.
3579 */
3580 ~TryCatch();
3581
3582 /**
3583 * Returns true if an exception has been caught by this try/catch block.
3584 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003585 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586
3587 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003588 * For certain types of exceptions, it makes no sense to continue
3589 * execution.
3590 *
3591 * Currently, the only type of exception that can be caught by a
3592 * TryCatch handler and for which it does not make sense to continue
3593 * is termination exception. Such exceptions are thrown when the
3594 * TerminateExecution methods are called to terminate a long-running
3595 * script.
3596 *
3597 * If CanContinue returns false, the correct action is to perform
3598 * any C++ cleanup needed and then return.
3599 */
3600 bool CanContinue() const;
3601
3602 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003603 * Throws the exception caught by this TryCatch in a way that avoids
3604 * it being caught again by this same TryCatch. As with ThrowException
3605 * it is illegal to execute any JavaScript operations after calling
3606 * ReThrow; the caller must return immediately to where the exception
3607 * is caught.
3608 */
3609 Handle<Value> ReThrow();
3610
3611 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003612 * Returns the exception caught by this try/catch block. If no exception has
3613 * been caught an empty handle is returned.
3614 *
3615 * The returned handle is valid until this TryCatch block has been destroyed.
3616 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003617 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003618
3619 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003620 * Returns the .stack property of the thrown object. If no .stack
3621 * property is present an empty handle is returned.
3622 */
3623 Local<Value> StackTrace() const;
3624
3625 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003626 * Returns the message associated with this exception. If there is
3627 * no message associated an empty handle is returned.
3628 *
3629 * The returned handle is valid until this TryCatch block has been
3630 * destroyed.
3631 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003632 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003633
3634 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003635 * Clears any exceptions that may have been caught by this try/catch block.
3636 * After this method has been called, HasCaught() will return false.
3637 *
3638 * It is not necessary to clear a try/catch block before using it again; if
3639 * another exception is thrown the previously caught exception will just be
3640 * overwritten. However, it is often a good idea since it makes it easier
3641 * to determine which operation threw a given exception.
3642 */
3643 void Reset();
3644
v8.team.kasperl727e9952008-09-02 14:56:44 +00003645 /**
3646 * Set verbosity of the external exception handler.
3647 *
3648 * By default, exceptions that are caught by an external exception
3649 * handler are not reported. Call SetVerbose with true on an
3650 * external exception handler to have exceptions caught by the
3651 * handler reported as if they were not caught.
3652 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003653 void SetVerbose(bool value);
3654
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003655 /**
3656 * Set whether or not this TryCatch should capture a Message object
3657 * which holds source information about where the exception
3658 * occurred. True by default.
3659 */
3660 void SetCaptureMessage(bool value);
3661
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003662 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003663 // Make it hard to create heap-allocated TryCatch blocks.
3664 TryCatch(const TryCatch&);
3665 void operator=(const TryCatch&);
3666 void* operator new(size_t size);
3667 void operator delete(void*, size_t);
3668
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003669 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003670 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003671 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003672 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003673 bool is_verbose_ : 1;
3674 bool can_continue_ : 1;
3675 bool capture_message_ : 1;
3676 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003677
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003678 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003679};
3680
3681
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003682// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003683
3684
3685/**
3686 * Ignore
3687 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003688class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003689 public:
3690 ExtensionConfiguration(int name_count, const char* names[])
3691 : name_count_(name_count), names_(names) { }
3692 private:
3693 friend class ImplementationUtilities;
3694 int name_count_;
3695 const char** names_;
3696};
3697
3698
3699/**
3700 * A sandboxed execution context with its own set of built-in objects
3701 * and functions.
3702 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003703class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003704 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003705 /**
3706 * Returns the global proxy object or global object itself for
3707 * detached contexts.
3708 *
3709 * Global proxy object is a thin wrapper whose prototype points to
3710 * actual context's global object with the properties like Object, etc.
3711 * This is done that way for security reasons (for more details see
3712 * https://wiki.mozilla.org/Gecko:SplitWindow).
3713 *
3714 * Please note that changes to global proxy object prototype most probably
3715 * would break VM---v8 expects only global object as a prototype of
3716 * global proxy object.
3717 *
3718 * If DetachGlobal() has been invoked, Global() would return actual global
3719 * object until global is reattached with ReattachGlobal().
3720 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003721 Local<Object> Global();
3722
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003723 /**
3724 * Detaches the global object from its context before
3725 * the global object can be reused to create a new context.
3726 */
3727 void DetachGlobal();
3728
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003729 /**
3730 * Reattaches a global object to a context. This can be used to
3731 * restore the connection between a global object and a context
3732 * after DetachGlobal has been called.
3733 *
3734 * \param global_object The global object to reattach to the
3735 * context. For this to work, the global object must be the global
3736 * object that was associated with this context before a call to
3737 * DetachGlobal.
3738 */
3739 void ReattachGlobal(Handle<Object> global_object);
3740
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003741 /** Creates a new context.
3742 *
3743 * Returns a persistent handle to the newly allocated context. This
3744 * persistent handle has to be disposed when the context is no
3745 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003746 *
3747 * \param extensions An optional extension configuration containing
3748 * the extensions to be installed in the newly created context.
3749 *
3750 * \param global_template An optional object template from which the
3751 * global object for the newly created context will be created.
3752 *
3753 * \param global_object An optional global object to be reused for
3754 * the newly created context. This global object must have been
3755 * created by a previous call to Context::New with the same global
3756 * template. The state of the global object will be completely reset
3757 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003758 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003759 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003760 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003761 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3762 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003763
kasper.lund44510672008-07-25 07:37:58 +00003764 /** Returns the last entered context. */
3765 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766
kasper.lund44510672008-07-25 07:37:58 +00003767 /** Returns the context that is on the top of the stack. */
3768 static Local<Context> GetCurrent();
3769
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003770 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003771 * Returns the context of the calling JavaScript code. That is the
3772 * context of the top-most JavaScript frame. If there are no
3773 * JavaScript frames an empty handle is returned.
3774 */
3775 static Local<Context> GetCalling();
3776
3777 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778 * Sets the security token for the context. To access an object in
3779 * another context, the security tokens must match.
3780 */
3781 void SetSecurityToken(Handle<Value> token);
3782
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003783 /** Restores the security token to the default value. */
3784 void UseDefaultSecurityToken();
3785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003786 /** Returns the security token of this context.*/
3787 Handle<Value> GetSecurityToken();
3788
v8.team.kasperl727e9952008-09-02 14:56:44 +00003789 /**
3790 * Enter this context. After entering a context, all code compiled
3791 * and run is compiled and run in this context. If another context
3792 * is already entered, this old context is saved so it can be
3793 * restored when the new context is exited.
3794 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003796
3797 /**
3798 * Exit this context. Exiting the current context restores the
3799 * context that was in place when entering the current context.
3800 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003801 void Exit();
3802
v8.team.kasperl727e9952008-09-02 14:56:44 +00003803 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804 bool HasOutOfMemoryException();
3805
v8.team.kasperl727e9952008-09-02 14:56:44 +00003806 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003807 static bool InContext();
3808
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003809 /** Returns an isolate associated with a current context. */
3810 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00003811
3812 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003813 * Gets the embedder data with the given index, which must have been set by a
3814 * previous call to SetEmbedderData with the same index. Note that index 0
3815 * currently has a special meaning for Chrome's debugger.
3816 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003817 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003818
3819 /**
3820 * Sets the embedder data with the given index, growing the data as
3821 * needed. Note that index 0 currently has a special meaning for Chrome's
3822 * debugger.
3823 */
3824 void SetEmbedderData(int index, Handle<Value> value);
3825
3826 /**
3827 * Gets a 2-byte-aligned native pointer from the embedder data with the given
3828 * index, which must have bees set by a previous call to
3829 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
3830 * currently has a special meaning for Chrome's debugger.
3831 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003832 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003833
3834 /**
3835 * Sets a 2-byte-aligned native pointer in the embedder data with the given
3836 * index, growing the data as needed. Note that index 0 currently has a
3837 * special meaning for Chrome's debugger.
3838 */
3839 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00003840
3841 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003842 * Control whether code generation from strings is allowed. Calling
3843 * this method with false will disable 'eval' and the 'Function'
3844 * constructor for code running in this context. If 'eval' or the
3845 * 'Function' constructor are used an exception will be thrown.
3846 *
3847 * If code generation from strings is not allowed the
3848 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3849 * set before blocking the call to 'eval' or the 'Function'
3850 * constructor. If that callback returns true, the call will be
3851 * allowed, otherwise an exception will be thrown. If no callback is
3852 * set an exception will be thrown.
3853 */
3854 void AllowCodeGenerationFromStrings(bool allow);
3855
3856 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003857 * Returns true if code generation from strings is allowed for the context.
3858 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3859 */
3860 bool IsCodeGenerationFromStringsAllowed();
3861
3862 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003863 * Sets the error description for the exception that is thrown when
3864 * code generation from strings is not allowed and 'eval' or the 'Function'
3865 * constructor are called.
3866 */
3867 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3868
3869 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003870 * Stack-allocated class which sets the execution context for all
3871 * operations executed within a local scope.
3872 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003873 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003874 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003875 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003876 context_->Enter();
3877 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003878 V8_INLINE(~Scope()) { context_->Exit(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003879 private:
3880 Handle<Context> context_;
3881 };
3882
3883 private:
3884 friend class Value;
3885 friend class Script;
3886 friend class Object;
3887 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003888
3889 Local<Value> SlowGetEmbedderData(int index);
3890 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003891};
3892
3893
3894/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003895 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
3896 * to use any given V8 isolate, see the comments in the Isolate class. The
3897 * definition of 'using a V8 isolate' includes accessing handles or holding onto
3898 * object pointers obtained from V8 handles while in the particular V8 isolate.
3899 * It is up to the user of V8 to ensure, perhaps with locking, that this
3900 * constraint is not violated. In addition to any other synchronization
3901 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
3902 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003904 * v8::Locker is a scoped lock object. While it's active, i.e. between its
3905 * construction and destruction, the current thread is allowed to use the locked
3906 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
3907 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003908 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003909 * Sample usage:
3910* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911 * ...
3912 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003913 * v8::Locker locker(isolate);
3914 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003915 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003916 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917 * ...
3918 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003919 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003921 * If you wish to stop using V8 in a thread A you can do this either by
3922 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
3923 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003924 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003925 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003926 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003927 * isolate->Exit();
3928 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003929 * ...
3930 * // Code not using V8 goes here while V8 can run in another thread.
3931 * ...
3932 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003933 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003934 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003935 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003936 * The Unlocker object is intended for use in a long-running callback from V8,
3937 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003938 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003939 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
3940 * given thread. This can be useful if you have code that can be called either
3941 * from code that holds the lock or from code that does not. The Unlocker is
3942 * not recursive so you can not have several Unlockers on the stack at once, and
3943 * 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 +00003944 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003945 * An unlocker will unlock several lockers if it has to and reinstate the
3946 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003947 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003948 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003949 * // V8 not locked.
3950 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003951 * v8::Locker locker(isolate);
3952 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953 * // V8 locked.
3954 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003955 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003956 * // V8 still locked (2 levels).
3957 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003958 * isolate->Exit();
3959 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003960 * // V8 not locked.
3961 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003962 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003963 * // V8 locked again (2 levels).
3964 * }
3965 * // V8 still locked (1 level).
3966 * }
3967 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003968 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003970class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003971 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003972 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003973 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003974 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003975 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
3976
3977 /**
3978 * Deprecated. Use Isolate version instead.
3979 */
3980 V8_DEPRECATED(Unlocker());
3981
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003983 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003984 void Initialize(Isolate* isolate);
3985
lrn@chromium.org1c092762011-05-09 09:42:16 +00003986 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003987};
3988
3989
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003990class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003992 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003993 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003994 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003995 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
3996
3997 /**
3998 * Deprecated. Use Isolate version instead.
3999 */
4000 V8_DEPRECATED(Locker());
4001
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004002 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004003
4004 /**
4005 * Start preemption.
4006 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004007 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004008 * that will switch between multiple threads that are in contention
4009 * for the V8 lock.
4010 */
4011 static void StartPreemption(int every_n_ms);
4012
4013 /**
4014 * Stop preemption.
4015 */
4016 static void StopPreemption();
4017
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004018 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004019 * Returns whether or not the locker for a given isolate, is locked by the
4020 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004021 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004022 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004023
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004024 /**
4025 * Returns whether v8::Locker is being used by this V8 instance.
4026 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004027 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004028
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004029 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004030 void Initialize(Isolate* isolate);
4031
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032 bool has_lock_;
4033 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004034 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004035
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004036 static bool active_;
4037
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004038 // Disallow copying and assigning.
4039 Locker(const Locker&);
4040 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004041};
4042
4043
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004044/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004045 * A struct for exporting HeapStats data from V8, using "push" model.
4046 */
4047struct HeapStatsUpdate;
4048
4049
4050/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004051 * An interface for exporting data from V8, using "push" model.
4052 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004053class V8EXPORT OutputStream { // NOLINT
4054 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004055 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004056 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004057 };
4058 enum WriteResult {
4059 kContinue = 0,
4060 kAbort = 1
4061 };
4062 virtual ~OutputStream() {}
4063 /** Notify about the end of stream. */
4064 virtual void EndOfStream() = 0;
4065 /** Get preferred output chunk size. Called only once. */
4066 virtual int GetChunkSize() { return 1024; }
4067 /** Get preferred output encoding. Called only once. */
4068 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4069 /**
4070 * Writes the next chunk of snapshot data into the stream. Writing
4071 * can be stopped by returning kAbort as function result. EndOfStream
4072 * will not be called in case writing was aborted.
4073 */
4074 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004075 /**
4076 * Writes the next chunk of heap stats data into the stream. Writing
4077 * can be stopped by returning kAbort as function result. EndOfStream
4078 * will not be called in case writing was aborted.
4079 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004080 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004081 return kAbort;
4082 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004083};
4084
4085
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004086/**
4087 * An interface for reporting progress and controlling long-running
4088 * activities.
4089 */
4090class V8EXPORT ActivityControl { // NOLINT
4091 public:
4092 enum ControlOption {
4093 kContinue = 0,
4094 kAbort = 1
4095 };
4096 virtual ~ActivityControl() {}
4097 /**
4098 * Notify about current progress. The activity can be stopped by
4099 * returning kAbort as the callback result.
4100 */
4101 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4102};
4103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004104
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004105// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004106
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004107
4108namespace internal {
4109
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004110const int kApiPointerSize = sizeof(void*); // NOLINT
4111const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004112
4113// Tag information for HeapObject.
4114const int kHeapObjectTag = 1;
4115const int kHeapObjectTagSize = 2;
4116const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4117
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004118// Tag information for Smi.
4119const int kSmiTag = 0;
4120const int kSmiTagSize = 1;
4121const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4122
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004123template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004124
4125// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004126template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004127 static const int kSmiShiftSize = 0;
4128 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004129 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004130 int shift_bits = kSmiTagSize + kSmiShiftSize;
4131 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4132 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4133 }
4134};
4135
4136// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004137template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004138 static const int kSmiShiftSize = 31;
4139 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004140 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004141 int shift_bits = kSmiTagSize + kSmiShiftSize;
4142 // Shift down and throw away top 32 bits.
4143 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4144 }
4145};
4146
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004147typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4148const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4149const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004150
4151/**
4152 * This class exports constants and functionality from within v8 that
4153 * is necessary to implement inline functions in the v8 api. Don't
4154 * depend on functions and constants defined here.
4155 */
4156class Internals {
4157 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004158 // These values match non-compiler-dependent values defined within
4159 // the implementation of v8.
4160 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004161 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004162 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004163
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004164 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004165 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004166 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004167 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4168 static const int kContextHeaderSize = 2 * kApiPointerSize;
4169 static const int kContextEmbedderDataIndex = 54;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004170 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004171 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004172 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004173 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004174
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004175 static const int kIsolateStateOffset = 0;
4176 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4177 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004178 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
4179 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004180 static const int kUndefinedValueRootIndex = 5;
4181 static const int kNullValueRootIndex = 7;
4182 static const int kTrueValueRootIndex = 8;
4183 static const int kFalseValueRootIndex = 9;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004184 static const int kEmptySymbolRootIndex = 119;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004185
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004186 static const int kNodeIsIndependentShift = 4;
4187 static const int kNodeIsPartiallyDependentShift = 5;
4188
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00004189 static const int kJSObjectType = 0xab;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004190 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004191 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004192 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004193
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004194 static const int kUndefinedOddballKind = 5;
4195 static const int kNullOddballKind = 3;
4196
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004197 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004198 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4199 kHeapObjectTag);
4200 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004201
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004202 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004203 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004204 }
4205
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004206 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004207 typedef internal::Object O;
4208 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4209 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4210 }
4211
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004212 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004213 typedef internal::Object O;
4214 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4215 }
4216
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004217 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004218 int representation = (instance_type & kFullStringRepresentationMask);
4219 return representation == kExternalTwoByteRepresentationTag;
4220 }
4221
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004222 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004223 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4224 return *reinterpret_cast<int*>(addr) == 1;
4225 }
4226
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004227 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
4228 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4229 return *addr & (1 << shift);
4230 }
4231
4232 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
4233 bool value, int shift)) {
4234 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4235 uint8_t mask = 1 << shift;
4236 *addr = (*addr & ~mask) | (value << shift);
4237 }
4238
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004239 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004240 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4241 kIsolateEmbedderDataOffset;
4242 *reinterpret_cast<void**>(addr) = data;
4243 }
4244
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004245 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004246 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4247 kIsolateEmbedderDataOffset;
4248 return *reinterpret_cast<void**>(addr);
4249 }
4250
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004251 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
4252 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004253 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4254 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4255 }
4256
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004257 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004258 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004259 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4260 return *reinterpret_cast<T*>(addr);
4261 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004262
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004263 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004264 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004265 typedef internal::Object O;
4266 typedef internal::Internals I;
4267 O* ctx = *reinterpret_cast<O**>(context);
4268 int embedder_data_offset = I::kContextHeaderSize +
4269 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4270 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4271 int value_offset =
4272 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4273 return I::ReadField<T>(embedder_data, value_offset);
4274 }
4275
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004276 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
4277 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
4278 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
4279 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
4280 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
4281 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
4282 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004283};
4284
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004285} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004286
4287
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004288template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004289Local<T>::Local() : Handle<T>() { }
4290
4291
4292template <class T>
4293Local<T> Local<T>::New(Handle<T> that) {
4294 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004295 T* that_ptr = *that;
4296 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4297 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4298 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4299 reinterpret_cast<internal::HeapObject*>(*p))));
4300 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004301 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4302}
4303
4304
4305template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004306 Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
4307 if (that.IsEmpty()) return Local<T>();
4308 T* that_ptr = *that;
4309 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4310 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4311 reinterpret_cast<internal::Isolate*>(isolate), *p)));
4312}
4313
4314
4315template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004316Persistent<T> Persistent<T>::New(Handle<T> that) {
4317 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004318 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004319 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4320}
4321
4322
4323template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004324bool Persistent<T>::IsIndependent() const {
4325 if (this->IsEmpty()) return false;
4326 return V8::IsGlobalIndependent(reinterpret_cast<internal::Object**>(**this));
4327}
4328
4329
4330template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004331bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004332 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004333 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004334 if (!I::IsInitialized(isolate)) return false;
4335 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(**this),
4336 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004337}
4338
4339
4340template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004341bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004342 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004343 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004344}
4345
4346
4347template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004348bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004349 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004350 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004351}
4352
4353
4354template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004355bool Persistent<T>::IsWeak(Isolate* isolate) const {
4356 if (this->IsEmpty()) return false;
4357 return V8::IsGlobalWeak(reinterpret_cast<internal::Isolate*>(isolate),
4358 reinterpret_cast<internal::Object**>(**this));
4359}
4360
4361
4362template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004363void Persistent<T>::Dispose() {
4364 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004365 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004366}
4367
4368
4369template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004370void Persistent<T>::Dispose(Isolate* isolate) {
4371 if (this->IsEmpty()) return;
4372 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4373 reinterpret_cast<internal::Object**>(**this));
4374}
4375
4376
4377template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004378Persistent<T>::Persistent() : Handle<T>() { }
4379
4380template <class T>
4381void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004382 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4383 parameters,
4384 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004385}
4386
4387template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004388void Persistent<T>::MakeWeak(Isolate* isolate, void* parameters,
4389 WeakReferenceCallback callback) {
4390 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4391 reinterpret_cast<internal::Object**>(**this),
4392 parameters,
4393 callback);
4394}
4395
4396template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004397void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004398 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004399}
4400
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004401template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004402void Persistent<T>::MarkIndependent() {
4403 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4404}
4405
4406template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004407void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004408 typedef internal::Internals I;
4409 if (this->IsEmpty()) return;
4410 if (!I::IsInitialized(isolate)) return;
4411 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
4412 true, I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004413}
4414
4415template <class T>
4416void Persistent<T>::MarkPartiallyDependent() {
4417 V8::MarkPartiallyDependent(reinterpret_cast<internal::Object**>(**this));
4418}
4419
4420template <class T>
4421void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004422 typedef internal::Internals I;
4423 if (this->IsEmpty()) return;
4424 if (!I::IsInitialized(isolate)) return;
4425 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
4426 true, I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004427}
4428
4429template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004430void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004431 typedef internal::Internals I;
4432 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4433 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4434 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004435}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004436
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004437template <class T>
4438uint16_t Persistent<T>::WrapperClassId() const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004439 typedef internal::Internals I;
4440 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4441 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4442 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004443}
4444
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004445Arguments::Arguments(internal::Object** implicit_args,
4446 internal::Object** values, int length,
4447 bool is_construct_call)
4448 : implicit_args_(implicit_args),
4449 values_(values),
4450 length_(length),
4451 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004452
4453
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004454Local<Value> Arguments::operator[](int i) const {
4455 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4456 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4457}
4458
4459
4460Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004461 return Local<Function>(reinterpret_cast<Function*>(
4462 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004463}
4464
4465
4466Local<Object> Arguments::This() const {
4467 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4468}
4469
4470
4471Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004472 return Local<Object>(reinterpret_cast<Object*>(
4473 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004474}
4475
4476
4477Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004478 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004479}
4480
4481
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004482Isolate* Arguments::GetIsolate() const {
4483 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4484}
4485
4486
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004487bool Arguments::IsConstructCall() const {
4488 return is_construct_call_;
4489}
4490
4491
4492int Arguments::Length() const {
4493 return length_;
4494}
4495
4496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004497template <class T>
4498Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004499 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4500 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004501 return Local<T>(reinterpret_cast<T*>(after));
4502}
4503
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004504Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004505 return resource_name_;
4506}
4507
4508
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004509Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004510 return resource_line_offset_;
4511}
4512
4513
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004514Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004515 return resource_column_offset_;
4516}
4517
4518
4519Handle<Boolean> Boolean::New(bool value) {
4520 return value ? True() : False();
4521}
4522
4523
4524void Template::Set(const char* name, v8::Handle<Data> value) {
4525 Set(v8::String::New(name), value);
4526}
4527
4528
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004529Local<Value> Object::GetInternalField(int index) {
4530#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004531 typedef internal::Object O;
4532 typedef internal::Internals I;
4533 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004534 // Fast path: If the object is a plain JSObject, which is the common case, we
4535 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004536 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004537 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004538 O* value = I::ReadField<O*>(obj, offset);
4539 O** result = HandleScope::CreateHandle(value);
4540 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004541 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004542#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004543 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004544}
4545
4546
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004547void* Object::GetAlignedPointerFromInternalField(int index) {
4548#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004549 typedef internal::Object O;
4550 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004551 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004552 // Fast path: If the object is a plain JSObject, which is the common case, we
4553 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004554 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004555 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004556 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004557 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004558#endif
4559 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004560}
4561
4562
4563String* String::Cast(v8::Value* value) {
4564#ifdef V8_ENABLE_CHECKS
4565 CheckCast(value);
4566#endif
4567 return static_cast<String*>(value);
4568}
4569
4570
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004571Local<String> String::Empty(Isolate* isolate) {
4572 typedef internal::Object* S;
4573 typedef internal::Internals I;
4574 if (!I::IsInitialized(isolate)) return Empty();
4575 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4576 return Local<String>(reinterpret_cast<String*>(slot));
4577}
4578
4579
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004580String::ExternalStringResource* String::GetExternalStringResource() const {
4581 typedef internal::Object O;
4582 typedef internal::Internals I;
4583 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004584 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004585 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004586 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4587 result = reinterpret_cast<String::ExternalStringResource*>(value);
4588 } else {
4589 result = NULL;
4590 }
4591#ifdef V8_ENABLE_CHECKS
4592 VerifyExternalStringResource(result);
4593#endif
4594 return result;
4595}
4596
4597
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004598String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4599 String::Encoding* encoding_out) const {
4600 typedef internal::Object O;
4601 typedef internal::Internals I;
4602 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4603 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4604 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4605 ExternalStringResourceBase* resource = NULL;
4606 if (type == I::kExternalAsciiRepresentationTag ||
4607 type == I::kExternalTwoByteRepresentationTag) {
4608 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4609 resource = static_cast<ExternalStringResourceBase*>(value);
4610 }
4611#ifdef V8_ENABLE_CHECKS
4612 VerifyExternalStringResourceBase(resource, *encoding_out);
4613#endif
4614 return resource;
4615}
4616
4617
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004618bool Value::IsUndefined() const {
4619#ifdef V8_ENABLE_CHECKS
4620 return FullIsUndefined();
4621#else
4622 return QuickIsUndefined();
4623#endif
4624}
4625
4626bool Value::QuickIsUndefined() const {
4627 typedef internal::Object O;
4628 typedef internal::Internals I;
4629 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4630 if (!I::HasHeapObjectTag(obj)) return false;
4631 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4632 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4633}
4634
4635
4636bool Value::IsNull() const {
4637#ifdef V8_ENABLE_CHECKS
4638 return FullIsNull();
4639#else
4640 return QuickIsNull();
4641#endif
4642}
4643
4644bool Value::QuickIsNull() const {
4645 typedef internal::Object O;
4646 typedef internal::Internals I;
4647 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4648 if (!I::HasHeapObjectTag(obj)) return false;
4649 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4650 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4651}
4652
4653
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004654bool Value::IsString() const {
4655#ifdef V8_ENABLE_CHECKS
4656 return FullIsString();
4657#else
4658 return QuickIsString();
4659#endif
4660}
4661
4662bool Value::QuickIsString() const {
4663 typedef internal::Object O;
4664 typedef internal::Internals I;
4665 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4666 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004667 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004668}
4669
4670
4671Number* Number::Cast(v8::Value* value) {
4672#ifdef V8_ENABLE_CHECKS
4673 CheckCast(value);
4674#endif
4675 return static_cast<Number*>(value);
4676}
4677
4678
4679Integer* Integer::Cast(v8::Value* value) {
4680#ifdef V8_ENABLE_CHECKS
4681 CheckCast(value);
4682#endif
4683 return static_cast<Integer*>(value);
4684}
4685
4686
4687Date* Date::Cast(v8::Value* value) {
4688#ifdef V8_ENABLE_CHECKS
4689 CheckCast(value);
4690#endif
4691 return static_cast<Date*>(value);
4692}
4693
4694
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004695StringObject* StringObject::Cast(v8::Value* value) {
4696#ifdef V8_ENABLE_CHECKS
4697 CheckCast(value);
4698#endif
4699 return static_cast<StringObject*>(value);
4700}
4701
4702
4703NumberObject* NumberObject::Cast(v8::Value* value) {
4704#ifdef V8_ENABLE_CHECKS
4705 CheckCast(value);
4706#endif
4707 return static_cast<NumberObject*>(value);
4708}
4709
4710
4711BooleanObject* BooleanObject::Cast(v8::Value* value) {
4712#ifdef V8_ENABLE_CHECKS
4713 CheckCast(value);
4714#endif
4715 return static_cast<BooleanObject*>(value);
4716}
4717
4718
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004719RegExp* RegExp::Cast(v8::Value* value) {
4720#ifdef V8_ENABLE_CHECKS
4721 CheckCast(value);
4722#endif
4723 return static_cast<RegExp*>(value);
4724}
4725
4726
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004727Object* Object::Cast(v8::Value* value) {
4728#ifdef V8_ENABLE_CHECKS
4729 CheckCast(value);
4730#endif
4731 return static_cast<Object*>(value);
4732}
4733
4734
4735Array* Array::Cast(v8::Value* value) {
4736#ifdef V8_ENABLE_CHECKS
4737 CheckCast(value);
4738#endif
4739 return static_cast<Array*>(value);
4740}
4741
4742
4743Function* Function::Cast(v8::Value* value) {
4744#ifdef V8_ENABLE_CHECKS
4745 CheckCast(value);
4746#endif
4747 return static_cast<Function*>(value);
4748}
4749
4750
4751External* External::Cast(v8::Value* value) {
4752#ifdef V8_ENABLE_CHECKS
4753 CheckCast(value);
4754#endif
4755 return static_cast<External*>(value);
4756}
4757
4758
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004759Isolate* AccessorInfo::GetIsolate() const {
4760 return *reinterpret_cast<Isolate**>(&args_[-3]);
4761}
4762
4763
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004764Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004765 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004766}
4767
4768
4769Local<Object> AccessorInfo::This() const {
4770 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4771}
4772
4773
4774Local<Object> AccessorInfo::Holder() const {
4775 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4776}
4777
4778
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004779Handle<Primitive> Undefined(Isolate* isolate) {
4780 typedef internal::Object* S;
4781 typedef internal::Internals I;
4782 if (!I::IsInitialized(isolate)) return Undefined();
4783 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4784 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4785}
4786
4787
4788Handle<Primitive> Null(Isolate* isolate) {
4789 typedef internal::Object* S;
4790 typedef internal::Internals I;
4791 if (!I::IsInitialized(isolate)) return Null();
4792 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4793 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4794}
4795
4796
4797Handle<Boolean> True(Isolate* isolate) {
4798 typedef internal::Object* S;
4799 typedef internal::Internals I;
4800 if (!I::IsInitialized(isolate)) return True();
4801 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4802 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4803}
4804
4805
4806Handle<Boolean> False(Isolate* isolate) {
4807 typedef internal::Object* S;
4808 typedef internal::Internals I;
4809 if (!I::IsInitialized(isolate)) return False();
4810 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4811 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4812}
4813
4814
4815void Isolate::SetData(void* data) {
4816 typedef internal::Internals I;
4817 I::SetEmbedderData(this, data);
4818}
4819
4820
4821void* Isolate::GetData() {
4822 typedef internal::Internals I;
4823 return I::GetEmbedderData(this);
4824}
4825
4826
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004827Local<Value> Context::GetEmbedderData(int index) {
4828#ifndef V8_ENABLE_CHECKS
4829 typedef internal::Object O;
4830 typedef internal::Internals I;
4831 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
4832 return Local<Value>(reinterpret_cast<Value*>(result));
4833#else
4834 return SlowGetEmbedderData(index);
4835#endif
4836}
4837
4838
4839void* Context::GetAlignedPointerFromEmbedderData(int index) {
4840#ifndef V8_ENABLE_CHECKS
4841 typedef internal::Internals I;
4842 return I::ReadEmbedderData<void*>(this, index);
4843#else
4844 return SlowGetAlignedPointerFromEmbedderData(index);
4845#endif
4846}
4847
4848
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004849/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004850 * \example shell.cc
4851 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004852 * command-line and executes them.
4853 */
4854
4855
4856/**
4857 * \example process.cc
4858 */
4859
4860
4861} // namespace v8
4862
4863
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004864#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004865#undef TYPE_CHECK
4866
4867
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004868#endif // V8_H_