blob: bf3beff5f0d22a56efc7756845b34ffac130138b [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
yangguo@chromium.org46a2a512013-01-18 16:29:40 +000041// TODO(svenpanne) Remove me when the Chrome bindings are adapted.
42#define V8_DISABLE_DEPRECATIONS 1
43
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000044#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045
46#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000047
48// Setup for Windows DLL export/import. When building the V8 DLL the
49// BUILDING_V8_SHARED needs to be defined. When building a program which uses
50// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
51// static library or building a program which uses the V8 static library neither
52// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000053#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
54#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
55 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056#endif
57
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000059#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000060#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000061#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000062#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000063#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000064#endif // BUILDING_V8_SHARED
65
66#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000067
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000069#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
70 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000071#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000072#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000073#else
74#define V8EXPORT
75#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000076#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000077#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000078#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079
80#endif // _WIN32
81
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000082#if defined(__GNUC__) && !defined(DEBUG)
83#define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
84#elif defined(_MSC_VER) && !defined(DEBUG)
85#define V8_INLINE(declarator) __forceinline declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000086#else
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000087#define V8_INLINE(declarator) inline declarator
88#endif
89
90#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
91#define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
92#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
93#define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
94#else
95#define V8_DEPRECATED(declarator) declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000096#endif
97
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000099 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 */
101namespace v8 {
102
103class Context;
104class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000105class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106class Value;
107class Utils;
108class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000109class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110class Object;
111class Array;
112class Int32;
113class Uint32;
114class External;
115class Primitive;
116class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000117class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118class Integer;
119class Function;
120class Date;
121class ImplementationUtilities;
122class Signature;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000123class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124template <class T> class Handle;
125template <class T> class Local;
126template <class T> class Persistent;
127class FunctionTemplate;
128class ObjectTemplate;
129class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000130class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000131class StackTrace;
132class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000133class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000135namespace internal {
136
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000137class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000138class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000139class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000140class HeapObject;
141class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000142}
143
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000145// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146
147
148/**
149 * A weak reference callback function.
150 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000151 * This callback should either explicitly invoke Dispose on |object| if
152 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
153 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 * \param object the weak global object to be reclaimed by the garbage collector
155 * \param parameter the value passed in when making the weak global object
156 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000157typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 void* parameter);
159
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:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000517 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000519 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
521 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522 * Closes the handle scope and returns the value as a handle in the
523 * previous scope, which is the new current scope after the call.
524 */
525 template <class T> Local<T> Close(Handle<T> value);
526
527 /**
528 * Counts the number of allocated handles.
529 */
530 static int NumberOfHandles();
531
532 /**
533 * Creates a new handle with the given value.
534 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000535 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000536 static internal::Object** CreateHandle(internal::Isolate* isolate,
537 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000538 // Faster version, uses HeapObject to obtain the current Isolate.
539 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540
541 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000542 // Make it hard to create heap-allocated or illegal handle scopes by
543 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 HandleScope(const HandleScope&);
545 void operator=(const HandleScope&);
546 void* operator new(size_t size);
547 void operator delete(void*, size_t);
548
ager@chromium.org3811b432009-10-28 14:53:37 +0000549 // This Data class is accessible internally as HandleScopeData through a
550 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000551 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000553 internal::Object** next;
554 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000555 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000556 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000558 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 }
560 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000561
lrn@chromium.org303ada72010-10-27 09:33:13 +0000562 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000564 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000565 internal::Object** prev_next_;
566 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000568 // Allow for the active closing of HandleScopes which allows to pass a handle
569 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000571 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 friend class ImplementationUtilities;
574};
575
576
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000577// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578
579
580/**
581 * The superclass of values and API object templates.
582 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000583class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 private:
585 Data();
586};
587
588
589/**
590 * Pre-compilation data that can be associated with a script. This
591 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000592 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000593 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000595class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 public:
597 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000598
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000599 /**
600 * Pre-compiles the specified script (context-independent).
601 *
602 * \param input Pointer to UTF-8 script source code.
603 * \param length Length of UTF-8 script source code.
604 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000607 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000608 * Pre-compiles the specified script (context-independent).
609 *
610 * NOTE: Pre-compilation using this method cannot happen on another thread
611 * without using Lockers.
612 *
613 * \param source Script source code.
614 */
615 static ScriptData* PreCompile(Handle<String> source);
616
617 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000618 * Load previous pre-compilation data.
619 *
620 * \param data Pointer to data returned by a call to Data() of a previous
621 * ScriptData. Ownership is not transferred.
622 * \param length Length of data.
623 */
624 static ScriptData* New(const char* data, int length);
625
626 /**
627 * Returns the length of Data().
628 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000630
631 /**
632 * Returns a serialized representation of this ScriptData that can later be
633 * passed to New(). NOTE: Serialized data is platform-dependent.
634 */
635 virtual const char* Data() = 0;
636
637 /**
638 * Returns true if the source code could not be parsed.
639 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000640 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641};
642
643
644/**
645 * The origin, within a file, of a script.
646 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000647class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000649 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000650 Handle<Value> resource_name,
651 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000652 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 : resource_name_(resource_name),
654 resource_line_offset_(resource_line_offset),
655 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000656 V8_INLINE(Handle<Value> ResourceName() const);
657 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
658 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000660 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661 Handle<Integer> resource_line_offset_;
662 Handle<Integer> resource_column_offset_;
663};
664
665
666/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000667 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000669class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000672 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000673 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000674 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000675 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000676 * when New() returns
677 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
678 * using pre_data speeds compilation if it's done multiple times.
679 * Owned by caller, no references are kept when New() returns.
680 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000681 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000682 * available to compile event handlers.
683 * \return Compiled script object (context independent; when run it
684 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000686 static Local<Script> New(Handle<String> source,
687 ScriptOrigin* origin = NULL,
688 ScriptData* pre_data = NULL,
689 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690
mads.s.agercbaa0602008-08-14 13:41:48 +0000691 /**
692 * Compiles the specified script using the specified file name
693 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000694 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000695 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000696 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000697 * as the script's origin.
698 * \return Compiled script object (context independent; when run it
699 * will use the currently entered context).
700 */
701 static Local<Script> New(Handle<String> source,
702 Handle<Value> file_name);
703
704 /**
705 * Compiles the specified script (bound to current context).
706 *
707 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000708 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000709 * when Compile() returns
710 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
711 * using pre_data speeds compilation if it's done multiple times.
712 * Owned by caller, no references are kept when Compile() returns.
713 * \param script_data Arbitrary data associated with script. Using
714 * this has same effect as calling SetData(), but makes data available
715 * earlier (i.e. to compile event handlers).
716 * \return Compiled script object, bound to the context that was active
717 * when this function was called. When run it will always use this
718 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000719 */
720 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000721 ScriptOrigin* origin = NULL,
722 ScriptData* pre_data = NULL,
723 Handle<String> script_data = Handle<String>());
724
725 /**
726 * Compiles the specified script using the specified file name
727 * object (typically a string) as the script's origin.
728 *
729 * \param source Script source code.
730 * \param file_name File name to use as script's origin
731 * \param script_data Arbitrary data associated with script. Using
732 * this has same effect as calling SetData(), but makes data available
733 * earlier (i.e. to compile event handlers).
734 * \return Compiled script object, bound to the context that was active
735 * when this function was called. When run it will always use this
736 * context.
737 */
738 static Local<Script> Compile(Handle<String> source,
739 Handle<Value> file_name,
740 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000741
v8.team.kasperl727e9952008-09-02 14:56:44 +0000742 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000743 * Runs the script returning the resulting value. If the script is
744 * context independent (created using ::New) it will be run in the
745 * currently entered context. If it is context specific (created
746 * using ::Compile) it will be run in the context in which it was
747 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000748 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000750
751 /**
752 * Returns the script id value.
753 */
754 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000755
756 /**
757 * Associate an additional data object with the script. This is mainly used
758 * with the debugger as this data object is only available through the
759 * debugger API.
760 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000761 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762};
763
764
765/**
766 * An error message.
767 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000768class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000770 Local<String> Get() const;
771 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000773 /**
774 * Returns the resource name for the script from where the function causing
775 * the error originates.
776 */
ager@chromium.org32912102009-01-16 10:38:43 +0000777 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000779 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000780 * Returns the resource data for the script from where the function causing
781 * the error originates.
782 */
783 Handle<Value> GetScriptData() const;
784
785 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000786 * Exception stack trace. By default stack traces are not captured for
787 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
788 * to change this option.
789 */
790 Handle<StackTrace> GetStackTrace() const;
791
792 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000793 * Returns the number, 1-based, of the line where the error occurred.
794 */
ager@chromium.org32912102009-01-16 10:38:43 +0000795 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000797 /**
798 * Returns the index within the script of the first character where
799 * the error occurred.
800 */
ager@chromium.org32912102009-01-16 10:38:43 +0000801 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000802
803 /**
804 * Returns the index within the script of the last character where
805 * the error occurred.
806 */
ager@chromium.org32912102009-01-16 10:38:43 +0000807 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000808
809 /**
810 * Returns the index within the line of the first character where
811 * the error occurred.
812 */
ager@chromium.org32912102009-01-16 10:38:43 +0000813 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000814
815 /**
816 * Returns the index within the line of the last character where
817 * the error occurred.
818 */
ager@chromium.org32912102009-01-16 10:38:43 +0000819 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000820
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821 // TODO(1245381): Print to a string instead of on a FILE.
822 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000823
824 static const int kNoLineNumberInfo = 0;
825 static const int kNoColumnInfo = 0;
826};
827
828
829/**
830 * Representation of a JavaScript stack trace. The information collected is a
831 * snapshot of the execution stack and the information remains valid after
832 * execution continues.
833 */
834class V8EXPORT StackTrace {
835 public:
836 /**
837 * Flags that determine what information is placed captured for each
838 * StackFrame when grabbing the current stack trace.
839 */
840 enum StackTraceOptions {
841 kLineNumber = 1,
842 kColumnOffset = 1 << 1 | kLineNumber,
843 kScriptName = 1 << 2,
844 kFunctionName = 1 << 3,
845 kIsEval = 1 << 4,
846 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000847 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000848 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000849 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000850 };
851
852 /**
853 * Returns a StackFrame at a particular index.
854 */
855 Local<StackFrame> GetFrame(uint32_t index) const;
856
857 /**
858 * Returns the number of StackFrames.
859 */
860 int GetFrameCount() const;
861
862 /**
863 * Returns StackTrace as a v8::Array that contains StackFrame objects.
864 */
865 Local<Array> AsArray();
866
867 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000868 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000869 *
870 * \param frame_limit The maximum number of stack frames we want to capture.
871 * \param options Enumerates the set of things we will capture for each
872 * StackFrame.
873 */
874 static Local<StackTrace> CurrentStackTrace(
875 int frame_limit,
876 StackTraceOptions options = kOverview);
877};
878
879
880/**
881 * A single JavaScript stack frame.
882 */
883class V8EXPORT StackFrame {
884 public:
885 /**
886 * Returns the number, 1-based, of the line for the associate function call.
887 * This method will return Message::kNoLineNumberInfo if it is unable to
888 * retrieve the line number, or if kLineNumber was not passed as an option
889 * when capturing the StackTrace.
890 */
891 int GetLineNumber() const;
892
893 /**
894 * Returns the 1-based column offset on the line for the associated function
895 * call.
896 * This method will return Message::kNoColumnInfo if it is unable to retrieve
897 * the column number, or if kColumnOffset was not passed as an option when
898 * capturing the StackTrace.
899 */
900 int GetColumn() const;
901
902 /**
903 * Returns the name of the resource that contains the script for the
904 * function for this StackFrame.
905 */
906 Local<String> GetScriptName() const;
907
908 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000909 * Returns the name of the resource that contains the script for the
910 * function for this StackFrame or sourceURL value if the script name
911 * is undefined and its source ends with //@ sourceURL=... string.
912 */
913 Local<String> GetScriptNameOrSourceURL() const;
914
915 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000916 * Returns the name of the function associated with this stack frame.
917 */
918 Local<String> GetFunctionName() const;
919
920 /**
921 * Returns whether or not the associated function is compiled via a call to
922 * eval().
923 */
924 bool IsEval() const;
925
926 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000927 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000928 * constructor via "new".
929 */
930 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931};
932
933
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000934// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935
936
937/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000938 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000940class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 /**
943 * Returns true if this value is the undefined value. See ECMA-262
944 * 4.3.10.
945 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000946 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947
948 /**
949 * Returns true if this value is the null value. See ECMA-262
950 * 4.3.11.
951 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000952 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953
954 /**
955 * Returns true if this value is true.
956 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000957 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958
959 /**
960 * Returns true if this value is false.
961 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000962 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000963
964 /**
965 * Returns true if this value is an instance of the String type.
966 * See ECMA-262 8.4.
967 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000968 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969
970 /**
971 * Returns true if this value is a function.
972 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000973 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974
975 /**
976 * Returns true if this value is an array.
977 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000978 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979
v8.team.kasperl727e9952008-09-02 14:56:44 +0000980 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 * Returns true if this value is an object.
982 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000983 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984
v8.team.kasperl727e9952008-09-02 14:56:44 +0000985 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 * Returns true if this value is boolean.
987 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000988 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000989
v8.team.kasperl727e9952008-09-02 14:56:44 +0000990 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 * Returns true if this value is a number.
992 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000993 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994
v8.team.kasperl727e9952008-09-02 14:56:44 +0000995 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 * Returns true if this value is external.
997 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000998 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999
v8.team.kasperl727e9952008-09-02 14:56:44 +00001000 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 * Returns true if this value is a 32-bit signed integer.
1002 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001003 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001005 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001006 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001007 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001008 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001009
1010 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001011 * Returns true if this value is a Date.
1012 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001013 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001014
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001015 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001016 * Returns true if this value is a Boolean object.
1017 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001018 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001019
1020 /**
1021 * Returns true if this value is a Number object.
1022 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001023 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001024
1025 /**
1026 * Returns true if this value is a String object.
1027 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001028 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001029
1030 /**
1031 * Returns true if this value is a NativeError.
1032 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001033 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001034
1035 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001036 * Returns true if this value is a RegExp.
1037 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001038 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001039
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001040 Local<Boolean> ToBoolean() const;
1041 Local<Number> ToNumber() const;
1042 Local<String> ToString() const;
1043 Local<String> ToDetailString() const;
1044 Local<Object> ToObject() const;
1045 Local<Integer> ToInteger() const;
1046 Local<Uint32> ToUint32() const;
1047 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048
1049 /**
1050 * Attempts to convert a string to an array index.
1051 * Returns an empty handle if the conversion fails.
1052 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001053 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001055 bool BooleanValue() const;
1056 double NumberValue() const;
1057 int64_t IntegerValue() const;
1058 uint32_t Uint32Value() const;
1059 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060
1061 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001062 bool Equals(Handle<Value> that) const;
1063 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001064
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001065 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001066 V8_INLINE(bool QuickIsUndefined() const);
1067 V8_INLINE(bool QuickIsNull() const);
1068 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001069 bool FullIsUndefined() const;
1070 bool FullIsNull() const;
1071 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072};
1073
1074
1075/**
1076 * The superclass of primitive values. See ECMA-262 4.3.2.
1077 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001078class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079
1080
1081/**
1082 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1083 * or false value.
1084 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001085class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001087 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001088 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089};
1090
1091
1092/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001093 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001095class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001097 enum Encoding {
1098 UNKNOWN_ENCODING = 0x1,
1099 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001100 ASCII_ENCODING = 0x4,
1101 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001102 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001103 /**
1104 * Returns the number of characters in this string.
1105 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001106 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107
v8.team.kasperl727e9952008-09-02 14:56:44 +00001108 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001109 * Returns the number of bytes in the UTF-8 encoded
1110 * representation of this string.
1111 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001112 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001113
1114 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001115 * A fast conservative check for non-ASCII characters. May
1116 * return true even for ASCII strings, but if it returns
1117 * false you can be sure that all characters are in the range
1118 * 0-127.
1119 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001120 bool MayContainNonAscii() const;
1121
1122 /**
1123 * Returns whether this string contains only one byte data.
1124 */
1125 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001126
1127 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001128 * Write the contents of the string to an external buffer.
1129 * If no arguments are given, expects the buffer to be large
1130 * enough to hold the entire string and NULL terminator. Copies
1131 * the contents of the string and the NULL terminator into the
1132 * buffer.
1133 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001134 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1135 * before the end of the buffer.
1136 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001137 * Copies up to length characters into the output buffer.
1138 * Only null-terminates if there is enough space in the buffer.
1139 *
1140 * \param buffer The buffer into which the string will be copied.
1141 * \param start The starting position within the string at which
1142 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001143 * \param length The number of characters to copy from the string. For
1144 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001145 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001146 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001147 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001148 * \return The number of characters copied to the buffer excluding the null
1149 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001150 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001151 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001152 enum WriteOptions {
1153 NO_OPTIONS = 0,
1154 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001155 NO_NULL_TERMINATION = 2,
1156 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001157 };
1158
lrn@chromium.org34e60782011-09-15 07:25:40 +00001159 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001160 int Write(uint16_t* buffer,
1161 int start = 0,
1162 int length = -1,
1163 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001164 // ASCII characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001165 int WriteAscii(char* buffer,
1166 int start = 0,
1167 int length = -1,
1168 int options = NO_OPTIONS) const;
1169 // One byte characters.
1170 int WriteOneByte(uint8_t* buffer,
1171 int start = 0,
1172 int length = -1,
1173 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001174 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001175 int WriteUtf8(char* buffer,
1176 int length = -1,
1177 int* nchars_ref = NULL,
1178 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179
v8.team.kasperl727e9952008-09-02 14:56:44 +00001180 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001181 * A zero length string.
1182 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001183 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001184 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001185
1186 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001187 * Returns true if the string is external
1188 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001189 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190
v8.team.kasperl727e9952008-09-02 14:56:44 +00001191 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001192 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001193 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001194 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001195
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001196 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001197 public:
1198 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001199
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001200 protected:
1201 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001202
1203 /**
1204 * Internally V8 will call this Dispose method when the external string
1205 * resource is no longer needed. The default implementation will use the
1206 * delete operator. This method can be overridden in subclasses to
1207 * control how allocated external string resources are disposed.
1208 */
1209 virtual void Dispose() { delete this; }
1210
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001211 private:
1212 // Disallow copying and assigning.
1213 ExternalStringResourceBase(const ExternalStringResourceBase&);
1214 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001215
1216 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001217 };
1218
v8.team.kasperl727e9952008-09-02 14:56:44 +00001219 /**
1220 * An ExternalStringResource is a wrapper around a two-byte string
1221 * buffer that resides outside V8's heap. Implement an
1222 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001223 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001224 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001225 class V8EXPORT ExternalStringResource
1226 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227 public:
1228 /**
1229 * Override the destructor to manage the life cycle of the underlying
1230 * buffer.
1231 */
1232 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001233
1234 /**
1235 * The string data from the underlying buffer.
1236 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001238
1239 /**
1240 * The length of the string. That is, the number of two-byte characters.
1241 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001243
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244 protected:
1245 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 };
1247
1248 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001249 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001250 * string buffer that resides outside V8's heap. Implement an
1251 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001252 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001253 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001254 * UTF-8, which would require special treatment internally in the
1255 * engine and, in the case of UTF-8, do not allow efficient indexing.
1256 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001257 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001259 class V8EXPORT ExternalAsciiStringResource
1260 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 public:
1262 /**
1263 * Override the destructor to manage the life cycle of the underlying
1264 * buffer.
1265 */
1266 virtual ~ExternalAsciiStringResource() {}
1267 /** The string data from the underlying buffer.*/
1268 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001269 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 virtual size_t length() const = 0;
1271 protected:
1272 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 };
1274
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001275 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1276
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001278 * If the string is an external string, return the ExternalStringResourceBase
1279 * regardless of the encoding, otherwise return NULL. The encoding of the
1280 * string is returned in encoding_out.
1281 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001282 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1283 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001284
1285 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001286 * Get the ExternalStringResource for an external string. Returns
1287 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001289 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290
1291 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001292 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001293 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001295 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001297 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298
1299 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001300 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001301 * The second parameter 'length' gives the buffer length. If omitted,
1302 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001304 static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305
lrn@chromium.org34e60782011-09-15 07:25:40 +00001306 /** Allocates a new string from 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001307 static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308
1309 /** Creates a symbol. Returns one if it exists already.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001310 static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311
v8.team.kasperl727e9952008-09-02 14:56:44 +00001312 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001313 * Creates a new string by concatenating the left and the right strings
1314 * passed in as parameters.
1315 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001316 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001317
1318 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001319 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001320 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001321 * resource will be disposed by calling its Dispose method. The caller of
1322 * this function should not otherwise delete or modify the resource. Neither
1323 * should the underlying buffer be deallocated or modified except through the
1324 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001325 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001326 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001327
ager@chromium.org6f10e412009-02-13 10:11:16 +00001328 /**
1329 * Associate an external string resource with this string by transforming it
1330 * in place so that existing references to this string in the JavaScript heap
1331 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001332 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001333 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001334 * The string is not modified if the operation fails. See NewExternal for
1335 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001336 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001337 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338
v8.team.kasperl727e9952008-09-02 14:56:44 +00001339 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001340 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001341 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001342 * resource will be disposed by calling its Dispose method. The caller of
1343 * this function should not otherwise delete or modify the resource. Neither
1344 * should the underlying buffer be deallocated or modified except through the
1345 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001346 */
1347 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001348
ager@chromium.org6f10e412009-02-13 10:11:16 +00001349 /**
1350 * Associate an external string resource with this string by transforming it
1351 * in place so that existing references to this string in the JavaScript heap
1352 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001353 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001354 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001355 * The string is not modified if the operation fails. See NewExternal for
1356 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001357 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001358 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001360 /**
1361 * Returns true if this string can be made external.
1362 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001363 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001364
lrn@chromium.org34e60782011-09-15 07:25:40 +00001365 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001366 static Local<String> NewUndetectable(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367
lrn@chromium.org34e60782011-09-15 07:25:40 +00001368 /** Creates an undetectable string from the supplied 16-bit character codes.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001369 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370
1371 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001372 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001373 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001374 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001375 * then the length() method returns 0 and the * operator returns
1376 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001377 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001378 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001379 public:
1380 explicit Utf8Value(Handle<v8::Value> obj);
1381 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001382 char* operator*() { return str_; }
1383 const char* operator*() const { return str_; }
1384 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001385 private:
1386 char* str_;
1387 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001388
1389 // Disallow copying and assigning.
1390 Utf8Value(const Utf8Value&);
1391 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001392 };
1393
1394 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001395 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001397 * If conversion to a string fails (eg. due to an exception in the toString()
1398 * method of the object) then the length() method returns 0 and the * operator
1399 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001401 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001402 public:
1403 explicit AsciiValue(Handle<v8::Value> obj);
1404 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001405 char* operator*() { return str_; }
1406 const char* operator*() const { return str_; }
1407 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 private:
1409 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001410 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001411
1412 // Disallow copying and assigning.
1413 AsciiValue(const AsciiValue&);
1414 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 };
1416
1417 /**
1418 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001419 * If conversion to a string fails (eg. due to an exception in the toString()
1420 * method of the object) then the length() method returns 0 and the * operator
1421 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001422 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001423 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424 public:
1425 explicit Value(Handle<v8::Value> obj);
1426 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001427 uint16_t* operator*() { return str_; }
1428 const uint16_t* operator*() const { return str_; }
1429 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 private:
1431 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001432 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001433
1434 // Disallow copying and assigning.
1435 Value(const Value&);
1436 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001438
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001439 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001440 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1441 Encoding encoding) const;
1442 void VerifyExternalStringResource(ExternalStringResource* val) const;
1443 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444};
1445
1446
1447/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001448 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001450class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001452 double Value() const;
1453 static Local<Number> New(double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001454 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001456 Number();
1457 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458};
1459
1460
1461/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001462 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001464class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001466 static Local<Integer> New(int32_t value);
1467 static Local<Integer> NewFromUnsigned(uint32_t value);
1468 static Local<Integer> New(int32_t value, Isolate*);
1469 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1470 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001471 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001473 Integer();
1474 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475};
1476
1477
1478/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001479 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001481class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001483 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001485 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486};
1487
1488
1489/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001490 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001492class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001493 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001494 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001496 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497};
1498
1499
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500enum PropertyAttribute {
1501 None = 0,
1502 ReadOnly = 1 << 0,
1503 DontEnum = 1 << 1,
1504 DontDelete = 1 << 2
1505};
1506
ager@chromium.org3811b432009-10-28 14:53:37 +00001507enum ExternalArrayType {
1508 kExternalByteArray = 1,
1509 kExternalUnsignedByteArray,
1510 kExternalShortArray,
1511 kExternalUnsignedShortArray,
1512 kExternalIntArray,
1513 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001514 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001515 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001516 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001517};
1518
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001519/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001520 * Accessor[Getter|Setter] are used as callback functions when
1521 * setting|getting a particular property. See Object and ObjectTemplate's
1522 * method SetAccessor.
1523 */
1524typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1525 const AccessorInfo& info);
1526
1527
1528typedef void (*AccessorSetter)(Local<String> property,
1529 Local<Value> value,
1530 const AccessorInfo& info);
1531
1532
1533/**
1534 * Access control specifications.
1535 *
1536 * Some accessors should be accessible across contexts. These
1537 * accessors have an explicit access control parameter which specifies
1538 * the kind of cross-context access that should be allowed.
1539 *
1540 * Additionally, for security, accessors can prohibit overwriting by
1541 * accessors defined in JavaScript. For objects that have such
1542 * accessors either locally or in their prototype chain it is not
1543 * possible to overwrite the accessor by using __defineGetter__ or
1544 * __defineSetter__ from JavaScript code.
1545 */
1546enum AccessControl {
1547 DEFAULT = 0,
1548 ALL_CAN_READ = 1,
1549 ALL_CAN_WRITE = 1 << 1,
1550 PROHIBITS_OVERWRITING = 1 << 2
1551};
1552
1553
1554/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001555 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001557class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001559 bool Set(Handle<Value> key,
1560 Handle<Value> value,
1561 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001562
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001563 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001564
ager@chromium.orge2902be2009-06-08 12:21:35 +00001565 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001566 // overriding accessors or read-only properties.
1567 //
1568 // Note that if the object has an interceptor the property will be set
1569 // locally, but since the interceptor takes precedence the local property
1570 // will only be returned if the interceptor doesn't return a value.
1571 //
1572 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001573 bool ForceSet(Handle<Value> key,
1574 Handle<Value> value,
1575 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001576
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001577 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001579 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001580
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001581 /**
1582 * Gets the property attributes of a property which can be None or
1583 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1584 * None when the property doesn't exist.
1585 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001586 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001588 // TODO(1245389): Replace the type-specific versions of these
1589 // functions with generic ones that accept a Handle<Value> key.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001590 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001591
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001592 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001593
1594 // Delete a property on this object bypassing interceptors and
1595 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001596 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001597
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001598 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001599
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001600 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001602 bool SetAccessor(Handle<String> name,
1603 AccessorGetter getter,
1604 AccessorSetter setter = 0,
1605 Handle<Value> data = Handle<Value>(),
1606 AccessControl settings = DEFAULT,
1607 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001608
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001610 * Returns an array containing the names of the enumerable properties
1611 * of this object, including properties from prototype objects. The
1612 * array returned by this method contains the same values as would
1613 * be enumerated by a for-in statement over this object.
1614 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001615 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001616
1617 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001618 * This function has the same functionality as GetPropertyNames but
1619 * the returned array doesn't contain the names of properties from
1620 * prototype objects.
1621 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001622 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001623
1624 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001625 * Get the prototype object. This does not skip objects marked to
1626 * be skipped by __proto__ and it does not consult the security
1627 * handler.
1628 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001629 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001630
1631 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001632 * Set the prototype object. This does not skip objects marked to
1633 * be skipped by __proto__ and it does not consult the security
1634 * handler.
1635 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001636 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001637
1638 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001639 * Finds an instance of the given function template in the prototype
1640 * chain.
1641 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001642 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001643
1644 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 * Call builtin Object.prototype.toString on this object.
1646 * This is different from Value::ToString() that may call
1647 * user-defined toString function. This one does not.
1648 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001649 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001651 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001652 * Returns the function invoked as a constructor for this object.
1653 * May be the null value.
1654 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001655 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001656
1657 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001658 * Returns the name of the function invoked as a constructor for this object.
1659 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001660 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001661
kasper.lund212ac232008-07-16 07:07:30 +00001662 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001663 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001664
1665 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001666 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001667
kasper.lund212ac232008-07-16 07:07:30 +00001668 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001669 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001670
1671 /**
1672 * Gets a 2-byte-aligned native pointer from an internal field. This field
1673 * must have been set by SetAlignedPointerInInternalField, everything else
1674 * leads to undefined behavior.
1675 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001676 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001677
1678 /**
1679 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1680 * a field, GetAlignedPointerFromInternalField must be used, everything else
1681 * leads to undefined behavior.
1682 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001683 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001684
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001686 bool HasOwnProperty(Handle<String> key);
1687 bool HasRealNamedProperty(Handle<String> key);
1688 bool HasRealIndexedProperty(uint32_t index);
1689 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690
1691 /**
1692 * If result.IsEmpty() no real property was located in the prototype chain.
1693 * This means interceptors in the prototype chain are not called.
1694 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001695 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001696
1697 /**
1698 * If result.IsEmpty() no real property was located on the object or
1699 * in the prototype chain.
1700 * This means interceptors in the prototype chain are not called.
1701 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001702 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703
1704 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001705 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706
kasper.lund212ac232008-07-16 07:07:30 +00001707 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001708 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001710 /**
1711 * Turns on access check on the object if the object is an instance of
1712 * a template that has access check callbacks. If an object has no
1713 * access check info, the object cannot be accessed by anyone.
1714 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001715 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001716
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001717 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001718 * Returns the identity hash for this object. The current implementation
1719 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001720 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001721 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001722 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001723 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001724 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001725
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001726 /**
1727 * Access hidden properties on JavaScript objects. These properties are
1728 * hidden from the executing JavaScript and only accessible through the V8
1729 * C++ API. Hidden properties introduced by V8 internally (for example the
1730 * identity hash) are prefixed with "v8::".
1731 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001732 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1733 Local<Value> GetHiddenValue(Handle<String> key);
1734 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001735
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001736 /**
1737 * Returns true if this is an instance of an api function (one
1738 * created from a function created from a function template) and has
1739 * been modified since it was created. Note that this method is
1740 * conservative and may return true for objects that haven't actually
1741 * been modified.
1742 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001743 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001744
1745 /**
1746 * Clone this object with a fast but shallow copy. Values will point
1747 * to the same values as the original object.
1748 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001749 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001750
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001751 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001752 * Returns the context in which the object was created.
1753 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001754 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001755
1756 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001757 * Set the backing store of the indexed properties to be managed by the
1758 * embedding layer. Access to the indexed properties will follow the rules
1759 * spelled out in CanvasPixelArray.
1760 * Note: The embedding program still owns the data and needs to ensure that
1761 * the backing store is preserved while V8 has a reference.
1762 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001763 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1764 bool HasIndexedPropertiesInPixelData();
1765 uint8_t* GetIndexedPropertiesPixelData();
1766 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001767
ager@chromium.org3811b432009-10-28 14:53:37 +00001768 /**
1769 * Set the backing store of the indexed properties to be managed by the
1770 * embedding layer. Access to the indexed properties will follow the rules
1771 * spelled out for the CanvasArray subtypes in the WebGL specification.
1772 * Note: The embedding program still owns the data and needs to ensure that
1773 * the backing store is preserved while V8 has a reference.
1774 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001775 void SetIndexedPropertiesToExternalArrayData(void* data,
1776 ExternalArrayType array_type,
1777 int number_of_elements);
1778 bool HasIndexedPropertiesInExternalArrayData();
1779 void* GetIndexedPropertiesExternalArrayData();
1780 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1781 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001782
lrn@chromium.org1c092762011-05-09 09:42:16 +00001783 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001784 * Checks whether a callback is set by the
1785 * ObjectTemplate::SetCallAsFunctionHandler method.
1786 * When an Object is callable this method returns true.
1787 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001788 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001789
1790 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001791 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001792 * ObjectTemplate::SetCallAsFunctionHandler method.
1793 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001794 Local<Value> CallAsFunction(Handle<Object> recv,
1795 int argc,
1796 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001797
1798 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001799 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001800 * ObjectTemplate::SetCallAsFunctionHandler method.
1801 * Note: This method behaves like the Function::NewInstance method.
1802 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001803 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001804
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001805 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001806 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001807
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001809 Object();
1810 static void CheckCast(Value* obj);
1811 Local<Value> SlowGetInternalField(int index);
1812 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813};
1814
1815
1816/**
1817 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1818 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001819class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001821 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822
ager@chromium.org3e875802009-06-29 08:26:34 +00001823 /**
1824 * Clones an element at index |index|. Returns an empty
1825 * handle if cloning fails (for any reason).
1826 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001827 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001828
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001829 /**
1830 * Creates a JavaScript array with the given length. If the length
1831 * is negative the returned array will have length 0.
1832 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001833 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001834
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001835 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001837 Array();
1838 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839};
1840
1841
1842/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001843 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001845class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001847 Local<Object> NewInstance() const;
1848 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1849 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1850 void SetName(Handle<String> name);
1851 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001852
1853 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001854 * Name inferred from variable or property assignment of this function.
1855 * Used to facilitate debugging and profiling of JavaScript code written
1856 * in an OO style, where many functions are anonymous but are assigned
1857 * to object properties.
1858 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001859 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001860
1861 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001862 * Returns zero based line number of function body and
1863 * kLineOffsetNotFound if no information available.
1864 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001865 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001866 /**
1867 * Returns zero based column number of function body and
1868 * kLineOffsetNotFound if no information available.
1869 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001870 int GetScriptColumnNumber() const;
1871 Handle<Value> GetScriptId() const;
1872 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001873 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001874 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001875
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001877 Function();
1878 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879};
1880
1881
1882/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001883 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1884 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001885class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001886 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001887 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001888
1889 /**
1890 * A specialization of Value::NumberValue that is more efficient
1891 * because we know the structure of this object.
1892 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001893 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001894
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001895 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001896
1897 /**
1898 * Notification that the embedder has changed the time zone,
1899 * daylight savings time, or other date / time configuration
1900 * parameters. V8 keeps a cache of various values used for
1901 * date / time computation. This notification will reset
1902 * those cached values for the current context so that date /
1903 * time configuration changes would be reflected in the Date
1904 * object.
1905 *
1906 * This API should not be called more than needed as it will
1907 * negatively impact the performance of date operations.
1908 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001909 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001910
1911 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001912 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001913};
1914
1915
1916/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001917 * A Number object (ECMA-262, 4.3.21).
1918 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001919class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001920 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001921 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001922
1923 /**
1924 * Returns the Number held by the object.
1925 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001926 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001927
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001928 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001929
1930 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001931 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001932};
1933
1934
1935/**
1936 * A Boolean object (ECMA-262, 4.3.15).
1937 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001938class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001939 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001940 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001941
1942 /**
1943 * Returns the Boolean held by the object.
1944 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001945 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001946
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001947 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001948
1949 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001950 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001951};
1952
1953
1954/**
1955 * A String object (ECMA-262, 4.3.18).
1956 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001957class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001958 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001959 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001960
1961 /**
1962 * Returns the String held by the object.
1963 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001964 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001965
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001966 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001967
1968 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001969 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001970};
1971
1972
1973/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001974 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1975 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001976class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001977 public:
1978 /**
1979 * Regular expression flag bits. They can be or'ed to enable a set
1980 * of flags.
1981 */
1982 enum Flags {
1983 kNone = 0,
1984 kGlobal = 1,
1985 kIgnoreCase = 2,
1986 kMultiline = 4
1987 };
1988
1989 /**
1990 * Creates a regular expression from the given pattern string and
1991 * the flags bit field. May throw a JavaScript exception as
1992 * described in ECMA-262, 15.10.4.1.
1993 *
1994 * For example,
1995 * RegExp::New(v8::String::New("foo"),
1996 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1997 * is equivalent to evaluating "/foo/gm".
1998 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001999 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002000
2001 /**
2002 * Returns the value of the source property: a string representing
2003 * the regular expression.
2004 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002005 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002006
2007 /**
2008 * Returns the flags bit field.
2009 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002010 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002011
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002012 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002013
2014 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002015 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002016};
2017
2018
2019/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002020 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2021 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002023class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002025 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002026 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002027 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002029 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030};
2031
2032
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002033// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034
2035
2036/**
2037 * The superclass of object and function templates.
2038 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002039class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 public:
2041 /** Adds a property to each instance created by this template.*/
2042 void Set(Handle<String> name, Handle<Data> value,
2043 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002044 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 private:
2046 Template();
2047
2048 friend class ObjectTemplate;
2049 friend class FunctionTemplate;
2050};
2051
2052
2053/**
2054 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002055 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 * including the receiver, the number and values of arguments, and
2057 * the holder of the function.
2058 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002059class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002061 V8_INLINE(int Length() const);
2062 V8_INLINE(Local<Value> operator[](int i) const);
2063 V8_INLINE(Local<Function> Callee() const);
2064 V8_INLINE(Local<Object> This() const);
2065 V8_INLINE(Local<Object> Holder() const);
2066 V8_INLINE(bool IsConstructCall() const);
2067 V8_INLINE(Local<Value> Data() const);
2068 V8_INLINE(Isolate* GetIsolate() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002071 static const int kIsolateIndex = 0;
2072 static const int kDataIndex = -1;
2073 static const int kCalleeIndex = -2;
2074 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 friend class ImplementationUtilities;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002077 V8_INLINE(Arguments(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002078 internal::Object** values,
2079 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002080 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002081 internal::Object** implicit_args_;
2082 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002084 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085};
2086
2087
2088/**
2089 * The information passed to an accessor callback about the context
2090 * of the property access.
2091 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002092class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002094 V8_INLINE(AccessorInfo(internal::Object** args))
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002095 : args_(args) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002096 V8_INLINE(Isolate* GetIsolate() const);
2097 V8_INLINE(Local<Value> Data() const);
2098 V8_INLINE(Local<Object> This() const);
2099 V8_INLINE(Local<Object> Holder() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002102 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103};
2104
2105
2106typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 * NamedProperty[Getter|Setter] are used as interceptors on object.
2110 * See ObjectTemplate::SetNamedPropertyHandler.
2111 */
2112typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2113 const AccessorInfo& info);
2114
2115
2116/**
2117 * Returns the value if the setter intercepts the request.
2118 * Otherwise, returns an empty handle.
2119 */
2120typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2121 Local<Value> value,
2122 const AccessorInfo& info);
2123
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124/**
2125 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002126 * The result is an integer encoding property attributes (like v8::None,
2127 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002129typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2130 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131
2132
2133/**
2134 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002135 * The return value is true if the property could be deleted and false
2136 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 */
2138typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2139 const AccessorInfo& info);
2140
2141/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002142 * Returns an array containing the names of the properties the named
2143 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144 */
2145typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2146
v8.team.kasperl727e9952008-09-02 14:56:44 +00002147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002149 * Returns the value of the property if the getter intercepts the
2150 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 */
2152typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2153 const AccessorInfo& info);
2154
2155
2156/**
2157 * Returns the value if the setter intercepts the request.
2158 * Otherwise, returns an empty handle.
2159 */
2160typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2161 Local<Value> value,
2162 const AccessorInfo& info);
2163
2164
2165/**
2166 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002167 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002169typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2170 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171
2172/**
2173 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002174 * The return value is true if the property could be deleted and false
2175 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 */
2177typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2178 const AccessorInfo& info);
2179
v8.team.kasperl727e9952008-09-02 14:56:44 +00002180/**
2181 * Returns an array containing the indices of the properties the
2182 * indexed property getter intercepts.
2183 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2185
2186
2187/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002188 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 */
2190enum AccessType {
2191 ACCESS_GET,
2192 ACCESS_SET,
2193 ACCESS_HAS,
2194 ACCESS_DELETE,
2195 ACCESS_KEYS
2196};
2197
v8.team.kasperl727e9952008-09-02 14:56:44 +00002198
2199/**
2200 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002201 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002202 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002203typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 Local<Value> key,
2205 AccessType type,
2206 Local<Value> data);
2207
v8.team.kasperl727e9952008-09-02 14:56:44 +00002208
2209/**
2210 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002211 * property with the given index 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 (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214 uint32_t index,
2215 AccessType type,
2216 Local<Value> data);
2217
2218
2219/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002220 * A FunctionTemplate is used to create functions at runtime. There
2221 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002222 * context. The lifetime of the created function is equal to the
2223 * lifetime of the context. So in case the embedder needs to create
2224 * temporary functions that can be collected using Scripts is
2225 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 *
2227 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002228 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002230 * A FunctionTemplate has a corresponding instance template which is
2231 * used to create object instances when the function is used as a
2232 * constructor. Properties added to the instance template are added to
2233 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234 *
2235 * A FunctionTemplate can have a prototype template. The prototype template
2236 * is used to create the prototype object of the function.
2237 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002238 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002240 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2242 * t->Set("func_property", v8::Number::New(1));
2243 *
2244 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2245 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2246 * proto_t->Set("proto_const", v8::Number::New(2));
2247 *
2248 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2249 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2250 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2251 * instance_t->Set("instance_property", Number::New(3));
2252 *
2253 * v8::Local<v8::Function> function = t->GetFunction();
2254 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002255 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 *
2257 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002258 * and "instance" for the instance object created above. The function
2259 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002261 * \code
2262 * func_property in function == true;
2263 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002265 * function.prototype.proto_method() invokes 'InvokeCallback'
2266 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002268 * instance instanceof function == true;
2269 * instance.instance_accessor calls 'InstanceAccessorCallback'
2270 * instance.instance_property == 3;
2271 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002273 * A FunctionTemplate can inherit from another one by calling the
2274 * FunctionTemplate::Inherit method. The following graph illustrates
2275 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002277 * \code
2278 * FunctionTemplate Parent -> Parent() . prototype -> { }
2279 * ^ ^
2280 * | Inherit(Parent) | .__proto__
2281 * | |
2282 * FunctionTemplate Child -> Child() . prototype -> { }
2283 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002285 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2286 * object of the Child() function has __proto__ pointing to the
2287 * Parent() function's prototype object. An instance of the Child
2288 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002290 * Let Parent be the FunctionTemplate initialized in the previous
2291 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002293 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 * Local<FunctionTemplate> parent = t;
2295 * Local<FunctionTemplate> child = FunctionTemplate::New();
2296 * child->Inherit(parent);
2297 *
2298 * Local<Function> child_function = child->GetFunction();
2299 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002300 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002302 * The Child function and Child instance will have the following
2303 * properties:
2304 *
2305 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002307 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002309 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002311class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 public:
2313 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002314 static Local<FunctionTemplate> New(
2315 InvocationCallback callback = 0,
2316 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002317 Handle<Signature> signature = Handle<Signature>(),
2318 int length = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 /** Returns the unique function instance in the current execution context.*/
2320 Local<Function> GetFunction();
2321
v8.team.kasperl727e9952008-09-02 14:56:44 +00002322 /**
2323 * Set the call-handler callback for a FunctionTemplate. This
2324 * callback is called whenever the function created from this
2325 * FunctionTemplate is called.
2326 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327 void SetCallHandler(InvocationCallback callback,
2328 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002330 /** Set the predefined length property for the FunctionTemplate. */
2331 void SetLength(int length);
2332
v8.team.kasperl727e9952008-09-02 14:56:44 +00002333 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 Local<ObjectTemplate> InstanceTemplate();
2335
2336 /** Causes the function template to inherit from a parent function template.*/
2337 void Inherit(Handle<FunctionTemplate> parent);
2338
2339 /**
2340 * A PrototypeTemplate is the template used to create the prototype object
2341 * of the function created by this template.
2342 */
2343 Local<ObjectTemplate> PrototypeTemplate();
2344
v8.team.kasperl727e9952008-09-02 14:56:44 +00002345 /**
2346 * Set the class name of the FunctionTemplate. This is used for
2347 * printing objects created with the function created from the
2348 * FunctionTemplate as its constructor.
2349 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 void SetClassName(Handle<String> name);
2351
2352 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002353 * Determines whether the __proto__ accessor ignores instances of
2354 * the function template. If instances of the function template are
2355 * ignored, __proto__ skips all instances and instead returns the
2356 * next object in the prototype chain.
2357 *
2358 * Call with a value of true to make the __proto__ accessor ignore
2359 * instances of the function template. Call with a value of false
2360 * to make the __proto__ accessor not ignore instances of the
2361 * function template. By default, instances of a function template
2362 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002363 */
2364 void SetHiddenPrototype(bool value);
2365
2366 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002367 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2368 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002369 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002370 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002371
2372 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002373 * Returns true if the given object is an instance of this function
2374 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002375 */
2376 bool HasInstance(Handle<Value> object);
2377
2378 private:
2379 FunctionTemplate();
2380 void AddInstancePropertyAccessor(Handle<String> name,
2381 AccessorGetter getter,
2382 AccessorSetter setter,
2383 Handle<Value> data,
2384 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002385 PropertyAttribute attributes,
2386 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2388 NamedPropertySetter setter,
2389 NamedPropertyQuery query,
2390 NamedPropertyDeleter remover,
2391 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002392 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2394 IndexedPropertySetter setter,
2395 IndexedPropertyQuery query,
2396 IndexedPropertyDeleter remover,
2397 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002398 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002399 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2400 Handle<Value> data);
2401
2402 friend class Context;
2403 friend class ObjectTemplate;
2404};
2405
2406
2407/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002408 * An ObjectTemplate is used to create objects at runtime.
2409 *
2410 * Properties added to an ObjectTemplate are added to each object
2411 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002412 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002413class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002415 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002416 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002417
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 /** Creates a new instance of this template.*/
2419 Local<Object> NewInstance();
2420
2421 /**
2422 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002423 *
2424 * Whenever the property with the given name is accessed on objects
2425 * created from this ObjectTemplate the getter and setter callbacks
2426 * are called instead of getting and setting the property directly
2427 * on the JavaScript object.
2428 *
2429 * \param name The name of the property for which an accessor is added.
2430 * \param getter The callback to invoke when getting the property.
2431 * \param setter The callback to invoke when setting the property.
2432 * \param data A piece of data that will be passed to the getter and setter
2433 * callbacks whenever they are invoked.
2434 * \param settings Access control settings for the accessor. This is a bit
2435 * field consisting of one of more of
2436 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2437 * The default is to not allow cross-context access.
2438 * ALL_CAN_READ means that all cross-context reads are allowed.
2439 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2440 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2441 * cross-context access.
2442 * \param attribute The attributes of the property for which an accessor
2443 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002444 * \param signature The signature describes valid receivers for the accessor
2445 * and is used to perform implicit instance checks against them. If the
2446 * receiver is incompatible (i.e. is not an instance of the constructor as
2447 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2448 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449 */
2450 void SetAccessor(Handle<String> name,
2451 AccessorGetter getter,
2452 AccessorSetter setter = 0,
2453 Handle<Value> data = Handle<Value>(),
2454 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002455 PropertyAttribute attribute = None,
2456 Handle<AccessorSignature> signature =
2457 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458
2459 /**
2460 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002461 *
2462 * Whenever a named property is accessed on objects created from
2463 * this object template, the provided callback is invoked instead of
2464 * accessing the property directly on the JavaScript object.
2465 *
2466 * \param getter The callback to invoke when getting a property.
2467 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002468 * \param query The callback to invoke to check if a property is present,
2469 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002470 * \param deleter The callback to invoke when deleting a property.
2471 * \param enumerator The callback to invoke to enumerate all the named
2472 * properties of an object.
2473 * \param data A piece of data that will be passed to the callbacks
2474 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475 */
2476 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2477 NamedPropertySetter setter = 0,
2478 NamedPropertyQuery query = 0,
2479 NamedPropertyDeleter deleter = 0,
2480 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002481 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482
2483 /**
2484 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002485 *
2486 * Whenever an indexed property is accessed on objects created from
2487 * this object template, the provided callback is invoked instead of
2488 * accessing the property directly on the JavaScript object.
2489 *
2490 * \param getter The callback to invoke when getting a property.
2491 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002492 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002493 * \param deleter The callback to invoke when deleting a property.
2494 * \param enumerator The callback to invoke to enumerate all the indexed
2495 * properties of an object.
2496 * \param data A piece of data that will be passed to the callbacks
2497 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498 */
2499 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2500 IndexedPropertySetter setter = 0,
2501 IndexedPropertyQuery query = 0,
2502 IndexedPropertyDeleter deleter = 0,
2503 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002504 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002505
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506 /**
2507 * Sets the callback to be used when calling instances created from
2508 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002509 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510 * function.
2511 */
2512 void SetCallAsFunctionHandler(InvocationCallback callback,
2513 Handle<Value> data = Handle<Value>());
2514
v8.team.kasperl727e9952008-09-02 14:56:44 +00002515 /**
2516 * Mark object instances of the template as undetectable.
2517 *
2518 * In many ways, undetectable objects behave as though they are not
2519 * there. They behave like 'undefined' in conditionals and when
2520 * printed. However, properties can be accessed and called as on
2521 * normal objects.
2522 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523 void MarkAsUndetectable();
2524
v8.team.kasperl727e9952008-09-02 14:56:44 +00002525 /**
2526 * Sets access check callbacks on the object template.
2527 *
2528 * When accessing properties on instances of this object template,
2529 * the access check callback will be called to determine whether or
2530 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002531 * The last parameter specifies whether access checks are turned
2532 * on by default on instances. If access checks are off by default,
2533 * they can be turned on on individual instances by calling
2534 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002535 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002536 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2537 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002538 Handle<Value> data = Handle<Value>(),
2539 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540
kasper.lund212ac232008-07-16 07:07:30 +00002541 /**
2542 * Gets the number of internal fields for objects generated from
2543 * this template.
2544 */
2545 int InternalFieldCount();
2546
2547 /**
2548 * Sets the number of internal fields for objects generated from
2549 * this template.
2550 */
2551 void SetInternalFieldCount(int value);
2552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553 private:
2554 ObjectTemplate();
2555 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2556 friend class FunctionTemplate;
2557};
2558
2559
2560/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002561 * A Signature specifies which receivers and arguments are valid
2562 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002564class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565 public:
2566 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2567 Handle<FunctionTemplate>(),
2568 int argc = 0,
2569 Handle<FunctionTemplate> argv[] = 0);
2570 private:
2571 Signature();
2572};
2573
2574
2575/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002576 * An AccessorSignature specifies which receivers are valid parameters
2577 * to an accessor callback.
2578 */
2579class V8EXPORT AccessorSignature : public Data {
2580 public:
2581 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2582 Handle<FunctionTemplate>());
2583 private:
2584 AccessorSignature();
2585};
2586
2587
2588/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002589 * A utility for determining the type of objects based on the template
2590 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002592class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 public:
2594 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2595 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2596 int match(Handle<Value> value);
2597 private:
2598 TypeSwitch();
2599};
2600
2601
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002602// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002604class V8EXPORT ExternalAsciiStringResourceImpl
2605 : public String::ExternalAsciiStringResource {
2606 public:
2607 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2608 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2609 : data_(data), length_(length) {}
2610 const char* data() const { return data_; }
2611 size_t length() const { return length_; }
2612
2613 private:
2614 const char* data_;
2615 size_t length_;
2616};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617
2618/**
2619 * Ignore
2620 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002621class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002623 // Note that the strings passed into this constructor must live as long
2624 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002626 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002628 const char** deps = 0,
2629 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 virtual ~Extension() { }
2631 virtual v8::Handle<v8::FunctionTemplate>
2632 GetNativeFunction(v8::Handle<v8::String> name) {
2633 return v8::Handle<v8::FunctionTemplate>();
2634 }
2635
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002636 const char* name() const { return name_; }
2637 size_t source_length() const { return source_length_; }
2638 const String::ExternalAsciiStringResource* source() const {
2639 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 int dependency_count() { return dep_count_; }
2641 const char** dependencies() { return deps_; }
2642 void set_auto_enable(bool value) { auto_enable_ = value; }
2643 bool auto_enable() { return auto_enable_; }
2644
2645 private:
2646 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002647 size_t source_length_; // expected to initialize before source_
2648 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649 int dep_count_;
2650 const char** deps_;
2651 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002652
2653 // Disallow copying and assigning.
2654 Extension(const Extension&);
2655 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656};
2657
2658
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002659void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660
2661
2662/**
2663 * Ignore
2664 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002665class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002667 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668 RegisterExtension(extension);
2669 }
2670};
2671
2672
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002673// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674
2675
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002676Handle<Primitive> V8EXPORT Undefined();
2677Handle<Primitive> V8EXPORT Null();
2678Handle<Boolean> V8EXPORT True();
2679Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002681V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
2682V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
2683V8_INLINE(Handle<Boolean> True(Isolate* isolate));
2684V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686
2687/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002688 * A set of constraints that specifies the limits of the runtime's memory use.
2689 * You must set the heap size before initializing the VM - the size cannot be
2690 * adjusted after the VM is initialized.
2691 *
2692 * If you are using threads then you should hold the V8::Locker lock while
2693 * setting the stack limit and you must set a non-default stack limit separately
2694 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002696class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697 public:
2698 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002699 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002701 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002703 int max_executable_size() { return max_executable_size_; }
2704 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002705 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002706 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2708 private:
2709 int max_young_space_size_;
2710 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002711 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002712 uint32_t* stack_limit_;
2713};
2714
2715
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002716bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717
2718
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002719// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720
2721
2722typedef void (*FatalErrorCallback)(const char* location, const char* message);
2723
2724
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002725typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726
2727
2728/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002729 * Schedules an exception to be thrown when returning to JavaScript. When an
2730 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002732 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002734Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735
2736/**
2737 * Create new error objects by calling the corresponding error object
2738 * constructor with the message.
2739 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002740class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741 public:
2742 static Local<Value> RangeError(Handle<String> message);
2743 static Local<Value> ReferenceError(Handle<String> message);
2744 static Local<Value> SyntaxError(Handle<String> message);
2745 static Local<Value> TypeError(Handle<String> message);
2746 static Local<Value> Error(Handle<String> message);
2747};
2748
2749
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002750// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002752typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002754typedef void* (*CreateHistogramCallback)(const char* name,
2755 int min,
2756 int max,
2757 size_t buckets);
2758
2759typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2760
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002761// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002762 enum ObjectSpace {
2763 kObjectSpaceNewSpace = 1 << 0,
2764 kObjectSpaceOldPointerSpace = 1 << 1,
2765 kObjectSpaceOldDataSpace = 1 << 2,
2766 kObjectSpaceCodeSpace = 1 << 3,
2767 kObjectSpaceMapSpace = 1 << 4,
2768 kObjectSpaceLoSpace = 1 << 5,
2769
2770 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2771 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2772 kObjectSpaceLoSpace
2773 };
2774
2775 enum AllocationAction {
2776 kAllocationActionAllocate = 1 << 0,
2777 kAllocationActionFree = 1 << 1,
2778 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2779 };
2780
2781typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2782 AllocationAction action,
2783 int size);
2784
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002785// --- Leave Script Callback ---
2786typedef void (*CallCompletedCallback)();
2787
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002788// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2790 AccessType type,
2791 Local<Value> data);
2792
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002793// --- AllowCodeGenerationFromStrings callbacks ---
2794
2795/**
2796 * Callback to check if code generation from strings is allowed. See
2797 * Context::AllowCodeGenerationFromStrings.
2798 */
2799typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2800
2801// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802
2803/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002804 * Applications can register callback functions which will be called
2805 * before and after a garbage collection. Allocations are not
2806 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002807 * objects (set or delete properties for example) since it is possible
2808 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002810enum GCType {
2811 kGCTypeScavenge = 1 << 0,
2812 kGCTypeMarkSweepCompact = 1 << 1,
2813 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2814};
2815
2816enum GCCallbackFlags {
2817 kNoGCCallbackFlags = 0,
2818 kGCCallbackFlagCompacted = 1 << 0
2819};
2820
2821typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2822typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2823
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824typedef void (*GCCallback)();
2825
2826
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002827/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002828 * Collection of V8 heap information.
2829 *
2830 * Instances of this class can be passed to v8::V8::HeapStatistics to
2831 * get heap statistics from V8.
2832 */
2833class V8EXPORT HeapStatistics {
2834 public:
2835 HeapStatistics();
2836 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002837 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00002838 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002839 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002840 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002841
2842 private:
2843 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002844 void set_total_heap_size_executable(size_t size) {
2845 total_heap_size_executable_ = size;
2846 }
danno@chromium.org72204d52012-10-31 10:02:10 +00002847 void set_total_physical_size(size_t size) {
2848 total_physical_size_ = size;
2849 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002850 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002851 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002852
2853 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002854 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00002855 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002856 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002857 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002858
2859 friend class V8;
2860};
2861
2862
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002863class RetainedObjectInfo;
2864
ager@chromium.org3811b432009-10-28 14:53:37 +00002865/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002866 * Isolate represents an isolated instance of the V8 engine. V8
2867 * isolates have completely separate states. Objects from one isolate
2868 * must not be used in other isolates. When V8 is initialized a
2869 * default isolate is implicitly created and entered. The embedder
2870 * can create additional isolates and use them in parallel in multiple
2871 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002872 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002873 */
2874class V8EXPORT Isolate {
2875 public:
2876 /**
2877 * Stack-allocated class which sets the isolate for all operations
2878 * executed within a local scope.
2879 */
2880 class V8EXPORT Scope {
2881 public:
2882 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2883 isolate->Enter();
2884 }
2885
2886 ~Scope() { isolate_->Exit(); }
2887
2888 private:
2889 Isolate* const isolate_;
2890
2891 // Prevent copying of Scope objects.
2892 Scope(const Scope&);
2893 Scope& operator=(const Scope&);
2894 };
2895
2896 /**
2897 * Creates a new isolate. Does not change the currently entered
2898 * isolate.
2899 *
2900 * When an isolate is no longer used its resources should be freed
2901 * by calling Dispose(). Using the delete operator is not allowed.
2902 */
2903 static Isolate* New();
2904
2905 /**
2906 * Returns the entered isolate for the current thread or NULL in
2907 * case there is no current isolate.
2908 */
2909 static Isolate* GetCurrent();
2910
2911 /**
2912 * Methods below this point require holding a lock (using Locker) in
2913 * a multi-threaded environment.
2914 */
2915
2916 /**
2917 * Sets this isolate as the entered one for the current thread.
2918 * Saves the previously entered one (if any), so that it can be
2919 * restored when exiting. Re-entering an isolate is allowed.
2920 */
2921 void Enter();
2922
2923 /**
2924 * Exits this isolate by restoring the previously entered one in the
2925 * current thread. The isolate may still stay the same, if it was
2926 * entered more than once.
2927 *
2928 * Requires: this == Isolate::GetCurrent().
2929 */
2930 void Exit();
2931
2932 /**
2933 * Disposes the isolate. The isolate must not be entered by any
2934 * thread to be disposable.
2935 */
2936 void Dispose();
2937
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002938 /**
2939 * Associate embedder-specific data with the isolate
2940 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002941 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002942
2943 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002944 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002945 * Returns NULL if SetData has never been called.
2946 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002947 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002948
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002949 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002950 Isolate();
2951 Isolate(const Isolate&);
2952 ~Isolate();
2953 Isolate& operator=(const Isolate&);
2954 void* operator new(size_t size);
2955 void operator delete(void*, size_t);
2956};
2957
2958
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002959class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002960 public:
2961 enum CompressionAlgorithm {
2962 kUncompressed,
2963 kBZip2
2964 };
2965
2966 const char* data;
2967 int compressed_size;
2968 int raw_size;
2969};
2970
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002971
2972/**
2973 * A helper class for driving V8 startup data decompression. It is based on
2974 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2975 * for an embedder to use this class, instead, API functions can be used
2976 * directly.
2977 *
2978 * For an example of the class usage, see the "shell.cc" sample application.
2979 */
2980class V8EXPORT StartupDataDecompressor { // NOLINT
2981 public:
2982 StartupDataDecompressor();
2983 virtual ~StartupDataDecompressor();
2984 int Decompress();
2985
2986 protected:
2987 virtual int DecompressData(char* raw_data,
2988 int* raw_data_size,
2989 const char* compressed_data,
2990 int compressed_data_size) = 0;
2991
2992 private:
2993 char** raw_data;
2994};
2995
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002996
2997/**
2998 * EntropySource is used as a callback function when v8 needs a source
2999 * of entropy.
3000 */
3001typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3002
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003003
3004/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003005 * ReturnAddressLocationResolver is used as a callback function when v8 is
3006 * resolving the location of a return address on the stack. Profilers that
3007 * change the return address on the stack can use this to resolve the stack
3008 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003009 *
3010 * \param return_addr_location points to a location on stack where a machine
3011 * return address resides.
3012 * \returns either return_addr_location, or else a pointer to the profiler's
3013 * copy of the original return address.
3014 *
3015 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003016 */
3017typedef uintptr_t (*ReturnAddressLocationResolver)(
3018 uintptr_t return_addr_location);
3019
3020
3021/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003022 * FunctionEntryHook is the type of the profile entry hook called at entry to
3023 * any generated function when function-level profiling is enabled.
3024 *
3025 * \param function the address of the function that's being entered.
3026 * \param return_addr_location points to a location on stack where the machine
3027 * return address resides. This can be used to identify the caller of
3028 * \p function, and/or modified to divert execution when \p function exits.
3029 *
3030 * \note the entry hook must not cause garbage collection.
3031 */
3032typedef void (*FunctionEntryHook)(uintptr_t function,
3033 uintptr_t return_addr_location);
3034
3035
3036/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003037 * A JIT code event is issued each time code is added, moved or removed.
3038 *
3039 * \note removal events are not currently issued.
3040 */
3041struct JitCodeEvent {
3042 enum EventType {
3043 CODE_ADDED,
3044 CODE_MOVED,
3045 CODE_REMOVED
3046 };
3047
3048 // Type of event.
3049 EventType type;
3050 // Start of the instructions.
3051 void* code_start;
3052 // Size of the instructions.
3053 size_t code_len;
3054
3055 union {
3056 // Only valid for CODE_ADDED.
3057 struct {
3058 // Name of the object associated with the code, note that the string is
3059 // not zero-terminated.
3060 const char* str;
3061 // Number of chars in str.
3062 size_t len;
3063 } name;
3064 // New location of instructions. Only valid for CODE_MOVED.
3065 void* new_code_start;
3066 };
3067};
3068
3069/**
3070 * Option flags passed to the SetJitCodeEventHandler function.
3071 */
3072enum JitCodeEventOptions {
3073 kJitCodeEventDefault = 0,
3074 // Generate callbacks for already existent code.
3075 kJitCodeEventEnumExisting = 1
3076};
3077
3078
3079/**
3080 * Callback function passed to SetJitCodeEventHandler.
3081 *
3082 * \param event code add, move or removal event.
3083 */
3084typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3085
3086
3087/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003088 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003089 */
3090class V8EXPORT ExternalResourceVisitor { // NOLINT
3091 public:
3092 virtual ~ExternalResourceVisitor() {}
3093 virtual void VisitExternalString(Handle<String> string) {}
3094};
3095
3096
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003097/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003098 * Interface for iterating through all the persistent handles in the heap.
3099 */
3100class V8EXPORT PersistentHandleVisitor { // NOLINT
3101 public:
3102 virtual ~PersistentHandleVisitor() {}
3103 virtual void VisitPersistentHandle(Persistent<Value> value,
3104 uint16_t class_id) {}
3105};
3106
3107
3108/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109 * Container class for static utility functions.
3110 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003111class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003112 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003113 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003114 static void SetFatalErrorHandler(FatalErrorCallback that);
3115
v8.team.kasperl727e9952008-09-02 14:56:44 +00003116 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003117 * Set the callback to invoke to check if code generation from
3118 * strings should be allowed.
3119 */
3120 static void SetAllowCodeGenerationFromStringsCallback(
3121 AllowCodeGenerationFromStringsCallback that);
3122
3123 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003124 * Ignore out-of-memory exceptions.
3125 *
3126 * V8 running out of memory is treated as a fatal error by default.
3127 * This means that the fatal error handler is called and that V8 is
3128 * terminated.
3129 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003130 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003131 * out-of-memory situation as a fatal error. This way, the contexts
3132 * that did not cause the out of memory problem might be able to
3133 * continue execution.
3134 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135 static void IgnoreOutOfMemoryException();
3136
v8.team.kasperl727e9952008-09-02 14:56:44 +00003137 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003138 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003139 * fatal errors such as out-of-memory situations.
3140 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141 static bool IsDead();
3142
3143 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003144 * The following 4 functions are to be used when V8 is built with
3145 * the 'compress_startup_data' flag enabled. In this case, the
3146 * embedder must decompress startup data prior to initializing V8.
3147 *
3148 * This is how interaction with V8 should look like:
3149 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3150 * v8::StartupData* compressed_data =
3151 * new v8::StartupData[compressed_data_count];
3152 * v8::V8::GetCompressedStartupData(compressed_data);
3153 * ... decompress data (compressed_data can be updated in-place) ...
3154 * v8::V8::SetDecompressedStartupData(compressed_data);
3155 * ... now V8 can be initialized
3156 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003157 *
3158 * A helper class StartupDataDecompressor is provided. It implements
3159 * the protocol of the interaction described above, and can be used in
3160 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003161 */
3162 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3163 static int GetCompressedStartupDataCount();
3164 static void GetCompressedStartupData(StartupData* compressed_data);
3165 static void SetDecompressedStartupData(StartupData* decompressed_data);
3166
3167 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003168 * Adds a message listener.
3169 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003170 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003171 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003172 */
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003173 static bool AddMessageListener(MessageCallback that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003174
3175 /**
3176 * Remove all message listeners from the specified callback function.
3177 */
3178 static void RemoveMessageListeners(MessageCallback that);
3179
3180 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003181 * Tells V8 to capture current stack trace when uncaught exception occurs
3182 * and report it to the message listeners. The option is off by default.
3183 */
3184 static void SetCaptureStackTraceForUncaughtExceptions(
3185 bool capture,
3186 int frame_limit = 10,
3187 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3188
3189 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003190 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003191 */
3192 static void SetFlagsFromString(const char* str, int length);
3193
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003194 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003195 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003196 */
3197 static void SetFlagsFromCommandLine(int* argc,
3198 char** argv,
3199 bool remove_flags);
3200
kasper.lund7276f142008-07-30 08:49:36 +00003201 /** Get the version string. */
3202 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003203
3204 /**
3205 * Enables the host application to provide a mechanism for recording
3206 * statistics counters.
3207 */
3208 static void SetCounterFunction(CounterLookupCallback);
3209
3210 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003211 * Enables the host application to provide a mechanism for recording
3212 * histograms. The CreateHistogram function returns a
3213 * histogram which will later be passed to the AddHistogramSample
3214 * function.
3215 */
3216 static void SetCreateHistogramFunction(CreateHistogramCallback);
3217 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3218
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003219 /** Callback function for reporting failed access checks.*/
3220 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3221
3222 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003223 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003224 * garbage collection. Allocations are not allowed in the
3225 * callback function, you therefore cannot manipulate objects (set
3226 * or delete properties for example) since it is possible such
3227 * operations will result in the allocation of objects. It is possible
3228 * to specify the GCType filter for your callback. But it is not possible to
3229 * register the same callback function two times with different
3230 * GCType filters.
3231 */
3232 static void AddGCPrologueCallback(
3233 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3234
3235 /**
3236 * This function removes callback which was installed by
3237 * AddGCPrologueCallback function.
3238 */
3239 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3240
3241 /**
3242 * The function is deprecated. Please use AddGCPrologueCallback instead.
3243 * Enables the host application to receive a notification before a
3244 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003245 * callback function, you therefore cannot manipulate objects (set
3246 * or delete properties for example) since it is possible such
3247 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003248 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003249 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003250
3251 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003252 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003253 * garbage collection. Allocations are not allowed in the
3254 * callback function, you therefore cannot manipulate objects (set
3255 * or delete properties for example) since it is possible such
3256 * operations will result in the allocation of objects. It is possible
3257 * to specify the GCType filter for your callback. But it is not possible to
3258 * register the same callback function two times with different
3259 * GCType filters.
3260 */
3261 static void AddGCEpilogueCallback(
3262 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3263
3264 /**
3265 * This function removes callback which was installed by
3266 * AddGCEpilogueCallback function.
3267 */
3268 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3269
3270 /**
3271 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3272 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003273 * major garbage collection. Allocations are not allowed in the
3274 * callback function, you therefore cannot manipulate objects (set
3275 * or delete properties for example) since it is possible such
3276 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003277 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003278 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003279
3280 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003281 * Enables the host application to provide a mechanism to be notified
3282 * and perform custom logging when V8 Allocates Executable Memory.
3283 */
3284 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3285 ObjectSpace space,
3286 AllocationAction action);
3287
3288 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003289 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003290 */
3291 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3292
3293 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003294 * Adds a callback to notify the host application when a script finished
3295 * running. If a script re-enters the runtime during executing, the
3296 * CallCompletedCallback is only invoked when the outer-most script
3297 * execution ends. Executing scripts inside the callback do not trigger
3298 * further callbacks.
3299 */
3300 static void AddCallCompletedCallback(CallCompletedCallback callback);
3301
3302 /**
3303 * Removes callback that was installed by AddCallCompletedCallback.
3304 */
3305 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3306
3307 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003308 * Allows the host application to group objects together. If one
3309 * object in the group is alive, all objects in the group are alive.
3310 * After each garbage collection, object groups are removed. It is
3311 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003312 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003313 * wrapper objects. Object groups for all dependent handles need to
3314 * be provided for kGCTypeMarkSweepCompact collections, for all other
3315 * garbage collection types it is sufficient to provide object groups
3316 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003317 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003318 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003319 static void AddObjectGroup(Persistent<Value>* objects,
3320 size_t length,
3321 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003322 static void AddObjectGroup(Isolate* isolate,
3323 Persistent<Value>* objects,
3324 size_t length,
3325 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003326
3327 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003328 * Allows the host application to declare implicit references between
3329 * the objects: if |parent| is alive, all |children| are alive too.
3330 * After each garbage collection, all implicit references
3331 * are removed. It is intended to be used in the before-garbage-collection
3332 * callback function.
3333 */
3334 static void AddImplicitReferences(Persistent<Object> parent,
3335 Persistent<Value>* children,
3336 size_t length);
3337
3338 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003339 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003340 * initialize from scratch. This function is called implicitly if
3341 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003342 */
3343 static bool Initialize();
3344
kasper.lund7276f142008-07-30 08:49:36 +00003345 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003346 * Allows the host application to provide a callback which can be used
3347 * as a source of entropy for random number generators.
3348 */
3349 static void SetEntropySource(EntropySource source);
3350
3351 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003352 * Allows the host application to provide a callback that allows v8 to
3353 * cooperate with a profiler that rewrites return addresses on stack.
3354 */
3355 static void SetReturnAddressLocationResolver(
3356 ReturnAddressLocationResolver return_address_resolver);
3357
3358 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003359 * Allows the host application to provide the address of a function that's
3360 * invoked on entry to every V8-generated function.
3361 * Note that \p entry_hook is invoked at the very start of each
3362 * generated function.
3363 *
3364 * \param entry_hook a function that will be invoked on entry to every
3365 * V8-generated function.
3366 * \returns true on success on supported platforms, false on failure.
3367 * \note Setting a new entry hook function when one is already active will
3368 * fail.
3369 */
3370 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3371
3372 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003373 * Allows the host application to provide the address of a function that is
3374 * notified each time code is added, moved or removed.
3375 *
3376 * \param options options for the JIT code event handler.
3377 * \param event_handler the JIT code event handler, which will be invoked
3378 * each time code is added, moved or removed.
3379 * \note \p event_handler won't get notified of existent code.
3380 * \note since code removal notifications are not currently issued, the
3381 * \p event_handler may get notifications of code that overlaps earlier
3382 * code notifications. This happens when code areas are reused, and the
3383 * earlier overlapping code areas should therefore be discarded.
3384 * \note the events passed to \p event_handler and the strings they point to
3385 * are not guaranteed to live past each call. The \p event_handler must
3386 * copy strings and other parameters it needs to keep around.
3387 * \note the set of events declared in JitCodeEvent::EventType is expected to
3388 * grow over time, and the JitCodeEvent structure is expected to accrue
3389 * new members. The \p event_handler function must ignore event codes
3390 * it does not recognize to maintain future compatibility.
3391 */
3392 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3393 JitCodeEventHandler event_handler);
3394
3395 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003396 * Adjusts the amount of registered external memory. Used to give
3397 * V8 an indication of the amount of externally allocated memory
3398 * that is kept alive by JavaScript objects. V8 uses this to decide
3399 * when to perform global garbage collections. Registering
3400 * externally allocated memory will trigger global garbage
3401 * collections more often than otherwise in an attempt to garbage
3402 * collect the JavaScript objects keeping the externally allocated
3403 * memory alive.
3404 *
3405 * \param change_in_bytes the change in externally allocated memory
3406 * that is kept alive by JavaScript objects.
3407 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003408 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003409 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3410 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003411
iposva@chromium.org245aa852009-02-10 00:49:54 +00003412 /**
3413 * Suspends recording of tick samples in the profiler.
3414 * When the V8 profiling mode is enabled (usually via command line
3415 * switches) this function suspends recording of tick samples.
3416 * Profiling ticks are discarded until ResumeProfiler() is called.
3417 *
3418 * See also the --prof and --prof_auto command line switches to
3419 * enable V8 profiling.
3420 */
3421 static void PauseProfiler();
3422
3423 /**
3424 * Resumes recording of tick samples in the profiler.
3425 * See also PauseProfiler().
3426 */
3427 static void ResumeProfiler();
3428
ager@chromium.org41826e72009-03-30 13:30:57 +00003429 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003430 * Return whether profiler is currently paused.
3431 */
3432 static bool IsProfilerPaused();
3433
3434 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003435 * Retrieve the V8 thread id of the calling thread.
3436 *
3437 * The thread id for a thread should only be retrieved after the V8
3438 * lock has been acquired with a Locker object with that thread.
3439 */
3440 static int GetCurrentThreadId();
3441
3442 /**
3443 * Forcefully terminate execution of a JavaScript thread. This can
3444 * be used to terminate long-running scripts.
3445 *
3446 * TerminateExecution should only be called when then V8 lock has
3447 * been acquired with a Locker object. Therefore, in order to be
3448 * able to terminate long-running threads, preemption must be
3449 * enabled to allow the user of TerminateExecution to acquire the
3450 * lock.
3451 *
3452 * The termination is achieved by throwing an exception that is
3453 * uncatchable by JavaScript exception handlers. Termination
3454 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003455 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003456 * exception handler that catches an exception should check if that
3457 * exception is a termination exception and immediately return if
3458 * that is the case. Returning immediately in that case will
3459 * continue the propagation of the termination exception if needed.
3460 *
3461 * The thread id passed to TerminateExecution must have been
3462 * obtained by calling GetCurrentThreadId on the thread in question.
3463 *
3464 * \param thread_id The thread id of the thread to terminate.
3465 */
3466 static void TerminateExecution(int thread_id);
3467
3468 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003469 * Forcefully terminate the current thread of JavaScript execution
3470 * in the given isolate. If no isolate is provided, the default
3471 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003472 *
3473 * This method can be used by any thread even if that thread has not
3474 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003475 *
3476 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003477 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003478 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003479
3480 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003481 * Is V8 terminating JavaScript execution.
3482 *
3483 * Returns true if JavaScript execution is currently terminating
3484 * because of a call to TerminateExecution. In that case there are
3485 * still JavaScript frames on the stack and the termination
3486 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003487 *
3488 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003489 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003490 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003491
3492 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003493 * Releases any resources used by v8 and stops any utility threads
3494 * that may be running. Note that disposing v8 is permanent, it
3495 * cannot be reinitialized.
3496 *
3497 * It should generally not be necessary to dispose v8 before exiting
3498 * a process, this should happen automatically. It is only necessary
3499 * to use if the process needs the resources taken up by v8.
3500 */
3501 static bool Dispose();
3502
ager@chromium.org3811b432009-10-28 14:53:37 +00003503 /**
3504 * Get statistics about the heap memory usage.
3505 */
3506 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003507
3508 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003509 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003510 * heap. GC is not invoked prior to iterating, therefore there is no
3511 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003512 */
3513 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3514
3515 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003516 * Iterates through all the persistent handles in the current isolate's heap
3517 * that have class_ids.
3518 */
3519 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3520
3521 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003522 * Iterates through all the persistent handles in the current isolate's heap
3523 * that have class_ids and are candidates to be marked as partially dependent
3524 * handles. This will visit handles to young objects created since the last
3525 * garbage collection but is free to visit an arbitrary superset of these
3526 * objects.
3527 */
3528 static void VisitHandlesForPartialDependence(
3529 Isolate* isolate, PersistentHandleVisitor* visitor);
3530
3531 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003532 * Optional notification that the embedder is idle.
3533 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003534 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003535 * Returns true if the embedder should stop calling IdleNotification
3536 * until real work has been done. This indicates that V8 has done
3537 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003538 *
3539 * The hint argument specifies the amount of work to be done in the function
3540 * on scale from 1 to 1000. There is no guarantee that the actual work will
3541 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003542 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003543 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003544
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003545 /**
3546 * Optional notification that the system is running low on memory.
3547 * V8 uses these notifications to attempt to free memory.
3548 */
3549 static void LowMemoryNotification();
3550
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003551 /**
3552 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003553 * these notifications to guide the GC heuristic. Returns the number
3554 * of context disposals - including this one - since the last time
3555 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003556 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003557 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003558
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003559 private:
3560 V8();
3561
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003562 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
3563 internal::Object** handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003564 static void DisposeGlobal(internal::Isolate* isolate,
3565 internal::Object** global_handle);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003566 static void MakeWeak(internal::Isolate* isolate,
3567 internal::Object** global_handle,
3568 void* data,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003569 WeakReferenceCallback weak_reference_callback,
3570 NearDeathCallback near_death_callback);
3571 static void ClearWeak(internal::Isolate* isolate,
3572 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003573
3574 template <class T> friend class Handle;
3575 template <class T> friend class Local;
3576 template <class T> friend class Persistent;
3577 friend class Context;
3578};
3579
3580
3581/**
3582 * An external exception handler.
3583 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003584class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003585 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003587 * Creates a new try/catch block and registers it with v8. Note that
3588 * all TryCatch blocks should be stack allocated because the memory
3589 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003590 */
3591 TryCatch();
3592
3593 /**
3594 * Unregisters and deletes this try/catch block.
3595 */
3596 ~TryCatch();
3597
3598 /**
3599 * Returns true if an exception has been caught by this try/catch block.
3600 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003601 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003602
3603 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003604 * For certain types of exceptions, it makes no sense to continue
3605 * execution.
3606 *
3607 * Currently, the only type of exception that can be caught by a
3608 * TryCatch handler and for which it does not make sense to continue
3609 * is termination exception. Such exceptions are thrown when the
3610 * TerminateExecution methods are called to terminate a long-running
3611 * script.
3612 *
3613 * If CanContinue returns false, the correct action is to perform
3614 * any C++ cleanup needed and then return.
3615 */
3616 bool CanContinue() const;
3617
3618 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003619 * Throws the exception caught by this TryCatch in a way that avoids
3620 * it being caught again by this same TryCatch. As with ThrowException
3621 * it is illegal to execute any JavaScript operations after calling
3622 * ReThrow; the caller must return immediately to where the exception
3623 * is caught.
3624 */
3625 Handle<Value> ReThrow();
3626
3627 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628 * Returns the exception caught by this try/catch block. If no exception has
3629 * been caught an empty handle is returned.
3630 *
3631 * The returned handle is valid until this TryCatch block has been destroyed.
3632 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003633 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003634
3635 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003636 * Returns the .stack property of the thrown object. If no .stack
3637 * property is present an empty handle is returned.
3638 */
3639 Local<Value> StackTrace() const;
3640
3641 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003642 * Returns the message associated with this exception. If there is
3643 * no message associated an empty handle is returned.
3644 *
3645 * The returned handle is valid until this TryCatch block has been
3646 * destroyed.
3647 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003648 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003649
3650 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003651 * Clears any exceptions that may have been caught by this try/catch block.
3652 * After this method has been called, HasCaught() will return false.
3653 *
3654 * It is not necessary to clear a try/catch block before using it again; if
3655 * another exception is thrown the previously caught exception will just be
3656 * overwritten. However, it is often a good idea since it makes it easier
3657 * to determine which operation threw a given exception.
3658 */
3659 void Reset();
3660
v8.team.kasperl727e9952008-09-02 14:56:44 +00003661 /**
3662 * Set verbosity of the external exception handler.
3663 *
3664 * By default, exceptions that are caught by an external exception
3665 * handler are not reported. Call SetVerbose with true on an
3666 * external exception handler to have exceptions caught by the
3667 * handler reported as if they were not caught.
3668 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003669 void SetVerbose(bool value);
3670
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003671 /**
3672 * Set whether or not this TryCatch should capture a Message object
3673 * which holds source information about where the exception
3674 * occurred. True by default.
3675 */
3676 void SetCaptureMessage(bool value);
3677
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003678 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003679 // Make it hard to create heap-allocated TryCatch blocks.
3680 TryCatch(const TryCatch&);
3681 void operator=(const TryCatch&);
3682 void* operator new(size_t size);
3683 void operator delete(void*, size_t);
3684
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003685 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003686 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003687 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003688 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003689 bool is_verbose_ : 1;
3690 bool can_continue_ : 1;
3691 bool capture_message_ : 1;
3692 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003693
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003694 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695};
3696
3697
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003698// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003699
3700
3701/**
3702 * Ignore
3703 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003704class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003705 public:
3706 ExtensionConfiguration(int name_count, const char* names[])
3707 : name_count_(name_count), names_(names) { }
3708 private:
3709 friend class ImplementationUtilities;
3710 int name_count_;
3711 const char** names_;
3712};
3713
3714
3715/**
3716 * A sandboxed execution context with its own set of built-in objects
3717 * and functions.
3718 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003719class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003720 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003721 /**
3722 * Returns the global proxy object or global object itself for
3723 * detached contexts.
3724 *
3725 * Global proxy object is a thin wrapper whose prototype points to
3726 * actual context's global object with the properties like Object, etc.
3727 * This is done that way for security reasons (for more details see
3728 * https://wiki.mozilla.org/Gecko:SplitWindow).
3729 *
3730 * Please note that changes to global proxy object prototype most probably
3731 * would break VM---v8 expects only global object as a prototype of
3732 * global proxy object.
3733 *
3734 * If DetachGlobal() has been invoked, Global() would return actual global
3735 * object until global is reattached with ReattachGlobal().
3736 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003737 Local<Object> Global();
3738
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003739 /**
3740 * Detaches the global object from its context before
3741 * the global object can be reused to create a new context.
3742 */
3743 void DetachGlobal();
3744
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003745 /**
3746 * Reattaches a global object to a context. This can be used to
3747 * restore the connection between a global object and a context
3748 * after DetachGlobal has been called.
3749 *
3750 * \param global_object The global object to reattach to the
3751 * context. For this to work, the global object must be the global
3752 * object that was associated with this context before a call to
3753 * DetachGlobal.
3754 */
3755 void ReattachGlobal(Handle<Object> global_object);
3756
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003757 /** Creates a new context.
3758 *
3759 * Returns a persistent handle to the newly allocated context. This
3760 * persistent handle has to be disposed when the context is no
3761 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003762 *
3763 * \param extensions An optional extension configuration containing
3764 * the extensions to be installed in the newly created context.
3765 *
3766 * \param global_template An optional object template from which the
3767 * global object for the newly created context will be created.
3768 *
3769 * \param global_object An optional global object to be reused for
3770 * the newly created context. This global object must have been
3771 * created by a previous call to Context::New with the same global
3772 * template. The state of the global object will be completely reset
3773 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003774 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003775 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003776 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003777 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3778 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003779
kasper.lund44510672008-07-25 07:37:58 +00003780 /** Returns the last entered context. */
3781 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003782
kasper.lund44510672008-07-25 07:37:58 +00003783 /** Returns the context that is on the top of the stack. */
3784 static Local<Context> GetCurrent();
3785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003786 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003787 * Returns the context of the calling JavaScript code. That is the
3788 * context of the top-most JavaScript frame. If there are no
3789 * JavaScript frames an empty handle is returned.
3790 */
3791 static Local<Context> GetCalling();
3792
3793 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003794 * Sets the security token for the context. To access an object in
3795 * another context, the security tokens must match.
3796 */
3797 void SetSecurityToken(Handle<Value> token);
3798
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003799 /** Restores the security token to the default value. */
3800 void UseDefaultSecurityToken();
3801
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003802 /** Returns the security token of this context.*/
3803 Handle<Value> GetSecurityToken();
3804
v8.team.kasperl727e9952008-09-02 14:56:44 +00003805 /**
3806 * Enter this context. After entering a context, all code compiled
3807 * and run is compiled and run in this context. If another context
3808 * is already entered, this old context is saved so it can be
3809 * restored when the new context is exited.
3810 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003811 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003812
3813 /**
3814 * Exit this context. Exiting the current context restores the
3815 * context that was in place when entering the current context.
3816 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003817 void Exit();
3818
v8.team.kasperl727e9952008-09-02 14:56:44 +00003819 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003820 bool HasOutOfMemoryException();
3821
v8.team.kasperl727e9952008-09-02 14:56:44 +00003822 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003823 static bool InContext();
3824
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003825 /** Returns an isolate associated with a current context. */
3826 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00003827
3828 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003829 * Gets the embedder data with the given index, which must have been set by a
3830 * previous call to SetEmbedderData with the same index. Note that index 0
3831 * currently has a special meaning for Chrome's debugger.
3832 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003833 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003834
3835 /**
3836 * Sets the embedder data with the given index, growing the data as
3837 * needed. Note that index 0 currently has a special meaning for Chrome's
3838 * debugger.
3839 */
3840 void SetEmbedderData(int index, Handle<Value> value);
3841
3842 /**
3843 * Gets a 2-byte-aligned native pointer from the embedder data with the given
3844 * index, which must have bees set by a previous call to
3845 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
3846 * currently has a special meaning for Chrome's debugger.
3847 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003848 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003849
3850 /**
3851 * Sets a 2-byte-aligned native pointer in the embedder data with the given
3852 * index, growing the data as needed. Note that index 0 currently has a
3853 * special meaning for Chrome's debugger.
3854 */
3855 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00003856
3857 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003858 * Control whether code generation from strings is allowed. Calling
3859 * this method with false will disable 'eval' and the 'Function'
3860 * constructor for code running in this context. If 'eval' or the
3861 * 'Function' constructor are used an exception will be thrown.
3862 *
3863 * If code generation from strings is not allowed the
3864 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3865 * set before blocking the call to 'eval' or the 'Function'
3866 * constructor. If that callback returns true, the call will be
3867 * allowed, otherwise an exception will be thrown. If no callback is
3868 * set an exception will be thrown.
3869 */
3870 void AllowCodeGenerationFromStrings(bool allow);
3871
3872 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003873 * Returns true if code generation from strings is allowed for the context.
3874 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3875 */
3876 bool IsCodeGenerationFromStringsAllowed();
3877
3878 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003879 * Sets the error description for the exception that is thrown when
3880 * code generation from strings is not allowed and 'eval' or the 'Function'
3881 * constructor are called.
3882 */
3883 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3884
3885 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003886 * Stack-allocated class which sets the execution context for all
3887 * operations executed within a local scope.
3888 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003889 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003890 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003891 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003892 context_->Enter();
3893 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003894 V8_INLINE(~Scope()) { context_->Exit(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003895 private:
3896 Handle<Context> context_;
3897 };
3898
3899 private:
3900 friend class Value;
3901 friend class Script;
3902 friend class Object;
3903 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003904
3905 Local<Value> SlowGetEmbedderData(int index);
3906 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907};
3908
3909
3910/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003911 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
3912 * to use any given V8 isolate, see the comments in the Isolate class. The
3913 * definition of 'using a V8 isolate' includes accessing handles or holding onto
3914 * object pointers obtained from V8 handles while in the particular V8 isolate.
3915 * It is up to the user of V8 to ensure, perhaps with locking, that this
3916 * constraint is not violated. In addition to any other synchronization
3917 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
3918 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003920 * v8::Locker is a scoped lock object. While it's active, i.e. between its
3921 * construction and destruction, the current thread is allowed to use the locked
3922 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
3923 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003924 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003925 * Sample usage:
3926* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003927 * ...
3928 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003929 * v8::Locker locker(isolate);
3930 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003931 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003932 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003933 * ...
3934 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003935 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003937 * If you wish to stop using V8 in a thread A you can do this either by
3938 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
3939 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003940 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003941 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003942 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003943 * isolate->Exit();
3944 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003945 * ...
3946 * // Code not using V8 goes here while V8 can run in another thread.
3947 * ...
3948 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003949 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003950 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003951 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003952 * The Unlocker object is intended for use in a long-running callback from V8,
3953 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003954 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003955 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
3956 * given thread. This can be useful if you have code that can be called either
3957 * from code that holds the lock or from code that does not. The Unlocker is
3958 * not recursive so you can not have several Unlockers on the stack at once, and
3959 * 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 +00003960 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003961 * An unlocker will unlock several lockers if it has to and reinstate the
3962 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003963 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003964 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003965 * // V8 not locked.
3966 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003967 * v8::Locker locker(isolate);
3968 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969 * // V8 locked.
3970 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003971 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003972 * // V8 still locked (2 levels).
3973 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003974 * isolate->Exit();
3975 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003976 * // V8 not locked.
3977 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003978 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003979 * // V8 locked again (2 levels).
3980 * }
3981 * // V8 still locked (1 level).
3982 * }
3983 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003984 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003985 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003986class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003987 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003988 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003989 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003990 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003991 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
3992
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003993 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003994 V8_DEPRECATED(Unlocker());
3995
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003996 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003997 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003998 void Initialize(Isolate* isolate);
3999
lrn@chromium.org1c092762011-05-09 09:42:16 +00004000 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004001};
4002
4003
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004004class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004005 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004006 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004007 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004008 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004009 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
4010
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004011 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004012 V8_DEPRECATED(Locker());
4013
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004014 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004015
4016 /**
4017 * Start preemption.
4018 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004019 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004020 * that will switch between multiple threads that are in contention
4021 * for the V8 lock.
4022 */
4023 static void StartPreemption(int every_n_ms);
4024
4025 /**
4026 * Stop preemption.
4027 */
4028 static void StopPreemption();
4029
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004030 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004031 * Returns whether or not the locker for a given isolate, is locked by the
4032 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004033 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004034 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004035
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004036 /**
4037 * Returns whether v8::Locker is being used by this V8 instance.
4038 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004039 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004041 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004042 void Initialize(Isolate* isolate);
4043
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004044 bool has_lock_;
4045 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004046 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004047
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004048 static bool active_;
4049
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004050 // Disallow copying and assigning.
4051 Locker(const Locker&);
4052 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004053};
4054
4055
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004056/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004057 * A struct for exporting HeapStats data from V8, using "push" model.
4058 */
4059struct HeapStatsUpdate;
4060
4061
4062/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004063 * An interface for exporting data from V8, using "push" model.
4064 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004065class V8EXPORT OutputStream { // NOLINT
4066 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004067 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004068 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004069 };
4070 enum WriteResult {
4071 kContinue = 0,
4072 kAbort = 1
4073 };
4074 virtual ~OutputStream() {}
4075 /** Notify about the end of stream. */
4076 virtual void EndOfStream() = 0;
4077 /** Get preferred output chunk size. Called only once. */
4078 virtual int GetChunkSize() { return 1024; }
4079 /** Get preferred output encoding. Called only once. */
4080 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4081 /**
4082 * Writes the next chunk of snapshot data into the stream. Writing
4083 * can be stopped by returning kAbort as function result. EndOfStream
4084 * will not be called in case writing was aborted.
4085 */
4086 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004087 /**
4088 * Writes the next chunk of heap stats data into the stream. Writing
4089 * can be stopped by returning kAbort as function result. EndOfStream
4090 * will not be called in case writing was aborted.
4091 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004092 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004093 return kAbort;
4094 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004095};
4096
4097
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004098/**
4099 * An interface for reporting progress and controlling long-running
4100 * activities.
4101 */
4102class V8EXPORT ActivityControl { // NOLINT
4103 public:
4104 enum ControlOption {
4105 kContinue = 0,
4106 kAbort = 1
4107 };
4108 virtual ~ActivityControl() {}
4109 /**
4110 * Notify about current progress. The activity can be stopped by
4111 * returning kAbort as the callback result.
4112 */
4113 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4114};
4115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004116
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004117// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004118
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004119
4120namespace internal {
4121
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004122const int kApiPointerSize = sizeof(void*); // NOLINT
4123const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004124
4125// Tag information for HeapObject.
4126const int kHeapObjectTag = 1;
4127const int kHeapObjectTagSize = 2;
4128const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4129
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004130// Tag information for Smi.
4131const int kSmiTag = 0;
4132const int kSmiTagSize = 1;
4133const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4134
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004135template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004136
4137// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004138template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004139 static const int kSmiShiftSize = 0;
4140 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004141 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004142 int shift_bits = kSmiTagSize + kSmiShiftSize;
4143 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4144 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4145 }
4146};
4147
4148// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004149template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004150 static const int kSmiShiftSize = 31;
4151 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004152 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004153 int shift_bits = kSmiTagSize + kSmiShiftSize;
4154 // Shift down and throw away top 32 bits.
4155 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4156 }
4157};
4158
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004159typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4160const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4161const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004162
4163/**
4164 * This class exports constants and functionality from within v8 that
4165 * is necessary to implement inline functions in the v8 api. Don't
4166 * depend on functions and constants defined here.
4167 */
4168class Internals {
4169 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004170 // These values match non-compiler-dependent values defined within
4171 // the implementation of v8.
4172 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004173 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004174 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004175
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004176 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004177 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004178 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004179 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4180 static const int kContextHeaderSize = 2 * kApiPointerSize;
4181 static const int kContextEmbedderDataIndex = 54;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004182 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004183 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004184 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004185 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004186
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004187 static const int kIsolateStateOffset = 0;
4188 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4189 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4190 static const int kUndefinedValueRootIndex = 5;
4191 static const int kNullValueRootIndex = 7;
4192 static const int kTrueValueRootIndex = 8;
4193 static const int kFalseValueRootIndex = 9;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004194 static const int kEmptySymbolRootIndex = 119;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004195
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004196 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
4197 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
4198 static const int kNodeStateMask = 0xf;
4199 static const int kNodeStateIsWeakValue = 2;
4200 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004201 static const int kNodeIsIndependentShift = 4;
4202 static const int kNodeIsPartiallyDependentShift = 5;
4203
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00004204 static const int kJSObjectType = 0xab;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004205 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004206 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004207 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004208
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004209 static const int kUndefinedOddballKind = 5;
4210 static const int kNullOddballKind = 3;
4211
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004212 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004213 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4214 kHeapObjectTag);
4215 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004216
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004217 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004218 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004219 }
4220
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004221 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004222 typedef internal::Object O;
4223 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4224 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4225 }
4226
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004227 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004228 typedef internal::Object O;
4229 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4230 }
4231
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004232 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004233 int representation = (instance_type & kFullStringRepresentationMask);
4234 return representation == kExternalTwoByteRepresentationTag;
4235 }
4236
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004237 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004238 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4239 return *reinterpret_cast<int*>(addr) == 1;
4240 }
4241
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004242 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
4243 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4244 return *addr & (1 << shift);
4245 }
4246
4247 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
4248 bool value, int shift)) {
4249 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4250 uint8_t mask = 1 << shift;
4251 *addr = (*addr & ~mask) | (value << shift);
4252 }
4253
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004254 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
4255 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4256 return *addr & kNodeStateMask;
4257 }
4258
4259 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
4260 uint8_t value)) {
4261 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4262 *addr = (*addr & ~kNodeStateMask) | value;
4263 }
4264
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004265 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004266 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4267 kIsolateEmbedderDataOffset;
4268 *reinterpret_cast<void**>(addr) = data;
4269 }
4270
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004271 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004272 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4273 kIsolateEmbedderDataOffset;
4274 return *reinterpret_cast<void**>(addr);
4275 }
4276
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004277 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
4278 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004279 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4280 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4281 }
4282
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004283 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004284 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004285 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4286 return *reinterpret_cast<T*>(addr);
4287 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004288
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004289 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004290 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004291 typedef internal::Object O;
4292 typedef internal::Internals I;
4293 O* ctx = *reinterpret_cast<O**>(context);
4294 int embedder_data_offset = I::kContextHeaderSize +
4295 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4296 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4297 int value_offset =
4298 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4299 return I::ReadField<T>(embedder_data, value_offset);
4300 }
4301
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004302 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
4303 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
4304 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
4305 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
4306 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
4307 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
4308 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004309};
4310
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004311} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004312
4313
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004314template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004315Local<T>::Local() : Handle<T>() { }
4316
4317
4318template <class T>
4319Local<T> Local<T>::New(Handle<T> that) {
4320 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004321 T* that_ptr = *that;
4322 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4323 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4324 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4325 reinterpret_cast<internal::HeapObject*>(*p))));
4326 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004327 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4328}
4329
4330
4331template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004332Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004333 if (that.IsEmpty()) return Local<T>();
4334 T* that_ptr = *that;
4335 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4336 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4337 reinterpret_cast<internal::Isolate*>(isolate), *p)));
4338}
4339
4340
4341template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004342Persistent<T> Persistent<T>::New(Handle<T> that) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004343 return New(Isolate::GetCurrent(), that);
4344}
4345
4346
4347template <class T>
4348Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004349 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004350 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004351 return Persistent<T>(reinterpret_cast<T*>(
4352 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
4353 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004354}
4355
4356
4357template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004358bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004359 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004360}
4361
4362
4363template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004364bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004365 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004366 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004367 if (!I::IsInitialized(isolate)) return false;
4368 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(**this),
4369 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004370}
4371
4372
4373template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004374bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004375 return IsNearDeath(Isolate::GetCurrent());
4376}
4377
4378
4379template <class T>
4380bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
4381 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004382 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004383 if (!I::IsInitialized(isolate)) return false;
4384 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4385 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004386}
4387
4388
4389template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004390bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004391 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004392}
4393
4394
4395template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004396bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004397 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004398 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004399 if (!I::IsInitialized(isolate)) return false;
4400 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4401 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004402}
4403
4404
4405template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004406void Persistent<T>::Dispose() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004407 Dispose(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004408}
4409
4410
4411template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004412void Persistent<T>::Dispose(Isolate* isolate) {
4413 if (this->IsEmpty()) return;
4414 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4415 reinterpret_cast<internal::Object**>(**this));
4416}
4417
4418
4419template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004420Persistent<T>::Persistent() : Handle<T>() { }
4421
4422template <class T>
4423void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004424 Isolate* isolate = Isolate::GetCurrent();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004425 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4426 reinterpret_cast<internal::Object**>(**this),
4427 parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004428 callback,
4429 NULL);
4430}
4431
4432template <class T>
4433void Persistent<T>::MakeWeak(Isolate* isolate,
4434 void* parameters,
4435 NearDeathCallback callback) {
4436 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4437 reinterpret_cast<internal::Object**>(**this),
4438 parameters,
4439 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004440 callback);
4441}
4442
4443template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004444void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004445 ClearWeak(Isolate::GetCurrent());
4446}
4447
4448template <class T>
4449void Persistent<T>::ClearWeak(Isolate* isolate) {
4450 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
4451 reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004452}
4453
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004454template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004455void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004456 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004457}
4458
4459template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004460void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004461 typedef internal::Internals I;
4462 if (this->IsEmpty()) return;
4463 if (!I::IsInitialized(isolate)) return;
4464 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004465 true,
4466 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004467}
4468
4469template <class T>
4470void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004471 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004472}
4473
4474template <class T>
4475void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004476 typedef internal::Internals I;
4477 if (this->IsEmpty()) return;
4478 if (!I::IsInitialized(isolate)) return;
4479 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004480 true,
4481 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004482}
4483
4484template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004485void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004486 SetWrapperClassId(Isolate::GetCurrent(), class_id);
4487}
4488
4489template <class T>
4490void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004491 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004492 if (this->IsEmpty()) return;
4493 if (!I::IsInitialized(isolate)) return;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004494 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4495 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4496 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004497}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004498
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004499template <class T>
4500uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004501 return WrapperClassId(Isolate::GetCurrent());
4502}
4503
4504template <class T>
4505uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004506 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004507 if (this->IsEmpty()) return 0;
4508 if (!I::IsInitialized(isolate)) return 0;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004509 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4510 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4511 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004512}
4513
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004514Arguments::Arguments(internal::Object** implicit_args,
4515 internal::Object** values, int length,
4516 bool is_construct_call)
4517 : implicit_args_(implicit_args),
4518 values_(values),
4519 length_(length),
4520 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004521
4522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004523Local<Value> Arguments::operator[](int i) const {
4524 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4525 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4526}
4527
4528
4529Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004530 return Local<Function>(reinterpret_cast<Function*>(
4531 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004532}
4533
4534
4535Local<Object> Arguments::This() const {
4536 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4537}
4538
4539
4540Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004541 return Local<Object>(reinterpret_cast<Object*>(
4542 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004543}
4544
4545
4546Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004547 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004548}
4549
4550
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004551Isolate* Arguments::GetIsolate() const {
4552 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4553}
4554
4555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004556bool Arguments::IsConstructCall() const {
4557 return is_construct_call_;
4558}
4559
4560
4561int Arguments::Length() const {
4562 return length_;
4563}
4564
4565
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004566template <class T>
4567Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004568 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4569 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004570 return Local<T>(reinterpret_cast<T*>(after));
4571}
4572
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004573Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004574 return resource_name_;
4575}
4576
4577
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004578Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004579 return resource_line_offset_;
4580}
4581
4582
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004583Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004584 return resource_column_offset_;
4585}
4586
4587
4588Handle<Boolean> Boolean::New(bool value) {
4589 return value ? True() : False();
4590}
4591
4592
4593void Template::Set(const char* name, v8::Handle<Data> value) {
4594 Set(v8::String::New(name), value);
4595}
4596
4597
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004598Local<Value> Object::GetInternalField(int index) {
4599#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004600 typedef internal::Object O;
4601 typedef internal::Internals I;
4602 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004603 // Fast path: If the object is a plain JSObject, which is the common case, we
4604 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004605 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004606 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004607 O* value = I::ReadField<O*>(obj, offset);
4608 O** result = HandleScope::CreateHandle(value);
4609 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004610 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004611#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004612 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004613}
4614
4615
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004616void* Object::GetAlignedPointerFromInternalField(int index) {
4617#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004618 typedef internal::Object O;
4619 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004620 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004621 // Fast path: If the object is a plain JSObject, which is the common case, we
4622 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004623 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004624 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004625 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004626 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004627#endif
4628 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004629}
4630
4631
4632String* String::Cast(v8::Value* value) {
4633#ifdef V8_ENABLE_CHECKS
4634 CheckCast(value);
4635#endif
4636 return static_cast<String*>(value);
4637}
4638
4639
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004640Local<String> String::Empty(Isolate* isolate) {
4641 typedef internal::Object* S;
4642 typedef internal::Internals I;
4643 if (!I::IsInitialized(isolate)) return Empty();
4644 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4645 return Local<String>(reinterpret_cast<String*>(slot));
4646}
4647
4648
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004649String::ExternalStringResource* String::GetExternalStringResource() const {
4650 typedef internal::Object O;
4651 typedef internal::Internals I;
4652 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004653 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004654 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004655 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4656 result = reinterpret_cast<String::ExternalStringResource*>(value);
4657 } else {
4658 result = NULL;
4659 }
4660#ifdef V8_ENABLE_CHECKS
4661 VerifyExternalStringResource(result);
4662#endif
4663 return result;
4664}
4665
4666
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004667String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4668 String::Encoding* encoding_out) const {
4669 typedef internal::Object O;
4670 typedef internal::Internals I;
4671 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4672 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4673 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4674 ExternalStringResourceBase* resource = NULL;
4675 if (type == I::kExternalAsciiRepresentationTag ||
4676 type == I::kExternalTwoByteRepresentationTag) {
4677 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4678 resource = static_cast<ExternalStringResourceBase*>(value);
4679 }
4680#ifdef V8_ENABLE_CHECKS
4681 VerifyExternalStringResourceBase(resource, *encoding_out);
4682#endif
4683 return resource;
4684}
4685
4686
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004687bool Value::IsUndefined() const {
4688#ifdef V8_ENABLE_CHECKS
4689 return FullIsUndefined();
4690#else
4691 return QuickIsUndefined();
4692#endif
4693}
4694
4695bool Value::QuickIsUndefined() const {
4696 typedef internal::Object O;
4697 typedef internal::Internals I;
4698 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4699 if (!I::HasHeapObjectTag(obj)) return false;
4700 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4701 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4702}
4703
4704
4705bool Value::IsNull() const {
4706#ifdef V8_ENABLE_CHECKS
4707 return FullIsNull();
4708#else
4709 return QuickIsNull();
4710#endif
4711}
4712
4713bool Value::QuickIsNull() const {
4714 typedef internal::Object O;
4715 typedef internal::Internals I;
4716 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4717 if (!I::HasHeapObjectTag(obj)) return false;
4718 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4719 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4720}
4721
4722
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004723bool Value::IsString() const {
4724#ifdef V8_ENABLE_CHECKS
4725 return FullIsString();
4726#else
4727 return QuickIsString();
4728#endif
4729}
4730
4731bool Value::QuickIsString() const {
4732 typedef internal::Object O;
4733 typedef internal::Internals I;
4734 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4735 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004736 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004737}
4738
4739
4740Number* Number::Cast(v8::Value* value) {
4741#ifdef V8_ENABLE_CHECKS
4742 CheckCast(value);
4743#endif
4744 return static_cast<Number*>(value);
4745}
4746
4747
4748Integer* Integer::Cast(v8::Value* value) {
4749#ifdef V8_ENABLE_CHECKS
4750 CheckCast(value);
4751#endif
4752 return static_cast<Integer*>(value);
4753}
4754
4755
4756Date* Date::Cast(v8::Value* value) {
4757#ifdef V8_ENABLE_CHECKS
4758 CheckCast(value);
4759#endif
4760 return static_cast<Date*>(value);
4761}
4762
4763
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004764StringObject* StringObject::Cast(v8::Value* value) {
4765#ifdef V8_ENABLE_CHECKS
4766 CheckCast(value);
4767#endif
4768 return static_cast<StringObject*>(value);
4769}
4770
4771
4772NumberObject* NumberObject::Cast(v8::Value* value) {
4773#ifdef V8_ENABLE_CHECKS
4774 CheckCast(value);
4775#endif
4776 return static_cast<NumberObject*>(value);
4777}
4778
4779
4780BooleanObject* BooleanObject::Cast(v8::Value* value) {
4781#ifdef V8_ENABLE_CHECKS
4782 CheckCast(value);
4783#endif
4784 return static_cast<BooleanObject*>(value);
4785}
4786
4787
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004788RegExp* RegExp::Cast(v8::Value* value) {
4789#ifdef V8_ENABLE_CHECKS
4790 CheckCast(value);
4791#endif
4792 return static_cast<RegExp*>(value);
4793}
4794
4795
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004796Object* Object::Cast(v8::Value* value) {
4797#ifdef V8_ENABLE_CHECKS
4798 CheckCast(value);
4799#endif
4800 return static_cast<Object*>(value);
4801}
4802
4803
4804Array* Array::Cast(v8::Value* value) {
4805#ifdef V8_ENABLE_CHECKS
4806 CheckCast(value);
4807#endif
4808 return static_cast<Array*>(value);
4809}
4810
4811
4812Function* Function::Cast(v8::Value* value) {
4813#ifdef V8_ENABLE_CHECKS
4814 CheckCast(value);
4815#endif
4816 return static_cast<Function*>(value);
4817}
4818
4819
4820External* External::Cast(v8::Value* value) {
4821#ifdef V8_ENABLE_CHECKS
4822 CheckCast(value);
4823#endif
4824 return static_cast<External*>(value);
4825}
4826
4827
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004828Isolate* AccessorInfo::GetIsolate() const {
4829 return *reinterpret_cast<Isolate**>(&args_[-3]);
4830}
4831
4832
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004833Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004834 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004835}
4836
4837
4838Local<Object> AccessorInfo::This() const {
4839 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4840}
4841
4842
4843Local<Object> AccessorInfo::Holder() const {
4844 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4845}
4846
4847
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004848Handle<Primitive> Undefined(Isolate* isolate) {
4849 typedef internal::Object* S;
4850 typedef internal::Internals I;
4851 if (!I::IsInitialized(isolate)) return Undefined();
4852 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4853 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4854}
4855
4856
4857Handle<Primitive> Null(Isolate* isolate) {
4858 typedef internal::Object* S;
4859 typedef internal::Internals I;
4860 if (!I::IsInitialized(isolate)) return Null();
4861 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4862 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4863}
4864
4865
4866Handle<Boolean> True(Isolate* isolate) {
4867 typedef internal::Object* S;
4868 typedef internal::Internals I;
4869 if (!I::IsInitialized(isolate)) return True();
4870 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4871 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4872}
4873
4874
4875Handle<Boolean> False(Isolate* isolate) {
4876 typedef internal::Object* S;
4877 typedef internal::Internals I;
4878 if (!I::IsInitialized(isolate)) return False();
4879 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4880 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4881}
4882
4883
4884void Isolate::SetData(void* data) {
4885 typedef internal::Internals I;
4886 I::SetEmbedderData(this, data);
4887}
4888
4889
4890void* Isolate::GetData() {
4891 typedef internal::Internals I;
4892 return I::GetEmbedderData(this);
4893}
4894
4895
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004896Local<Value> Context::GetEmbedderData(int index) {
4897#ifndef V8_ENABLE_CHECKS
4898 typedef internal::Object O;
4899 typedef internal::Internals I;
4900 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
4901 return Local<Value>(reinterpret_cast<Value*>(result));
4902#else
4903 return SlowGetEmbedderData(index);
4904#endif
4905}
4906
4907
4908void* Context::GetAlignedPointerFromEmbedderData(int index) {
4909#ifndef V8_ENABLE_CHECKS
4910 typedef internal::Internals I;
4911 return I::ReadEmbedderData<void*>(this, index);
4912#else
4913 return SlowGetAlignedPointerFromEmbedderData(index);
4914#endif
4915}
4916
4917
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004918/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004919 * \example shell.cc
4920 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004921 * command-line and executes them.
4922 */
4923
4924
4925/**
4926 * \example process.cc
4927 */
4928
4929
4930} // namespace v8
4931
4932
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004933#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004934#undef TYPE_CHECK
4935
4936
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004937#endif // V8_H_