blob: 1e4d2f927d930158fd832f88b6b3b884a074e2ff [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000065// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000066#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
67 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070#else
71#define V8EXPORT
72#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000075#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076
77#endif // _WIN32
78
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000079#if defined(__GNUC__) && !defined(DEBUG)
80#define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
81#elif defined(_MSC_VER) && !defined(DEBUG)
82#define V8_INLINE(declarator) __forceinline declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000083#else
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000084#define V8_INLINE(declarator) inline declarator
85#endif
86
87#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
88#define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
89#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
90#define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
91#else
92#define V8_DEPRECATED(declarator) declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000093#endif
94
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000096 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 */
98namespace v8 {
99
100class Context;
101class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000102class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103class Value;
104class Utils;
105class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000106class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107class Object;
108class Array;
109class Int32;
110class Uint32;
111class External;
112class Primitive;
113class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000114class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115class Integer;
116class Function;
117class Date;
118class ImplementationUtilities;
119class Signature;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000120class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121template <class T> class Handle;
122template <class T> class Local;
123template <class T> class Persistent;
124class FunctionTemplate;
125class ObjectTemplate;
126class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000127class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000128class StackTrace;
129class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000130class Isolate;
ulan@chromium.org750145a2013-03-07 15:14:13 +0000131class DeclaredAccessorDescriptor;
132class ObjectOperationDescriptor;
133class RawOperationDescriptor;
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
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000160// TODO(svenpanne) Temporary definition until Chrome is in sync.
161typedef void (*NearDeathCallback)(Isolate* isolate,
162 Persistent<Value> object,
163 void* parameter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000165// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000167#define TYPE_CHECK(T, S) \
168 while (false) { \
169 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 }
171
172/**
173 * An object reference managed by the v8 garbage collector.
174 *
175 * All objects returned from v8 have to be tracked by the garbage
176 * collector so that it knows that the objects are still alive. Also,
177 * because the garbage collector may move objects, it is unsafe to
178 * point directly to an object. Instead, all objects are stored in
179 * handles which are known by the garbage collector and updated
180 * whenever an object moves. Handles should always be passed by value
181 * (except in cases like out-parameters) and they should never be
182 * allocated on the heap.
183 *
184 * There are two types of handles: local and persistent handles.
185 * Local handles are light-weight and transient and typically used in
186 * local operations. They are managed by HandleScopes. Persistent
187 * handles can be used when storing objects across several independent
188 * operations and have to be explicitly deallocated when they're no
189 * longer used.
190 *
191 * It is safe to extract the object stored in the handle by
192 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000193 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 * behind the scenes and the same rules apply to these values as to
195 * their handles.
196 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000197template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 /**
200 * Creates an empty handle.
201 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000202 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203
204 /**
205 * Creates a new handle for the specified value.
206 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000207 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208
209 /**
210 * Creates a handle for the contents of the specified handle. This
211 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000212 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000214 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215 * between compatible handles, for instance assigning a
216 * Handle<String> to a variable declared as Handle<Value>, is legal
217 * because String is a subclass of Value.
218 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000219 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220 : val_(reinterpret_cast<T*>(*that)) {
221 /**
222 * This check fails when trying to convert between incompatible
223 * handles. For example, converting from a Handle<String> to a
224 * Handle<Number>.
225 */
226 TYPE_CHECK(T, S);
227 }
228
229 /**
230 * Returns true if the handle is empty.
231 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000232 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234 /**
235 * Sets the handle to be empty. IsEmpty() will then return true.
236 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000237 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000238
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000239 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000240
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000241 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242
243 /**
244 * Checks whether two handles are the same.
245 * Returns true if both are empty, or if the objects
246 * to which they refer are identical.
247 * The handles' references are not checked.
248 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000249 template <class S> V8_INLINE(bool operator==(Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000250 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
251 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252 if (a == 0) return b == 0;
253 if (b == 0) return false;
254 return *a == *b;
255 }
256
257 /**
258 * Checks whether two handles are different.
259 * Returns true if only one of the handles is empty, or if
260 * the objects to which they refer are different.
261 * The handles' references are not checked.
262 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000263 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 return !operator==(that);
265 }
266
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000267 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000268#ifdef V8_ENABLE_CHECKS
269 // If we're going to perform the type check then we have to check
270 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000272#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 return Handle<T>(T::Cast(*that));
274 }
275
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000276 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000277 return Handle<S>::Cast(*this);
278 }
279
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 private:
281 T* val_;
282};
283
284
285/**
286 * A light-weight stack-allocated object handle. All operations
287 * that return objects from within v8 return them in local handles. They
288 * are created within HandleScopes, and all local handles allocated within a
289 * handle scope are destroyed when the handle scope is destroyed. Hence it
290 * is not necessary to explicitly deallocate local handles.
291 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000292template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000294 V8_INLINE(Local());
295 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 : Handle<T>(reinterpret_cast<T*>(*that)) {
297 /**
298 * This check fails when trying to convert between incompatible
299 * handles. For example, converting from a Handle<String> to a
300 * Handle<Number>.
301 */
302 TYPE_CHECK(T, S);
303 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000304 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
305 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000306#ifdef V8_ENABLE_CHECKS
307 // If we're going to perform the type check then we have to check
308 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000310#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 return Local<T>(T::Cast(*that));
312 }
313
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000314 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000315 return Local<S>::Cast(*this);
316 }
317
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000318 /**
319 * Create a local handle for the content of another handle.
320 * The referee is kept alive by the local handle even when
321 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000323 V8_INLINE(static Local<T> New(Handle<T> that));
324 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325};
326
327
328/**
329 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000330 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 * allocated, a Persistent handle remains valid until it is explicitly
332 * disposed.
333 *
334 * A persistent handle contains a reference to a storage cell within
335 * the v8 engine which holds an object value and which is updated by
336 * the garbage collector whenever the object is moved. A new storage
337 * cell can be created using Persistent::New and existing handles can
338 * be disposed using Persistent::Dispose. Since persistent handles
339 * are passed by value you may have many persistent handle objects
340 * that point to the same storage cell. For instance, if you pass a
341 * persistent handle as an argument to a function you will not get two
342 * different storage cells but rather two references to the same
343 * storage cell.
344 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000345template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 /**
348 * Creates an empty persistent handle that doesn't point to any
349 * storage cell.
350 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000351 V8_INLINE(Persistent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352
353 /**
354 * Creates a persistent handle for the same storage cell as the
355 * specified handle. This constructor allows you to pass persistent
356 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000357 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000359 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 * between compatible persistent handles, for instance assigning a
361 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000362 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000364 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 : Handle<T>(reinterpret_cast<T*>(*that)) {
366 /**
367 * This check fails when trying to convert between incompatible
368 * handles. For example, converting from a Handle<String> to a
369 * Handle<Number>.
370 */
371 TYPE_CHECK(T, S);
372 }
373
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000374 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000376 /**
377 * "Casts" a plain handle which is known to be a persistent handle
378 * to a persistent handle.
379 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000380 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 : Handle<T>(*that) { }
382
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000383 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000384#ifdef V8_ENABLE_CHECKS
385 // If we're going to perform the type check then we have to check
386 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000388#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389 return Persistent<T>(T::Cast(*that));
390 }
391
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000392 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000393 return Persistent<S>::Cast(*this);
394 }
395
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000396 /** Deprecated. Use Isolate version instead. */
397 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
398
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000400 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000402 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
403
404 /** Deprecated. Use Isolate version instead. */
405 V8_DEPRECATED(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406
407 /**
408 * Releases the storage cell referenced by this persistent handle.
409 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000410 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 * cell remain and IsEmpty will still return false.
412 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000413 V8_INLINE(void Dispose(Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000415 /** Deprecated. Use Isolate version instead. */
416 V8_DEPRECATED(void MakeWeak(void* parameters,
417 WeakReferenceCallback callback));
418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 /**
420 * Make the reference to this object weak. When only weak handles
421 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000422 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 * it the object reference and the given parameters.
424 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000425 V8_INLINE(void MakeWeak(Isolate* isolate,
426 void* parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000427 NearDeathCallback callback));
428
429 /** Deprecated. Use Isolate version instead. */
430 V8_DEPRECATED(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000432 /** Clears the weak reference to this object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000433 V8_INLINE(void ClearWeak(Isolate* isolate));
434
435 /** Deprecated. Use Isolate version instead. */
436 V8_DEPRECATED(void MarkIndependent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437
438 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000439 * Marks the reference to this object independent. Garbage collector is free
440 * to ignore any object groups containing this object. Weak callback for an
441 * independent handle should not assume that it will be preceded by a global
442 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000443 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000444 V8_INLINE(void MarkIndependent(Isolate* isolate));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000445
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000446 /** Deprecated. Use Isolate version instead. */
447 V8_DEPRECATED(void MarkPartiallyDependent());
448
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000449 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000450 * Marks the reference to this object partially dependent. Partially dependent
451 * handles only depend on other partially dependent handles and these
452 * dependencies are provided through object groups. It provides a way to build
453 * smaller object groups for young objects that represent only a subset of all
454 * external dependencies. This mark is automatically cleared after each
455 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000456 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000457 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000458
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000459 /** Deprecated. Use Isolate version instead. */
460 V8_DEPRECATED(bool IsIndependent() const);
461
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000462 /** Returns true if this handle was previously marked as independent. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000463 V8_INLINE(bool IsIndependent(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000464
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000465 /** Deprecated. Use Isolate version instead. */
466 V8_DEPRECATED(bool IsNearDeath() const);
467
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000468 /** Checks if the handle holds the only reference to an object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000469 V8_INLINE(bool IsNearDeath(Isolate* isolate) const);
470
471 /** Deprecated. Use Isolate version instead. */
472 V8_DEPRECATED(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000474 /** Returns true if the handle's reference is weak. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000475 V8_INLINE(bool IsWeak(Isolate* isolate) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000477 /** Deprecated. Use Isolate version instead. */
478 V8_DEPRECATED(void SetWrapperClassId(uint16_t class_id));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000479
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000480 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000481 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
482 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000483 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000484 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id));
485
486 /** Deprecated. Use Isolate version instead. */
487 V8_DEPRECATED(uint16_t WrapperClassId() const);
488
489 /**
490 * Returns the class ID previously assigned to this handle or 0 if no class ID
491 * was previously assigned.
492 */
493 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000494
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 private:
496 friend class ImplementationUtilities;
497 friend class ObjectTemplate;
498};
499
500
v8.team.kasperl727e9952008-09-02 14:56:44 +0000501 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 * A stack-allocated class that governs a number of local handles.
503 * After a handle scope has been created, all local handles will be
504 * allocated within that handle scope until either the handle scope is
505 * deleted or another handle scope is created. If there is already a
506 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000507 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 * new handles will again be allocated in the original handle scope.
509 *
510 * After the handle scope of a local handle has been deleted the
511 * garbage collector will no longer track the object stored in the
512 * handle and may deallocate it. The behavior of accessing a handle
513 * for which the handle scope has been deleted is undefined.
514 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000515class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000517 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000518 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000520 HandleScope(Isolate* isolate);
521
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000522 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523
524 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 * Closes the handle scope and returns the value as a handle in the
526 * previous scope, which is the new current scope after the call.
527 */
528 template <class T> Local<T> Close(Handle<T> value);
529
530 /**
531 * Counts the number of allocated handles.
532 */
533 static int NumberOfHandles();
534
535 /**
536 * Creates a new handle with the given value.
537 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000538 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000539 static internal::Object** CreateHandle(internal::Isolate* isolate,
540 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000541 // Faster version, uses HeapObject to obtain the current Isolate.
542 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543
544 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000545 // Make it hard to create heap-allocated or illegal handle scopes by
546 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 HandleScope(const HandleScope&);
548 void operator=(const HandleScope&);
549 void* operator new(size_t size);
550 void operator delete(void*, size_t);
551
ager@chromium.org3811b432009-10-28 14:53:37 +0000552 // This Data class is accessible internally as HandleScopeData through a
553 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000554 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000556 internal::Object** next;
557 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000558 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000559 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000561 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 }
563 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000564
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000565 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000566 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000568 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000569 internal::Object** prev_next_;
570 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000572 // Allow for the active closing of HandleScopes which allows to pass a handle
573 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000575 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 friend class ImplementationUtilities;
578};
579
580
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000581// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582
583
584/**
585 * The superclass of values and API object templates.
586 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000587class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 private:
589 Data();
590};
591
592
593/**
594 * Pre-compilation data that can be associated with a script. This
595 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000596 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000597 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000599class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 public:
601 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000602
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000603 /**
604 * Pre-compiles the specified script (context-independent).
605 *
606 * \param input Pointer to UTF-8 script source code.
607 * \param length Length of UTF-8 script source code.
608 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000611 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000612 * Pre-compiles the specified script (context-independent).
613 *
614 * NOTE: Pre-compilation using this method cannot happen on another thread
615 * without using Lockers.
616 *
617 * \param source Script source code.
618 */
619 static ScriptData* PreCompile(Handle<String> source);
620
621 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000622 * Load previous pre-compilation data.
623 *
624 * \param data Pointer to data returned by a call to Data() of a previous
625 * ScriptData. Ownership is not transferred.
626 * \param length Length of data.
627 */
628 static ScriptData* New(const char* data, int length);
629
630 /**
631 * Returns the length of Data().
632 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000634
635 /**
636 * Returns a serialized representation of this ScriptData that can later be
637 * passed to New(). NOTE: Serialized data is platform-dependent.
638 */
639 virtual const char* Data() = 0;
640
641 /**
642 * Returns true if the source code could not be parsed.
643 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000644 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645};
646
647
648/**
649 * The origin, within a file, of a script.
650 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000651class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000653 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000654 Handle<Value> resource_name,
655 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000656 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000657 : resource_name_(resource_name),
658 resource_line_offset_(resource_line_offset),
659 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000660 V8_INLINE(Handle<Value> ResourceName() const);
661 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
662 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000664 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 Handle<Integer> resource_line_offset_;
666 Handle<Integer> resource_column_offset_;
667};
668
669
670/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000671 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000673class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000676 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000677 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000678 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000679 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000680 * when New() returns
681 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
682 * using pre_data speeds compilation if it's done multiple times.
683 * Owned by caller, no references are kept when New() returns.
684 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000685 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000686 * available to compile event handlers.
687 * \return Compiled script object (context independent; when run it
688 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000690 static Local<Script> New(Handle<String> source,
691 ScriptOrigin* origin = NULL,
692 ScriptData* pre_data = NULL,
693 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694
mads.s.agercbaa0602008-08-14 13:41:48 +0000695 /**
696 * Compiles the specified script using the specified file name
697 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000698 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000699 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000700 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000701 * as the script's origin.
702 * \return Compiled script object (context independent; when run it
703 * will use the currently entered context).
704 */
705 static Local<Script> New(Handle<String> source,
706 Handle<Value> file_name);
707
708 /**
709 * Compiles the specified script (bound to current context).
710 *
711 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000712 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000713 * when Compile() returns
714 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
715 * using pre_data speeds compilation if it's done multiple times.
716 * Owned by caller, no references are kept when Compile() returns.
717 * \param script_data Arbitrary data associated with script. Using
718 * this has same effect as calling SetData(), but makes data available
719 * earlier (i.e. to compile event handlers).
720 * \return Compiled script object, bound to the context that was active
721 * when this function was called. When run it will always use this
722 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000723 */
724 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000725 ScriptOrigin* origin = NULL,
726 ScriptData* pre_data = NULL,
727 Handle<String> script_data = Handle<String>());
728
729 /**
730 * Compiles the specified script using the specified file name
731 * object (typically a string) as the script's origin.
732 *
733 * \param source Script source code.
734 * \param file_name File name to use as script's origin
735 * \param script_data Arbitrary data associated with script. Using
736 * this has same effect as calling SetData(), but makes data available
737 * earlier (i.e. to compile event handlers).
738 * \return Compiled script object, bound to the context that was active
739 * when this function was called. When run it will always use this
740 * context.
741 */
742 static Local<Script> Compile(Handle<String> source,
743 Handle<Value> file_name,
744 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000745
v8.team.kasperl727e9952008-09-02 14:56:44 +0000746 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000747 * Runs the script returning the resulting value. If the script is
748 * context independent (created using ::New) it will be run in the
749 * currently entered context. If it is context specific (created
750 * using ::Compile) it will be run in the context in which it was
751 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000752 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000754
755 /**
756 * Returns the script id value.
757 */
758 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000759
760 /**
761 * Associate an additional data object with the script. This is mainly used
762 * with the debugger as this data object is only available through the
763 * debugger API.
764 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000765 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766};
767
768
769/**
770 * An error message.
771 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000772class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000774 Local<String> Get() const;
775 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000777 /**
778 * Returns the resource name for the script from where the function causing
779 * the error originates.
780 */
ager@chromium.org32912102009-01-16 10:38:43 +0000781 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000783 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000784 * Returns the resource data for the script from where the function causing
785 * the error originates.
786 */
787 Handle<Value> GetScriptData() const;
788
789 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000790 * Exception stack trace. By default stack traces are not captured for
791 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
792 * to change this option.
793 */
794 Handle<StackTrace> GetStackTrace() const;
795
796 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000797 * Returns the number, 1-based, of the line where the error occurred.
798 */
ager@chromium.org32912102009-01-16 10:38:43 +0000799 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000801 /**
802 * Returns the index within the script of the first character where
803 * the error occurred.
804 */
ager@chromium.org32912102009-01-16 10:38:43 +0000805 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000806
807 /**
808 * Returns the index within the script of the last character where
809 * the error occurred.
810 */
ager@chromium.org32912102009-01-16 10:38:43 +0000811 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000812
813 /**
814 * Returns the index within the line of the first character where
815 * the error occurred.
816 */
ager@chromium.org32912102009-01-16 10:38:43 +0000817 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000818
819 /**
820 * Returns the index within the line of the last character where
821 * the error occurred.
822 */
ager@chromium.org32912102009-01-16 10:38:43 +0000823 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000824
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825 // TODO(1245381): Print to a string instead of on a FILE.
826 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000827
828 static const int kNoLineNumberInfo = 0;
829 static const int kNoColumnInfo = 0;
830};
831
832
833/**
834 * Representation of a JavaScript stack trace. The information collected is a
835 * snapshot of the execution stack and the information remains valid after
836 * execution continues.
837 */
838class V8EXPORT StackTrace {
839 public:
840 /**
841 * Flags that determine what information is placed captured for each
842 * StackFrame when grabbing the current stack trace.
843 */
844 enum StackTraceOptions {
845 kLineNumber = 1,
846 kColumnOffset = 1 << 1 | kLineNumber,
847 kScriptName = 1 << 2,
848 kFunctionName = 1 << 3,
849 kIsEval = 1 << 4,
850 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000851 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000852 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000853 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000854 };
855
856 /**
857 * Returns a StackFrame at a particular index.
858 */
859 Local<StackFrame> GetFrame(uint32_t index) const;
860
861 /**
862 * Returns the number of StackFrames.
863 */
864 int GetFrameCount() const;
865
866 /**
867 * Returns StackTrace as a v8::Array that contains StackFrame objects.
868 */
869 Local<Array> AsArray();
870
871 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000872 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000873 *
874 * \param frame_limit The maximum number of stack frames we want to capture.
875 * \param options Enumerates the set of things we will capture for each
876 * StackFrame.
877 */
878 static Local<StackTrace> CurrentStackTrace(
879 int frame_limit,
880 StackTraceOptions options = kOverview);
881};
882
883
884/**
885 * A single JavaScript stack frame.
886 */
887class V8EXPORT StackFrame {
888 public:
889 /**
890 * Returns the number, 1-based, of the line for the associate function call.
891 * This method will return Message::kNoLineNumberInfo if it is unable to
892 * retrieve the line number, or if kLineNumber was not passed as an option
893 * when capturing the StackTrace.
894 */
895 int GetLineNumber() const;
896
897 /**
898 * Returns the 1-based column offset on the line for the associated function
899 * call.
900 * This method will return Message::kNoColumnInfo if it is unable to retrieve
901 * the column number, or if kColumnOffset was not passed as an option when
902 * capturing the StackTrace.
903 */
904 int GetColumn() const;
905
906 /**
907 * Returns the name of the resource that contains the script for the
908 * function for this StackFrame.
909 */
910 Local<String> GetScriptName() const;
911
912 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000913 * Returns the name of the resource that contains the script for the
914 * function for this StackFrame or sourceURL value if the script name
915 * is undefined and its source ends with //@ sourceURL=... string.
916 */
917 Local<String> GetScriptNameOrSourceURL() const;
918
919 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000920 * Returns the name of the function associated with this stack frame.
921 */
922 Local<String> GetFunctionName() const;
923
924 /**
925 * Returns whether or not the associated function is compiled via a call to
926 * eval().
927 */
928 bool IsEval() const;
929
930 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000931 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000932 * constructor via "new".
933 */
934 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935};
936
937
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000938// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939
940
941/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000942 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000944class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946 /**
947 * Returns true if this value is the undefined value. See ECMA-262
948 * 4.3.10.
949 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000950 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951
952 /**
953 * Returns true if this value is the null value. See ECMA-262
954 * 4.3.11.
955 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000956 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957
958 /**
959 * Returns true if this value is true.
960 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000961 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962
963 /**
964 * Returns true if this value is false.
965 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000966 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967
968 /**
969 * Returns true if this value is an instance of the String type.
970 * See ECMA-262 8.4.
971 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000972 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973
974 /**
975 * Returns true if this value is a function.
976 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000977 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978
979 /**
980 * Returns true if this value is an array.
981 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000982 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983
v8.team.kasperl727e9952008-09-02 14:56:44 +0000984 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985 * Returns true if this value is an object.
986 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000987 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988
v8.team.kasperl727e9952008-09-02 14:56:44 +0000989 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990 * Returns true if this value is boolean.
991 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000992 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000993
v8.team.kasperl727e9952008-09-02 14:56:44 +0000994 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 * Returns true if this value is a number.
996 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000997 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998
v8.team.kasperl727e9952008-09-02 14:56:44 +0000999 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000 * Returns true if this value is external.
1001 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001002 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003
v8.team.kasperl727e9952008-09-02 14:56:44 +00001004 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 * Returns true if this value is a 32-bit signed integer.
1006 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001007 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001009 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001010 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001011 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001012 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001013
1014 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001015 * Returns true if this value is a Date.
1016 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001017 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001018
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001019 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001020 * Returns true if this value is a Boolean object.
1021 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001022 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001023
1024 /**
1025 * Returns true if this value is a Number object.
1026 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001027 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001028
1029 /**
1030 * Returns true if this value is a String object.
1031 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001032 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001033
1034 /**
1035 * Returns true if this value is a NativeError.
1036 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001037 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001038
1039 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001040 * Returns true if this value is a RegExp.
1041 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001042 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001043
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001044 Local<Boolean> ToBoolean() const;
1045 Local<Number> ToNumber() const;
1046 Local<String> ToString() const;
1047 Local<String> ToDetailString() const;
1048 Local<Object> ToObject() const;
1049 Local<Integer> ToInteger() const;
1050 Local<Uint32> ToUint32() const;
1051 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052
1053 /**
1054 * Attempts to convert a string to an array index.
1055 * Returns an empty handle if the conversion fails.
1056 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001057 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001059 bool BooleanValue() const;
1060 double NumberValue() const;
1061 int64_t IntegerValue() const;
1062 uint32_t Uint32Value() const;
1063 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064
1065 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001066 bool Equals(Handle<Value> that) const;
1067 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001068
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001069 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001070 V8_INLINE(bool QuickIsUndefined() const);
1071 V8_INLINE(bool QuickIsNull() const);
1072 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001073 bool FullIsUndefined() const;
1074 bool FullIsNull() const;
1075 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076};
1077
1078
1079/**
1080 * The superclass of primitive values. See ECMA-262 4.3.2.
1081 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001082class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083
1084
1085/**
1086 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1087 * or false value.
1088 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001089class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001091 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001092 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093};
1094
1095
1096/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001097 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001099class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001101 enum Encoding {
1102 UNKNOWN_ENCODING = 0x1,
1103 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001104 ASCII_ENCODING = 0x4,
1105 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001106 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001107 /**
1108 * Returns the number of characters in this string.
1109 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001110 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111
v8.team.kasperl727e9952008-09-02 14:56:44 +00001112 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001113 * Returns the number of bytes in the UTF-8 encoded
1114 * representation of this string.
1115 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001116 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001117
1118 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001119 * A fast conservative check for non-ASCII characters. May
1120 * return true even for ASCII strings, but if it returns
1121 * false you can be sure that all characters are in the range
1122 * 0-127.
1123 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001124 bool MayContainNonAscii() const;
1125
1126 /**
1127 * Returns whether this string contains only one byte data.
1128 */
1129 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001130
1131 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001132 * Write the contents of the string to an external buffer.
1133 * If no arguments are given, expects the buffer to be large
1134 * enough to hold the entire string and NULL terminator. Copies
1135 * the contents of the string and the NULL terminator into the
1136 * buffer.
1137 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001138 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1139 * before the end of the buffer.
1140 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001141 * Copies up to length characters into the output buffer.
1142 * Only null-terminates if there is enough space in the buffer.
1143 *
1144 * \param buffer The buffer into which the string will be copied.
1145 * \param start The starting position within the string at which
1146 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001147 * \param length The number of characters to copy from the string. For
1148 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001149 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001150 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001151 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001152 * \return The number of characters copied to the buffer excluding the null
1153 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001154 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001155 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001156 enum WriteOptions {
1157 NO_OPTIONS = 0,
1158 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001159 NO_NULL_TERMINATION = 2,
1160 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001161 };
1162
lrn@chromium.org34e60782011-09-15 07:25:40 +00001163 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001164 int Write(uint16_t* buffer,
1165 int start = 0,
1166 int length = -1,
1167 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001168 // ASCII characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001169 int WriteAscii(char* buffer,
1170 int start = 0,
1171 int length = -1,
1172 int options = NO_OPTIONS) const;
1173 // One byte characters.
1174 int WriteOneByte(uint8_t* buffer,
1175 int start = 0,
1176 int length = -1,
1177 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001178 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001179 int WriteUtf8(char* buffer,
1180 int length = -1,
1181 int* nchars_ref = NULL,
1182 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183
v8.team.kasperl727e9952008-09-02 14:56:44 +00001184 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001185 * A zero length string.
1186 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001187 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001188 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001189
1190 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001191 * Returns true if the string is external
1192 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001193 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194
v8.team.kasperl727e9952008-09-02 14:56:44 +00001195 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001196 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001197 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001198 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001199
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001200 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001201 public:
1202 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001203
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001204 protected:
1205 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001206
1207 /**
1208 * Internally V8 will call this Dispose method when the external string
1209 * resource is no longer needed. The default implementation will use the
1210 * delete operator. This method can be overridden in subclasses to
1211 * control how allocated external string resources are disposed.
1212 */
1213 virtual void Dispose() { delete this; }
1214
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001215 private:
1216 // Disallow copying and assigning.
1217 ExternalStringResourceBase(const ExternalStringResourceBase&);
1218 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001219
1220 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001221 };
1222
v8.team.kasperl727e9952008-09-02 14:56:44 +00001223 /**
1224 * An ExternalStringResource is a wrapper around a two-byte string
1225 * buffer that resides outside V8's heap. Implement an
1226 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001227 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001228 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001229 class V8EXPORT ExternalStringResource
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 ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001237
1238 /**
1239 * The string data from the underlying buffer.
1240 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001242
1243 /**
1244 * The length of the string. That is, the number of two-byte characters.
1245 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248 protected:
1249 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 };
1251
1252 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001253 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001254 * string buffer that resides outside V8's heap. Implement an
1255 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001256 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001257 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001258 * UTF-8, which would require special treatment internally in the
1259 * engine and, in the case of UTF-8, do not allow efficient indexing.
1260 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001261 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001263 class V8EXPORT ExternalAsciiStringResource
1264 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 public:
1266 /**
1267 * Override the destructor to manage the life cycle of the underlying
1268 * buffer.
1269 */
1270 virtual ~ExternalAsciiStringResource() {}
1271 /** The string data from the underlying buffer.*/
1272 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001273 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 virtual size_t length() const = 0;
1275 protected:
1276 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 };
1278
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001279 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001282 * If the string is an external string, return the ExternalStringResourceBase
1283 * regardless of the encoding, otherwise return NULL. The encoding of the
1284 * string is returned in encoding_out.
1285 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001286 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1287 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001288
1289 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001290 * Get the ExternalStringResource for an external string. Returns
1291 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001293 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294
1295 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001296 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001297 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001299 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001301 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302
1303 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001304 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001305 * The second parameter 'length' gives the buffer length. If omitted,
1306 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001308 static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309
lrn@chromium.org34e60782011-09-15 07:25:40 +00001310 /** Allocates a new string from 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001311 static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312
1313 /** Creates a symbol. Returns one if it exists already.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001314 static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315
v8.team.kasperl727e9952008-09-02 14:56:44 +00001316 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001317 * Creates a new string by concatenating the left and the right strings
1318 * passed in as parameters.
1319 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001320 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001321
1322 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001323 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001324 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001325 * resource will be disposed by calling its Dispose method. The caller of
1326 * this function should not otherwise delete or modify the resource. Neither
1327 * should the underlying buffer be deallocated or modified except through the
1328 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001329 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001330 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001331
ager@chromium.org6f10e412009-02-13 10:11:16 +00001332 /**
1333 * Associate an external string resource with this string by transforming it
1334 * in place so that existing references to this string in the JavaScript heap
1335 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001336 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001337 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001338 * The string is not modified if the operation fails. See NewExternal for
1339 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001340 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001341 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342
v8.team.kasperl727e9952008-09-02 14:56:44 +00001343 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001344 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001345 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001346 * resource will be disposed by calling its Dispose method. The caller of
1347 * this function should not otherwise delete or modify the resource. Neither
1348 * should the underlying buffer be deallocated or modified except through the
1349 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001350 */
1351 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001352
ager@chromium.org6f10e412009-02-13 10:11:16 +00001353 /**
1354 * Associate an external string resource with this string by transforming it
1355 * in place so that existing references to this string in the JavaScript heap
1356 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001357 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001358 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001359 * The string is not modified if the operation fails. See NewExternal for
1360 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001361 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001362 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001364 /**
1365 * Returns true if this string can be made external.
1366 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001367 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001368
lrn@chromium.org34e60782011-09-15 07:25:40 +00001369 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001370 static Local<String> NewUndetectable(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371
lrn@chromium.org34e60782011-09-15 07:25:40 +00001372 /** Creates an undetectable string from the supplied 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001373 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374
1375 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001376 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001377 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001378 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001379 * then the length() method returns 0 and the * operator returns
1380 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001381 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001382 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001383 public:
1384 explicit Utf8Value(Handle<v8::Value> obj);
1385 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001386 char* operator*() { return str_; }
1387 const char* operator*() const { return str_; }
1388 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001389 private:
1390 char* str_;
1391 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001392
1393 // Disallow copying and assigning.
1394 Utf8Value(const Utf8Value&);
1395 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001396 };
1397
1398 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001399 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001401 * If conversion to a string fails (eg. due to an exception in the toString()
1402 * method of the object) then the length() method returns 0 and the * operator
1403 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001405 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 public:
1407 explicit AsciiValue(Handle<v8::Value> obj);
1408 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001409 char* operator*() { return str_; }
1410 const char* operator*() const { return str_; }
1411 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 private:
1413 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001414 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001415
1416 // Disallow copying and assigning.
1417 AsciiValue(const AsciiValue&);
1418 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 };
1420
1421 /**
1422 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001423 * If conversion to a string fails (eg. due to an exception in the toString()
1424 * method of the object) then the length() method returns 0 and the * operator
1425 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001427 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 public:
1429 explicit Value(Handle<v8::Value> obj);
1430 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001431 uint16_t* operator*() { return str_; }
1432 const uint16_t* operator*() const { return str_; }
1433 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 private:
1435 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001436 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001437
1438 // Disallow copying and assigning.
1439 Value(const Value&);
1440 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001442
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001443 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001444 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1445 Encoding encoding) const;
1446 void VerifyExternalStringResource(ExternalStringResource* val) const;
1447 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448};
1449
1450
1451/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001452 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001454class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001456 double Value() const;
1457 static Local<Number> New(double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001458 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001460 Number();
1461 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462};
1463
1464
1465/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001466 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001468class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001470 static Local<Integer> New(int32_t value);
1471 static Local<Integer> NewFromUnsigned(uint32_t value);
1472 static Local<Integer> New(int32_t value, Isolate*);
1473 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1474 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001475 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001477 Integer();
1478 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479};
1480
1481
1482/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001483 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001485class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001487 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001489 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490};
1491
1492
1493/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001494 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001496class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001498 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001500 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501};
1502
1503
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504enum PropertyAttribute {
1505 None = 0,
1506 ReadOnly = 1 << 0,
1507 DontEnum = 1 << 1,
1508 DontDelete = 1 << 2
1509};
1510
ager@chromium.org3811b432009-10-28 14:53:37 +00001511enum ExternalArrayType {
1512 kExternalByteArray = 1,
1513 kExternalUnsignedByteArray,
1514 kExternalShortArray,
1515 kExternalUnsignedShortArray,
1516 kExternalIntArray,
1517 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001518 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001519 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001520 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001521};
1522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001524 * Accessor[Getter|Setter] are used as callback functions when
1525 * setting|getting a particular property. See Object and ObjectTemplate's
1526 * method SetAccessor.
1527 */
1528typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1529 const AccessorInfo& info);
1530
1531
1532typedef void (*AccessorSetter)(Local<String> property,
1533 Local<Value> value,
1534 const AccessorInfo& info);
1535
1536
1537/**
1538 * Access control specifications.
1539 *
1540 * Some accessors should be accessible across contexts. These
1541 * accessors have an explicit access control parameter which specifies
1542 * the kind of cross-context access that should be allowed.
1543 *
1544 * Additionally, for security, accessors can prohibit overwriting by
1545 * accessors defined in JavaScript. For objects that have such
1546 * accessors either locally or in their prototype chain it is not
1547 * possible to overwrite the accessor by using __defineGetter__ or
1548 * __defineSetter__ from JavaScript code.
1549 */
1550enum AccessControl {
1551 DEFAULT = 0,
1552 ALL_CAN_READ = 1,
1553 ALL_CAN_WRITE = 1 << 1,
1554 PROHIBITS_OVERWRITING = 1 << 2
1555};
1556
1557
1558/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001559 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001561class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001563 bool Set(Handle<Value> key,
1564 Handle<Value> value,
1565 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001566
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001567 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001568
ager@chromium.orge2902be2009-06-08 12:21:35 +00001569 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001570 // overriding accessors or read-only properties.
1571 //
1572 // Note that if the object has an interceptor the property will be set
1573 // locally, but since the interceptor takes precedence the local property
1574 // will only be returned if the interceptor doesn't return a value.
1575 //
1576 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001577 bool ForceSet(Handle<Value> key,
1578 Handle<Value> value,
1579 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001580
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001581 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001583 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001584
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001585 /**
1586 * Gets the property attributes of a property which can be None or
1587 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1588 * None when the property doesn't exist.
1589 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001590 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001591
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 // TODO(1245389): Replace the type-specific versions of these
1593 // functions with generic ones that accept a Handle<Value> key.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001594 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001595
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001596 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001597
1598 // Delete a property on this object bypassing interceptors and
1599 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001600 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001601
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001602 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001603
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001604 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001606 bool SetAccessor(Handle<String> name,
1607 AccessorGetter getter,
1608 AccessorSetter setter = 0,
1609 Handle<Value> data = Handle<Value>(),
1610 AccessControl settings = DEFAULT,
1611 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001612
ulan@chromium.org750145a2013-03-07 15:14:13 +00001613 // This function is not yet stable and should not be used at this time.
1614 bool SetAccessor(Handle<String> name,
1615 Handle<DeclaredAccessorDescriptor> descriptor,
1616 AccessControl settings = DEFAULT,
1617 PropertyAttribute attribute = None);
1618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001620 * Returns an array containing the names of the enumerable properties
1621 * of this object, including properties from prototype objects. The
1622 * array returned by this method contains the same values as would
1623 * be enumerated by a for-in statement over this object.
1624 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001625 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001626
1627 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001628 * This function has the same functionality as GetPropertyNames but
1629 * the returned array doesn't contain the names of properties from
1630 * prototype objects.
1631 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001632 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001633
1634 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635 * Get the prototype object. This does not skip objects marked to
1636 * be skipped by __proto__ and it does not consult the security
1637 * handler.
1638 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001639 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640
1641 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001642 * Set the prototype object. This does not skip objects marked to
1643 * be skipped by __proto__ and it does not consult the security
1644 * handler.
1645 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001646 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001647
1648 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001649 * Finds an instance of the given function template in the prototype
1650 * chain.
1651 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001652 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001653
1654 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655 * Call builtin Object.prototype.toString on this object.
1656 * This is different from Value::ToString() that may call
1657 * user-defined toString function. This one does not.
1658 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001659 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001661 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001662 * Returns the function invoked as a constructor for this object.
1663 * May be the null value.
1664 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001665 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001666
1667 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001668 * Returns the name of the function invoked as a constructor for this object.
1669 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001670 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001671
kasper.lund212ac232008-07-16 07:07:30 +00001672 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001673 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001674
1675 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001676 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001677
kasper.lund212ac232008-07-16 07:07:30 +00001678 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001679 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001680
1681 /**
1682 * Gets a 2-byte-aligned native pointer from an internal field. This field
1683 * must have been set by SetAlignedPointerInInternalField, everything else
1684 * leads to undefined behavior.
1685 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001686 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001687
1688 /**
1689 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1690 * a field, GetAlignedPointerFromInternalField must be used, everything else
1691 * leads to undefined behavior.
1692 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001693 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001694
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001695 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001696 bool HasOwnProperty(Handle<String> key);
1697 bool HasRealNamedProperty(Handle<String> key);
1698 bool HasRealIndexedProperty(uint32_t index);
1699 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700
1701 /**
1702 * If result.IsEmpty() no real property was located in the prototype chain.
1703 * This means interceptors in the prototype chain are not called.
1704 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001705 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001706
1707 /**
1708 * If result.IsEmpty() no real property was located on the object or
1709 * in the prototype chain.
1710 * This means interceptors in the prototype chain are not called.
1711 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001712 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713
1714 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001715 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716
kasper.lund212ac232008-07-16 07:07:30 +00001717 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001718 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001720 /**
1721 * Turns on access check on the object if the object is an instance of
1722 * a template that has access check callbacks. If an object has no
1723 * access check info, the object cannot be accessed by anyone.
1724 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001725 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001726
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001727 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001728 * Returns the identity hash for this object. The current implementation
1729 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001730 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001731 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001732 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001733 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001734 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001735
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001736 /**
1737 * Access hidden properties on JavaScript objects. These properties are
1738 * hidden from the executing JavaScript and only accessible through the V8
1739 * C++ API. Hidden properties introduced by V8 internally (for example the
1740 * identity hash) are prefixed with "v8::".
1741 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001742 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1743 Local<Value> GetHiddenValue(Handle<String> key);
1744 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001745
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001746 /**
1747 * Returns true if this is an instance of an api function (one
1748 * created from a function created from a function template) and has
1749 * been modified since it was created. Note that this method is
1750 * conservative and may return true for objects that haven't actually
1751 * been modified.
1752 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001753 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001754
1755 /**
1756 * Clone this object with a fast but shallow copy. Values will point
1757 * to the same values as the original object.
1758 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001759 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001761 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001762 * Returns the context in which the object was created.
1763 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001764 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001765
1766 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001767 * Set the backing store of the indexed properties to be managed by the
1768 * embedding layer. Access to the indexed properties will follow the rules
1769 * spelled out in CanvasPixelArray.
1770 * Note: The embedding program still owns the data and needs to ensure that
1771 * the backing store is preserved while V8 has a reference.
1772 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001773 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1774 bool HasIndexedPropertiesInPixelData();
1775 uint8_t* GetIndexedPropertiesPixelData();
1776 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001777
ager@chromium.org3811b432009-10-28 14:53:37 +00001778 /**
1779 * Set the backing store of the indexed properties to be managed by the
1780 * embedding layer. Access to the indexed properties will follow the rules
1781 * spelled out for the CanvasArray subtypes in the WebGL specification.
1782 * Note: The embedding program still owns the data and needs to ensure that
1783 * the backing store is preserved while V8 has a reference.
1784 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001785 void SetIndexedPropertiesToExternalArrayData(void* data,
1786 ExternalArrayType array_type,
1787 int number_of_elements);
1788 bool HasIndexedPropertiesInExternalArrayData();
1789 void* GetIndexedPropertiesExternalArrayData();
1790 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1791 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001792
lrn@chromium.org1c092762011-05-09 09:42:16 +00001793 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001794 * Checks whether a callback is set by the
1795 * ObjectTemplate::SetCallAsFunctionHandler method.
1796 * When an Object is callable this method returns true.
1797 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001798 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001799
1800 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001801 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001802 * ObjectTemplate::SetCallAsFunctionHandler method.
1803 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001804 Local<Value> CallAsFunction(Handle<Object> recv,
1805 int argc,
1806 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001807
1808 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001809 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001810 * ObjectTemplate::SetCallAsFunctionHandler method.
1811 * Note: This method behaves like the Function::NewInstance method.
1812 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001813 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001814
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001815 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001816 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001817
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001819 Object();
1820 static void CheckCast(Value* obj);
1821 Local<Value> SlowGetInternalField(int index);
1822 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823};
1824
1825
1826/**
1827 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1828 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001829class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001831 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832
ager@chromium.org3e875802009-06-29 08:26:34 +00001833 /**
1834 * Clones an element at index |index|. Returns an empty
1835 * handle if cloning fails (for any reason).
1836 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001837 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001838
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001839 /**
1840 * Creates a JavaScript array with the given length. If the length
1841 * is negative the returned array will have length 0.
1842 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001843 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001844
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001845 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001847 Array();
1848 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849};
1850
1851
1852/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001853 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001855class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001857 Local<Object> NewInstance() const;
1858 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1859 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1860 void SetName(Handle<String> name);
1861 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001862
1863 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001864 * Name inferred from variable or property assignment of this function.
1865 * Used to facilitate debugging and profiling of JavaScript code written
1866 * in an OO style, where many functions are anonymous but are assigned
1867 * to object properties.
1868 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001869 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001870
1871 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001872 * Returns zero based line number of function body and
1873 * kLineOffsetNotFound if no information available.
1874 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001875 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001876 /**
1877 * Returns zero based column number of function body and
1878 * kLineOffsetNotFound if no information available.
1879 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001880 int GetScriptColumnNumber() const;
1881 Handle<Value> GetScriptId() const;
1882 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001883 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001884 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001887 Function();
1888 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889};
1890
1891
1892/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001893 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1894 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001895class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001896 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001897 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001898
1899 /**
1900 * A specialization of Value::NumberValue that is more efficient
1901 * because we know the structure of this object.
1902 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001903 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001904
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001905 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001906
1907 /**
1908 * Notification that the embedder has changed the time zone,
1909 * daylight savings time, or other date / time configuration
1910 * parameters. V8 keeps a cache of various values used for
1911 * date / time computation. This notification will reset
1912 * those cached values for the current context so that date /
1913 * time configuration changes would be reflected in the Date
1914 * object.
1915 *
1916 * This API should not be called more than needed as it will
1917 * negatively impact the performance of date operations.
1918 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001919 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001920
1921 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001922 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001923};
1924
1925
1926/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001927 * A Number object (ECMA-262, 4.3.21).
1928 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001929class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001930 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001931 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001932
1933 /**
1934 * Returns the Number held by the object.
1935 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001936 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001937
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001938 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001939
1940 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001941 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001942};
1943
1944
1945/**
1946 * A Boolean object (ECMA-262, 4.3.15).
1947 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001948class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001949 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001950 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001951
1952 /**
1953 * Returns the Boolean held by the object.
1954 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001955 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001956
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001957 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001958
1959 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001960 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001961};
1962
1963
1964/**
1965 * A String object (ECMA-262, 4.3.18).
1966 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001967class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001968 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001969 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001970
1971 /**
1972 * Returns the String held by the object.
1973 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001974 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001975
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001976 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001977
1978 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001979 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001980};
1981
1982
1983/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001984 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1985 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001986class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001987 public:
1988 /**
1989 * Regular expression flag bits. They can be or'ed to enable a set
1990 * of flags.
1991 */
1992 enum Flags {
1993 kNone = 0,
1994 kGlobal = 1,
1995 kIgnoreCase = 2,
1996 kMultiline = 4
1997 };
1998
1999 /**
2000 * Creates a regular expression from the given pattern string and
2001 * the flags bit field. May throw a JavaScript exception as
2002 * described in ECMA-262, 15.10.4.1.
2003 *
2004 * For example,
2005 * RegExp::New(v8::String::New("foo"),
2006 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2007 * is equivalent to evaluating "/foo/gm".
2008 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002009 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002010
2011 /**
2012 * Returns the value of the source property: a string representing
2013 * the regular expression.
2014 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002015 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002016
2017 /**
2018 * Returns the flags bit field.
2019 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002020 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002021
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002022 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002023
2024 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002025 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002026};
2027
2028
2029/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002030 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2031 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002033class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002035 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002036 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002037 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002039 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040};
2041
2042
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002043// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044
2045
2046/**
2047 * The superclass of object and function templates.
2048 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002049class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 public:
2051 /** Adds a property to each instance created by this template.*/
2052 void Set(Handle<String> name, Handle<Data> value,
2053 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002054 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 private:
2056 Template();
2057
2058 friend class ObjectTemplate;
2059 friend class FunctionTemplate;
2060};
2061
2062
2063/**
2064 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002065 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 * including the receiver, the number and values of arguments, and
2067 * the holder of the function.
2068 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002069class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002071 V8_INLINE(int Length() const);
2072 V8_INLINE(Local<Value> operator[](int i) const);
2073 V8_INLINE(Local<Function> Callee() const);
2074 V8_INLINE(Local<Object> This() const);
2075 V8_INLINE(Local<Object> Holder() const);
2076 V8_INLINE(bool IsConstructCall() const);
2077 V8_INLINE(Local<Value> Data() const);
2078 V8_INLINE(Isolate* GetIsolate() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002079
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002081 static const int kIsolateIndex = 0;
2082 static const int kDataIndex = -1;
2083 static const int kCalleeIndex = -2;
2084 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 friend class ImplementationUtilities;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002087 V8_INLINE(Arguments(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002088 internal::Object** values,
2089 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002090 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002091 internal::Object** implicit_args_;
2092 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002094 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095};
2096
2097
2098/**
2099 * The information passed to an accessor callback about the context
2100 * of the property access.
2101 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002102class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002104 V8_INLINE(AccessorInfo(internal::Object** args))
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002105 : args_(args) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002106 V8_INLINE(Isolate* GetIsolate() const);
2107 V8_INLINE(Local<Value> Data() const);
2108 V8_INLINE(Local<Object> This() const);
2109 V8_INLINE(Local<Object> Holder() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002112 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113};
2114
2115
2116typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 * NamedProperty[Getter|Setter] are used as interceptors on object.
2120 * See ObjectTemplate::SetNamedPropertyHandler.
2121 */
2122typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
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> (*NamedPropertySetter)(Local<String> property,
2131 Local<Value> value,
2132 const AccessorInfo& info);
2133
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134/**
2135 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002136 * The result is an integer encoding property attributes (like v8::None,
2137 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002139typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2140 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141
2142
2143/**
2144 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002145 * The return value is true if the property could be deleted and false
2146 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147 */
2148typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2149 const AccessorInfo& info);
2150
2151/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002152 * Returns an array containing the names of the properties the named
2153 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154 */
2155typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2156
v8.team.kasperl727e9952008-09-02 14:56:44 +00002157
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002159 * Returns the value of the property if the getter intercepts the
2160 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 */
2162typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2163 const AccessorInfo& info);
2164
2165
2166/**
2167 * Returns the value if the setter intercepts the request.
2168 * Otherwise, returns an empty handle.
2169 */
2170typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2171 Local<Value> value,
2172 const AccessorInfo& info);
2173
2174
2175/**
2176 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002177 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002179typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2180 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181
2182/**
2183 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002184 * The return value is true if the property could be deleted and false
2185 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 */
2187typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2188 const AccessorInfo& info);
2189
v8.team.kasperl727e9952008-09-02 14:56:44 +00002190/**
2191 * Returns an array containing the indices of the properties the
2192 * indexed property getter intercepts.
2193 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2195
2196
2197/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002198 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 */
2200enum AccessType {
2201 ACCESS_GET,
2202 ACCESS_SET,
2203 ACCESS_HAS,
2204 ACCESS_DELETE,
2205 ACCESS_KEYS
2206};
2207
v8.team.kasperl727e9952008-09-02 14:56:44 +00002208
2209/**
2210 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002211 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002212 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002213typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214 Local<Value> key,
2215 AccessType type,
2216 Local<Value> data);
2217
v8.team.kasperl727e9952008-09-02 14:56:44 +00002218
2219/**
2220 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002221 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002222 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002223typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224 uint32_t index,
2225 AccessType type,
2226 Local<Value> data);
2227
2228
2229/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002230 * A FunctionTemplate is used to create functions at runtime. There
2231 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002232 * context. The lifetime of the created function is equal to the
2233 * lifetime of the context. So in case the embedder needs to create
2234 * temporary functions that can be collected using Scripts is
2235 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 *
2237 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002238 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002240 * A FunctionTemplate has a corresponding instance template which is
2241 * used to create object instances when the function is used as a
2242 * constructor. Properties added to the instance template are added to
2243 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 *
2245 * A FunctionTemplate can have a prototype template. The prototype template
2246 * is used to create the prototype object of the function.
2247 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002250 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2252 * t->Set("func_property", v8::Number::New(1));
2253 *
2254 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2255 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2256 * proto_t->Set("proto_const", v8::Number::New(2));
2257 *
2258 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2259 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2260 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2261 * instance_t->Set("instance_property", Number::New(3));
2262 *
2263 * v8::Local<v8::Function> function = t->GetFunction();
2264 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002265 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 *
2267 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002268 * and "instance" for the instance object created above. The function
2269 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002271 * \code
2272 * func_property in function == true;
2273 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002275 * function.prototype.proto_method() invokes 'InvokeCallback'
2276 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002278 * instance instanceof function == true;
2279 * instance.instance_accessor calls 'InstanceAccessorCallback'
2280 * instance.instance_property == 3;
2281 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002283 * A FunctionTemplate can inherit from another one by calling the
2284 * FunctionTemplate::Inherit method. The following graph illustrates
2285 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002287 * \code
2288 * FunctionTemplate Parent -> Parent() . prototype -> { }
2289 * ^ ^
2290 * | Inherit(Parent) | .__proto__
2291 * | |
2292 * FunctionTemplate Child -> Child() . prototype -> { }
2293 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002295 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2296 * object of the Child() function has __proto__ pointing to the
2297 * Parent() function's prototype object. An instance of the Child
2298 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002300 * Let Parent be the FunctionTemplate initialized in the previous
2301 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002303 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304 * Local<FunctionTemplate> parent = t;
2305 * Local<FunctionTemplate> child = FunctionTemplate::New();
2306 * child->Inherit(parent);
2307 *
2308 * Local<Function> child_function = child->GetFunction();
2309 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002310 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002312 * The Child function and Child instance will have the following
2313 * properties:
2314 *
2315 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002317 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002319 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002321class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 public:
2323 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002324 static Local<FunctionTemplate> New(
2325 InvocationCallback callback = 0,
2326 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002327 Handle<Signature> signature = Handle<Signature>(),
2328 int length = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329 /** Returns the unique function instance in the current execution context.*/
2330 Local<Function> GetFunction();
2331
v8.team.kasperl727e9952008-09-02 14:56:44 +00002332 /**
2333 * Set the call-handler callback for a FunctionTemplate. This
2334 * callback is called whenever the function created from this
2335 * FunctionTemplate is called.
2336 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337 void SetCallHandler(InvocationCallback callback,
2338 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002340 /** Set the predefined length property for the FunctionTemplate. */
2341 void SetLength(int length);
2342
v8.team.kasperl727e9952008-09-02 14:56:44 +00002343 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 Local<ObjectTemplate> InstanceTemplate();
2345
2346 /** Causes the function template to inherit from a parent function template.*/
2347 void Inherit(Handle<FunctionTemplate> parent);
2348
2349 /**
2350 * A PrototypeTemplate is the template used to create the prototype object
2351 * of the function created by this template.
2352 */
2353 Local<ObjectTemplate> PrototypeTemplate();
2354
v8.team.kasperl727e9952008-09-02 14:56:44 +00002355 /**
2356 * Set the class name of the FunctionTemplate. This is used for
2357 * printing objects created with the function created from the
2358 * FunctionTemplate as its constructor.
2359 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360 void SetClassName(Handle<String> name);
2361
2362 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002363 * Determines whether the __proto__ accessor ignores instances of
2364 * the function template. If instances of the function template are
2365 * ignored, __proto__ skips all instances and instead returns the
2366 * next object in the prototype chain.
2367 *
2368 * Call with a value of true to make the __proto__ accessor ignore
2369 * instances of the function template. Call with a value of false
2370 * to make the __proto__ accessor not ignore instances of the
2371 * function template. By default, instances of a function template
2372 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373 */
2374 void SetHiddenPrototype(bool value);
2375
2376 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002377 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2378 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002379 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002380 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002381
2382 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002383 * Returns true if the given object is an instance of this function
2384 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002385 */
2386 bool HasInstance(Handle<Value> object);
2387
2388 private:
2389 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2391 NamedPropertySetter setter,
2392 NamedPropertyQuery query,
2393 NamedPropertyDeleter remover,
2394 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002395 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2397 IndexedPropertySetter setter,
2398 IndexedPropertyQuery query,
2399 IndexedPropertyDeleter remover,
2400 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002401 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2403 Handle<Value> data);
2404
2405 friend class Context;
2406 friend class ObjectTemplate;
2407};
2408
2409
2410/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002411 * An ObjectTemplate is used to create objects at runtime.
2412 *
2413 * Properties added to an ObjectTemplate are added to each object
2414 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002415 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002416class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002418 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 /** Creates a new instance of this template.*/
2422 Local<Object> NewInstance();
2423
2424 /**
2425 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002426 *
2427 * Whenever the property with the given name is accessed on objects
2428 * created from this ObjectTemplate the getter and setter callbacks
2429 * are called instead of getting and setting the property directly
2430 * on the JavaScript object.
2431 *
2432 * \param name The name of the property for which an accessor is added.
2433 * \param getter The callback to invoke when getting the property.
2434 * \param setter The callback to invoke when setting the property.
2435 * \param data A piece of data that will be passed to the getter and setter
2436 * callbacks whenever they are invoked.
2437 * \param settings Access control settings for the accessor. This is a bit
2438 * field consisting of one of more of
2439 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2440 * The default is to not allow cross-context access.
2441 * ALL_CAN_READ means that all cross-context reads are allowed.
2442 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2443 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2444 * cross-context access.
2445 * \param attribute The attributes of the property for which an accessor
2446 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002447 * \param signature The signature describes valid receivers for the accessor
2448 * and is used to perform implicit instance checks against them. If the
2449 * receiver is incompatible (i.e. is not an instance of the constructor as
2450 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2451 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 */
2453 void SetAccessor(Handle<String> name,
2454 AccessorGetter getter,
2455 AccessorSetter setter = 0,
2456 Handle<Value> data = Handle<Value>(),
2457 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002458 PropertyAttribute attribute = None,
2459 Handle<AccessorSignature> signature =
2460 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461
ulan@chromium.org750145a2013-03-07 15:14:13 +00002462 // This function is not yet stable and should not be used at this time.
2463 bool SetAccessor(Handle<String> name,
2464 Handle<DeclaredAccessorDescriptor> descriptor,
2465 AccessControl settings = DEFAULT,
2466 PropertyAttribute attribute = None,
2467 Handle<AccessorSignature> signature =
2468 Handle<AccessorSignature>());
2469
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470 /**
2471 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002472 *
2473 * Whenever a named property is accessed on objects created from
2474 * this object template, the provided callback is invoked instead of
2475 * accessing the property directly on the JavaScript object.
2476 *
2477 * \param getter The callback to invoke when getting a property.
2478 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002479 * \param query The callback to invoke to check if a property is present,
2480 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002481 * \param deleter The callback to invoke when deleting a property.
2482 * \param enumerator The callback to invoke to enumerate all the named
2483 * properties of an object.
2484 * \param data A piece of data that will be passed to the callbacks
2485 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486 */
2487 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2488 NamedPropertySetter setter = 0,
2489 NamedPropertyQuery query = 0,
2490 NamedPropertyDeleter deleter = 0,
2491 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002492 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002493
2494 /**
2495 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002496 *
2497 * Whenever an indexed property is accessed on objects created from
2498 * this object template, the provided callback is invoked instead of
2499 * accessing the property directly on the JavaScript object.
2500 *
2501 * \param getter The callback to invoke when getting a property.
2502 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002503 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002504 * \param deleter The callback to invoke when deleting a property.
2505 * \param enumerator The callback to invoke to enumerate all the indexed
2506 * properties of an object.
2507 * \param data A piece of data that will be passed to the callbacks
2508 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509 */
2510 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2511 IndexedPropertySetter setter = 0,
2512 IndexedPropertyQuery query = 0,
2513 IndexedPropertyDeleter deleter = 0,
2514 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002515 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002516
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517 /**
2518 * Sets the callback to be used when calling instances created from
2519 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002520 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002521 * function.
2522 */
2523 void SetCallAsFunctionHandler(InvocationCallback callback,
2524 Handle<Value> data = Handle<Value>());
2525
v8.team.kasperl727e9952008-09-02 14:56:44 +00002526 /**
2527 * Mark object instances of the template as undetectable.
2528 *
2529 * In many ways, undetectable objects behave as though they are not
2530 * there. They behave like 'undefined' in conditionals and when
2531 * printed. However, properties can be accessed and called as on
2532 * normal objects.
2533 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534 void MarkAsUndetectable();
2535
v8.team.kasperl727e9952008-09-02 14:56:44 +00002536 /**
2537 * Sets access check callbacks on the object template.
2538 *
2539 * When accessing properties on instances of this object template,
2540 * the access check callback will be called to determine whether or
2541 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002542 * The last parameter specifies whether access checks are turned
2543 * on by default on instances. If access checks are off by default,
2544 * they can be turned on on individual instances by calling
2545 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002546 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2548 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002549 Handle<Value> data = Handle<Value>(),
2550 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002551
kasper.lund212ac232008-07-16 07:07:30 +00002552 /**
2553 * Gets the number of internal fields for objects generated from
2554 * this template.
2555 */
2556 int InternalFieldCount();
2557
2558 /**
2559 * Sets the number of internal fields for objects generated from
2560 * this template.
2561 */
2562 void SetInternalFieldCount(int value);
2563
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564 private:
2565 ObjectTemplate();
2566 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2567 friend class FunctionTemplate;
2568};
2569
2570
2571/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002572 * A Signature specifies which receivers and arguments are valid
2573 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002575class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002576 public:
2577 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2578 Handle<FunctionTemplate>(),
2579 int argc = 0,
2580 Handle<FunctionTemplate> argv[] = 0);
2581 private:
2582 Signature();
2583};
2584
2585
2586/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002587 * An AccessorSignature specifies which receivers are valid parameters
2588 * to an accessor callback.
2589 */
2590class V8EXPORT AccessorSignature : public Data {
2591 public:
2592 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2593 Handle<FunctionTemplate>());
2594 private:
2595 AccessorSignature();
2596};
2597
2598
ulan@chromium.org750145a2013-03-07 15:14:13 +00002599class V8EXPORT DeclaredAccessorDescriptor : public Data {
2600 private:
2601 DeclaredAccessorDescriptor();
2602};
2603
2604
2605class V8EXPORT ObjectOperationDescriptor : public Data {
2606 public:
2607 // This function is not yet stable and should not be used at this time.
2608 static Local<RawOperationDescriptor> NewInternalFieldDereference(
2609 Isolate* isolate,
2610 int internal_field);
2611 private:
2612 ObjectOperationDescriptor();
2613};
2614
2615
2616enum DeclaredAccessorDescriptorDataType {
2617 kDescriptorBoolType,
2618 kDescriptorInt8Type, kDescriptorUint8Type,
2619 kDescriptorInt16Type, kDescriptorUint16Type,
2620 kDescriptorInt32Type, kDescriptorUint32Type,
2621 kDescriptorFloatType, kDescriptorDoubleType
2622};
2623
2624
2625class V8EXPORT RawOperationDescriptor : public Data {
2626 public:
2627 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
2628 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
2629 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
2630 int16_t byte_offset);
2631 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
2632 void* compare_value);
2633 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
2634 Isolate* isolate,
2635 DeclaredAccessorDescriptorDataType data_type,
2636 uint8_t bool_offset = 0);
2637 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
2638 uint8_t bitmask,
2639 uint8_t compare_value);
2640 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
2641 Isolate* isolate,
2642 uint16_t bitmask,
2643 uint16_t compare_value);
2644 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
2645 Isolate* isolate,
2646 uint32_t bitmask,
2647 uint32_t compare_value);
2648
2649 private:
2650 RawOperationDescriptor();
2651};
2652
2653
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002654/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002655 * A utility for determining the type of objects based on the template
2656 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002658class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659 public:
2660 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2661 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2662 int match(Handle<Value> value);
2663 private:
2664 TypeSwitch();
2665};
2666
2667
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002668// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002670class V8EXPORT ExternalAsciiStringResourceImpl
2671 : public String::ExternalAsciiStringResource {
2672 public:
2673 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2674 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2675 : data_(data), length_(length) {}
2676 const char* data() const { return data_; }
2677 size_t length() const { return length_; }
2678
2679 private:
2680 const char* data_;
2681 size_t length_;
2682};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683
2684/**
2685 * Ignore
2686 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002687class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002689 // Note that the strings passed into this constructor must live as long
2690 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002692 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002694 const char** deps = 0,
2695 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696 virtual ~Extension() { }
2697 virtual v8::Handle<v8::FunctionTemplate>
2698 GetNativeFunction(v8::Handle<v8::String> name) {
2699 return v8::Handle<v8::FunctionTemplate>();
2700 }
2701
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002702 const char* name() const { return name_; }
2703 size_t source_length() const { return source_length_; }
2704 const String::ExternalAsciiStringResource* source() const {
2705 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 int dependency_count() { return dep_count_; }
2707 const char** dependencies() { return deps_; }
2708 void set_auto_enable(bool value) { auto_enable_ = value; }
2709 bool auto_enable() { return auto_enable_; }
2710
2711 private:
2712 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002713 size_t source_length_; // expected to initialize before source_
2714 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715 int dep_count_;
2716 const char** deps_;
2717 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002718
2719 // Disallow copying and assigning.
2720 Extension(const Extension&);
2721 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002722};
2723
2724
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002725void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726
2727
2728/**
2729 * Ignore
2730 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002731class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002732 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002733 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002734 RegisterExtension(extension);
2735 }
2736};
2737
2738
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002739// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740
2741
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002742Handle<Primitive> V8EXPORT Undefined();
2743Handle<Primitive> V8EXPORT Null();
2744Handle<Boolean> V8EXPORT True();
2745Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002747V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
2748V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
2749V8_INLINE(Handle<Boolean> True(Isolate* isolate));
2750V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002751
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752
2753/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002754 * A set of constraints that specifies the limits of the runtime's memory use.
2755 * You must set the heap size before initializing the VM - the size cannot be
2756 * adjusted after the VM is initialized.
2757 *
2758 * If you are using threads then you should hold the V8::Locker lock while
2759 * setting the stack limit and you must set a non-default stack limit separately
2760 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002762class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763 public:
2764 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002765 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002766 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002767 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002769 int max_executable_size() { return max_executable_size_; }
2770 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002771 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002772 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002773 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2774 private:
2775 int max_young_space_size_;
2776 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002777 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778 uint32_t* stack_limit_;
2779};
2780
2781
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002782bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783
2784
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002785// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786
2787
2788typedef void (*FatalErrorCallback)(const char* location, const char* message);
2789
2790
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002791typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792
2793
2794/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002795 * Schedules an exception to be thrown when returning to JavaScript. When an
2796 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002798 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002800Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002801
2802/**
2803 * Create new error objects by calling the corresponding error object
2804 * constructor with the message.
2805 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002806class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002807 public:
2808 static Local<Value> RangeError(Handle<String> message);
2809 static Local<Value> ReferenceError(Handle<String> message);
2810 static Local<Value> SyntaxError(Handle<String> message);
2811 static Local<Value> TypeError(Handle<String> message);
2812 static Local<Value> Error(Handle<String> message);
2813};
2814
2815
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002816// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002817
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002818typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002820typedef void* (*CreateHistogramCallback)(const char* name,
2821 int min,
2822 int max,
2823 size_t buckets);
2824
2825typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2826
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002827// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002828 enum ObjectSpace {
2829 kObjectSpaceNewSpace = 1 << 0,
2830 kObjectSpaceOldPointerSpace = 1 << 1,
2831 kObjectSpaceOldDataSpace = 1 << 2,
2832 kObjectSpaceCodeSpace = 1 << 3,
2833 kObjectSpaceMapSpace = 1 << 4,
2834 kObjectSpaceLoSpace = 1 << 5,
2835
2836 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2837 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2838 kObjectSpaceLoSpace
2839 };
2840
2841 enum AllocationAction {
2842 kAllocationActionAllocate = 1 << 0,
2843 kAllocationActionFree = 1 << 1,
2844 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2845 };
2846
2847typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2848 AllocationAction action,
2849 int size);
2850
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002851// --- Leave Script Callback ---
2852typedef void (*CallCompletedCallback)();
2853
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002854// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002855typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2856 AccessType type,
2857 Local<Value> data);
2858
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002859// --- AllowCodeGenerationFromStrings callbacks ---
2860
2861/**
2862 * Callback to check if code generation from strings is allowed. See
2863 * Context::AllowCodeGenerationFromStrings.
2864 */
2865typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2866
2867// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868
2869/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002870 * Applications can register callback functions which will be called
2871 * before and after a garbage collection. Allocations are not
2872 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002873 * objects (set or delete properties for example) since it is possible
2874 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002876enum GCType {
2877 kGCTypeScavenge = 1 << 0,
2878 kGCTypeMarkSweepCompact = 1 << 1,
2879 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2880};
2881
2882enum GCCallbackFlags {
2883 kNoGCCallbackFlags = 0,
2884 kGCCallbackFlagCompacted = 1 << 0
2885};
2886
2887typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2888typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002890typedef void (*GCCallback)();
2891
2892
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002893/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002894 * Collection of V8 heap information.
2895 *
2896 * Instances of this class can be passed to v8::V8::HeapStatistics to
2897 * get heap statistics from V8.
2898 */
2899class V8EXPORT HeapStatistics {
2900 public:
2901 HeapStatistics();
2902 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002903 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00002904 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002905 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002906 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002907
2908 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00002909 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002910 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00002911 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002912 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002913 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002914
2915 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002916 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00002917};
2918
2919
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002920class RetainedObjectInfo;
2921
ager@chromium.org3811b432009-10-28 14:53:37 +00002922/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002923 * Isolate represents an isolated instance of the V8 engine. V8
2924 * isolates have completely separate states. Objects from one isolate
2925 * must not be used in other isolates. When V8 is initialized a
2926 * default isolate is implicitly created and entered. The embedder
2927 * can create additional isolates and use them in parallel in multiple
2928 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002929 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002930 */
2931class V8EXPORT Isolate {
2932 public:
2933 /**
2934 * Stack-allocated class which sets the isolate for all operations
2935 * executed within a local scope.
2936 */
2937 class V8EXPORT Scope {
2938 public:
2939 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2940 isolate->Enter();
2941 }
2942
2943 ~Scope() { isolate_->Exit(); }
2944
2945 private:
2946 Isolate* const isolate_;
2947
2948 // Prevent copying of Scope objects.
2949 Scope(const Scope&);
2950 Scope& operator=(const Scope&);
2951 };
2952
2953 /**
2954 * Creates a new isolate. Does not change the currently entered
2955 * isolate.
2956 *
2957 * When an isolate is no longer used its resources should be freed
2958 * by calling Dispose(). Using the delete operator is not allowed.
2959 */
2960 static Isolate* New();
2961
2962 /**
2963 * Returns the entered isolate for the current thread or NULL in
2964 * case there is no current isolate.
2965 */
2966 static Isolate* GetCurrent();
2967
2968 /**
2969 * Methods below this point require holding a lock (using Locker) in
2970 * a multi-threaded environment.
2971 */
2972
2973 /**
2974 * Sets this isolate as the entered one for the current thread.
2975 * Saves the previously entered one (if any), so that it can be
2976 * restored when exiting. Re-entering an isolate is allowed.
2977 */
2978 void Enter();
2979
2980 /**
2981 * Exits this isolate by restoring the previously entered one in the
2982 * current thread. The isolate may still stay the same, if it was
2983 * entered more than once.
2984 *
2985 * Requires: this == Isolate::GetCurrent().
2986 */
2987 void Exit();
2988
2989 /**
2990 * Disposes the isolate. The isolate must not be entered by any
2991 * thread to be disposable.
2992 */
2993 void Dispose();
2994
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002995 /**
2996 * Associate embedder-specific data with the isolate
2997 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002998 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002999
3000 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003001 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003002 * Returns NULL if SetData has never been called.
3003 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003004 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003005
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003006 /**
3007 * Get statistics about the heap memory usage.
3008 */
3009 void GetHeapStatistics(HeapStatistics* heap_statistics);
3010
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003011 /**
3012 * Adjusts the amount of registered external memory. Used to give V8 an
3013 * indication of the amount of externally allocated memory that is kept alive
3014 * by JavaScript objects. V8 uses this to decide when to perform global
3015 * garbage collections. Registering externally allocated memory will trigger
3016 * global garbage collections more often than it would otherwise in an attempt
3017 * to garbage collect the JavaScript objects that keep the externally
3018 * allocated memory alive.
3019 *
3020 * \param change_in_bytes the change in externally allocated memory that is
3021 * kept alive by JavaScript objects.
3022 * \returns the adjusted value.
3023 */
3024 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3025
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003026 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003027 Isolate();
3028 Isolate(const Isolate&);
3029 ~Isolate();
3030 Isolate& operator=(const Isolate&);
3031 void* operator new(size_t size);
3032 void operator delete(void*, size_t);
3033};
3034
3035
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003036class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003037 public:
3038 enum CompressionAlgorithm {
3039 kUncompressed,
3040 kBZip2
3041 };
3042
3043 const char* data;
3044 int compressed_size;
3045 int raw_size;
3046};
3047
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003048
3049/**
3050 * A helper class for driving V8 startup data decompression. It is based on
3051 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3052 * for an embedder to use this class, instead, API functions can be used
3053 * directly.
3054 *
3055 * For an example of the class usage, see the "shell.cc" sample application.
3056 */
3057class V8EXPORT StartupDataDecompressor { // NOLINT
3058 public:
3059 StartupDataDecompressor();
3060 virtual ~StartupDataDecompressor();
3061 int Decompress();
3062
3063 protected:
3064 virtual int DecompressData(char* raw_data,
3065 int* raw_data_size,
3066 const char* compressed_data,
3067 int compressed_data_size) = 0;
3068
3069 private:
3070 char** raw_data;
3071};
3072
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003073
3074/**
3075 * EntropySource is used as a callback function when v8 needs a source
3076 * of entropy.
3077 */
3078typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3079
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003080
3081/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003082 * ReturnAddressLocationResolver is used as a callback function when v8 is
3083 * resolving the location of a return address on the stack. Profilers that
3084 * change the return address on the stack can use this to resolve the stack
3085 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003086 *
3087 * \param return_addr_location points to a location on stack where a machine
3088 * return address resides.
3089 * \returns either return_addr_location, or else a pointer to the profiler's
3090 * copy of the original return address.
3091 *
3092 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003093 */
3094typedef uintptr_t (*ReturnAddressLocationResolver)(
3095 uintptr_t return_addr_location);
3096
3097
3098/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003099 * FunctionEntryHook is the type of the profile entry hook called at entry to
3100 * any generated function when function-level profiling is enabled.
3101 *
3102 * \param function the address of the function that's being entered.
3103 * \param return_addr_location points to a location on stack where the machine
3104 * return address resides. This can be used to identify the caller of
3105 * \p function, and/or modified to divert execution when \p function exits.
3106 *
3107 * \note the entry hook must not cause garbage collection.
3108 */
3109typedef void (*FunctionEntryHook)(uintptr_t function,
3110 uintptr_t return_addr_location);
3111
3112
3113/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003114 * A JIT code event is issued each time code is added, moved or removed.
3115 *
3116 * \note removal events are not currently issued.
3117 */
3118struct JitCodeEvent {
3119 enum EventType {
3120 CODE_ADDED,
3121 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003122 CODE_REMOVED,
3123 CODE_ADD_LINE_POS_INFO,
3124 CODE_START_LINE_INFO_RECORDING,
3125 CODE_END_LINE_INFO_RECORDING
3126 };
3127 // Definition of the code position type. The "POSITION" type means the place
3128 // in the source code which are of interest when making stack traces to
3129 // pin-point the source location of a stack frame as close as possible.
3130 // The "STATEMENT_POSITION" means the place at the beginning of each
3131 // statement, and is used to indicate possible break locations.
3132 enum PositionType {
3133 POSITION,
3134 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003135 };
3136
3137 // Type of event.
3138 EventType type;
3139 // Start of the instructions.
3140 void* code_start;
3141 // Size of the instructions.
3142 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003143 // Script info for CODE_ADDED event.
3144 Handle<Script> script;
3145 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
3146 // code line information which is returned from the
3147 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
3148 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
3149 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003150
3151 union {
3152 // Only valid for CODE_ADDED.
3153 struct {
3154 // Name of the object associated with the code, note that the string is
3155 // not zero-terminated.
3156 const char* str;
3157 // Number of chars in str.
3158 size_t len;
3159 } name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003160
3161 // Only valid for CODE_ADD_LINE_POS_INFO
3162 struct {
3163 // PC offset
3164 size_t offset;
3165 // Code postion
3166 size_t pos;
3167 // The position type.
3168 PositionType position_type;
3169 } line_info;
3170
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003171 // New location of instructions. Only valid for CODE_MOVED.
3172 void* new_code_start;
3173 };
3174};
3175
3176/**
3177 * Option flags passed to the SetJitCodeEventHandler function.
3178 */
3179enum JitCodeEventOptions {
3180 kJitCodeEventDefault = 0,
3181 // Generate callbacks for already existent code.
3182 kJitCodeEventEnumExisting = 1
3183};
3184
3185
3186/**
3187 * Callback function passed to SetJitCodeEventHandler.
3188 *
3189 * \param event code add, move or removal event.
3190 */
3191typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3192
3193
3194/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003195 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003196 */
3197class V8EXPORT ExternalResourceVisitor { // NOLINT
3198 public:
3199 virtual ~ExternalResourceVisitor() {}
3200 virtual void VisitExternalString(Handle<String> string) {}
3201};
3202
3203
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003204/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003205 * Interface for iterating through all the persistent handles in the heap.
3206 */
3207class V8EXPORT PersistentHandleVisitor { // NOLINT
3208 public:
3209 virtual ~PersistentHandleVisitor() {}
3210 virtual void VisitPersistentHandle(Persistent<Value> value,
3211 uint16_t class_id) {}
3212};
3213
3214
3215/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003216 * Container class for static utility functions.
3217 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003218class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003219 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003220 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003221 static void SetFatalErrorHandler(FatalErrorCallback that);
3222
v8.team.kasperl727e9952008-09-02 14:56:44 +00003223 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003224 * Set the callback to invoke to check if code generation from
3225 * strings should be allowed.
3226 */
3227 static void SetAllowCodeGenerationFromStringsCallback(
3228 AllowCodeGenerationFromStringsCallback that);
3229
3230 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003231 * Ignore out-of-memory exceptions.
3232 *
3233 * V8 running out of memory is treated as a fatal error by default.
3234 * This means that the fatal error handler is called and that V8 is
3235 * terminated.
3236 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003237 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003238 * out-of-memory situation as a fatal error. This way, the contexts
3239 * that did not cause the out of memory problem might be able to
3240 * continue execution.
3241 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003242 static void IgnoreOutOfMemoryException();
3243
v8.team.kasperl727e9952008-09-02 14:56:44 +00003244 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003245 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003246 * fatal errors such as out-of-memory situations.
3247 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003248 static bool IsDead();
3249
3250 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003251 * The following 4 functions are to be used when V8 is built with
3252 * the 'compress_startup_data' flag enabled. In this case, the
3253 * embedder must decompress startup data prior to initializing V8.
3254 *
3255 * This is how interaction with V8 should look like:
3256 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3257 * v8::StartupData* compressed_data =
3258 * new v8::StartupData[compressed_data_count];
3259 * v8::V8::GetCompressedStartupData(compressed_data);
3260 * ... decompress data (compressed_data can be updated in-place) ...
3261 * v8::V8::SetDecompressedStartupData(compressed_data);
3262 * ... now V8 can be initialized
3263 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003264 *
3265 * A helper class StartupDataDecompressor is provided. It implements
3266 * the protocol of the interaction described above, and can be used in
3267 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003268 */
3269 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3270 static int GetCompressedStartupDataCount();
3271 static void GetCompressedStartupData(StartupData* compressed_data);
3272 static void SetDecompressedStartupData(StartupData* decompressed_data);
3273
3274 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003275 * Adds a message listener.
3276 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003277 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003278 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003279 *
3280 * If data is specified, it will be passed to the callback when it is called.
3281 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003282 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003283 static bool AddMessageListener(MessageCallback that,
3284 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003285
3286 /**
3287 * Remove all message listeners from the specified callback function.
3288 */
3289 static void RemoveMessageListeners(MessageCallback that);
3290
3291 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003292 * Tells V8 to capture current stack trace when uncaught exception occurs
3293 * and report it to the message listeners. The option is off by default.
3294 */
3295 static void SetCaptureStackTraceForUncaughtExceptions(
3296 bool capture,
3297 int frame_limit = 10,
3298 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3299
3300 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003301 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003302 */
3303 static void SetFlagsFromString(const char* str, int length);
3304
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003305 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003306 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003307 */
3308 static void SetFlagsFromCommandLine(int* argc,
3309 char** argv,
3310 bool remove_flags);
3311
kasper.lund7276f142008-07-30 08:49:36 +00003312 /** Get the version string. */
3313 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003314
3315 /**
3316 * Enables the host application to provide a mechanism for recording
3317 * statistics counters.
3318 */
3319 static void SetCounterFunction(CounterLookupCallback);
3320
3321 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003322 * Enables the host application to provide a mechanism for recording
3323 * histograms. The CreateHistogram function returns a
3324 * histogram which will later be passed to the AddHistogramSample
3325 * function.
3326 */
3327 static void SetCreateHistogramFunction(CreateHistogramCallback);
3328 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3329
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003330 /** Callback function for reporting failed access checks.*/
3331 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3332
3333 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003334 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003335 * garbage collection. Allocations are not allowed in the
3336 * callback function, you therefore cannot manipulate objects (set
3337 * or delete properties for example) since it is possible such
3338 * operations will result in the allocation of objects. It is possible
3339 * to specify the GCType filter for your callback. But it is not possible to
3340 * register the same callback function two times with different
3341 * GCType filters.
3342 */
3343 static void AddGCPrologueCallback(
3344 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3345
3346 /**
3347 * This function removes callback which was installed by
3348 * AddGCPrologueCallback function.
3349 */
3350 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3351
3352 /**
3353 * The function is deprecated. Please use AddGCPrologueCallback instead.
3354 * Enables the host application to receive a notification before a
3355 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003356 * callback function, you therefore cannot manipulate objects (set
3357 * or delete properties for example) since it is possible such
3358 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003360 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003361
3362 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003363 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003364 * garbage collection. Allocations are not allowed in the
3365 * callback function, you therefore cannot manipulate objects (set
3366 * or delete properties for example) since it is possible such
3367 * operations will result in the allocation of objects. It is possible
3368 * to specify the GCType filter for your callback. But it is not possible to
3369 * register the same callback function two times with different
3370 * GCType filters.
3371 */
3372 static void AddGCEpilogueCallback(
3373 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3374
3375 /**
3376 * This function removes callback which was installed by
3377 * AddGCEpilogueCallback function.
3378 */
3379 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3380
3381 /**
3382 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3383 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003384 * major garbage collection. Allocations are not allowed in the
3385 * callback function, you therefore cannot manipulate objects (set
3386 * or delete properties for example) since it is possible such
3387 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003388 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003389 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003390
3391 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003392 * Enables the host application to provide a mechanism to be notified
3393 * and perform custom logging when V8 Allocates Executable Memory.
3394 */
3395 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3396 ObjectSpace space,
3397 AllocationAction action);
3398
3399 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003400 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003401 */
3402 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3403
3404 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003405 * Adds a callback to notify the host application when a script finished
3406 * running. If a script re-enters the runtime during executing, the
3407 * CallCompletedCallback is only invoked when the outer-most script
3408 * execution ends. Executing scripts inside the callback do not trigger
3409 * further callbacks.
3410 */
3411 static void AddCallCompletedCallback(CallCompletedCallback callback);
3412
3413 /**
3414 * Removes callback that was installed by AddCallCompletedCallback.
3415 */
3416 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3417
3418 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003419 * Allows the host application to group objects together. If one
3420 * object in the group is alive, all objects in the group are alive.
3421 * After each garbage collection, object groups are removed. It is
3422 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003423 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003424 * wrapper objects. Object groups for all dependent handles need to
3425 * be provided for kGCTypeMarkSweepCompact collections, for all other
3426 * garbage collection types it is sufficient to provide object groups
3427 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003428 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003429 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003430 static void AddObjectGroup(Persistent<Value>* objects,
3431 size_t length,
3432 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003433 static void AddObjectGroup(Isolate* isolate,
3434 Persistent<Value>* objects,
3435 size_t length,
3436 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003437
3438 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003439 * Allows the host application to declare implicit references between
3440 * the objects: if |parent| is alive, all |children| are alive too.
3441 * After each garbage collection, all implicit references
3442 * are removed. It is intended to be used in the before-garbage-collection
3443 * callback function.
3444 */
3445 static void AddImplicitReferences(Persistent<Object> parent,
3446 Persistent<Value>* children,
3447 size_t length);
3448
3449 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003450 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003451 * initialize from scratch. This function is called implicitly if
3452 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003453 */
3454 static bool Initialize();
3455
kasper.lund7276f142008-07-30 08:49:36 +00003456 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003457 * Allows the host application to provide a callback which can be used
3458 * as a source of entropy for random number generators.
3459 */
3460 static void SetEntropySource(EntropySource source);
3461
3462 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003463 * Allows the host application to provide a callback that allows v8 to
3464 * cooperate with a profiler that rewrites return addresses on stack.
3465 */
3466 static void SetReturnAddressLocationResolver(
3467 ReturnAddressLocationResolver return_address_resolver);
3468
3469 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003470 * Allows the host application to provide the address of a function that's
3471 * invoked on entry to every V8-generated function.
3472 * Note that \p entry_hook is invoked at the very start of each
3473 * generated function.
3474 *
3475 * \param entry_hook a function that will be invoked on entry to every
3476 * V8-generated function.
3477 * \returns true on success on supported platforms, false on failure.
3478 * \note Setting a new entry hook function when one is already active will
3479 * fail.
3480 */
3481 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3482
3483 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003484 * Allows the host application to provide the address of a function that is
3485 * notified each time code is added, moved or removed.
3486 *
3487 * \param options options for the JIT code event handler.
3488 * \param event_handler the JIT code event handler, which will be invoked
3489 * each time code is added, moved or removed.
3490 * \note \p event_handler won't get notified of existent code.
3491 * \note since code removal notifications are not currently issued, the
3492 * \p event_handler may get notifications of code that overlaps earlier
3493 * code notifications. This happens when code areas are reused, and the
3494 * earlier overlapping code areas should therefore be discarded.
3495 * \note the events passed to \p event_handler and the strings they point to
3496 * are not guaranteed to live past each call. The \p event_handler must
3497 * copy strings and other parameters it needs to keep around.
3498 * \note the set of events declared in JitCodeEvent::EventType is expected to
3499 * grow over time, and the JitCodeEvent structure is expected to accrue
3500 * new members. The \p event_handler function must ignore event codes
3501 * it does not recognize to maintain future compatibility.
3502 */
3503 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3504 JitCodeEventHandler event_handler);
3505
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003506 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
3507 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003508 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3509 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003510
iposva@chromium.org245aa852009-02-10 00:49:54 +00003511 /**
3512 * Suspends recording of tick samples in the profiler.
3513 * When the V8 profiling mode is enabled (usually via command line
3514 * switches) this function suspends recording of tick samples.
3515 * Profiling ticks are discarded until ResumeProfiler() is called.
3516 *
3517 * See also the --prof and --prof_auto command line switches to
3518 * enable V8 profiling.
3519 */
3520 static void PauseProfiler();
3521
3522 /**
3523 * Resumes recording of tick samples in the profiler.
3524 * See also PauseProfiler().
3525 */
3526 static void ResumeProfiler();
3527
ager@chromium.org41826e72009-03-30 13:30:57 +00003528 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003529 * Return whether profiler is currently paused.
3530 */
3531 static bool IsProfilerPaused();
3532
3533 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003534 * Retrieve the V8 thread id of the calling thread.
3535 *
3536 * The thread id for a thread should only be retrieved after the V8
3537 * lock has been acquired with a Locker object with that thread.
3538 */
3539 static int GetCurrentThreadId();
3540
3541 /**
3542 * Forcefully terminate execution of a JavaScript thread. This can
3543 * be used to terminate long-running scripts.
3544 *
3545 * TerminateExecution should only be called when then V8 lock has
3546 * been acquired with a Locker object. Therefore, in order to be
3547 * able to terminate long-running threads, preemption must be
3548 * enabled to allow the user of TerminateExecution to acquire the
3549 * lock.
3550 *
3551 * The termination is achieved by throwing an exception that is
3552 * uncatchable by JavaScript exception handlers. Termination
3553 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003554 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003555 * exception handler that catches an exception should check if that
3556 * exception is a termination exception and immediately return if
3557 * that is the case. Returning immediately in that case will
3558 * continue the propagation of the termination exception if needed.
3559 *
3560 * The thread id passed to TerminateExecution must have been
3561 * obtained by calling GetCurrentThreadId on the thread in question.
3562 *
3563 * \param thread_id The thread id of the thread to terminate.
3564 */
3565 static void TerminateExecution(int thread_id);
3566
3567 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003568 * Forcefully terminate the current thread of JavaScript execution
3569 * in the given isolate. If no isolate is provided, the default
3570 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003571 *
3572 * This method can be used by any thread even if that thread has not
3573 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003574 *
3575 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003576 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003577 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003578
3579 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003580 * Is V8 terminating JavaScript execution.
3581 *
3582 * Returns true if JavaScript execution is currently terminating
3583 * because of a call to TerminateExecution. In that case there are
3584 * still JavaScript frames on the stack and the termination
3585 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003586 *
3587 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003588 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003589 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003590
3591 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003592 * Releases any resources used by v8 and stops any utility threads
3593 * that may be running. Note that disposing v8 is permanent, it
3594 * cannot be reinitialized.
3595 *
3596 * It should generally not be necessary to dispose v8 before exiting
3597 * a process, this should happen automatically. It is only necessary
3598 * to use if the process needs the resources taken up by v8.
3599 */
3600 static bool Dispose();
3601
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003602 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
3603 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003604
3605 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003606 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003607 * heap. GC is not invoked prior to iterating, therefore there is no
3608 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003609 */
3610 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3611
3612 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003613 * Iterates through all the persistent handles in the current isolate's heap
3614 * that have class_ids.
3615 */
3616 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3617
3618 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003619 * Iterates through all the persistent handles in the current isolate's heap
3620 * that have class_ids and are candidates to be marked as partially dependent
3621 * handles. This will visit handles to young objects created since the last
3622 * garbage collection but is free to visit an arbitrary superset of these
3623 * objects.
3624 */
3625 static void VisitHandlesForPartialDependence(
3626 Isolate* isolate, PersistentHandleVisitor* visitor);
3627
3628 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003629 * Optional notification that the embedder is idle.
3630 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003631 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003632 * Returns true if the embedder should stop calling IdleNotification
3633 * until real work has been done. This indicates that V8 has done
3634 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003635 *
3636 * The hint argument specifies the amount of work to be done in the function
3637 * on scale from 1 to 1000. There is no guarantee that the actual work will
3638 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003639 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003640 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003641
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003642 /**
3643 * Optional notification that the system is running low on memory.
3644 * V8 uses these notifications to attempt to free memory.
3645 */
3646 static void LowMemoryNotification();
3647
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003648 /**
3649 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003650 * these notifications to guide the GC heuristic. Returns the number
3651 * of context disposals - including this one - since the last time
3652 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003653 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003654 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003655
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003656 private:
3657 V8();
3658
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003659 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
3660 internal::Object** handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003661 static void DisposeGlobal(internal::Isolate* isolate,
3662 internal::Object** global_handle);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003663 static void MakeWeak(internal::Isolate* isolate,
3664 internal::Object** global_handle,
3665 void* data,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003666 WeakReferenceCallback weak_reference_callback,
3667 NearDeathCallback near_death_callback);
3668 static void ClearWeak(internal::Isolate* isolate,
3669 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003670
3671 template <class T> friend class Handle;
3672 template <class T> friend class Local;
3673 template <class T> friend class Persistent;
3674 friend class Context;
3675};
3676
3677
3678/**
3679 * An external exception handler.
3680 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003681class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003682 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003683 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003684 * Creates a new try/catch block and registers it with v8. Note that
3685 * all TryCatch blocks should be stack allocated because the memory
3686 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003687 */
3688 TryCatch();
3689
3690 /**
3691 * Unregisters and deletes this try/catch block.
3692 */
3693 ~TryCatch();
3694
3695 /**
3696 * Returns true if an exception has been caught by this try/catch block.
3697 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003698 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003699
3700 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003701 * For certain types of exceptions, it makes no sense to continue
3702 * execution.
3703 *
3704 * Currently, the only type of exception that can be caught by a
3705 * TryCatch handler and for which it does not make sense to continue
3706 * is termination exception. Such exceptions are thrown when the
3707 * TerminateExecution methods are called to terminate a long-running
3708 * script.
3709 *
3710 * If CanContinue returns false, the correct action is to perform
3711 * any C++ cleanup needed and then return.
3712 */
3713 bool CanContinue() const;
3714
3715 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003716 * Throws the exception caught by this TryCatch in a way that avoids
3717 * it being caught again by this same TryCatch. As with ThrowException
3718 * it is illegal to execute any JavaScript operations after calling
3719 * ReThrow; the caller must return immediately to where the exception
3720 * is caught.
3721 */
3722 Handle<Value> ReThrow();
3723
3724 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003725 * Returns the exception caught by this try/catch block. If no exception has
3726 * been caught an empty handle is returned.
3727 *
3728 * The returned handle is valid until this TryCatch block has been destroyed.
3729 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003730 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003731
3732 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003733 * Returns the .stack property of the thrown object. If no .stack
3734 * property is present an empty handle is returned.
3735 */
3736 Local<Value> StackTrace() const;
3737
3738 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003739 * Returns the message associated with this exception. If there is
3740 * no message associated an empty handle is returned.
3741 *
3742 * The returned handle is valid until this TryCatch block has been
3743 * destroyed.
3744 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003745 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003746
3747 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003748 * Clears any exceptions that may have been caught by this try/catch block.
3749 * After this method has been called, HasCaught() will return false.
3750 *
3751 * It is not necessary to clear a try/catch block before using it again; if
3752 * another exception is thrown the previously caught exception will just be
3753 * overwritten. However, it is often a good idea since it makes it easier
3754 * to determine which operation threw a given exception.
3755 */
3756 void Reset();
3757
v8.team.kasperl727e9952008-09-02 14:56:44 +00003758 /**
3759 * Set verbosity of the external exception handler.
3760 *
3761 * By default, exceptions that are caught by an external exception
3762 * handler are not reported. Call SetVerbose with true on an
3763 * external exception handler to have exceptions caught by the
3764 * handler reported as if they were not caught.
3765 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766 void SetVerbose(bool value);
3767
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003768 /**
3769 * Set whether or not this TryCatch should capture a Message object
3770 * which holds source information about where the exception
3771 * occurred. True by default.
3772 */
3773 void SetCaptureMessage(bool value);
3774
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003775 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003776 // Make it hard to create heap-allocated TryCatch blocks.
3777 TryCatch(const TryCatch&);
3778 void operator=(const TryCatch&);
3779 void* operator new(size_t size);
3780 void operator delete(void*, size_t);
3781
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003782 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003783 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003785 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003786 bool is_verbose_ : 1;
3787 bool can_continue_ : 1;
3788 bool capture_message_ : 1;
3789 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003790
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003791 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792};
3793
3794
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003795// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003796
3797
3798/**
3799 * Ignore
3800 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003801class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003802 public:
3803 ExtensionConfiguration(int name_count, const char* names[])
3804 : name_count_(name_count), names_(names) { }
3805 private:
3806 friend class ImplementationUtilities;
3807 int name_count_;
3808 const char** names_;
3809};
3810
3811
3812/**
3813 * A sandboxed execution context with its own set of built-in objects
3814 * and functions.
3815 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003816class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003817 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003818 /**
3819 * Returns the global proxy object or global object itself for
3820 * detached contexts.
3821 *
3822 * Global proxy object is a thin wrapper whose prototype points to
3823 * actual context's global object with the properties like Object, etc.
3824 * This is done that way for security reasons (for more details see
3825 * https://wiki.mozilla.org/Gecko:SplitWindow).
3826 *
3827 * Please note that changes to global proxy object prototype most probably
3828 * would break VM---v8 expects only global object as a prototype of
3829 * global proxy object.
3830 *
3831 * If DetachGlobal() has been invoked, Global() would return actual global
3832 * object until global is reattached with ReattachGlobal().
3833 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834 Local<Object> Global();
3835
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003836 /**
3837 * Detaches the global object from its context before
3838 * the global object can be reused to create a new context.
3839 */
3840 void DetachGlobal();
3841
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003842 /**
3843 * Reattaches a global object to a context. This can be used to
3844 * restore the connection between a global object and a context
3845 * after DetachGlobal has been called.
3846 *
3847 * \param global_object The global object to reattach to the
3848 * context. For this to work, the global object must be the global
3849 * object that was associated with this context before a call to
3850 * DetachGlobal.
3851 */
3852 void ReattachGlobal(Handle<Object> global_object);
3853
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003854 /** Creates a new context.
3855 *
3856 * Returns a persistent handle to the newly allocated context. This
3857 * persistent handle has to be disposed when the context is no
3858 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003859 *
3860 * \param extensions An optional extension configuration containing
3861 * the extensions to be installed in the newly created context.
3862 *
3863 * \param global_template An optional object template from which the
3864 * global object for the newly created context will be created.
3865 *
3866 * \param global_object An optional global object to be reused for
3867 * the newly created context. This global object must have been
3868 * created by a previous call to Context::New with the same global
3869 * template. The state of the global object will be completely reset
3870 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003871 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003872 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003873 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003874 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3875 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003876
kasper.lund44510672008-07-25 07:37:58 +00003877 /** Returns the last entered context. */
3878 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003879
kasper.lund44510672008-07-25 07:37:58 +00003880 /** Returns the context that is on the top of the stack. */
3881 static Local<Context> GetCurrent();
3882
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003883 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003884 * Returns the context of the calling JavaScript code. That is the
3885 * context of the top-most JavaScript frame. If there are no
3886 * JavaScript frames an empty handle is returned.
3887 */
3888 static Local<Context> GetCalling();
3889
3890 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003891 * Sets the security token for the context. To access an object in
3892 * another context, the security tokens must match.
3893 */
3894 void SetSecurityToken(Handle<Value> token);
3895
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003896 /** Restores the security token to the default value. */
3897 void UseDefaultSecurityToken();
3898
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003899 /** Returns the security token of this context.*/
3900 Handle<Value> GetSecurityToken();
3901
v8.team.kasperl727e9952008-09-02 14:56:44 +00003902 /**
3903 * Enter this context. After entering a context, all code compiled
3904 * and run is compiled and run in this context. If another context
3905 * is already entered, this old context is saved so it can be
3906 * restored when the new context is exited.
3907 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003908 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003909
3910 /**
3911 * Exit this context. Exiting the current context restores the
3912 * context that was in place when entering the current context.
3913 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003914 void Exit();
3915
v8.team.kasperl727e9952008-09-02 14:56:44 +00003916 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917 bool HasOutOfMemoryException();
3918
v8.team.kasperl727e9952008-09-02 14:56:44 +00003919 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920 static bool InContext();
3921
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003922 /** Returns an isolate associated with a current context. */
3923 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00003924
3925 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003926 * Gets the embedder data with the given index, which must have been set by a
3927 * previous call to SetEmbedderData with the same index. Note that index 0
3928 * currently has a special meaning for Chrome's debugger.
3929 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003930 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003931
3932 /**
3933 * Sets the embedder data with the given index, growing the data as
3934 * needed. Note that index 0 currently has a special meaning for Chrome's
3935 * debugger.
3936 */
3937 void SetEmbedderData(int index, Handle<Value> value);
3938
3939 /**
3940 * Gets a 2-byte-aligned native pointer from the embedder data with the given
3941 * index, which must have bees set by a previous call to
3942 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
3943 * currently has a special meaning for Chrome's debugger.
3944 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003945 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003946
3947 /**
3948 * Sets a 2-byte-aligned native pointer in the embedder data with the given
3949 * index, growing the data as needed. Note that index 0 currently has a
3950 * special meaning for Chrome's debugger.
3951 */
3952 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00003953
3954 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003955 * Control whether code generation from strings is allowed. Calling
3956 * this method with false will disable 'eval' and the 'Function'
3957 * constructor for code running in this context. If 'eval' or the
3958 * 'Function' constructor are used an exception will be thrown.
3959 *
3960 * If code generation from strings is not allowed the
3961 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3962 * set before blocking the call to 'eval' or the 'Function'
3963 * constructor. If that callback returns true, the call will be
3964 * allowed, otherwise an exception will be thrown. If no callback is
3965 * set an exception will be thrown.
3966 */
3967 void AllowCodeGenerationFromStrings(bool allow);
3968
3969 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003970 * Returns true if code generation from strings is allowed for the context.
3971 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3972 */
3973 bool IsCodeGenerationFromStringsAllowed();
3974
3975 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003976 * Sets the error description for the exception that is thrown when
3977 * code generation from strings is not allowed and 'eval' or the 'Function'
3978 * constructor are called.
3979 */
3980 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3981
3982 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003983 * Stack-allocated class which sets the execution context for all
3984 * operations executed within a local scope.
3985 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003986 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003987 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003988 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003989 context_->Enter();
3990 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003991 V8_INLINE(~Scope()) { context_->Exit(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003992 private:
3993 Handle<Context> context_;
3994 };
3995
3996 private:
3997 friend class Value;
3998 friend class Script;
3999 friend class Object;
4000 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004001
4002 Local<Value> SlowGetEmbedderData(int index);
4003 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004004};
4005
4006
4007/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004008 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4009 * to use any given V8 isolate, see the comments in the Isolate class. The
4010 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4011 * object pointers obtained from V8 handles while in the particular V8 isolate.
4012 * It is up to the user of V8 to ensure, perhaps with locking, that this
4013 * constraint is not violated. In addition to any other synchronization
4014 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4015 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004016 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004017 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4018 * construction and destruction, the current thread is allowed to use the locked
4019 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4020 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004021 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004022 * Sample usage:
4023* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 * ...
4025 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004026 * v8::Locker locker(isolate);
4027 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004028 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004029 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004030 * ...
4031 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00004032 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004034 * If you wish to stop using V8 in a thread A you can do this either by
4035 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
4036 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004037 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004038 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004039 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004040 * isolate->Exit();
4041 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004042 * ...
4043 * // Code not using V8 goes here while V8 can run in another thread.
4044 * ...
4045 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004046 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004047 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004048 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004049 * The Unlocker object is intended for use in a long-running callback from V8,
4050 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004051 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004052 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
4053 * given thread. This can be useful if you have code that can be called either
4054 * from code that holds the lock or from code that does not. The Unlocker is
4055 * not recursive so you can not have several Unlockers on the stack at once, and
4056 * 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 +00004057 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004058 * An unlocker will unlock several lockers if it has to and reinstate the
4059 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004060 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004061 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004062 * // V8 not locked.
4063 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004064 * v8::Locker locker(isolate);
4065 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004066 * // V8 locked.
4067 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004068 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004069 * // V8 still locked (2 levels).
4070 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004071 * isolate->Exit();
4072 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004073 * // V8 not locked.
4074 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004075 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004076 * // V8 locked again (2 levels).
4077 * }
4078 * // V8 still locked (1 level).
4079 * }
4080 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004081 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004082 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004083class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004085 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004086 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004087 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004088 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
4089
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004090 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004091 V8_DEPRECATED(Unlocker());
4092
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004093 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00004094 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004095 void Initialize(Isolate* isolate);
4096
lrn@chromium.org1c092762011-05-09 09:42:16 +00004097 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004098};
4099
4100
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004101class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004102 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004103 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004104 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004105 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004106 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
4107
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004108 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004109 V8_DEPRECATED(Locker());
4110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004111 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004112
4113 /**
4114 * Start preemption.
4115 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004116 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004117 * that will switch between multiple threads that are in contention
4118 * for the V8 lock.
4119 */
4120 static void StartPreemption(int every_n_ms);
4121
4122 /**
4123 * Stop preemption.
4124 */
4125 static void StopPreemption();
4126
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004127 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004128 * Returns whether or not the locker for a given isolate, is locked by the
4129 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004130 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004131 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004132
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004133 /**
4134 * Returns whether v8::Locker is being used by this V8 instance.
4135 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004136 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004138 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004139 void Initialize(Isolate* isolate);
4140
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004141 bool has_lock_;
4142 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004143 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004144
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004145 static bool active_;
4146
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004147 // Disallow copying and assigning.
4148 Locker(const Locker&);
4149 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004150};
4151
4152
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004153/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004154 * A struct for exporting HeapStats data from V8, using "push" model.
4155 */
4156struct HeapStatsUpdate;
4157
4158
4159/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004160 * An interface for exporting data from V8, using "push" model.
4161 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004162class V8EXPORT OutputStream { // NOLINT
4163 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004164 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004165 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004166 };
4167 enum WriteResult {
4168 kContinue = 0,
4169 kAbort = 1
4170 };
4171 virtual ~OutputStream() {}
4172 /** Notify about the end of stream. */
4173 virtual void EndOfStream() = 0;
4174 /** Get preferred output chunk size. Called only once. */
4175 virtual int GetChunkSize() { return 1024; }
4176 /** Get preferred output encoding. Called only once. */
4177 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4178 /**
4179 * Writes the next chunk of snapshot data into the stream. Writing
4180 * can be stopped by returning kAbort as function result. EndOfStream
4181 * will not be called in case writing was aborted.
4182 */
4183 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004184 /**
4185 * Writes the next chunk of heap stats data into the stream. Writing
4186 * can be stopped by returning kAbort as function result. EndOfStream
4187 * will not be called in case writing was aborted.
4188 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004189 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004190 return kAbort;
4191 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004192};
4193
4194
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004195/**
4196 * An interface for reporting progress and controlling long-running
4197 * activities.
4198 */
4199class V8EXPORT ActivityControl { // NOLINT
4200 public:
4201 enum ControlOption {
4202 kContinue = 0,
4203 kAbort = 1
4204 };
4205 virtual ~ActivityControl() {}
4206 /**
4207 * Notify about current progress. The activity can be stopped by
4208 * returning kAbort as the callback result.
4209 */
4210 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4211};
4212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004213
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004214// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004215
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004216
4217namespace internal {
4218
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004219const int kApiPointerSize = sizeof(void*); // NOLINT
4220const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004221
4222// Tag information for HeapObject.
4223const int kHeapObjectTag = 1;
4224const int kHeapObjectTagSize = 2;
4225const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4226
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004227// Tag information for Smi.
4228const int kSmiTag = 0;
4229const int kSmiTagSize = 1;
4230const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4231
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004232template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004233
4234// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004235template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004236 static const int kSmiShiftSize = 0;
4237 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004238 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004239 int shift_bits = kSmiTagSize + kSmiShiftSize;
4240 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4241 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4242 }
4243};
4244
4245// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004246template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004247 static const int kSmiShiftSize = 31;
4248 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004249 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004250 int shift_bits = kSmiTagSize + kSmiShiftSize;
4251 // Shift down and throw away top 32 bits.
4252 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4253 }
4254};
4255
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004256typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4257const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4258const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004259
4260/**
4261 * This class exports constants and functionality from within v8 that
4262 * is necessary to implement inline functions in the v8 api. Don't
4263 * depend on functions and constants defined here.
4264 */
4265class Internals {
4266 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004267 // These values match non-compiler-dependent values defined within
4268 // the implementation of v8.
4269 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004270 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004271 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004272
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004273 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004274 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004275 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004276 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4277 static const int kContextHeaderSize = 2 * kApiPointerSize;
4278 static const int kContextEmbedderDataIndex = 54;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004279 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004280 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004281 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004282 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004283
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004284 static const int kIsolateStateOffset = 0;
4285 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4286 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4287 static const int kUndefinedValueRootIndex = 5;
4288 static const int kNullValueRootIndex = 7;
4289 static const int kTrueValueRootIndex = 8;
4290 static const int kFalseValueRootIndex = 9;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004291 static const int kEmptyStringRootIndex = 119;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004292
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004293 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
4294 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
4295 static const int kNodeStateMask = 0xf;
4296 static const int kNodeStateIsWeakValue = 2;
4297 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004298 static const int kNodeIsIndependentShift = 4;
4299 static const int kNodeIsPartiallyDependentShift = 5;
4300
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004301 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004302 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004303 static const int kOddballType = 0x83;
4304 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004305
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004306 static const int kUndefinedOddballKind = 5;
4307 static const int kNullOddballKind = 3;
4308
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004309 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004310 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4311 kHeapObjectTag);
4312 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004313
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004314 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004315 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004316 }
4317
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004318 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004319 typedef internal::Object O;
4320 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4321 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4322 }
4323
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004324 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004325 typedef internal::Object O;
4326 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4327 }
4328
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004329 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004330 int representation = (instance_type & kFullStringRepresentationMask);
4331 return representation == kExternalTwoByteRepresentationTag;
4332 }
4333
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004334 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004335 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4336 return *reinterpret_cast<int*>(addr) == 1;
4337 }
4338
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004339 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
4340 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4341 return *addr & (1 << shift);
4342 }
4343
4344 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
4345 bool value, int shift)) {
4346 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4347 uint8_t mask = 1 << shift;
4348 *addr = (*addr & ~mask) | (value << shift);
4349 }
4350
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004351 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
4352 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4353 return *addr & kNodeStateMask;
4354 }
4355
4356 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
4357 uint8_t value)) {
4358 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4359 *addr = (*addr & ~kNodeStateMask) | value;
4360 }
4361
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004362 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004363 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4364 kIsolateEmbedderDataOffset;
4365 *reinterpret_cast<void**>(addr) = data;
4366 }
4367
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004368 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004369 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4370 kIsolateEmbedderDataOffset;
4371 return *reinterpret_cast<void**>(addr);
4372 }
4373
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004374 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
4375 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004376 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4377 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4378 }
4379
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004380 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004381 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004382 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4383 return *reinterpret_cast<T*>(addr);
4384 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004385
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004386 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004387 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004388 typedef internal::Object O;
4389 typedef internal::Internals I;
4390 O* ctx = *reinterpret_cast<O**>(context);
4391 int embedder_data_offset = I::kContextHeaderSize +
4392 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4393 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4394 int value_offset =
4395 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4396 return I::ReadField<T>(embedder_data, value_offset);
4397 }
4398
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004399 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
4400 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
4401 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
4402 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
4403 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
4404 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
4405 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004406};
4407
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004408} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004409
4410
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004411template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004412Local<T>::Local() : Handle<T>() { }
4413
4414
4415template <class T>
4416Local<T> Local<T>::New(Handle<T> that) {
4417 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004418 T* that_ptr = *that;
4419 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4420 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4421 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4422 reinterpret_cast<internal::HeapObject*>(*p))));
4423 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004424 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4425}
4426
4427
4428template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004429Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004430 if (that.IsEmpty()) return Local<T>();
4431 T* that_ptr = *that;
4432 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4433 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4434 reinterpret_cast<internal::Isolate*>(isolate), *p)));
4435}
4436
4437
4438template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004439Persistent<T> Persistent<T>::New(Handle<T> that) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004440 return New(Isolate::GetCurrent(), that);
4441}
4442
4443
4444template <class T>
4445Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004446 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004447 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004448 return Persistent<T>(reinterpret_cast<T*>(
4449 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
4450 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004451}
4452
4453
4454template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004455bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004456 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004457}
4458
4459
4460template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004461bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004462 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004463 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004464 if (!I::IsInitialized(isolate)) return false;
4465 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(**this),
4466 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004467}
4468
4469
4470template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004471bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004472 return IsNearDeath(Isolate::GetCurrent());
4473}
4474
4475
4476template <class T>
4477bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
4478 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004479 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004480 if (!I::IsInitialized(isolate)) return false;
4481 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4482 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004483}
4484
4485
4486template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004487bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004488 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004489}
4490
4491
4492template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004493bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004494 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004495 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004496 if (!I::IsInitialized(isolate)) return false;
4497 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4498 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004499}
4500
4501
4502template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004503void Persistent<T>::Dispose() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004504 Dispose(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004505}
4506
4507
4508template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004509void Persistent<T>::Dispose(Isolate* isolate) {
4510 if (this->IsEmpty()) return;
4511 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4512 reinterpret_cast<internal::Object**>(**this));
4513}
4514
4515
4516template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004517Persistent<T>::Persistent() : Handle<T>() { }
4518
4519template <class T>
4520void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004521 Isolate* isolate = Isolate::GetCurrent();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004522 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4523 reinterpret_cast<internal::Object**>(**this),
4524 parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004525 callback,
4526 NULL);
4527}
4528
4529template <class T>
4530void Persistent<T>::MakeWeak(Isolate* isolate,
4531 void* parameters,
4532 NearDeathCallback callback) {
4533 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4534 reinterpret_cast<internal::Object**>(**this),
4535 parameters,
4536 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004537 callback);
4538}
4539
4540template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004541void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004542 ClearWeak(Isolate::GetCurrent());
4543}
4544
4545template <class T>
4546void Persistent<T>::ClearWeak(Isolate* isolate) {
4547 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
4548 reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004549}
4550
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004551template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004552void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004553 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004554}
4555
4556template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004557void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004558 typedef internal::Internals I;
4559 if (this->IsEmpty()) return;
4560 if (!I::IsInitialized(isolate)) return;
4561 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004562 true,
4563 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004564}
4565
4566template <class T>
4567void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004568 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004569}
4570
4571template <class T>
4572void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004573 typedef internal::Internals I;
4574 if (this->IsEmpty()) return;
4575 if (!I::IsInitialized(isolate)) return;
4576 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004577 true,
4578 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004579}
4580
4581template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004582void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004583 SetWrapperClassId(Isolate::GetCurrent(), class_id);
4584}
4585
4586template <class T>
4587void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004588 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004589 if (this->IsEmpty()) return;
4590 if (!I::IsInitialized(isolate)) return;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004591 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4592 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4593 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004594}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004595
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004596template <class T>
4597uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004598 return WrapperClassId(Isolate::GetCurrent());
4599}
4600
4601template <class T>
4602uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004603 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004604 if (this->IsEmpty()) return 0;
4605 if (!I::IsInitialized(isolate)) return 0;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004606 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4607 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4608 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004609}
4610
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004611Arguments::Arguments(internal::Object** implicit_args,
4612 internal::Object** values, int length,
4613 bool is_construct_call)
4614 : implicit_args_(implicit_args),
4615 values_(values),
4616 length_(length),
4617 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004618
4619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004620Local<Value> Arguments::operator[](int i) const {
4621 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4622 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4623}
4624
4625
4626Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004627 return Local<Function>(reinterpret_cast<Function*>(
4628 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004629}
4630
4631
4632Local<Object> Arguments::This() const {
4633 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4634}
4635
4636
4637Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004638 return Local<Object>(reinterpret_cast<Object*>(
4639 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004640}
4641
4642
4643Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004644 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004645}
4646
4647
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004648Isolate* Arguments::GetIsolate() const {
4649 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4650}
4651
4652
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004653bool Arguments::IsConstructCall() const {
4654 return is_construct_call_;
4655}
4656
4657
4658int Arguments::Length() const {
4659 return length_;
4660}
4661
4662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004663template <class T>
4664Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004665 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4666 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004667 return Local<T>(reinterpret_cast<T*>(after));
4668}
4669
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004670Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004671 return resource_name_;
4672}
4673
4674
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004675Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004676 return resource_line_offset_;
4677}
4678
4679
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004680Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004681 return resource_column_offset_;
4682}
4683
4684
4685Handle<Boolean> Boolean::New(bool value) {
4686 return value ? True() : False();
4687}
4688
4689
4690void Template::Set(const char* name, v8::Handle<Data> value) {
4691 Set(v8::String::New(name), value);
4692}
4693
4694
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004695Local<Value> Object::GetInternalField(int index) {
4696#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004697 typedef internal::Object O;
4698 typedef internal::Internals I;
4699 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004700 // Fast path: If the object is a plain JSObject, which is the common case, we
4701 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004702 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004703 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004704 O* value = I::ReadField<O*>(obj, offset);
4705 O** result = HandleScope::CreateHandle(value);
4706 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004707 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004708#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004709 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004710}
4711
4712
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004713void* Object::GetAlignedPointerFromInternalField(int index) {
4714#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004715 typedef internal::Object O;
4716 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004717 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004718 // Fast path: If the object is a plain JSObject, which is the common case, we
4719 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004720 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004721 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004722 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004723 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004724#endif
4725 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004726}
4727
4728
4729String* String::Cast(v8::Value* value) {
4730#ifdef V8_ENABLE_CHECKS
4731 CheckCast(value);
4732#endif
4733 return static_cast<String*>(value);
4734}
4735
4736
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004737Local<String> String::Empty(Isolate* isolate) {
4738 typedef internal::Object* S;
4739 typedef internal::Internals I;
4740 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004741 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004742 return Local<String>(reinterpret_cast<String*>(slot));
4743}
4744
4745
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004746String::ExternalStringResource* String::GetExternalStringResource() const {
4747 typedef internal::Object O;
4748 typedef internal::Internals I;
4749 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004750 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004751 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004752 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4753 result = reinterpret_cast<String::ExternalStringResource*>(value);
4754 } else {
4755 result = NULL;
4756 }
4757#ifdef V8_ENABLE_CHECKS
4758 VerifyExternalStringResource(result);
4759#endif
4760 return result;
4761}
4762
4763
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004764String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4765 String::Encoding* encoding_out) const {
4766 typedef internal::Object O;
4767 typedef internal::Internals I;
4768 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4769 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4770 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4771 ExternalStringResourceBase* resource = NULL;
4772 if (type == I::kExternalAsciiRepresentationTag ||
4773 type == I::kExternalTwoByteRepresentationTag) {
4774 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4775 resource = static_cast<ExternalStringResourceBase*>(value);
4776 }
4777#ifdef V8_ENABLE_CHECKS
4778 VerifyExternalStringResourceBase(resource, *encoding_out);
4779#endif
4780 return resource;
4781}
4782
4783
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004784bool Value::IsUndefined() const {
4785#ifdef V8_ENABLE_CHECKS
4786 return FullIsUndefined();
4787#else
4788 return QuickIsUndefined();
4789#endif
4790}
4791
4792bool Value::QuickIsUndefined() const {
4793 typedef internal::Object O;
4794 typedef internal::Internals I;
4795 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4796 if (!I::HasHeapObjectTag(obj)) return false;
4797 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4798 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4799}
4800
4801
4802bool Value::IsNull() const {
4803#ifdef V8_ENABLE_CHECKS
4804 return FullIsNull();
4805#else
4806 return QuickIsNull();
4807#endif
4808}
4809
4810bool Value::QuickIsNull() const {
4811 typedef internal::Object O;
4812 typedef internal::Internals I;
4813 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4814 if (!I::HasHeapObjectTag(obj)) return false;
4815 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4816 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4817}
4818
4819
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004820bool Value::IsString() const {
4821#ifdef V8_ENABLE_CHECKS
4822 return FullIsString();
4823#else
4824 return QuickIsString();
4825#endif
4826}
4827
4828bool Value::QuickIsString() const {
4829 typedef internal::Object O;
4830 typedef internal::Internals I;
4831 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4832 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004833 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004834}
4835
4836
4837Number* Number::Cast(v8::Value* value) {
4838#ifdef V8_ENABLE_CHECKS
4839 CheckCast(value);
4840#endif
4841 return static_cast<Number*>(value);
4842}
4843
4844
4845Integer* Integer::Cast(v8::Value* value) {
4846#ifdef V8_ENABLE_CHECKS
4847 CheckCast(value);
4848#endif
4849 return static_cast<Integer*>(value);
4850}
4851
4852
4853Date* Date::Cast(v8::Value* value) {
4854#ifdef V8_ENABLE_CHECKS
4855 CheckCast(value);
4856#endif
4857 return static_cast<Date*>(value);
4858}
4859
4860
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004861StringObject* StringObject::Cast(v8::Value* value) {
4862#ifdef V8_ENABLE_CHECKS
4863 CheckCast(value);
4864#endif
4865 return static_cast<StringObject*>(value);
4866}
4867
4868
4869NumberObject* NumberObject::Cast(v8::Value* value) {
4870#ifdef V8_ENABLE_CHECKS
4871 CheckCast(value);
4872#endif
4873 return static_cast<NumberObject*>(value);
4874}
4875
4876
4877BooleanObject* BooleanObject::Cast(v8::Value* value) {
4878#ifdef V8_ENABLE_CHECKS
4879 CheckCast(value);
4880#endif
4881 return static_cast<BooleanObject*>(value);
4882}
4883
4884
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004885RegExp* RegExp::Cast(v8::Value* value) {
4886#ifdef V8_ENABLE_CHECKS
4887 CheckCast(value);
4888#endif
4889 return static_cast<RegExp*>(value);
4890}
4891
4892
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004893Object* Object::Cast(v8::Value* value) {
4894#ifdef V8_ENABLE_CHECKS
4895 CheckCast(value);
4896#endif
4897 return static_cast<Object*>(value);
4898}
4899
4900
4901Array* Array::Cast(v8::Value* value) {
4902#ifdef V8_ENABLE_CHECKS
4903 CheckCast(value);
4904#endif
4905 return static_cast<Array*>(value);
4906}
4907
4908
4909Function* Function::Cast(v8::Value* value) {
4910#ifdef V8_ENABLE_CHECKS
4911 CheckCast(value);
4912#endif
4913 return static_cast<Function*>(value);
4914}
4915
4916
4917External* External::Cast(v8::Value* value) {
4918#ifdef V8_ENABLE_CHECKS
4919 CheckCast(value);
4920#endif
4921 return static_cast<External*>(value);
4922}
4923
4924
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004925Isolate* AccessorInfo::GetIsolate() const {
4926 return *reinterpret_cast<Isolate**>(&args_[-3]);
4927}
4928
4929
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004930Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004931 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004932}
4933
4934
4935Local<Object> AccessorInfo::This() const {
4936 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4937}
4938
4939
4940Local<Object> AccessorInfo::Holder() const {
4941 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4942}
4943
4944
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004945Handle<Primitive> Undefined(Isolate* isolate) {
4946 typedef internal::Object* S;
4947 typedef internal::Internals I;
4948 if (!I::IsInitialized(isolate)) return Undefined();
4949 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4950 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4951}
4952
4953
4954Handle<Primitive> Null(Isolate* isolate) {
4955 typedef internal::Object* S;
4956 typedef internal::Internals I;
4957 if (!I::IsInitialized(isolate)) return Null();
4958 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4959 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4960}
4961
4962
4963Handle<Boolean> True(Isolate* isolate) {
4964 typedef internal::Object* S;
4965 typedef internal::Internals I;
4966 if (!I::IsInitialized(isolate)) return True();
4967 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4968 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4969}
4970
4971
4972Handle<Boolean> False(Isolate* isolate) {
4973 typedef internal::Object* S;
4974 typedef internal::Internals I;
4975 if (!I::IsInitialized(isolate)) return False();
4976 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4977 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4978}
4979
4980
4981void Isolate::SetData(void* data) {
4982 typedef internal::Internals I;
4983 I::SetEmbedderData(this, data);
4984}
4985
4986
4987void* Isolate::GetData() {
4988 typedef internal::Internals I;
4989 return I::GetEmbedderData(this);
4990}
4991
4992
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004993Local<Value> Context::GetEmbedderData(int index) {
4994#ifndef V8_ENABLE_CHECKS
4995 typedef internal::Object O;
4996 typedef internal::Internals I;
4997 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
4998 return Local<Value>(reinterpret_cast<Value*>(result));
4999#else
5000 return SlowGetEmbedderData(index);
5001#endif
5002}
5003
5004
5005void* Context::GetAlignedPointerFromEmbedderData(int index) {
5006#ifndef V8_ENABLE_CHECKS
5007 typedef internal::Internals I;
5008 return I::ReadEmbedderData<void*>(this, index);
5009#else
5010 return SlowGetAlignedPointerFromEmbedderData(index);
5011#endif
5012}
5013
5014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005015/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00005016 * \example shell.cc
5017 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005018 * command-line and executes them.
5019 */
5020
5021
5022/**
5023 * \example process.cc
5024 */
5025
5026
5027} // namespace v8
5028
5029
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005030#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005031#undef TYPE_CHECK
5032
5033
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005034#endif // V8_H_