blob: 245dc5a826d0eb35aed37b5ae62ec3da82cbcdf5 [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000065// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000066#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
67 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070#else
71#define V8EXPORT
72#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000075#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076
77#endif // _WIN32
78
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000080 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 */
82namespace v8 {
83
84class Context;
85class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000086class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087class Value;
88class Utils;
89class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000090class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091class Object;
92class Array;
93class Int32;
94class Uint32;
95class External;
96class Primitive;
97class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000098class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099class Integer;
100class Function;
101class Date;
102class ImplementationUtilities;
103class Signature;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000104class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105template <class T> class Handle;
106template <class T> class Local;
107template <class T> class Persistent;
108class FunctionTemplate;
109class ObjectTemplate;
110class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000111class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000112class StackTrace;
113class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000114class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000116namespace internal {
117
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000118class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000119class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000120class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121class HeapObject;
122class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000123}
124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000126// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127
128
129/**
130 * A weak reference callback function.
131 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000132 * This callback should either explicitly invoke Dispose on |object| if
133 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
134 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 * \param object the weak global object to be reclaimed by the garbage collector
136 * \param parameter the value passed in when making the weak global object
137 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000138typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 void* parameter);
140
141
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000142// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000144#define TYPE_CHECK(T, S) \
145 while (false) { \
146 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 }
148
149/**
150 * An object reference managed by the v8 garbage collector.
151 *
152 * All objects returned from v8 have to be tracked by the garbage
153 * collector so that it knows that the objects are still alive. Also,
154 * because the garbage collector may move objects, it is unsafe to
155 * point directly to an object. Instead, all objects are stored in
156 * handles which are known by the garbage collector and updated
157 * whenever an object moves. Handles should always be passed by value
158 * (except in cases like out-parameters) and they should never be
159 * allocated on the heap.
160 *
161 * There are two types of handles: local and persistent handles.
162 * Local handles are light-weight and transient and typically used in
163 * local operations. They are managed by HandleScopes. Persistent
164 * handles can be used when storing objects across several independent
165 * operations and have to be explicitly deallocated when they're no
166 * longer used.
167 *
168 * It is safe to extract the object stored in the handle by
169 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000170 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 * behind the scenes and the same rules apply to these values as to
172 * their handles.
173 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000174template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 /**
177 * Creates an empty handle.
178 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000179 inline Handle() : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
181 /**
182 * Creates a new handle for the specified value.
183 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000184 inline explicit Handle(T* val) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185
186 /**
187 * Creates a handle for the contents of the specified handle. This
188 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000189 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000191 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192 * between compatible handles, for instance assigning a
193 * Handle<String> to a variable declared as Handle<Value>, is legal
194 * because String is a subclass of Value.
195 */
196 template <class S> inline Handle(Handle<S> that)
197 : val_(reinterpret_cast<T*>(*that)) {
198 /**
199 * This check fails when trying to convert between incompatible
200 * handles. For example, converting from a Handle<String> to a
201 * Handle<Number>.
202 */
203 TYPE_CHECK(T, S);
204 }
205
206 /**
207 * Returns true if the handle is empty.
208 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000209 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 /**
212 * Sets the handle to be empty. IsEmpty() will then return true.
213 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000214 inline void Clear() { val_ = 0; }
215
216 inline T* operator->() const { return val_; }
217
218 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
220 /**
221 * Checks whether two handles are the same.
222 * Returns true if both are empty, or if the objects
223 * to which they refer are identical.
224 * The handles' references are not checked.
225 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000226 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000227 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
228 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 if (a == 0) return b == 0;
230 if (b == 0) return false;
231 return *a == *b;
232 }
233
234 /**
235 * Checks whether two handles are different.
236 * Returns true if only one of the handles is empty, or if
237 * the objects to which they refer are different.
238 * The handles' references are not checked.
239 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000240 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 return !operator==(that);
242 }
243
244 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000245#ifdef V8_ENABLE_CHECKS
246 // If we're going to perform the type check then we have to check
247 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000249#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 return Handle<T>(T::Cast(*that));
251 }
252
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000253 template <class S> inline Handle<S> As() {
254 return Handle<S>::Cast(*this);
255 }
256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 private:
258 T* val_;
259};
260
261
262/**
263 * A light-weight stack-allocated object handle. All operations
264 * that return objects from within v8 return them in local handles. They
265 * are created within HandleScopes, and all local handles allocated within a
266 * handle scope are destroyed when the handle scope is destroyed. Hence it
267 * is not necessary to explicitly deallocate local handles.
268 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000269template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000271 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 template <class S> inline Local(Local<S> that)
273 : Handle<T>(reinterpret_cast<T*>(*that)) {
274 /**
275 * This check fails when trying to convert between incompatible
276 * handles. For example, converting from a Handle<String> to a
277 * Handle<Number>.
278 */
279 TYPE_CHECK(T, S);
280 }
281 template <class S> inline Local(S* that) : Handle<T>(that) { }
282 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000283#ifdef V8_ENABLE_CHECKS
284 // If we're going to perform the type check then we have to check
285 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000287#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 return Local<T>(T::Cast(*that));
289 }
290
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000291 template <class S> inline Local<S> As() {
292 return Local<S>::Cast(*this);
293 }
294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 /** Create a local handle for the content of another handle.
296 * The referee is kept alive by the local handle even when
297 * the original handle is destroyed/disposed.
298 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000299 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300};
301
302
303/**
304 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000305 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306 * allocated, a Persistent handle remains valid until it is explicitly
307 * disposed.
308 *
309 * A persistent handle contains a reference to a storage cell within
310 * the v8 engine which holds an object value and which is updated by
311 * the garbage collector whenever the object is moved. A new storage
312 * cell can be created using Persistent::New and existing handles can
313 * be disposed using Persistent::Dispose. Since persistent handles
314 * are passed by value you may have many persistent handle objects
315 * that point to the same storage cell. For instance, if you pass a
316 * persistent handle as an argument to a function you will not get two
317 * different storage cells but rather two references to the same
318 * storage cell.
319 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000320template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 /**
323 * Creates an empty persistent handle that doesn't point to any
324 * storage cell.
325 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000326 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327
328 /**
329 * Creates a persistent handle for the same storage cell as the
330 * specified handle. This constructor allows you to pass persistent
331 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000332 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000334 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 * between compatible persistent handles, for instance assigning a
336 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000337 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 */
339 template <class S> inline Persistent(Persistent<S> that)
340 : Handle<T>(reinterpret_cast<T*>(*that)) {
341 /**
342 * This check fails when trying to convert between incompatible
343 * handles. For example, converting from a Handle<String> to a
344 * Handle<Number>.
345 */
346 TYPE_CHECK(T, S);
347 }
348
349 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
350
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000351 /**
352 * "Casts" a plain handle which is known to be a persistent handle
353 * to a persistent handle.
354 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 template <class S> explicit inline Persistent(Handle<S> that)
356 : Handle<T>(*that) { }
357
358 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000359#ifdef V8_ENABLE_CHECKS
360 // If we're going to perform the type check then we have to check
361 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000363#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 return Persistent<T>(T::Cast(*that));
365 }
366
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000367 template <class S> inline Persistent<S> As() {
368 return Persistent<S>::Cast(*this);
369 }
370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000372 * Creates a new persistent handle for an existing local or
373 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000375 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 /**
378 * Releases the storage cell referenced by this persistent handle.
379 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000380 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 * cell remain and IsEmpty will still return false.
382 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000383 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385 /**
386 * Make the reference to this object weak. When only weak handles
387 * refer to the object, the garbage collector will perform a
388 * callback to the given V8::WeakReferenceCallback function, passing
389 * it the object reference and the given parameters.
390 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000391 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000393 /** Clears the weak reference to this object. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000394 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395
396 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000397 * Marks the reference to this object independent. Garbage collector
398 * is free to ignore any object groups containing this object.
399 * Weak callback for an independent handle should not
400 * assume that it will be preceded by a global GC prologue callback
401 * or followed by a global GC epilogue callback.
402 */
403 inline void MarkIndependent();
404
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000405 /** Returns true if this handle was previously marked as independent. */
406 inline bool IsIndependent() const;
407
408 /** Checks if the handle holds the only reference to an object. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000409 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000411 /** Returns true if the handle's reference is weak. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000412 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000414 /**
415 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
416 * interface description in v8-profiler.h for details.
417 */
418 inline void SetWrapperClassId(uint16_t class_id);
419
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000420 /**
421 * Returns the class ID previously assigned to this handle or 0 if no class
422 * ID was previously assigned.
423 */
424 inline uint16_t WrapperClassId() const;
425
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 private:
427 friend class ImplementationUtilities;
428 friend class ObjectTemplate;
429};
430
431
v8.team.kasperl727e9952008-09-02 14:56:44 +0000432 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 * A stack-allocated class that governs a number of local handles.
434 * After a handle scope has been created, all local handles will be
435 * allocated within that handle scope until either the handle scope is
436 * deleted or another handle scope is created. If there is already a
437 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000438 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439 * new handles will again be allocated in the original handle scope.
440 *
441 * After the handle scope of a local handle has been deleted the
442 * garbage collector will no longer track the object stored in the
443 * handle and may deallocate it. The behavior of accessing a handle
444 * for which the handle scope has been deleted is undefined.
445 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000446class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000448 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000450 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451
452 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 * Closes the handle scope and returns the value as a handle in the
454 * previous scope, which is the new current scope after the call.
455 */
456 template <class T> Local<T> Close(Handle<T> value);
457
458 /**
459 * Counts the number of allocated handles.
460 */
461 static int NumberOfHandles();
462
463 /**
464 * Creates a new handle with the given value.
465 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000466 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000467 // Faster version, uses HeapObject to obtain the current Isolate.
468 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469
470 private:
471 // Make it impossible to create heap-allocated or illegal handle
472 // scopes by disallowing certain operations.
473 HandleScope(const HandleScope&);
474 void operator=(const HandleScope&);
475 void* operator new(size_t size);
476 void operator delete(void*, size_t);
477
ager@chromium.org3811b432009-10-28 14:53:37 +0000478 // This Data class is accessible internally as HandleScopeData through a
479 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000480 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000482 internal::Object** next;
483 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000484 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000487 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 }
489 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000490
lrn@chromium.org303ada72010-10-27 09:33:13 +0000491 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000493 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000494 internal::Object** prev_next_;
495 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000497 // Allow for the active closing of HandleScopes which allows to pass a handle
498 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000500 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 friend class ImplementationUtilities;
503};
504
505
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000506// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507
508
509/**
510 * The superclass of values and API object templates.
511 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000512class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 private:
514 Data();
515};
516
517
518/**
519 * Pre-compilation data that can be associated with a script. This
520 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000521 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000522 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000524class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 public:
526 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000527
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000528 /**
529 * Pre-compiles the specified script (context-independent).
530 *
531 * \param input Pointer to UTF-8 script source code.
532 * \param length Length of UTF-8 script source code.
533 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000536 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000537 * Pre-compiles the specified script (context-independent).
538 *
539 * NOTE: Pre-compilation using this method cannot happen on another thread
540 * without using Lockers.
541 *
542 * \param source Script source code.
543 */
544 static ScriptData* PreCompile(Handle<String> source);
545
546 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000547 * Load previous pre-compilation data.
548 *
549 * \param data Pointer to data returned by a call to Data() of a previous
550 * ScriptData. Ownership is not transferred.
551 * \param length Length of data.
552 */
553 static ScriptData* New(const char* data, int length);
554
555 /**
556 * Returns the length of Data().
557 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000559
560 /**
561 * Returns a serialized representation of this ScriptData that can later be
562 * passed to New(). NOTE: Serialized data is platform-dependent.
563 */
564 virtual const char* Data() = 0;
565
566 /**
567 * Returns true if the source code could not be parsed.
568 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000569 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570};
571
572
573/**
574 * The origin, within a file, of a script.
575 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000576class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000578 inline ScriptOrigin(
579 Handle<Value> resource_name,
580 Handle<Integer> resource_line_offset = Handle<Integer>(),
581 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 : resource_name_(resource_name),
583 resource_line_offset_(resource_line_offset),
584 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000585 inline Handle<Value> ResourceName() const;
586 inline Handle<Integer> ResourceLineOffset() const;
587 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000589 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 Handle<Integer> resource_line_offset_;
591 Handle<Integer> resource_column_offset_;
592};
593
594
595/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000596 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000598class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000601 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000602 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000603 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000604 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000605 * when New() returns
606 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
607 * using pre_data speeds compilation if it's done multiple times.
608 * Owned by caller, no references are kept when New() returns.
609 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000610 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000611 * available to compile event handlers.
612 * \return Compiled script object (context independent; when run it
613 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000615 static Local<Script> New(Handle<String> source,
616 ScriptOrigin* origin = NULL,
617 ScriptData* pre_data = NULL,
618 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619
mads.s.agercbaa0602008-08-14 13:41:48 +0000620 /**
621 * Compiles the specified script using the specified file name
622 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000623 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000624 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000625 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000626 * as the script's origin.
627 * \return Compiled script object (context independent; when run it
628 * will use the currently entered context).
629 */
630 static Local<Script> New(Handle<String> source,
631 Handle<Value> file_name);
632
633 /**
634 * Compiles the specified script (bound to current context).
635 *
636 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000637 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000638 * when Compile() returns
639 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
640 * using pre_data speeds compilation if it's done multiple times.
641 * Owned by caller, no references are kept when Compile() returns.
642 * \param script_data Arbitrary data associated with script. Using
643 * this has same effect as calling SetData(), but makes data available
644 * earlier (i.e. to compile event handlers).
645 * \return Compiled script object, bound to the context that was active
646 * when this function was called. When run it will always use this
647 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000648 */
649 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000650 ScriptOrigin* origin = NULL,
651 ScriptData* pre_data = NULL,
652 Handle<String> script_data = Handle<String>());
653
654 /**
655 * Compiles the specified script using the specified file name
656 * object (typically a string) as the script's origin.
657 *
658 * \param source Script source code.
659 * \param file_name File name to use as script's origin
660 * \param script_data Arbitrary data associated with script. Using
661 * this has same effect as calling SetData(), but makes data available
662 * earlier (i.e. to compile event handlers).
663 * \return Compiled script object, bound to the context that was active
664 * when this function was called. When run it will always use this
665 * context.
666 */
667 static Local<Script> Compile(Handle<String> source,
668 Handle<Value> file_name,
669 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000670
v8.team.kasperl727e9952008-09-02 14:56:44 +0000671 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000672 * Runs the script returning the resulting value. If the script is
673 * context independent (created using ::New) it will be run in the
674 * currently entered context. If it is context specific (created
675 * using ::Compile) it will be run in the context in which it was
676 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000677 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000679
680 /**
681 * Returns the script id value.
682 */
683 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000684
685 /**
686 * Associate an additional data object with the script. This is mainly used
687 * with the debugger as this data object is only available through the
688 * debugger API.
689 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000690 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691};
692
693
694/**
695 * An error message.
696 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000697class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000699 Local<String> Get() const;
700 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000702 /**
703 * Returns the resource name for the script from where the function causing
704 * the error originates.
705 */
ager@chromium.org32912102009-01-16 10:38:43 +0000706 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000708 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000709 * Returns the resource data for the script from where the function causing
710 * the error originates.
711 */
712 Handle<Value> GetScriptData() const;
713
714 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000715 * Exception stack trace. By default stack traces are not captured for
716 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
717 * to change this option.
718 */
719 Handle<StackTrace> GetStackTrace() const;
720
721 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000722 * Returns the number, 1-based, of the line where the error occurred.
723 */
ager@chromium.org32912102009-01-16 10:38:43 +0000724 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000726 /**
727 * Returns the index within the script of the first character where
728 * the error occurred.
729 */
ager@chromium.org32912102009-01-16 10:38:43 +0000730 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000731
732 /**
733 * Returns the index within the script of the last character where
734 * the error occurred.
735 */
ager@chromium.org32912102009-01-16 10:38:43 +0000736 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000737
738 /**
739 * Returns the index within the line of the first character where
740 * the error occurred.
741 */
ager@chromium.org32912102009-01-16 10:38:43 +0000742 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000743
744 /**
745 * Returns the index within the line of the last character where
746 * the error occurred.
747 */
ager@chromium.org32912102009-01-16 10:38:43 +0000748 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000749
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 // TODO(1245381): Print to a string instead of on a FILE.
751 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000752
753 static const int kNoLineNumberInfo = 0;
754 static const int kNoColumnInfo = 0;
755};
756
757
758/**
759 * Representation of a JavaScript stack trace. The information collected is a
760 * snapshot of the execution stack and the information remains valid after
761 * execution continues.
762 */
763class V8EXPORT StackTrace {
764 public:
765 /**
766 * Flags that determine what information is placed captured for each
767 * StackFrame when grabbing the current stack trace.
768 */
769 enum StackTraceOptions {
770 kLineNumber = 1,
771 kColumnOffset = 1 << 1 | kLineNumber,
772 kScriptName = 1 << 2,
773 kFunctionName = 1 << 3,
774 kIsEval = 1 << 4,
775 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000776 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000777 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000778 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000779 };
780
781 /**
782 * Returns a StackFrame at a particular index.
783 */
784 Local<StackFrame> GetFrame(uint32_t index) const;
785
786 /**
787 * Returns the number of StackFrames.
788 */
789 int GetFrameCount() const;
790
791 /**
792 * Returns StackTrace as a v8::Array that contains StackFrame objects.
793 */
794 Local<Array> AsArray();
795
796 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000797 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000798 *
799 * \param frame_limit The maximum number of stack frames we want to capture.
800 * \param options Enumerates the set of things we will capture for each
801 * StackFrame.
802 */
803 static Local<StackTrace> CurrentStackTrace(
804 int frame_limit,
805 StackTraceOptions options = kOverview);
806};
807
808
809/**
810 * A single JavaScript stack frame.
811 */
812class V8EXPORT StackFrame {
813 public:
814 /**
815 * Returns the number, 1-based, of the line for the associate function call.
816 * This method will return Message::kNoLineNumberInfo if it is unable to
817 * retrieve the line number, or if kLineNumber was not passed as an option
818 * when capturing the StackTrace.
819 */
820 int GetLineNumber() const;
821
822 /**
823 * Returns the 1-based column offset on the line for the associated function
824 * call.
825 * This method will return Message::kNoColumnInfo if it is unable to retrieve
826 * the column number, or if kColumnOffset was not passed as an option when
827 * capturing the StackTrace.
828 */
829 int GetColumn() const;
830
831 /**
832 * Returns the name of the resource that contains the script for the
833 * function for this StackFrame.
834 */
835 Local<String> GetScriptName() const;
836
837 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000838 * Returns the name of the resource that contains the script for the
839 * function for this StackFrame or sourceURL value if the script name
840 * is undefined and its source ends with //@ sourceURL=... string.
841 */
842 Local<String> GetScriptNameOrSourceURL() const;
843
844 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000845 * Returns the name of the function associated with this stack frame.
846 */
847 Local<String> GetFunctionName() const;
848
849 /**
850 * Returns whether or not the associated function is compiled via a call to
851 * eval().
852 */
853 bool IsEval() const;
854
855 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000856 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000857 * constructor via "new".
858 */
859 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860};
861
862
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000863// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864
865
866/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000867 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000869class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871 /**
872 * Returns true if this value is the undefined value. See ECMA-262
873 * 4.3.10.
874 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000875 inline bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
877 /**
878 * Returns true if this value is the null value. See ECMA-262
879 * 4.3.11.
880 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000881 inline bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882
883 /**
884 * Returns true if this value is true.
885 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000886 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887
888 /**
889 * Returns true if this value is false.
890 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000891 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892
893 /**
894 * Returns true if this value is an instance of the String type.
895 * See ECMA-262 8.4.
896 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000897 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
899 /**
900 * Returns true if this value is a function.
901 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000902 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
904 /**
905 * Returns true if this value is an array.
906 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000907 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908
v8.team.kasperl727e9952008-09-02 14:56:44 +0000909 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910 * Returns true if this value is an object.
911 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000912 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913
v8.team.kasperl727e9952008-09-02 14:56:44 +0000914 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 * Returns true if this value is boolean.
916 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000917 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000918
v8.team.kasperl727e9952008-09-02 14:56:44 +0000919 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 * Returns true if this value is a number.
921 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000922 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923
v8.team.kasperl727e9952008-09-02 14:56:44 +0000924 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925 * Returns true if this value is external.
926 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000927 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
v8.team.kasperl727e9952008-09-02 14:56:44 +0000929 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930 * Returns true if this value is a 32-bit signed integer.
931 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000932 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000934 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000935 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000936 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000937 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000938
939 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000940 * Returns true if this value is a Date.
941 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000942 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000943
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000944 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000945 * Returns true if this value is a Boolean object.
946 */
947 V8EXPORT bool IsBooleanObject() const;
948
949 /**
950 * Returns true if this value is a Number object.
951 */
952 V8EXPORT bool IsNumberObject() const;
953
954 /**
955 * Returns true if this value is a String object.
956 */
957 V8EXPORT bool IsStringObject() const;
958
959 /**
960 * Returns true if this value is a NativeError.
961 */
962 V8EXPORT bool IsNativeError() const;
963
964 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000965 * Returns true if this value is a RegExp.
966 */
967 V8EXPORT bool IsRegExp() const;
968
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000969 V8EXPORT Local<Boolean> ToBoolean() const;
970 V8EXPORT Local<Number> ToNumber() const;
971 V8EXPORT Local<String> ToString() const;
972 V8EXPORT Local<String> ToDetailString() const;
973 V8EXPORT Local<Object> ToObject() const;
974 V8EXPORT Local<Integer> ToInteger() const;
975 V8EXPORT Local<Uint32> ToUint32() const;
976 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977
978 /**
979 * Attempts to convert a string to an array index.
980 * Returns an empty handle if the conversion fails.
981 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000982 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000984 V8EXPORT bool BooleanValue() const;
985 V8EXPORT double NumberValue() const;
986 V8EXPORT int64_t IntegerValue() const;
987 V8EXPORT uint32_t Uint32Value() const;
988 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989
990 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000991 V8EXPORT bool Equals(Handle<Value> that) const;
992 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000993
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000994 private:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000995 inline bool QuickIsUndefined() const;
996 inline bool QuickIsNull() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000997 inline bool QuickIsString() const;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000998 V8EXPORT bool FullIsUndefined() const;
999 V8EXPORT bool FullIsNull() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001000 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001};
1002
1003
1004/**
1005 * The superclass of primitive values. See ECMA-262 4.3.2.
1006 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001007class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008
1009
1010/**
1011 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1012 * or false value.
1013 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001014class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001016 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 static inline Handle<Boolean> New(bool value);
1018};
1019
1020
1021/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001022 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001024class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001026 enum Encoding {
1027 UNKNOWN_ENCODING = 0x1,
1028 TWO_BYTE_ENCODING = 0x0,
1029 ASCII_ENCODING = 0x4
1030 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001031 /**
1032 * Returns the number of characters in this string.
1033 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001034 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035
v8.team.kasperl727e9952008-09-02 14:56:44 +00001036 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001037 * Returns the number of bytes in the UTF-8 encoded
1038 * representation of this string.
1039 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001040 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001041
1042 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001043 * A fast conservative check for non-ASCII characters. May
1044 * return true even for ASCII strings, but if it returns
1045 * false you can be sure that all characters are in the range
1046 * 0-127.
1047 */
1048 V8EXPORT bool MayContainNonAscii() const;
1049
1050 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001051 * Write the contents of the string to an external buffer.
1052 * If no arguments are given, expects the buffer to be large
1053 * enough to hold the entire string and NULL terminator. Copies
1054 * the contents of the string and the NULL terminator into the
1055 * buffer.
1056 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001057 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1058 * before the end of the buffer.
1059 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001060 * Copies up to length characters into the output buffer.
1061 * Only null-terminates if there is enough space in the buffer.
1062 *
1063 * \param buffer The buffer into which the string will be copied.
1064 * \param start The starting position within the string at which
1065 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001066 * \param length The number of characters to copy from the string. For
1067 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001068 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001069 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001070 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001071 * \return The number of characters copied to the buffer excluding the null
1072 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001073 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001074 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001075 enum WriteOptions {
1076 NO_OPTIONS = 0,
1077 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001078 NO_NULL_TERMINATION = 2,
1079 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001080 };
1081
lrn@chromium.org34e60782011-09-15 07:25:40 +00001082 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001083 V8EXPORT int Write(uint16_t* buffer,
1084 int start = 0,
1085 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001086 int options = NO_OPTIONS) const;
1087 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001088 V8EXPORT int WriteAscii(char* buffer,
1089 int start = 0,
1090 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001091 int options = NO_OPTIONS) const;
1092 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001093 V8EXPORT int WriteUtf8(char* buffer,
1094 int length = -1,
1095 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001096 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097
v8.team.kasperl727e9952008-09-02 14:56:44 +00001098 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001099 * A zero length string.
1100 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001101 V8EXPORT static v8::Local<v8::String> Empty();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001102 inline static v8::Local<v8::String> Empty(Isolate* isolate);
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001103
1104 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001105 * Returns true if the string is external
1106 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001107 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108
v8.team.kasperl727e9952008-09-02 14:56:44 +00001109 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001110 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001111 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001112 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001113
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001114 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001115 public:
1116 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001117
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001118 protected:
1119 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001120
1121 /**
1122 * Internally V8 will call this Dispose method when the external string
1123 * resource is no longer needed. The default implementation will use the
1124 * delete operator. This method can be overridden in subclasses to
1125 * control how allocated external string resources are disposed.
1126 */
1127 virtual void Dispose() { delete this; }
1128
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001129 private:
1130 // Disallow copying and assigning.
1131 ExternalStringResourceBase(const ExternalStringResourceBase&);
1132 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001133
1134 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001135 };
1136
v8.team.kasperl727e9952008-09-02 14:56:44 +00001137 /**
1138 * An ExternalStringResource is a wrapper around a two-byte string
1139 * buffer that resides outside V8's heap. Implement an
1140 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001141 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001142 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001143 class V8EXPORT ExternalStringResource
1144 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 public:
1146 /**
1147 * Override the destructor to manage the life cycle of the underlying
1148 * buffer.
1149 */
1150 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001151
1152 /**
1153 * The string data from the underlying buffer.
1154 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001156
1157 /**
1158 * The length of the string. That is, the number of two-byte characters.
1159 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001161
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162 protected:
1163 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 };
1165
1166 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001167 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001168 * string buffer that resides outside V8's heap. Implement an
1169 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001170 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001171 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001172 * UTF-8, which would require special treatment internally in the
1173 * engine and, in the case of UTF-8, do not allow efficient indexing.
1174 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001175 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001177 class V8EXPORT ExternalAsciiStringResource
1178 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 public:
1180 /**
1181 * Override the destructor to manage the life cycle of the underlying
1182 * buffer.
1183 */
1184 virtual ~ExternalAsciiStringResource() {}
1185 /** The string data from the underlying buffer.*/
1186 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001187 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188 virtual size_t length() const = 0;
1189 protected:
1190 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191 };
1192
1193 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001194 * If the string is an external string, return the ExternalStringResourceBase
1195 * regardless of the encoding, otherwise return NULL. The encoding of the
1196 * string is returned in encoding_out.
1197 */
1198 inline ExternalStringResourceBase* GetExternalStringResourceBase(
1199 Encoding* encoding_out) const;
1200
1201 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001202 * Get the ExternalStringResource for an external string. Returns
1203 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001205 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206
1207 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001208 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001209 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001211 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1212 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001214 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215
1216 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001217 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001219 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 * be careful to supply the length parameter.
1221 * If it is not given, the function calls
1222 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001223 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001225 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226
lrn@chromium.org34e60782011-09-15 07:25:40 +00001227 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001228 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229
1230 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001231 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232
v8.team.kasperl727e9952008-09-02 14:56:44 +00001233 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001234 * Creates a new string by concatenating the left and the right strings
1235 * passed in as parameters.
1236 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001237 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001238 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001239
1240 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001241 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001242 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001243 * resource will be disposed by calling its Dispose method. The caller of
1244 * this function should not otherwise delete or modify the resource. Neither
1245 * should the underlying buffer be deallocated or modified except through the
1246 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001247 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001248 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001249
ager@chromium.org6f10e412009-02-13 10:11:16 +00001250 /**
1251 * Associate an external string resource with this string by transforming it
1252 * in place so that existing references to this string in the JavaScript heap
1253 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001254 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001255 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001256 * The string is not modified if the operation fails. See NewExternal for
1257 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001258 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001259 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260
v8.team.kasperl727e9952008-09-02 14:56:44 +00001261 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001262 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001263 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001264 * resource will be disposed by calling its Dispose method. The caller of
1265 * this function should not otherwise delete or modify the resource. Neither
1266 * should the underlying buffer be deallocated or modified except through the
1267 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001268 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001269 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001270
ager@chromium.org6f10e412009-02-13 10:11:16 +00001271 /**
1272 * Associate an external string resource with this string by transforming it
1273 * in place so that existing references to this string in the JavaScript heap
1274 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001275 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001276 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001277 * The string is not modified if the operation fails. See NewExternal for
1278 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001279 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001280 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001282 /**
1283 * Returns true if this string can be made external.
1284 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001285 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001286
lrn@chromium.org34e60782011-09-15 07:25:40 +00001287 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001288 V8EXPORT static Local<String> NewUndetectable(const char* data,
1289 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290
lrn@chromium.org34e60782011-09-15 07:25:40 +00001291 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001292 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1293 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294
1295 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001296 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001297 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001298 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001299 * then the length() method returns 0 and the * operator returns
1300 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001301 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001302 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001303 public:
1304 explicit Utf8Value(Handle<v8::Value> obj);
1305 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001306 char* operator*() { return str_; }
1307 const char* operator*() const { return str_; }
1308 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001309 private:
1310 char* str_;
1311 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001312
1313 // Disallow copying and assigning.
1314 Utf8Value(const Utf8Value&);
1315 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001316 };
1317
1318 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001319 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001321 * If conversion to a string fails (eg. due to an exception in the toString()
1322 * method of the object) then the length() method returns 0 and the * operator
1323 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001325 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 public:
1327 explicit AsciiValue(Handle<v8::Value> obj);
1328 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001329 char* operator*() { return str_; }
1330 const char* operator*() const { return str_; }
1331 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 private:
1333 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001334 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001335
1336 // Disallow copying and assigning.
1337 AsciiValue(const AsciiValue&);
1338 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 };
1340
1341 /**
1342 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001343 * If conversion to a string fails (eg. due to an exception in the toString()
1344 * method of the object) then the length() method returns 0 and the * operator
1345 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001347 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 public:
1349 explicit Value(Handle<v8::Value> obj);
1350 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001351 uint16_t* operator*() { return str_; }
1352 const uint16_t* operator*() const { return str_; }
1353 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 private:
1355 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001356 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001357
1358 // Disallow copying and assigning.
1359 Value(const Value&);
1360 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001362
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001363 private:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001364 V8EXPORT void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1365 Encoding encoding) const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001366 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1367 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368};
1369
1370
1371/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001372 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001374class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001376 V8EXPORT double Value() const;
1377 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001378 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001380 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001381 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382};
1383
1384
1385/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001386 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001388class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001390 V8EXPORT static Local<Integer> New(int32_t value);
1391 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001392 V8EXPORT static Local<Integer> New(int32_t value, Isolate*);
1393 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001394 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001395 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001397 V8EXPORT Integer();
1398 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399};
1400
1401
1402/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001403 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001405class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001407 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001409 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410};
1411
1412
1413/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001414 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001416class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001418 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001420 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421};
1422
1423
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424enum PropertyAttribute {
1425 None = 0,
1426 ReadOnly = 1 << 0,
1427 DontEnum = 1 << 1,
1428 DontDelete = 1 << 2
1429};
1430
ager@chromium.org3811b432009-10-28 14:53:37 +00001431enum ExternalArrayType {
1432 kExternalByteArray = 1,
1433 kExternalUnsignedByteArray,
1434 kExternalShortArray,
1435 kExternalUnsignedShortArray,
1436 kExternalIntArray,
1437 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001438 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001439 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001440 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001441};
1442
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001444 * Accessor[Getter|Setter] are used as callback functions when
1445 * setting|getting a particular property. See Object and ObjectTemplate's
1446 * method SetAccessor.
1447 */
1448typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1449 const AccessorInfo& info);
1450
1451
1452typedef void (*AccessorSetter)(Local<String> property,
1453 Local<Value> value,
1454 const AccessorInfo& info);
1455
1456
1457/**
1458 * Access control specifications.
1459 *
1460 * Some accessors should be accessible across contexts. These
1461 * accessors have an explicit access control parameter which specifies
1462 * the kind of cross-context access that should be allowed.
1463 *
1464 * Additionally, for security, accessors can prohibit overwriting by
1465 * accessors defined in JavaScript. For objects that have such
1466 * accessors either locally or in their prototype chain it is not
1467 * possible to overwrite the accessor by using __defineGetter__ or
1468 * __defineSetter__ from JavaScript code.
1469 */
1470enum AccessControl {
1471 DEFAULT = 0,
1472 ALL_CAN_READ = 1,
1473 ALL_CAN_WRITE = 1 << 1,
1474 PROHIBITS_OVERWRITING = 1 << 2
1475};
1476
1477
1478/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001479 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001481class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001483 V8EXPORT bool Set(Handle<Value> key,
1484 Handle<Value> value,
1485 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001486
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001487 V8EXPORT bool Set(uint32_t index,
1488 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001489
ager@chromium.orge2902be2009-06-08 12:21:35 +00001490 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001491 // overriding accessors or read-only properties.
1492 //
1493 // Note that if the object has an interceptor the property will be set
1494 // locally, but since the interceptor takes precedence the local property
1495 // will only be returned if the interceptor doesn't return a value.
1496 //
1497 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001498 V8EXPORT bool ForceSet(Handle<Value> key,
1499 Handle<Value> value,
1500 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001501
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001502 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001504 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001505
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001506 /**
1507 * Gets the property attributes of a property which can be None or
1508 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1509 * None when the property doesn't exist.
1510 */
1511 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1512
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 // TODO(1245389): Replace the type-specific versions of these
1514 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001515 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001516
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001517 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001518
1519 // Delete a property on this object bypassing interceptors and
1520 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001521 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001522
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001523 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001524
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001525 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001527 V8EXPORT bool SetAccessor(Handle<String> name,
1528 AccessorGetter getter,
1529 AccessorSetter setter = 0,
1530 Handle<Value> data = Handle<Value>(),
1531 AccessControl settings = DEFAULT,
1532 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001533
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001535 * Returns an array containing the names of the enumerable properties
1536 * of this object, including properties from prototype objects. The
1537 * array returned by this method contains the same values as would
1538 * be enumerated by a for-in statement over this object.
1539 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001540 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001541
1542 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001543 * This function has the same functionality as GetPropertyNames but
1544 * the returned array doesn't contain the names of properties from
1545 * prototype objects.
1546 */
1547 V8EXPORT Local<Array> GetOwnPropertyNames();
1548
1549 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550 * Get the prototype object. This does not skip objects marked to
1551 * be skipped by __proto__ and it does not consult the security
1552 * handler.
1553 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001554 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555
1556 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001557 * Set the prototype object. This does not skip objects marked to
1558 * be skipped by __proto__ and it does not consult the security
1559 * handler.
1560 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001561 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001562
1563 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001564 * Finds an instance of the given function template in the prototype
1565 * chain.
1566 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001567 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1568 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001569
1570 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 * Call builtin Object.prototype.toString on this object.
1572 * This is different from Value::ToString() that may call
1573 * user-defined toString function. This one does not.
1574 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001575 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001577 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001578 * Returns the function invoked as a constructor for this object.
1579 * May be the null value.
1580 */
1581 V8EXPORT Local<Value> GetConstructor();
1582
1583 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001584 * Returns the name of the function invoked as a constructor for this object.
1585 */
1586 V8EXPORT Local<String> GetConstructorName();
1587
kasper.lund212ac232008-07-16 07:07:30 +00001588 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001589 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001590 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001591 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001592 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001593 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001595 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001596 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001597
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001598 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001599 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001600
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001602 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001603 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1604 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1605 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606
1607 /**
1608 * If result.IsEmpty() no real property was located in the prototype chain.
1609 * This means interceptors in the prototype chain are not called.
1610 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001611 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1612 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001613
1614 /**
1615 * If result.IsEmpty() no real property was located on the object or
1616 * in the prototype chain.
1617 * This means interceptors in the prototype chain are not called.
1618 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001619 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620
1621 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001622 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001623
kasper.lund212ac232008-07-16 07:07:30 +00001624 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001625 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001627 /**
1628 * Turns on access check on the object if the object is an instance of
1629 * a template that has access check callbacks. If an object has no
1630 * access check info, the object cannot be accessed by anyone.
1631 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001632 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001633
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001634 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001635 * Returns the identity hash for this object. The current implementation
1636 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001637 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001638 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001639 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001640 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001641 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001642
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001643 /**
1644 * Access hidden properties on JavaScript objects. These properties are
1645 * hidden from the executing JavaScript and only accessible through the V8
1646 * C++ API. Hidden properties introduced by V8 internally (for example the
1647 * identity hash) are prefixed with "v8::".
1648 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001649 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1650 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1651 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001652
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001653 /**
1654 * Returns true if this is an instance of an api function (one
1655 * created from a function created from a function template) and has
1656 * been modified since it was created. Note that this method is
1657 * conservative and may return true for objects that haven't actually
1658 * been modified.
1659 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001660 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001661
1662 /**
1663 * Clone this object with a fast but shallow copy. Values will point
1664 * to the same values as the original object.
1665 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001666 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001668 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001669 * Returns the context in which the object was created.
1670 */
1671 V8EXPORT Local<Context> CreationContext();
1672
1673 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001674 * Set the backing store of the indexed properties to be managed by the
1675 * embedding layer. Access to the indexed properties will follow the rules
1676 * spelled out in CanvasPixelArray.
1677 * Note: The embedding program still owns the data and needs to ensure that
1678 * the backing store is preserved while V8 has a reference.
1679 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001680 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001681 V8EXPORT bool HasIndexedPropertiesInPixelData();
1682 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1683 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001684
ager@chromium.org3811b432009-10-28 14:53:37 +00001685 /**
1686 * Set the backing store of the indexed properties to be managed by the
1687 * embedding layer. Access to the indexed properties will follow the rules
1688 * spelled out for the CanvasArray subtypes in the WebGL specification.
1689 * Note: The embedding program still owns the data and needs to ensure that
1690 * the backing store is preserved while V8 has a reference.
1691 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001692 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1693 void* data,
1694 ExternalArrayType array_type,
1695 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001696 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1697 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1698 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1699 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001700
lrn@chromium.org1c092762011-05-09 09:42:16 +00001701 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001702 * Checks whether a callback is set by the
1703 * ObjectTemplate::SetCallAsFunctionHandler method.
1704 * When an Object is callable this method returns true.
1705 */
1706 V8EXPORT bool IsCallable();
1707
1708 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001709 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001710 * ObjectTemplate::SetCallAsFunctionHandler method.
1711 */
1712 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1713 int argc,
1714 Handle<Value> argv[]);
1715
1716 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001717 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001718 * ObjectTemplate::SetCallAsFunctionHandler method.
1719 * Note: This method behaves like the Function::NewInstance method.
1720 */
1721 V8EXPORT Local<Value> CallAsConstructor(int argc,
1722 Handle<Value> argv[]);
1723
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001724 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001725 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001726
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001728 V8EXPORT Object();
1729 V8EXPORT static void CheckCast(Value* obj);
1730 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1731 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001732
1733 /**
1734 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001735 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001736 */
1737 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738};
1739
1740
1741/**
1742 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1743 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001744class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001746 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747
ager@chromium.org3e875802009-06-29 08:26:34 +00001748 /**
1749 * Clones an element at index |index|. Returns an empty
1750 * handle if cloning fails (for any reason).
1751 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001752 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001753
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001754 /**
1755 * Creates a JavaScript array with the given length. If the length
1756 * is negative the returned array will have length 0.
1757 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001758 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001759
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001760 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001762 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001763 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764};
1765
1766
1767/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001768 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001770class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001772 V8EXPORT Local<Object> NewInstance() const;
1773 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1774 V8EXPORT Local<Value> Call(Handle<Object> recv,
1775 int argc,
1776 Handle<Value> argv[]);
1777 V8EXPORT void SetName(Handle<String> name);
1778 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001779
1780 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001781 * Name inferred from variable or property assignment of this function.
1782 * Used to facilitate debugging and profiling of JavaScript code written
1783 * in an OO style, where many functions are anonymous but are assigned
1784 * to object properties.
1785 */
1786 V8EXPORT Handle<Value> GetInferredName() const;
1787
1788 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001789 * Returns zero based line number of function body and
1790 * kLineOffsetNotFound if no information available.
1791 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001792 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001793 /**
1794 * Returns zero based column number of function body and
1795 * kLineOffsetNotFound if no information available.
1796 */
1797 V8EXPORT int GetScriptColumnNumber() const;
1798 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001799 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001800 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001801 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001802
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001804 V8EXPORT Function();
1805 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806};
1807
1808
1809/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001810 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1811 */
1812class Date : public Object {
1813 public:
1814 V8EXPORT static Local<Value> New(double time);
1815
1816 /**
1817 * A specialization of Value::NumberValue that is more efficient
1818 * because we know the structure of this object.
1819 */
1820 V8EXPORT double NumberValue() const;
1821
1822 static inline Date* Cast(v8::Value* obj);
1823
1824 /**
1825 * Notification that the embedder has changed the time zone,
1826 * daylight savings time, or other date / time configuration
1827 * parameters. V8 keeps a cache of various values used for
1828 * date / time computation. This notification will reset
1829 * those cached values for the current context so that date /
1830 * time configuration changes would be reflected in the Date
1831 * object.
1832 *
1833 * This API should not be called more than needed as it will
1834 * negatively impact the performance of date operations.
1835 */
1836 V8EXPORT static void DateTimeConfigurationChangeNotification();
1837
1838 private:
1839 V8EXPORT static void CheckCast(v8::Value* obj);
1840};
1841
1842
1843/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001844 * A Number object (ECMA-262, 4.3.21).
1845 */
1846class NumberObject : public Object {
1847 public:
1848 V8EXPORT static Local<Value> New(double value);
1849
1850 /**
1851 * Returns the Number held by the object.
1852 */
1853 V8EXPORT double NumberValue() const;
1854
1855 static inline NumberObject* Cast(v8::Value* obj);
1856
1857 private:
1858 V8EXPORT static void CheckCast(v8::Value* obj);
1859};
1860
1861
1862/**
1863 * A Boolean object (ECMA-262, 4.3.15).
1864 */
1865class BooleanObject : public Object {
1866 public:
1867 V8EXPORT static Local<Value> New(bool value);
1868
1869 /**
1870 * Returns the Boolean held by the object.
1871 */
1872 V8EXPORT bool BooleanValue() const;
1873
1874 static inline BooleanObject* Cast(v8::Value* obj);
1875
1876 private:
1877 V8EXPORT static void CheckCast(v8::Value* obj);
1878};
1879
1880
1881/**
1882 * A String object (ECMA-262, 4.3.18).
1883 */
1884class StringObject : public Object {
1885 public:
1886 V8EXPORT static Local<Value> New(Handle<String> value);
1887
1888 /**
1889 * Returns the String held by the object.
1890 */
1891 V8EXPORT Local<String> StringValue() const;
1892
1893 static inline StringObject* Cast(v8::Value* obj);
1894
1895 private:
1896 V8EXPORT static void CheckCast(v8::Value* obj);
1897};
1898
1899
1900/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001901 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1902 */
1903class RegExp : public Object {
1904 public:
1905 /**
1906 * Regular expression flag bits. They can be or'ed to enable a set
1907 * of flags.
1908 */
1909 enum Flags {
1910 kNone = 0,
1911 kGlobal = 1,
1912 kIgnoreCase = 2,
1913 kMultiline = 4
1914 };
1915
1916 /**
1917 * Creates a regular expression from the given pattern string and
1918 * the flags bit field. May throw a JavaScript exception as
1919 * described in ECMA-262, 15.10.4.1.
1920 *
1921 * For example,
1922 * RegExp::New(v8::String::New("foo"),
1923 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1924 * is equivalent to evaluating "/foo/gm".
1925 */
1926 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1927 Flags flags);
1928
1929 /**
1930 * Returns the value of the source property: a string representing
1931 * the regular expression.
1932 */
1933 V8EXPORT Local<String> GetSource() const;
1934
1935 /**
1936 * Returns the flags bit field.
1937 */
1938 V8EXPORT Flags GetFlags() const;
1939
1940 static inline RegExp* Cast(v8::Value* obj);
1941
1942 private:
1943 V8EXPORT static void CheckCast(v8::Value* obj);
1944};
1945
1946
1947/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001948 * A JavaScript value that wraps a C++ void*. This type of value is
1949 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001951 *
1952 * The Wrap function V8 will return the most optimal Value object wrapping the
1953 * C++ void*. The type of the value is not guaranteed to be an External object
1954 * and no assumptions about its type should be made. To access the wrapped
1955 * value Unwrap should be used, all other operations on that object will lead
1956 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001958class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001960 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001961 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001962
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001963 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001964 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001965 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001967 V8EXPORT External();
1968 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001969 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001970 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971};
1972
1973
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001974// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975
1976
1977/**
1978 * The superclass of object and function templates.
1979 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001980class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001981 public:
1982 /** Adds a property to each instance created by this template.*/
1983 void Set(Handle<String> name, Handle<Data> value,
1984 PropertyAttribute attributes = None);
1985 inline void Set(const char* name, Handle<Data> value);
1986 private:
1987 Template();
1988
1989 friend class ObjectTemplate;
1990 friend class FunctionTemplate;
1991};
1992
1993
1994/**
1995 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001996 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 * including the receiver, the number and values of arguments, and
1998 * the holder of the function.
1999 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002000class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 public:
2002 inline int Length() const;
2003 inline Local<Value> operator[](int i) const;
2004 inline Local<Function> Callee() const;
2005 inline Local<Object> This() const;
2006 inline Local<Object> Holder() const;
2007 inline bool IsConstructCall() const;
2008 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002009 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002010
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002012 static const int kIsolateIndex = 0;
2013 static const int kDataIndex = -1;
2014 static const int kCalleeIndex = -2;
2015 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002016
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002018 inline Arguments(internal::Object** implicit_args,
2019 internal::Object** values,
2020 int length,
2021 bool is_construct_call);
2022 internal::Object** implicit_args_;
2023 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002025 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026};
2027
2028
2029/**
2030 * The information passed to an accessor callback about the context
2031 * of the property access.
2032 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002033class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002035 inline AccessorInfo(internal::Object** args)
2036 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002037 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 inline Local<Value> Data() const;
2039 inline Local<Object> This() const;
2040 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002041
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002043 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044};
2045
2046
2047typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2048
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 * NamedProperty[Getter|Setter] are used as interceptors on object.
2051 * See ObjectTemplate::SetNamedPropertyHandler.
2052 */
2053typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2054 const AccessorInfo& info);
2055
2056
2057/**
2058 * Returns the value if the setter intercepts the request.
2059 * Otherwise, returns an empty handle.
2060 */
2061typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2062 Local<Value> value,
2063 const AccessorInfo& info);
2064
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065/**
2066 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002067 * The result is an integer encoding property attributes (like v8::None,
2068 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002070typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2071 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072
2073
2074/**
2075 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002076 * The return value is true if the property could be deleted and false
2077 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 */
2079typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2080 const AccessorInfo& info);
2081
2082/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002083 * Returns an array containing the names of the properties the named
2084 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 */
2086typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2087
v8.team.kasperl727e9952008-09-02 14:56:44 +00002088
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002090 * Returns the value of the property if the getter intercepts the
2091 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 */
2093typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2094 const AccessorInfo& info);
2095
2096
2097/**
2098 * Returns the value if the setter intercepts the request.
2099 * Otherwise, returns an empty handle.
2100 */
2101typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2102 Local<Value> value,
2103 const AccessorInfo& info);
2104
2105
2106/**
2107 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002108 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002110typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2111 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112
2113/**
2114 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002115 * The return value is true if the property could be deleted and false
2116 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117 */
2118typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2119 const AccessorInfo& info);
2120
v8.team.kasperl727e9952008-09-02 14:56:44 +00002121/**
2122 * Returns an array containing the indices of the properties the
2123 * indexed property getter intercepts.
2124 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2126
2127
2128/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002129 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130 */
2131enum AccessType {
2132 ACCESS_GET,
2133 ACCESS_SET,
2134 ACCESS_HAS,
2135 ACCESS_DELETE,
2136 ACCESS_KEYS
2137};
2138
v8.team.kasperl727e9952008-09-02 14:56:44 +00002139
2140/**
2141 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002142 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002143 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002144typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145 Local<Value> key,
2146 AccessType type,
2147 Local<Value> data);
2148
v8.team.kasperl727e9952008-09-02 14:56:44 +00002149
2150/**
2151 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002152 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002153 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002154typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155 uint32_t index,
2156 AccessType type,
2157 Local<Value> data);
2158
2159
2160/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002161 * A FunctionTemplate is used to create functions at runtime. There
2162 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002163 * context. The lifetime of the created function is equal to the
2164 * lifetime of the context. So in case the embedder needs to create
2165 * temporary functions that can be collected using Scripts is
2166 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 *
2168 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002169 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002171 * A FunctionTemplate has a corresponding instance template which is
2172 * used to create object instances when the function is used as a
2173 * constructor. Properties added to the instance template are added to
2174 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 *
2176 * A FunctionTemplate can have a prototype template. The prototype template
2177 * is used to create the prototype object of the function.
2178 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002179 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002181 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2183 * t->Set("func_property", v8::Number::New(1));
2184 *
2185 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2186 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2187 * proto_t->Set("proto_const", v8::Number::New(2));
2188 *
2189 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2190 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2191 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2192 * instance_t->Set("instance_property", Number::New(3));
2193 *
2194 * v8::Local<v8::Function> function = t->GetFunction();
2195 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002196 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197 *
2198 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002199 * and "instance" for the instance object created above. The function
2200 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002202 * \code
2203 * func_property in function == true;
2204 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002206 * function.prototype.proto_method() invokes 'InvokeCallback'
2207 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002209 * instance instanceof function == true;
2210 * instance.instance_accessor calls 'InstanceAccessorCallback'
2211 * instance.instance_property == 3;
2212 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002214 * A FunctionTemplate can inherit from another one by calling the
2215 * FunctionTemplate::Inherit method. The following graph illustrates
2216 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002218 * \code
2219 * FunctionTemplate Parent -> Parent() . prototype -> { }
2220 * ^ ^
2221 * | Inherit(Parent) | .__proto__
2222 * | |
2223 * FunctionTemplate Child -> Child() . prototype -> { }
2224 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002226 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2227 * object of the Child() function has __proto__ pointing to the
2228 * Parent() function's prototype object. An instance of the Child
2229 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002231 * Let Parent be the FunctionTemplate initialized in the previous
2232 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002234 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 * Local<FunctionTemplate> parent = t;
2236 * Local<FunctionTemplate> child = FunctionTemplate::New();
2237 * child->Inherit(parent);
2238 *
2239 * Local<Function> child_function = child->GetFunction();
2240 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002241 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002243 * The Child function and Child instance will have the following
2244 * properties:
2245 *
2246 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002250 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002252class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 public:
2254 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002255 static Local<FunctionTemplate> New(
2256 InvocationCallback callback = 0,
2257 Handle<Value> data = Handle<Value>(),
2258 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 /** Returns the unique function instance in the current execution context.*/
2260 Local<Function> GetFunction();
2261
v8.team.kasperl727e9952008-09-02 14:56:44 +00002262 /**
2263 * Set the call-handler callback for a FunctionTemplate. This
2264 * callback is called whenever the function created from this
2265 * FunctionTemplate is called.
2266 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 void SetCallHandler(InvocationCallback callback,
2268 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269
v8.team.kasperl727e9952008-09-02 14:56:44 +00002270 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 Local<ObjectTemplate> InstanceTemplate();
2272
2273 /** Causes the function template to inherit from a parent function template.*/
2274 void Inherit(Handle<FunctionTemplate> parent);
2275
2276 /**
2277 * A PrototypeTemplate is the template used to create the prototype object
2278 * of the function created by this template.
2279 */
2280 Local<ObjectTemplate> PrototypeTemplate();
2281
v8.team.kasperl727e9952008-09-02 14:56:44 +00002282
2283 /**
2284 * Set the class name of the FunctionTemplate. This is used for
2285 * printing objects created with the function created from the
2286 * FunctionTemplate as its constructor.
2287 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 void SetClassName(Handle<String> name);
2289
2290 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002291 * Determines whether the __proto__ accessor ignores instances of
2292 * the function template. If instances of the function template are
2293 * ignored, __proto__ skips all instances and instead returns the
2294 * next object in the prototype chain.
2295 *
2296 * Call with a value of true to make the __proto__ accessor ignore
2297 * instances of the function template. Call with a value of false
2298 * to make the __proto__ accessor not ignore instances of the
2299 * function template. By default, instances of a function template
2300 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 */
2302 void SetHiddenPrototype(bool value);
2303
2304 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002305 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2306 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002307 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002308 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002309
2310 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002311 * Returns true if the given object is an instance of this function
2312 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313 */
2314 bool HasInstance(Handle<Value> object);
2315
2316 private:
2317 FunctionTemplate();
2318 void AddInstancePropertyAccessor(Handle<String> name,
2319 AccessorGetter getter,
2320 AccessorSetter setter,
2321 Handle<Value> data,
2322 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002323 PropertyAttribute attributes,
2324 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2326 NamedPropertySetter setter,
2327 NamedPropertyQuery query,
2328 NamedPropertyDeleter remover,
2329 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002330 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2332 IndexedPropertySetter setter,
2333 IndexedPropertyQuery query,
2334 IndexedPropertyDeleter remover,
2335 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002336 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2338 Handle<Value> data);
2339
2340 friend class Context;
2341 friend class ObjectTemplate;
2342};
2343
2344
2345/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002346 * An ObjectTemplate is used to create objects at runtime.
2347 *
2348 * Properties added to an ObjectTemplate are added to each object
2349 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002351class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002353 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002355
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002356 /** Creates a new instance of this template.*/
2357 Local<Object> NewInstance();
2358
2359 /**
2360 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002361 *
2362 * Whenever the property with the given name is accessed on objects
2363 * created from this ObjectTemplate the getter and setter callbacks
2364 * are called instead of getting and setting the property directly
2365 * on the JavaScript object.
2366 *
2367 * \param name The name of the property for which an accessor is added.
2368 * \param getter The callback to invoke when getting the property.
2369 * \param setter The callback to invoke when setting the property.
2370 * \param data A piece of data that will be passed to the getter and setter
2371 * callbacks whenever they are invoked.
2372 * \param settings Access control settings for the accessor. This is a bit
2373 * field consisting of one of more of
2374 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2375 * The default is to not allow cross-context access.
2376 * ALL_CAN_READ means that all cross-context reads are allowed.
2377 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2378 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2379 * cross-context access.
2380 * \param attribute The attributes of the property for which an accessor
2381 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002382 * \param signature The signature describes valid receivers for the accessor
2383 * and is used to perform implicit instance checks against them. If the
2384 * receiver is incompatible (i.e. is not an instance of the constructor as
2385 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2386 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387 */
2388 void SetAccessor(Handle<String> name,
2389 AccessorGetter getter,
2390 AccessorSetter setter = 0,
2391 Handle<Value> data = Handle<Value>(),
2392 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002393 PropertyAttribute attribute = None,
2394 Handle<AccessorSignature> signature =
2395 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396
2397 /**
2398 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002399 *
2400 * Whenever a named property is accessed on objects created from
2401 * this object template, the provided callback is invoked instead of
2402 * accessing the property directly on the JavaScript object.
2403 *
2404 * \param getter The callback to invoke when getting a property.
2405 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002406 * \param query The callback to invoke to check if a property is present,
2407 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002408 * \param deleter The callback to invoke when deleting a property.
2409 * \param enumerator The callback to invoke to enumerate all the named
2410 * properties of an object.
2411 * \param data A piece of data that will be passed to the callbacks
2412 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 */
2414 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2415 NamedPropertySetter setter = 0,
2416 NamedPropertyQuery query = 0,
2417 NamedPropertyDeleter deleter = 0,
2418 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002419 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420
2421 /**
2422 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002423 *
2424 * Whenever an indexed property is accessed on objects created from
2425 * this object template, the provided callback is invoked instead of
2426 * accessing the property directly on the JavaScript object.
2427 *
2428 * \param getter The callback to invoke when getting a property.
2429 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002430 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002431 * \param deleter The callback to invoke when deleting a property.
2432 * \param enumerator The callback to invoke to enumerate all the indexed
2433 * properties of an object.
2434 * \param data A piece of data that will be passed to the callbacks
2435 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436 */
2437 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2438 IndexedPropertySetter setter = 0,
2439 IndexedPropertyQuery query = 0,
2440 IndexedPropertyDeleter deleter = 0,
2441 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002442 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002443
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 /**
2445 * Sets the callback to be used when calling instances created from
2446 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002447 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448 * function.
2449 */
2450 void SetCallAsFunctionHandler(InvocationCallback callback,
2451 Handle<Value> data = Handle<Value>());
2452
v8.team.kasperl727e9952008-09-02 14:56:44 +00002453 /**
2454 * Mark object instances of the template as undetectable.
2455 *
2456 * In many ways, undetectable objects behave as though they are not
2457 * there. They behave like 'undefined' in conditionals and when
2458 * printed. However, properties can be accessed and called as on
2459 * normal objects.
2460 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 void MarkAsUndetectable();
2462
v8.team.kasperl727e9952008-09-02 14:56:44 +00002463 /**
2464 * Sets access check callbacks on the object template.
2465 *
2466 * When accessing properties on instances of this object template,
2467 * the access check callback will be called to determine whether or
2468 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002469 * The last parameter specifies whether access checks are turned
2470 * on by default on instances. If access checks are off by default,
2471 * they can be turned on on individual instances by calling
2472 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002473 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2475 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002476 Handle<Value> data = Handle<Value>(),
2477 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478
kasper.lund212ac232008-07-16 07:07:30 +00002479 /**
2480 * Gets the number of internal fields for objects generated from
2481 * this template.
2482 */
2483 int InternalFieldCount();
2484
2485 /**
2486 * Sets the number of internal fields for objects generated from
2487 * this template.
2488 */
2489 void SetInternalFieldCount(int value);
2490
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491 private:
2492 ObjectTemplate();
2493 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2494 friend class FunctionTemplate;
2495};
2496
2497
2498/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002499 * A Signature specifies which receivers and arguments are valid
2500 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002501 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002502class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503 public:
2504 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2505 Handle<FunctionTemplate>(),
2506 int argc = 0,
2507 Handle<FunctionTemplate> argv[] = 0);
2508 private:
2509 Signature();
2510};
2511
2512
2513/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002514 * An AccessorSignature specifies which receivers are valid parameters
2515 * to an accessor callback.
2516 */
2517class V8EXPORT AccessorSignature : public Data {
2518 public:
2519 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2520 Handle<FunctionTemplate>());
2521 private:
2522 AccessorSignature();
2523};
2524
2525
2526/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002527 * A utility for determining the type of objects based on the template
2528 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002529 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002530class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531 public:
2532 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2533 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2534 int match(Handle<Value> value);
2535 private:
2536 TypeSwitch();
2537};
2538
2539
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002540// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002542class V8EXPORT ExternalAsciiStringResourceImpl
2543 : public String::ExternalAsciiStringResource {
2544 public:
2545 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2546 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2547 : data_(data), length_(length) {}
2548 const char* data() const { return data_; }
2549 size_t length() const { return length_; }
2550
2551 private:
2552 const char* data_;
2553 size_t length_;
2554};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002555
2556/**
2557 * Ignore
2558 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002559class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002560 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002561 // Note that the strings passed into this constructor must live as long
2562 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002564 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002566 const char** deps = 0,
2567 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568 virtual ~Extension() { }
2569 virtual v8::Handle<v8::FunctionTemplate>
2570 GetNativeFunction(v8::Handle<v8::String> name) {
2571 return v8::Handle<v8::FunctionTemplate>();
2572 }
2573
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002574 const char* name() const { return name_; }
2575 size_t source_length() const { return source_length_; }
2576 const String::ExternalAsciiStringResource* source() const {
2577 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002578 int dependency_count() { return dep_count_; }
2579 const char** dependencies() { return deps_; }
2580 void set_auto_enable(bool value) { auto_enable_ = value; }
2581 bool auto_enable() { return auto_enable_; }
2582
2583 private:
2584 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002585 size_t source_length_; // expected to initialize before source_
2586 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587 int dep_count_;
2588 const char** deps_;
2589 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002590
2591 // Disallow copying and assigning.
2592 Extension(const Extension&);
2593 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594};
2595
2596
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002597void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002598
2599
2600/**
2601 * Ignore
2602 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002603class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604 public:
2605 inline DeclareExtension(Extension* extension) {
2606 RegisterExtension(extension);
2607 }
2608};
2609
2610
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002611// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612
2613
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002614Handle<Primitive> V8EXPORT Undefined();
2615Handle<Primitive> V8EXPORT Null();
2616Handle<Boolean> V8EXPORT True();
2617Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002619inline Handle<Primitive> Undefined(Isolate* isolate);
2620inline Handle<Primitive> Null(Isolate* isolate);
2621inline Handle<Boolean> True(Isolate* isolate);
2622inline Handle<Boolean> False(Isolate* isolate);
2623
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624
2625/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002626 * A set of constraints that specifies the limits of the runtime's memory use.
2627 * You must set the heap size before initializing the VM - the size cannot be
2628 * adjusted after the VM is initialized.
2629 *
2630 * If you are using threads then you should hold the V8::Locker lock while
2631 * setting the stack limit and you must set a non-default stack limit separately
2632 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002634class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635 public:
2636 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002637 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002639 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002641 int max_executable_size() { return max_executable_size_; }
2642 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002643 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002644 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2646 private:
2647 int max_young_space_size_;
2648 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002649 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 uint32_t* stack_limit_;
2651};
2652
2653
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002654bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655
2656
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002657// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658
2659
2660typedef void (*FatalErrorCallback)(const char* location, const char* message);
2661
2662
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002663typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664
2665
2666/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002667 * Schedules an exception to be thrown when returning to JavaScript. When an
2668 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002670 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002672Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673
2674/**
2675 * Create new error objects by calling the corresponding error object
2676 * constructor with the message.
2677 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002678class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 public:
2680 static Local<Value> RangeError(Handle<String> message);
2681 static Local<Value> ReferenceError(Handle<String> message);
2682 static Local<Value> SyntaxError(Handle<String> message);
2683 static Local<Value> TypeError(Handle<String> message);
2684 static Local<Value> Error(Handle<String> message);
2685};
2686
2687
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002688// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002690typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002692typedef void* (*CreateHistogramCallback)(const char* name,
2693 int min,
2694 int max,
2695 size_t buckets);
2696
2697typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2698
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002699// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002700 enum ObjectSpace {
2701 kObjectSpaceNewSpace = 1 << 0,
2702 kObjectSpaceOldPointerSpace = 1 << 1,
2703 kObjectSpaceOldDataSpace = 1 << 2,
2704 kObjectSpaceCodeSpace = 1 << 3,
2705 kObjectSpaceMapSpace = 1 << 4,
2706 kObjectSpaceLoSpace = 1 << 5,
2707
2708 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2709 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2710 kObjectSpaceLoSpace
2711 };
2712
2713 enum AllocationAction {
2714 kAllocationActionAllocate = 1 << 0,
2715 kAllocationActionFree = 1 << 1,
2716 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2717 };
2718
2719typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2720 AllocationAction action,
2721 int size);
2722
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002723// --- Leave Script Callback ---
2724typedef void (*CallCompletedCallback)();
2725
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002726// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2728 AccessType type,
2729 Local<Value> data);
2730
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002731// --- AllowCodeGenerationFromStrings callbacks ---
2732
2733/**
2734 * Callback to check if code generation from strings is allowed. See
2735 * Context::AllowCodeGenerationFromStrings.
2736 */
2737typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2738
2739// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740
2741/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002742 * Applications can register callback functions which will be called
2743 * before and after a garbage collection. Allocations are not
2744 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002745 * objects (set or delete properties for example) since it is possible
2746 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002748enum GCType {
2749 kGCTypeScavenge = 1 << 0,
2750 kGCTypeMarkSweepCompact = 1 << 1,
2751 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2752};
2753
2754enum GCCallbackFlags {
2755 kNoGCCallbackFlags = 0,
2756 kGCCallbackFlagCompacted = 1 << 0
2757};
2758
2759typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2760typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2761
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762typedef void (*GCCallback)();
2763
2764
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002766 * Collection of V8 heap information.
2767 *
2768 * Instances of this class can be passed to v8::V8::HeapStatistics to
2769 * get heap statistics from V8.
2770 */
2771class V8EXPORT HeapStatistics {
2772 public:
2773 HeapStatistics();
2774 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002775 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002776 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002777 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002778
2779 private:
2780 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002781 void set_total_heap_size_executable(size_t size) {
2782 total_heap_size_executable_ = size;
2783 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002784 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002785 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002786
2787 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002788 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002789 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002790 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002791
2792 friend class V8;
2793};
2794
2795
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002796class RetainedObjectInfo;
2797
ager@chromium.org3811b432009-10-28 14:53:37 +00002798/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002799 * Isolate represents an isolated instance of the V8 engine. V8
2800 * isolates have completely separate states. Objects from one isolate
2801 * must not be used in other isolates. When V8 is initialized a
2802 * default isolate is implicitly created and entered. The embedder
2803 * can create additional isolates and use them in parallel in multiple
2804 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002805 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002806 */
2807class V8EXPORT Isolate {
2808 public:
2809 /**
2810 * Stack-allocated class which sets the isolate for all operations
2811 * executed within a local scope.
2812 */
2813 class V8EXPORT Scope {
2814 public:
2815 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2816 isolate->Enter();
2817 }
2818
2819 ~Scope() { isolate_->Exit(); }
2820
2821 private:
2822 Isolate* const isolate_;
2823
2824 // Prevent copying of Scope objects.
2825 Scope(const Scope&);
2826 Scope& operator=(const Scope&);
2827 };
2828
2829 /**
2830 * Creates a new isolate. Does not change the currently entered
2831 * isolate.
2832 *
2833 * When an isolate is no longer used its resources should be freed
2834 * by calling Dispose(). Using the delete operator is not allowed.
2835 */
2836 static Isolate* New();
2837
2838 /**
2839 * Returns the entered isolate for the current thread or NULL in
2840 * case there is no current isolate.
2841 */
2842 static Isolate* GetCurrent();
2843
2844 /**
2845 * Methods below this point require holding a lock (using Locker) in
2846 * a multi-threaded environment.
2847 */
2848
2849 /**
2850 * Sets this isolate as the entered one for the current thread.
2851 * Saves the previously entered one (if any), so that it can be
2852 * restored when exiting. Re-entering an isolate is allowed.
2853 */
2854 void Enter();
2855
2856 /**
2857 * Exits this isolate by restoring the previously entered one in the
2858 * current thread. The isolate may still stay the same, if it was
2859 * entered more than once.
2860 *
2861 * Requires: this == Isolate::GetCurrent().
2862 */
2863 void Exit();
2864
2865 /**
2866 * Disposes the isolate. The isolate must not be entered by any
2867 * thread to be disposable.
2868 */
2869 void Dispose();
2870
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002871 /**
2872 * Associate embedder-specific data with the isolate
2873 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002874 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002875
2876 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002877 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002878 * Returns NULL if SetData has never been called.
2879 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002880 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002881
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002882 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002883 Isolate();
2884 Isolate(const Isolate&);
2885 ~Isolate();
2886 Isolate& operator=(const Isolate&);
2887 void* operator new(size_t size);
2888 void operator delete(void*, size_t);
2889};
2890
2891
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002892class StartupData {
2893 public:
2894 enum CompressionAlgorithm {
2895 kUncompressed,
2896 kBZip2
2897 };
2898
2899 const char* data;
2900 int compressed_size;
2901 int raw_size;
2902};
2903
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002904
2905/**
2906 * A helper class for driving V8 startup data decompression. It is based on
2907 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2908 * for an embedder to use this class, instead, API functions can be used
2909 * directly.
2910 *
2911 * For an example of the class usage, see the "shell.cc" sample application.
2912 */
2913class V8EXPORT StartupDataDecompressor { // NOLINT
2914 public:
2915 StartupDataDecompressor();
2916 virtual ~StartupDataDecompressor();
2917 int Decompress();
2918
2919 protected:
2920 virtual int DecompressData(char* raw_data,
2921 int* raw_data_size,
2922 const char* compressed_data,
2923 int compressed_data_size) = 0;
2924
2925 private:
2926 char** raw_data;
2927};
2928
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002929
2930/**
2931 * EntropySource is used as a callback function when v8 needs a source
2932 * of entropy.
2933 */
2934typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2935
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002936
2937/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002938 * ReturnAddressLocationResolver is used as a callback function when v8 is
2939 * resolving the location of a return address on the stack. Profilers that
2940 * change the return address on the stack can use this to resolve the stack
2941 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002942 *
2943 * \param return_addr_location points to a location on stack where a machine
2944 * return address resides.
2945 * \returns either return_addr_location, or else a pointer to the profiler's
2946 * copy of the original return address.
2947 *
2948 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00002949 */
2950typedef uintptr_t (*ReturnAddressLocationResolver)(
2951 uintptr_t return_addr_location);
2952
2953
2954/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002955 * FunctionEntryHook is the type of the profile entry hook called at entry to
2956 * any generated function when function-level profiling is enabled.
2957 *
2958 * \param function the address of the function that's being entered.
2959 * \param return_addr_location points to a location on stack where the machine
2960 * return address resides. This can be used to identify the caller of
2961 * \p function, and/or modified to divert execution when \p function exits.
2962 *
2963 * \note the entry hook must not cause garbage collection.
2964 */
2965typedef void (*FunctionEntryHook)(uintptr_t function,
2966 uintptr_t return_addr_location);
2967
2968
2969/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002970 * A JIT code event is issued each time code is added, moved or removed.
2971 *
2972 * \note removal events are not currently issued.
2973 */
2974struct JitCodeEvent {
2975 enum EventType {
2976 CODE_ADDED,
2977 CODE_MOVED,
2978 CODE_REMOVED
2979 };
2980
2981 // Type of event.
2982 EventType type;
2983 // Start of the instructions.
2984 void* code_start;
2985 // Size of the instructions.
2986 size_t code_len;
2987
2988 union {
2989 // Only valid for CODE_ADDED.
2990 struct {
2991 // Name of the object associated with the code, note that the string is
2992 // not zero-terminated.
2993 const char* str;
2994 // Number of chars in str.
2995 size_t len;
2996 } name;
2997 // New location of instructions. Only valid for CODE_MOVED.
2998 void* new_code_start;
2999 };
3000};
3001
3002/**
3003 * Option flags passed to the SetJitCodeEventHandler function.
3004 */
3005enum JitCodeEventOptions {
3006 kJitCodeEventDefault = 0,
3007 // Generate callbacks for already existent code.
3008 kJitCodeEventEnumExisting = 1
3009};
3010
3011
3012/**
3013 * Callback function passed to SetJitCodeEventHandler.
3014 *
3015 * \param event code add, move or removal event.
3016 */
3017typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3018
3019
3020/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003021 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003022 */
3023class V8EXPORT ExternalResourceVisitor { // NOLINT
3024 public:
3025 virtual ~ExternalResourceVisitor() {}
3026 virtual void VisitExternalString(Handle<String> string) {}
3027};
3028
3029
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003030/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003031 * Interface for iterating through all the persistent handles in the heap.
3032 */
3033class V8EXPORT PersistentHandleVisitor { // NOLINT
3034 public:
3035 virtual ~PersistentHandleVisitor() {}
3036 virtual void VisitPersistentHandle(Persistent<Value> value,
3037 uint16_t class_id) {}
3038};
3039
3040
3041/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003042 * Container class for static utility functions.
3043 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003044class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003045 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003046 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003047 static void SetFatalErrorHandler(FatalErrorCallback that);
3048
v8.team.kasperl727e9952008-09-02 14:56:44 +00003049 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003050 * Set the callback to invoke to check if code generation from
3051 * strings should be allowed.
3052 */
3053 static void SetAllowCodeGenerationFromStringsCallback(
3054 AllowCodeGenerationFromStringsCallback that);
3055
3056 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003057 * Ignore out-of-memory exceptions.
3058 *
3059 * V8 running out of memory is treated as a fatal error by default.
3060 * This means that the fatal error handler is called and that V8 is
3061 * terminated.
3062 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003063 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003064 * out-of-memory situation as a fatal error. This way, the contexts
3065 * that did not cause the out of memory problem might be able to
3066 * continue execution.
3067 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068 static void IgnoreOutOfMemoryException();
3069
v8.team.kasperl727e9952008-09-02 14:56:44 +00003070 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003071 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003072 * fatal errors such as out-of-memory situations.
3073 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003074 static bool IsDead();
3075
3076 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003077 * The following 4 functions are to be used when V8 is built with
3078 * the 'compress_startup_data' flag enabled. In this case, the
3079 * embedder must decompress startup data prior to initializing V8.
3080 *
3081 * This is how interaction with V8 should look like:
3082 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3083 * v8::StartupData* compressed_data =
3084 * new v8::StartupData[compressed_data_count];
3085 * v8::V8::GetCompressedStartupData(compressed_data);
3086 * ... decompress data (compressed_data can be updated in-place) ...
3087 * v8::V8::SetDecompressedStartupData(compressed_data);
3088 * ... now V8 can be initialized
3089 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003090 *
3091 * A helper class StartupDataDecompressor is provided. It implements
3092 * the protocol of the interaction described above, and can be used in
3093 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003094 */
3095 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3096 static int GetCompressedStartupDataCount();
3097 static void GetCompressedStartupData(StartupData* compressed_data);
3098 static void SetDecompressedStartupData(StartupData* decompressed_data);
3099
3100 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003101 * Adds a message listener.
3102 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003103 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003104 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003105 */
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003106 static bool AddMessageListener(MessageCallback that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003107
3108 /**
3109 * Remove all message listeners from the specified callback function.
3110 */
3111 static void RemoveMessageListeners(MessageCallback that);
3112
3113 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003114 * Tells V8 to capture current stack trace when uncaught exception occurs
3115 * and report it to the message listeners. The option is off by default.
3116 */
3117 static void SetCaptureStackTraceForUncaughtExceptions(
3118 bool capture,
3119 int frame_limit = 10,
3120 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3121
3122 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003123 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003124 */
3125 static void SetFlagsFromString(const char* str, int length);
3126
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003127 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003128 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003129 */
3130 static void SetFlagsFromCommandLine(int* argc,
3131 char** argv,
3132 bool remove_flags);
3133
kasper.lund7276f142008-07-30 08:49:36 +00003134 /** Get the version string. */
3135 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003136
3137 /**
3138 * Enables the host application to provide a mechanism for recording
3139 * statistics counters.
3140 */
3141 static void SetCounterFunction(CounterLookupCallback);
3142
3143 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003144 * Enables the host application to provide a mechanism for recording
3145 * histograms. The CreateHistogram function returns a
3146 * histogram which will later be passed to the AddHistogramSample
3147 * function.
3148 */
3149 static void SetCreateHistogramFunction(CreateHistogramCallback);
3150 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3151
3152 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003153 * Enables the computation of a sliding window of states. The sliding
3154 * window information is recorded in statistics counters.
3155 */
3156 static void EnableSlidingStateWindow();
3157
3158 /** Callback function for reporting failed access checks.*/
3159 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3160
3161 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003162 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003163 * garbage collection. Allocations are not allowed in the
3164 * callback function, you therefore cannot manipulate objects (set
3165 * or delete properties for example) since it is possible such
3166 * operations will result in the allocation of objects. It is possible
3167 * to specify the GCType filter for your callback. But it is not possible to
3168 * register the same callback function two times with different
3169 * GCType filters.
3170 */
3171 static void AddGCPrologueCallback(
3172 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3173
3174 /**
3175 * This function removes callback which was installed by
3176 * AddGCPrologueCallback function.
3177 */
3178 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3179
3180 /**
3181 * The function is deprecated. Please use AddGCPrologueCallback instead.
3182 * Enables the host application to receive a notification before a
3183 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003184 * callback function, you therefore cannot manipulate objects (set
3185 * or delete properties for example) since it is possible such
3186 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003187 */
3188 static void SetGlobalGCPrologueCallback(GCCallback);
3189
3190 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003191 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003192 * garbage collection. Allocations are not allowed in the
3193 * callback function, you therefore cannot manipulate objects (set
3194 * or delete properties for example) since it is possible such
3195 * operations will result in the allocation of objects. It is possible
3196 * to specify the GCType filter for your callback. But it is not possible to
3197 * register the same callback function two times with different
3198 * GCType filters.
3199 */
3200 static void AddGCEpilogueCallback(
3201 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3202
3203 /**
3204 * This function removes callback which was installed by
3205 * AddGCEpilogueCallback function.
3206 */
3207 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3208
3209 /**
3210 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3211 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003212 * major garbage collection. Allocations are not allowed in the
3213 * callback function, you therefore cannot manipulate objects (set
3214 * or delete properties for example) since it is possible such
3215 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003216 */
3217 static void SetGlobalGCEpilogueCallback(GCCallback);
3218
3219 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003220 * Enables the host application to provide a mechanism to be notified
3221 * and perform custom logging when V8 Allocates Executable Memory.
3222 */
3223 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3224 ObjectSpace space,
3225 AllocationAction action);
3226
3227 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003228 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003229 */
3230 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3231
3232 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003233 * Adds a callback to notify the host application when a script finished
3234 * running. If a script re-enters the runtime during executing, the
3235 * CallCompletedCallback is only invoked when the outer-most script
3236 * execution ends. Executing scripts inside the callback do not trigger
3237 * further callbacks.
3238 */
3239 static void AddCallCompletedCallback(CallCompletedCallback callback);
3240
3241 /**
3242 * Removes callback that was installed by AddCallCompletedCallback.
3243 */
3244 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3245
3246 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003247 * Allows the host application to group objects together. If one
3248 * object in the group is alive, all objects in the group are alive.
3249 * After each garbage collection, object groups are removed. It is
3250 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003251 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00003252 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003253 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003254 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003255 static void AddObjectGroup(Persistent<Value>* objects,
3256 size_t length,
3257 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003258
3259 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003260 * Allows the host application to declare implicit references between
3261 * the objects: if |parent| is alive, all |children| are alive too.
3262 * After each garbage collection, all implicit references
3263 * are removed. It is intended to be used in the before-garbage-collection
3264 * callback function.
3265 */
3266 static void AddImplicitReferences(Persistent<Object> parent,
3267 Persistent<Value>* children,
3268 size_t length);
3269
3270 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003271 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003272 * initialize from scratch. This function is called implicitly if
3273 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274 */
3275 static bool Initialize();
3276
kasper.lund7276f142008-07-30 08:49:36 +00003277 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003278 * Allows the host application to provide a callback which can be used
3279 * as a source of entropy for random number generators.
3280 */
3281 static void SetEntropySource(EntropySource source);
3282
3283 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003284 * Allows the host application to provide a callback that allows v8 to
3285 * cooperate with a profiler that rewrites return addresses on stack.
3286 */
3287 static void SetReturnAddressLocationResolver(
3288 ReturnAddressLocationResolver return_address_resolver);
3289
3290 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003291 * Allows the host application to provide the address of a function that's
3292 * invoked on entry to every V8-generated function.
3293 * Note that \p entry_hook is invoked at the very start of each
3294 * generated function.
3295 *
3296 * \param entry_hook a function that will be invoked on entry to every
3297 * V8-generated function.
3298 * \returns true on success on supported platforms, false on failure.
3299 * \note Setting a new entry hook function when one is already active will
3300 * fail.
3301 */
3302 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3303
3304 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003305 * Allows the host application to provide the address of a function that is
3306 * notified each time code is added, moved or removed.
3307 *
3308 * \param options options for the JIT code event handler.
3309 * \param event_handler the JIT code event handler, which will be invoked
3310 * each time code is added, moved or removed.
3311 * \note \p event_handler won't get notified of existent code.
3312 * \note since code removal notifications are not currently issued, the
3313 * \p event_handler may get notifications of code that overlaps earlier
3314 * code notifications. This happens when code areas are reused, and the
3315 * earlier overlapping code areas should therefore be discarded.
3316 * \note the events passed to \p event_handler and the strings they point to
3317 * are not guaranteed to live past each call. The \p event_handler must
3318 * copy strings and other parameters it needs to keep around.
3319 * \note the set of events declared in JitCodeEvent::EventType is expected to
3320 * grow over time, and the JitCodeEvent structure is expected to accrue
3321 * new members. The \p event_handler function must ignore event codes
3322 * it does not recognize to maintain future compatibility.
3323 */
3324 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3325 JitCodeEventHandler event_handler);
3326
3327 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003328 * Adjusts the amount of registered external memory. Used to give
3329 * V8 an indication of the amount of externally allocated memory
3330 * that is kept alive by JavaScript objects. V8 uses this to decide
3331 * when to perform global garbage collections. Registering
3332 * externally allocated memory will trigger global garbage
3333 * collections more often than otherwise in an attempt to garbage
3334 * collect the JavaScript objects keeping the externally allocated
3335 * memory alive.
3336 *
3337 * \param change_in_bytes the change in externally allocated memory
3338 * that is kept alive by JavaScript objects.
3339 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003340 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003341 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3342 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003343
iposva@chromium.org245aa852009-02-10 00:49:54 +00003344 /**
3345 * Suspends recording of tick samples in the profiler.
3346 * When the V8 profiling mode is enabled (usually via command line
3347 * switches) this function suspends recording of tick samples.
3348 * Profiling ticks are discarded until ResumeProfiler() is called.
3349 *
3350 * See also the --prof and --prof_auto command line switches to
3351 * enable V8 profiling.
3352 */
3353 static void PauseProfiler();
3354
3355 /**
3356 * Resumes recording of tick samples in the profiler.
3357 * See also PauseProfiler().
3358 */
3359 static void ResumeProfiler();
3360
ager@chromium.org41826e72009-03-30 13:30:57 +00003361 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003362 * Return whether profiler is currently paused.
3363 */
3364 static bool IsProfilerPaused();
3365
3366 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003367 * Retrieve the V8 thread id of the calling thread.
3368 *
3369 * The thread id for a thread should only be retrieved after the V8
3370 * lock has been acquired with a Locker object with that thread.
3371 */
3372 static int GetCurrentThreadId();
3373
3374 /**
3375 * Forcefully terminate execution of a JavaScript thread. This can
3376 * be used to terminate long-running scripts.
3377 *
3378 * TerminateExecution should only be called when then V8 lock has
3379 * been acquired with a Locker object. Therefore, in order to be
3380 * able to terminate long-running threads, preemption must be
3381 * enabled to allow the user of TerminateExecution to acquire the
3382 * lock.
3383 *
3384 * The termination is achieved by throwing an exception that is
3385 * uncatchable by JavaScript exception handlers. Termination
3386 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003387 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003388 * exception handler that catches an exception should check if that
3389 * exception is a termination exception and immediately return if
3390 * that is the case. Returning immediately in that case will
3391 * continue the propagation of the termination exception if needed.
3392 *
3393 * The thread id passed to TerminateExecution must have been
3394 * obtained by calling GetCurrentThreadId on the thread in question.
3395 *
3396 * \param thread_id The thread id of the thread to terminate.
3397 */
3398 static void TerminateExecution(int thread_id);
3399
3400 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003401 * Forcefully terminate the current thread of JavaScript execution
3402 * in the given isolate. If no isolate is provided, the default
3403 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003404 *
3405 * This method can be used by any thread even if that thread has not
3406 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003407 *
3408 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003409 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003410 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003411
3412 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003413 * Is V8 terminating JavaScript execution.
3414 *
3415 * Returns true if JavaScript execution is currently terminating
3416 * because of a call to TerminateExecution. In that case there are
3417 * still JavaScript frames on the stack and the termination
3418 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003419 *
3420 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003421 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003422 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003423
3424 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003425 * Releases any resources used by v8 and stops any utility threads
3426 * that may be running. Note that disposing v8 is permanent, it
3427 * cannot be reinitialized.
3428 *
3429 * It should generally not be necessary to dispose v8 before exiting
3430 * a process, this should happen automatically. It is only necessary
3431 * to use if the process needs the resources taken up by v8.
3432 */
3433 static bool Dispose();
3434
ager@chromium.org3811b432009-10-28 14:53:37 +00003435 /**
3436 * Get statistics about the heap memory usage.
3437 */
3438 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003439
3440 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003441 * Iterates through all external resources referenced from current isolate
3442 * heap. This method is not expected to be used except for debugging purposes
3443 * and may be quite slow.
3444 */
3445 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3446
3447 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003448 * Iterates through all the persistent handles in the current isolate's heap
3449 * that have class_ids.
3450 */
3451 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3452
3453 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003454 * Optional notification that the embedder is idle.
3455 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003456 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003457 * Returns true if the embedder should stop calling IdleNotification
3458 * until real work has been done. This indicates that V8 has done
3459 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003460 *
3461 * The hint argument specifies the amount of work to be done in the function
3462 * on scale from 1 to 1000. There is no guarantee that the actual work will
3463 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003464 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003465 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003466
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003467 /**
3468 * Optional notification that the system is running low on memory.
3469 * V8 uses these notifications to attempt to free memory.
3470 */
3471 static void LowMemoryNotification();
3472
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003473 /**
3474 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003475 * these notifications to guide the GC heuristic. Returns the number
3476 * of context disposals - including this one - since the last time
3477 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003478 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003479 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003480
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003481 private:
3482 V8();
3483
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003484 static internal::Object** GlobalizeReference(internal::Object** handle);
3485 static void DisposeGlobal(internal::Object** global_handle);
3486 static void MakeWeak(internal::Object** global_handle,
3487 void* data,
3488 WeakReferenceCallback);
3489 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003490 static void MarkIndependent(internal::Object** global_handle);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003491 static bool IsGlobalIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003492 static bool IsGlobalNearDeath(internal::Object** global_handle);
3493 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003494 static void SetWrapperClassId(internal::Object** global_handle,
3495 uint16_t class_id);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003496 static uint16_t GetWrapperClassId(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003497
3498 template <class T> friend class Handle;
3499 template <class T> friend class Local;
3500 template <class T> friend class Persistent;
3501 friend class Context;
3502};
3503
3504
3505/**
3506 * An external exception handler.
3507 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003508class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003509 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003510 /**
3511 * Creates a new try/catch block and registers it with v8.
3512 */
3513 TryCatch();
3514
3515 /**
3516 * Unregisters and deletes this try/catch block.
3517 */
3518 ~TryCatch();
3519
3520 /**
3521 * Returns true if an exception has been caught by this try/catch block.
3522 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003523 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003524
3525 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003526 * For certain types of exceptions, it makes no sense to continue
3527 * execution.
3528 *
3529 * Currently, the only type of exception that can be caught by a
3530 * TryCatch handler and for which it does not make sense to continue
3531 * is termination exception. Such exceptions are thrown when the
3532 * TerminateExecution methods are called to terminate a long-running
3533 * script.
3534 *
3535 * If CanContinue returns false, the correct action is to perform
3536 * any C++ cleanup needed and then return.
3537 */
3538 bool CanContinue() const;
3539
3540 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003541 * Throws the exception caught by this TryCatch in a way that avoids
3542 * it being caught again by this same TryCatch. As with ThrowException
3543 * it is illegal to execute any JavaScript operations after calling
3544 * ReThrow; the caller must return immediately to where the exception
3545 * is caught.
3546 */
3547 Handle<Value> ReThrow();
3548
3549 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003550 * Returns the exception caught by this try/catch block. If no exception has
3551 * been caught an empty handle is returned.
3552 *
3553 * The returned handle is valid until this TryCatch block has been destroyed.
3554 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003555 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003556
3557 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003558 * Returns the .stack property of the thrown object. If no .stack
3559 * property is present an empty handle is returned.
3560 */
3561 Local<Value> StackTrace() const;
3562
3563 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003564 * Returns the message associated with this exception. If there is
3565 * no message associated an empty handle is returned.
3566 *
3567 * The returned handle is valid until this TryCatch block has been
3568 * destroyed.
3569 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003570 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003571
3572 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003573 * Clears any exceptions that may have been caught by this try/catch block.
3574 * After this method has been called, HasCaught() will return false.
3575 *
3576 * It is not necessary to clear a try/catch block before using it again; if
3577 * another exception is thrown the previously caught exception will just be
3578 * overwritten. However, it is often a good idea since it makes it easier
3579 * to determine which operation threw a given exception.
3580 */
3581 void Reset();
3582
v8.team.kasperl727e9952008-09-02 14:56:44 +00003583 /**
3584 * Set verbosity of the external exception handler.
3585 *
3586 * By default, exceptions that are caught by an external exception
3587 * handler are not reported. Call SetVerbose with true on an
3588 * external exception handler to have exceptions caught by the
3589 * handler reported as if they were not caught.
3590 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591 void SetVerbose(bool value);
3592
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003593 /**
3594 * Set whether or not this TryCatch should capture a Message object
3595 * which holds source information about where the exception
3596 * occurred. True by default.
3597 */
3598 void SetCaptureMessage(bool value);
3599
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003600 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003601 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003602 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003604 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003605 bool is_verbose_ : 1;
3606 bool can_continue_ : 1;
3607 bool capture_message_ : 1;
3608 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003609
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003610 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003611};
3612
3613
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003614// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615
3616
3617/**
3618 * Ignore
3619 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003620class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003621 public:
3622 ExtensionConfiguration(int name_count, const char* names[])
3623 : name_count_(name_count), names_(names) { }
3624 private:
3625 friend class ImplementationUtilities;
3626 int name_count_;
3627 const char** names_;
3628};
3629
3630
3631/**
3632 * A sandboxed execution context with its own set of built-in objects
3633 * and functions.
3634 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003635class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003636 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003637 /**
3638 * Returns the global proxy object or global object itself for
3639 * detached contexts.
3640 *
3641 * Global proxy object is a thin wrapper whose prototype points to
3642 * actual context's global object with the properties like Object, etc.
3643 * This is done that way for security reasons (for more details see
3644 * https://wiki.mozilla.org/Gecko:SplitWindow).
3645 *
3646 * Please note that changes to global proxy object prototype most probably
3647 * would break VM---v8 expects only global object as a prototype of
3648 * global proxy object.
3649 *
3650 * If DetachGlobal() has been invoked, Global() would return actual global
3651 * object until global is reattached with ReattachGlobal().
3652 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003653 Local<Object> Global();
3654
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003655 /**
3656 * Detaches the global object from its context before
3657 * the global object can be reused to create a new context.
3658 */
3659 void DetachGlobal();
3660
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003661 /**
3662 * Reattaches a global object to a context. This can be used to
3663 * restore the connection between a global object and a context
3664 * after DetachGlobal has been called.
3665 *
3666 * \param global_object The global object to reattach to the
3667 * context. For this to work, the global object must be the global
3668 * object that was associated with this context before a call to
3669 * DetachGlobal.
3670 */
3671 void ReattachGlobal(Handle<Object> global_object);
3672
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003673 /** Creates a new context.
3674 *
3675 * Returns a persistent handle to the newly allocated context. This
3676 * persistent handle has to be disposed when the context is no
3677 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003678 *
3679 * \param extensions An optional extension configuration containing
3680 * the extensions to be installed in the newly created context.
3681 *
3682 * \param global_template An optional object template from which the
3683 * global object for the newly created context will be created.
3684 *
3685 * \param global_object An optional global object to be reused for
3686 * the newly created context. This global object must have been
3687 * created by a previous call to Context::New with the same global
3688 * template. The state of the global object will be completely reset
3689 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003690 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003691 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003692 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003693 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3694 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695
kasper.lund44510672008-07-25 07:37:58 +00003696 /** Returns the last entered context. */
3697 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003698
kasper.lund44510672008-07-25 07:37:58 +00003699 /** Returns the context that is on the top of the stack. */
3700 static Local<Context> GetCurrent();
3701
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003702 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003703 * Returns the context of the calling JavaScript code. That is the
3704 * context of the top-most JavaScript frame. If there are no
3705 * JavaScript frames an empty handle is returned.
3706 */
3707 static Local<Context> GetCalling();
3708
3709 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003710 * Sets the security token for the context. To access an object in
3711 * another context, the security tokens must match.
3712 */
3713 void SetSecurityToken(Handle<Value> token);
3714
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003715 /** Restores the security token to the default value. */
3716 void UseDefaultSecurityToken();
3717
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003718 /** Returns the security token of this context.*/
3719 Handle<Value> GetSecurityToken();
3720
v8.team.kasperl727e9952008-09-02 14:56:44 +00003721 /**
3722 * Enter this context. After entering a context, all code compiled
3723 * and run is compiled and run in this context. If another context
3724 * is already entered, this old context is saved so it can be
3725 * restored when the new context is exited.
3726 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003728
3729 /**
3730 * Exit this context. Exiting the current context restores the
3731 * context that was in place when entering the current context.
3732 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003733 void Exit();
3734
v8.team.kasperl727e9952008-09-02 14:56:44 +00003735 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003736 bool HasOutOfMemoryException();
3737
v8.team.kasperl727e9952008-09-02 14:56:44 +00003738 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003739 static bool InContext();
3740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003741 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003742 * Associate an additional data object with the context. This is mainly used
3743 * with the debugger to provide additional information on the context through
3744 * the debugger API.
3745 */
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003746 void SetData(Handle<Value> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003747 Local<Value> GetData();
3748
3749 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003750 * Control whether code generation from strings is allowed. Calling
3751 * this method with false will disable 'eval' and the 'Function'
3752 * constructor for code running in this context. If 'eval' or the
3753 * 'Function' constructor are used an exception will be thrown.
3754 *
3755 * If code generation from strings is not allowed the
3756 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3757 * set before blocking the call to 'eval' or the 'Function'
3758 * constructor. If that callback returns true, the call will be
3759 * allowed, otherwise an exception will be thrown. If no callback is
3760 * set an exception will be thrown.
3761 */
3762 void AllowCodeGenerationFromStrings(bool allow);
3763
3764 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003765 * Returns true if code generation from strings is allowed for the context.
3766 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3767 */
3768 bool IsCodeGenerationFromStringsAllowed();
3769
3770 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003771 * Sets the error description for the exception that is thrown when
3772 * code generation from strings is not allowed and 'eval' or the 'Function'
3773 * constructor are called.
3774 */
3775 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3776
3777 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778 * Stack-allocated class which sets the execution context for all
3779 * operations executed within a local scope.
3780 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003781 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003782 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003783 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784 context_->Enter();
3785 }
3786 inline ~Scope() { context_->Exit(); }
3787 private:
3788 Handle<Context> context_;
3789 };
3790
3791 private:
3792 friend class Value;
3793 friend class Script;
3794 friend class Object;
3795 friend class Function;
3796};
3797
3798
3799/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003800 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003801 * is allowed to use any given V8 isolate. See Isolate class
3802 * comments. The definition of 'using V8 isolate' includes
3803 * accessing handles or holding onto object pointers obtained
3804 * from V8 handles while in the particular V8 isolate. It is up
3805 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003806 * constraint is not violated. In addition to any other synchronization
3807 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3808 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003810 * v8::Locker is a scoped lock object. While it's
3811 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003812 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3813 * locked by at most one thread at any time. In other words, the scope of a
3814 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003815 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003816 * Sample usage:
3817* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003818 * ...
3819 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003820 * v8::Locker locker(isolate);
3821 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003822 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003823 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003824 * ...
3825 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003826 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003827 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003828 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003829 * by destroying the v8::Locker object as above or by constructing a
3830 * v8::Unlocker object:
3831 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003832 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003834 * isolate->Exit();
3835 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003836 * ...
3837 * // Code not using V8 goes here while V8 can run in another thread.
3838 * ...
3839 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003840 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003841 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003842 *
3843 * The Unlocker object is intended for use in a long-running callback
3844 * from V8, where you want to release the V8 lock for other threads to
3845 * use.
3846 *
3847 * The v8::Locker is a recursive lock. That is, you can lock more than
3848 * once in a given thread. This can be useful if you have code that can
3849 * be called either from code that holds the lock or from code that does
3850 * not. The Unlocker is not recursive so you can not have several
3851 * Unlockers on the stack at once, and you can not use an Unlocker in a
3852 * thread that is not inside a Locker's scope.
3853 *
3854 * An unlocker will unlock several lockers if it has to and reinstate
3855 * the correct depth of locking on its destruction. eg.:
3856 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003857 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003858 * // V8 not locked.
3859 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003860 * v8::Locker locker(isolate);
3861 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003862 * // V8 locked.
3863 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003864 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003865 * // V8 still locked (2 levels).
3866 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003867 * isolate->Exit();
3868 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869 * // V8 not locked.
3870 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003871 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003872 * // V8 locked again (2 levels).
3873 * }
3874 * // V8 still locked (1 level).
3875 * }
3876 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003877 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003878 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003879 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003880 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003881class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003883 /**
3884 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3885 */
3886 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003887 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003888 private:
3889 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003890};
3891
3892
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003893class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003894 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003895 /**
3896 * Initialize Locker for a given Isolate. NULL means default isolate.
3897 */
3898 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003899 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003900
3901 /**
3902 * Start preemption.
3903 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003904 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003905 * that will switch between multiple threads that are in contention
3906 * for the V8 lock.
3907 */
3908 static void StartPreemption(int every_n_ms);
3909
3910 /**
3911 * Stop preemption.
3912 */
3913 static void StopPreemption();
3914
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003915 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003916 * Returns whether or not the locker for a given isolate, or default isolate
3917 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003918 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003919 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003920
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003921 /**
3922 * Returns whether v8::Locker is being used by this V8 instance.
3923 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003924 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003925
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003926 private:
3927 bool has_lock_;
3928 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003929 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003930
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003931 static bool active_;
3932
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003933 // Disallow copying and assigning.
3934 Locker(const Locker&);
3935 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936};
3937
3938
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003939/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003940 * A struct for exporting HeapStats data from V8, using "push" model.
3941 */
3942struct HeapStatsUpdate;
3943
3944
3945/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003946 * An interface for exporting data from V8, using "push" model.
3947 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003948class V8EXPORT OutputStream { // NOLINT
3949 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003950 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003951 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003952 };
3953 enum WriteResult {
3954 kContinue = 0,
3955 kAbort = 1
3956 };
3957 virtual ~OutputStream() {}
3958 /** Notify about the end of stream. */
3959 virtual void EndOfStream() = 0;
3960 /** Get preferred output chunk size. Called only once. */
3961 virtual int GetChunkSize() { return 1024; }
3962 /** Get preferred output encoding. Called only once. */
3963 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3964 /**
3965 * Writes the next chunk of snapshot data into the stream. Writing
3966 * can be stopped by returning kAbort as function result. EndOfStream
3967 * will not be called in case writing was aborted.
3968 */
3969 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003970 /**
3971 * Writes the next chunk of heap stats data into the stream. Writing
3972 * can be stopped by returning kAbort as function result. EndOfStream
3973 * will not be called in case writing was aborted.
3974 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003975 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00003976 return kAbort;
3977 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003978};
3979
3980
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003981/**
3982 * An interface for reporting progress and controlling long-running
3983 * activities.
3984 */
3985class V8EXPORT ActivityControl { // NOLINT
3986 public:
3987 enum ControlOption {
3988 kContinue = 0,
3989 kAbort = 1
3990 };
3991 virtual ~ActivityControl() {}
3992 /**
3993 * Notify about current progress. The activity can be stopped by
3994 * returning kAbort as the callback result.
3995 */
3996 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3997};
3998
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003999
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004000// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004001
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004002
4003namespace internal {
4004
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004005const int kApiPointerSize = sizeof(void*); // NOLINT
4006const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004007
4008// Tag information for HeapObject.
4009const int kHeapObjectTag = 1;
4010const int kHeapObjectTagSize = 2;
4011const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4012
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004013// Tag information for Smi.
4014const int kSmiTag = 0;
4015const int kSmiTagSize = 1;
4016const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4017
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004018template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004019
4020// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004021template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004022 static const int kSmiShiftSize = 0;
4023 static const int kSmiValueSize = 31;
4024 static inline int SmiToInt(internal::Object* value) {
4025 int shift_bits = kSmiTagSize + kSmiShiftSize;
4026 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4027 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4028 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004029
4030 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
4031 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004032 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004033 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004034};
4035
4036// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004037template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004038 static const int kSmiShiftSize = 31;
4039 static const int kSmiValueSize = 32;
4040 static inline int SmiToInt(internal::Object* value) {
4041 int shift_bits = kSmiTagSize + kSmiShiftSize;
4042 // Shift down and throw away top 32 bits.
4043 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4044 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004045
4046 // To maximize the range of pointers that can be encoded
4047 // in the available 32 bits, we require them to be 8 bytes aligned.
4048 // This gives 2 ^ (32 + 3) = 32G address space covered.
4049 // It might be not enough to cover stack allocated objects on some platforms.
4050 static const int kPointerAlignment = 3;
4051
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004052 static const uintptr_t kEncodablePointerMask =
4053 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004054
4055 static const int kPointerToSmiShift =
4056 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004057};
4058
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004059typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4060const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4061const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004062const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004063 PlatformSmiTagging::kEncodablePointerMask;
4064const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004065
4066/**
4067 * This class exports constants and functionality from within v8 that
4068 * is necessary to implement inline functions in the v8 api. Don't
4069 * depend on functions and constants defined here.
4070 */
4071class Internals {
4072 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004073 // These values match non-compiler-dependent values defined within
4074 // the implementation of v8.
4075 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004076 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004077 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004078
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004079 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004080 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004081 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004082 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004083 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004084 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004085 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004086
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004087 static const int kIsolateStateOffset = 0;
4088 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4089 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4090 static const int kUndefinedValueRootIndex = 5;
4091 static const int kNullValueRootIndex = 7;
4092 static const int kTrueValueRootIndex = 8;
4093 static const int kFalseValueRootIndex = 9;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00004094 static const int kEmptySymbolRootIndex = 117;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004095
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004096 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004097 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004098 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004099 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004100
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004101 static const int kUndefinedOddballKind = 5;
4102 static const int kNullOddballKind = 3;
4103
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004104 static inline bool HasHeapObjectTag(internal::Object* value) {
4105 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4106 kHeapObjectTag);
4107 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004108
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004109 static inline bool HasSmiTag(internal::Object* value) {
4110 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
4111 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004112
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004113 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004114 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004115 }
4116
4117 static inline int GetInstanceType(internal::Object* obj) {
4118 typedef internal::Object O;
4119 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4120 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4121 }
4122
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004123 static inline int GetOddballKind(internal::Object* obj) {
4124 typedef internal::Object O;
4125 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4126 }
4127
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004128 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004129 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004130 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
4131 }
4132
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004133 static inline void* GetExternalPointer(internal::Object* obj) {
4134 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004135 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004136 } else if (GetInstanceType(obj) == kForeignType) {
4137 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004138 } else {
4139 return NULL;
4140 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004141 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004142
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004143 static inline bool IsExternalTwoByteString(int instance_type) {
4144 int representation = (instance_type & kFullStringRepresentationMask);
4145 return representation == kExternalTwoByteRepresentationTag;
4146 }
4147
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004148 static inline bool IsInitialized(v8::Isolate* isolate) {
4149 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4150 return *reinterpret_cast<int*>(addr) == 1;
4151 }
4152
4153 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
4154 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4155 kIsolateEmbedderDataOffset;
4156 *reinterpret_cast<void**>(addr) = data;
4157 }
4158
4159 static inline void* GetEmbedderData(v8::Isolate* isolate) {
4160 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4161 kIsolateEmbedderDataOffset;
4162 return *reinterpret_cast<void**>(addr);
4163 }
4164
4165 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
4166 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4167 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4168 }
4169
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004170 template <typename T>
4171 static inline T ReadField(Object* ptr, int offset) {
4172 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4173 return *reinterpret_cast<T*>(addr);
4174 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004175
4176 static inline bool CanCastToHeapObject(void* o) { return false; }
4177 static inline bool CanCastToHeapObject(Context* o) { return true; }
4178 static inline bool CanCastToHeapObject(String* o) { return true; }
4179 static inline bool CanCastToHeapObject(Object* o) { return true; }
4180 static inline bool CanCastToHeapObject(Message* o) { return true; }
4181 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
4182 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004183};
4184
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004185} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004186
4187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004188template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004189Local<T>::Local() : Handle<T>() { }
4190
4191
4192template <class T>
4193Local<T> Local<T>::New(Handle<T> that) {
4194 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004195 T* that_ptr = *that;
4196 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4197 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4198 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4199 reinterpret_cast<internal::HeapObject*>(*p))));
4200 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004201 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4202}
4203
4204
4205template <class T>
4206Persistent<T> Persistent<T>::New(Handle<T> that) {
4207 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004208 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004209 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4210}
4211
4212
4213template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004214bool Persistent<T>::IsIndependent() const {
4215 if (this->IsEmpty()) return false;
4216 return V8::IsGlobalIndependent(reinterpret_cast<internal::Object**>(**this));
4217}
4218
4219
4220template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004221bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004223 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004224}
4225
4226
4227template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004228bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004229 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004230 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004231}
4232
4233
4234template <class T>
4235void Persistent<T>::Dispose() {
4236 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004237 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004238}
4239
4240
4241template <class T>
4242Persistent<T>::Persistent() : Handle<T>() { }
4243
4244template <class T>
4245void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004246 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4247 parameters,
4248 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004249}
4250
4251template <class T>
4252void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004253 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004254}
4255
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004256template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004257void Persistent<T>::MarkIndependent() {
4258 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4259}
4260
4261template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004262void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4263 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4264}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004265
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004266template <class T>
4267uint16_t Persistent<T>::WrapperClassId() const {
4268 return V8::GetWrapperClassId(reinterpret_cast<internal::Object**>(**this));
4269}
4270
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004271Arguments::Arguments(internal::Object** implicit_args,
4272 internal::Object** values, int length,
4273 bool is_construct_call)
4274 : implicit_args_(implicit_args),
4275 values_(values),
4276 length_(length),
4277 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004278
4279
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004280Local<Value> Arguments::operator[](int i) const {
4281 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4282 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4283}
4284
4285
4286Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004287 return Local<Function>(reinterpret_cast<Function*>(
4288 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004289}
4290
4291
4292Local<Object> Arguments::This() const {
4293 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4294}
4295
4296
4297Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004298 return Local<Object>(reinterpret_cast<Object*>(
4299 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004300}
4301
4302
4303Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004304 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004305}
4306
4307
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004308Isolate* Arguments::GetIsolate() const {
4309 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4310}
4311
4312
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004313bool Arguments::IsConstructCall() const {
4314 return is_construct_call_;
4315}
4316
4317
4318int Arguments::Length() const {
4319 return length_;
4320}
4321
4322
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004323template <class T>
4324Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004325 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4326 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004327 return Local<T>(reinterpret_cast<T*>(after));
4328}
4329
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004330Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004331 return resource_name_;
4332}
4333
4334
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004335Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004336 return resource_line_offset_;
4337}
4338
4339
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004340Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004341 return resource_column_offset_;
4342}
4343
4344
4345Handle<Boolean> Boolean::New(bool value) {
4346 return value ? True() : False();
4347}
4348
4349
4350void Template::Set(const char* name, v8::Handle<Data> value) {
4351 Set(v8::String::New(name), value);
4352}
4353
4354
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004355Local<Value> Object::GetInternalField(int index) {
4356#ifndef V8_ENABLE_CHECKS
4357 Local<Value> quick_result = UncheckedGetInternalField(index);
4358 if (!quick_result.IsEmpty()) return quick_result;
4359#endif
4360 return CheckedGetInternalField(index);
4361}
4362
4363
4364Local<Value> Object::UncheckedGetInternalField(int index) {
4365 typedef internal::Object O;
4366 typedef internal::Internals I;
4367 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004368 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004369 // If the object is a plain JSObject, which is the common case,
4370 // we know where to find the internal fields and can return the
4371 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004372 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004373 O* value = I::ReadField<O*>(obj, offset);
4374 O** result = HandleScope::CreateHandle(value);
4375 return Local<Value>(reinterpret_cast<Value*>(result));
4376 } else {
4377 return Local<Value>();
4378 }
4379}
4380
4381
4382void* External::Unwrap(Handle<v8::Value> obj) {
4383#ifdef V8_ENABLE_CHECKS
4384 return FullUnwrap(obj);
4385#else
4386 return QuickUnwrap(obj);
4387#endif
4388}
4389
4390
4391void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4392 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004393 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004394 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004395}
4396
4397
4398void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004399 typedef internal::Object O;
4400 typedef internal::Internals I;
4401
4402 O* obj = *reinterpret_cast<O**>(this);
4403
4404 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4405 // If the object is a plain JSObject, which is the common case,
4406 // we know where to find the internal fields and can return the
4407 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004408 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004409 O* value = I::ReadField<O*>(obj, offset);
4410 return I::GetExternalPointer(value);
4411 }
4412
4413 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004414}
4415
4416
4417String* String::Cast(v8::Value* value) {
4418#ifdef V8_ENABLE_CHECKS
4419 CheckCast(value);
4420#endif
4421 return static_cast<String*>(value);
4422}
4423
4424
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004425Local<String> String::Empty(Isolate* isolate) {
4426 typedef internal::Object* S;
4427 typedef internal::Internals I;
4428 if (!I::IsInitialized(isolate)) return Empty();
4429 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4430 return Local<String>(reinterpret_cast<String*>(slot));
4431}
4432
4433
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004434String::ExternalStringResource* String::GetExternalStringResource() const {
4435 typedef internal::Object O;
4436 typedef internal::Internals I;
4437 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004438 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004439 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004440 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4441 result = reinterpret_cast<String::ExternalStringResource*>(value);
4442 } else {
4443 result = NULL;
4444 }
4445#ifdef V8_ENABLE_CHECKS
4446 VerifyExternalStringResource(result);
4447#endif
4448 return result;
4449}
4450
4451
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004452String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4453 String::Encoding* encoding_out) const {
4454 typedef internal::Object O;
4455 typedef internal::Internals I;
4456 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4457 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4458 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4459 ExternalStringResourceBase* resource = NULL;
4460 if (type == I::kExternalAsciiRepresentationTag ||
4461 type == I::kExternalTwoByteRepresentationTag) {
4462 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4463 resource = static_cast<ExternalStringResourceBase*>(value);
4464 }
4465#ifdef V8_ENABLE_CHECKS
4466 VerifyExternalStringResourceBase(resource, *encoding_out);
4467#endif
4468 return resource;
4469}
4470
4471
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004472bool Value::IsUndefined() const {
4473#ifdef V8_ENABLE_CHECKS
4474 return FullIsUndefined();
4475#else
4476 return QuickIsUndefined();
4477#endif
4478}
4479
4480bool Value::QuickIsUndefined() const {
4481 typedef internal::Object O;
4482 typedef internal::Internals I;
4483 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4484 if (!I::HasHeapObjectTag(obj)) return false;
4485 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4486 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4487}
4488
4489
4490bool Value::IsNull() const {
4491#ifdef V8_ENABLE_CHECKS
4492 return FullIsNull();
4493#else
4494 return QuickIsNull();
4495#endif
4496}
4497
4498bool Value::QuickIsNull() const {
4499 typedef internal::Object O;
4500 typedef internal::Internals I;
4501 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4502 if (!I::HasHeapObjectTag(obj)) return false;
4503 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4504 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4505}
4506
4507
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004508bool Value::IsString() const {
4509#ifdef V8_ENABLE_CHECKS
4510 return FullIsString();
4511#else
4512 return QuickIsString();
4513#endif
4514}
4515
4516bool Value::QuickIsString() const {
4517 typedef internal::Object O;
4518 typedef internal::Internals I;
4519 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4520 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004521 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004522}
4523
4524
4525Number* Number::Cast(v8::Value* value) {
4526#ifdef V8_ENABLE_CHECKS
4527 CheckCast(value);
4528#endif
4529 return static_cast<Number*>(value);
4530}
4531
4532
4533Integer* Integer::Cast(v8::Value* value) {
4534#ifdef V8_ENABLE_CHECKS
4535 CheckCast(value);
4536#endif
4537 return static_cast<Integer*>(value);
4538}
4539
4540
4541Date* Date::Cast(v8::Value* value) {
4542#ifdef V8_ENABLE_CHECKS
4543 CheckCast(value);
4544#endif
4545 return static_cast<Date*>(value);
4546}
4547
4548
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004549StringObject* StringObject::Cast(v8::Value* value) {
4550#ifdef V8_ENABLE_CHECKS
4551 CheckCast(value);
4552#endif
4553 return static_cast<StringObject*>(value);
4554}
4555
4556
4557NumberObject* NumberObject::Cast(v8::Value* value) {
4558#ifdef V8_ENABLE_CHECKS
4559 CheckCast(value);
4560#endif
4561 return static_cast<NumberObject*>(value);
4562}
4563
4564
4565BooleanObject* BooleanObject::Cast(v8::Value* value) {
4566#ifdef V8_ENABLE_CHECKS
4567 CheckCast(value);
4568#endif
4569 return static_cast<BooleanObject*>(value);
4570}
4571
4572
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004573RegExp* RegExp::Cast(v8::Value* value) {
4574#ifdef V8_ENABLE_CHECKS
4575 CheckCast(value);
4576#endif
4577 return static_cast<RegExp*>(value);
4578}
4579
4580
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004581Object* Object::Cast(v8::Value* value) {
4582#ifdef V8_ENABLE_CHECKS
4583 CheckCast(value);
4584#endif
4585 return static_cast<Object*>(value);
4586}
4587
4588
4589Array* Array::Cast(v8::Value* value) {
4590#ifdef V8_ENABLE_CHECKS
4591 CheckCast(value);
4592#endif
4593 return static_cast<Array*>(value);
4594}
4595
4596
4597Function* Function::Cast(v8::Value* value) {
4598#ifdef V8_ENABLE_CHECKS
4599 CheckCast(value);
4600#endif
4601 return static_cast<Function*>(value);
4602}
4603
4604
4605External* External::Cast(v8::Value* value) {
4606#ifdef V8_ENABLE_CHECKS
4607 CheckCast(value);
4608#endif
4609 return static_cast<External*>(value);
4610}
4611
4612
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004613Isolate* AccessorInfo::GetIsolate() const {
4614 return *reinterpret_cast<Isolate**>(&args_[-3]);
4615}
4616
4617
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004618Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004619 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004620}
4621
4622
4623Local<Object> AccessorInfo::This() const {
4624 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4625}
4626
4627
4628Local<Object> AccessorInfo::Holder() const {
4629 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4630}
4631
4632
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004633Handle<Primitive> Undefined(Isolate* isolate) {
4634 typedef internal::Object* S;
4635 typedef internal::Internals I;
4636 if (!I::IsInitialized(isolate)) return Undefined();
4637 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4638 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4639}
4640
4641
4642Handle<Primitive> Null(Isolate* isolate) {
4643 typedef internal::Object* S;
4644 typedef internal::Internals I;
4645 if (!I::IsInitialized(isolate)) return Null();
4646 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4647 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4648}
4649
4650
4651Handle<Boolean> True(Isolate* isolate) {
4652 typedef internal::Object* S;
4653 typedef internal::Internals I;
4654 if (!I::IsInitialized(isolate)) return True();
4655 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4656 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4657}
4658
4659
4660Handle<Boolean> False(Isolate* isolate) {
4661 typedef internal::Object* S;
4662 typedef internal::Internals I;
4663 if (!I::IsInitialized(isolate)) return False();
4664 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4665 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4666}
4667
4668
4669void Isolate::SetData(void* data) {
4670 typedef internal::Internals I;
4671 I::SetEmbedderData(this, data);
4672}
4673
4674
4675void* Isolate::GetData() {
4676 typedef internal::Internals I;
4677 return I::GetEmbedderData(this);
4678}
4679
4680
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004681/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004682 * \example shell.cc
4683 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004684 * command-line and executes them.
4685 */
4686
4687
4688/**
4689 * \example process.cc
4690 */
4691
4692
4693} // namespace v8
4694
4695
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004696#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004697#undef TYPE_CHECK
4698
4699
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004700#endif // V8_H_