blob: cf9275d918ccadc9dda2735069f1182a206cd874 [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();
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +0000384 inline void Dispose(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385
386 /**
387 * Make the reference to this object weak. When only weak handles
388 * refer to the object, the garbage collector will perform a
389 * callback to the given V8::WeakReferenceCallback function, passing
390 * it the object reference and the given parameters.
391 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000392 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000394 /** Clears the weak reference to this object. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000395 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396
397 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000398 * Marks the reference to this object independent. Garbage collector
399 * is free to ignore any object groups containing this object.
400 * Weak callback for an independent handle should not
401 * assume that it will be preceded by a global GC prologue callback
402 * or followed by a global GC epilogue callback.
403 */
404 inline void MarkIndependent();
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000405 inline void MarkIndependent(Isolate* isolate);
406
407 /**
408 * Marks the reference to this object partially dependent. Partially
409 * dependent handles only depend on other partially dependent handles and
410 * these dependencies are provided through object groups. It provides a way
411 * to build smaller object groups for young objects that represent only a
412 * subset of all external dependencies. This mark is automatically cleared
413 * after each garbage collection.
414 */
415 inline void MarkPartiallyDependent();
416 inline void MarkPartiallyDependent(Isolate* isolate);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000417
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000418 /** Returns true if this handle was previously marked as independent. */
419 inline bool IsIndependent() const;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +0000420 inline bool IsIndependent(Isolate* isolate) const;
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000421
422 /** Checks if the handle holds the only reference to an object. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000423 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000425 /** Returns true if the handle's reference is weak. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000426 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000428 /**
429 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
430 * interface description in v8-profiler.h for details.
431 */
432 inline void SetWrapperClassId(uint16_t class_id);
433
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000434 /**
435 * Returns the class ID previously assigned to this handle or 0 if no class
436 * ID was previously assigned.
437 */
438 inline uint16_t WrapperClassId() const;
439
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440 private:
441 friend class ImplementationUtilities;
442 friend class ObjectTemplate;
443};
444
445
v8.team.kasperl727e9952008-09-02 14:56:44 +0000446 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447 * A stack-allocated class that governs a number of local handles.
448 * After a handle scope has been created, all local handles will be
449 * allocated within that handle scope until either the handle scope is
450 * deleted or another handle scope is created. If there is already a
451 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000452 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 * new handles will again be allocated in the original handle scope.
454 *
455 * After the handle scope of a local handle has been deleted the
456 * garbage collector will no longer track the object stored in the
457 * handle and may deallocate it. The behavior of accessing a handle
458 * for which the handle scope has been deleted is undefined.
459 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000460class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000462 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000464 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465
466 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 * Closes the handle scope and returns the value as a handle in the
468 * previous scope, which is the new current scope after the call.
469 */
470 template <class T> Local<T> Close(Handle<T> value);
471
472 /**
473 * Counts the number of allocated handles.
474 */
475 static int NumberOfHandles();
476
477 /**
478 * Creates a new handle with the given value.
479 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000480 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000481 // Faster version, uses HeapObject to obtain the current Isolate.
482 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483
484 private:
485 // Make it impossible to create heap-allocated or illegal handle
486 // scopes by disallowing certain operations.
487 HandleScope(const HandleScope&);
488 void operator=(const HandleScope&);
489 void* operator new(size_t size);
490 void operator delete(void*, size_t);
491
ager@chromium.org3811b432009-10-28 14:53:37 +0000492 // This Data class is accessible internally as HandleScopeData through a
493 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000494 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000496 internal::Object** next;
497 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000498 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000501 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 }
503 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000504
lrn@chromium.org303ada72010-10-27 09:33:13 +0000505 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000507 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000508 internal::Object** prev_next_;
509 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000511 // Allow for the active closing of HandleScopes which allows to pass a handle
512 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000514 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 friend class ImplementationUtilities;
517};
518
519
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000520// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521
522
523/**
524 * The superclass of values and API object templates.
525 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000526class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 private:
528 Data();
529};
530
531
532/**
533 * Pre-compilation data that can be associated with a script. This
534 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000535 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000536 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000538class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 public:
540 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000541
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000542 /**
543 * Pre-compiles the specified script (context-independent).
544 *
545 * \param input Pointer to UTF-8 script source code.
546 * \param length Length of UTF-8 script source code.
547 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000550 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000551 * Pre-compiles the specified script (context-independent).
552 *
553 * NOTE: Pre-compilation using this method cannot happen on another thread
554 * without using Lockers.
555 *
556 * \param source Script source code.
557 */
558 static ScriptData* PreCompile(Handle<String> source);
559
560 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000561 * Load previous pre-compilation data.
562 *
563 * \param data Pointer to data returned by a call to Data() of a previous
564 * ScriptData. Ownership is not transferred.
565 * \param length Length of data.
566 */
567 static ScriptData* New(const char* data, int length);
568
569 /**
570 * Returns the length of Data().
571 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000573
574 /**
575 * Returns a serialized representation of this ScriptData that can later be
576 * passed to New(). NOTE: Serialized data is platform-dependent.
577 */
578 virtual const char* Data() = 0;
579
580 /**
581 * Returns true if the source code could not be parsed.
582 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000583 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584};
585
586
587/**
588 * The origin, within a file, of a script.
589 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000590class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000592 inline ScriptOrigin(
593 Handle<Value> resource_name,
594 Handle<Integer> resource_line_offset = Handle<Integer>(),
595 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 : resource_name_(resource_name),
597 resource_line_offset_(resource_line_offset),
598 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000599 inline Handle<Value> ResourceName() const;
600 inline Handle<Integer> ResourceLineOffset() const;
601 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000603 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 Handle<Integer> resource_line_offset_;
605 Handle<Integer> resource_column_offset_;
606};
607
608
609/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000610 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000612class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000615 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000616 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000617 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000618 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000619 * when New() returns
620 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
621 * using pre_data speeds compilation if it's done multiple times.
622 * Owned by caller, no references are kept when New() returns.
623 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000624 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000625 * available to compile event handlers.
626 * \return Compiled script object (context independent; when run it
627 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000629 static Local<Script> New(Handle<String> source,
630 ScriptOrigin* origin = NULL,
631 ScriptData* pre_data = NULL,
632 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633
mads.s.agercbaa0602008-08-14 13:41:48 +0000634 /**
635 * Compiles the specified script using the specified file name
636 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000637 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000638 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000639 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000640 * as the script's origin.
641 * \return Compiled script object (context independent; when run it
642 * will use the currently entered context).
643 */
644 static Local<Script> New(Handle<String> source,
645 Handle<Value> file_name);
646
647 /**
648 * Compiles the specified script (bound to current context).
649 *
650 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000651 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000652 * when Compile() returns
653 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
654 * using pre_data speeds compilation if it's done multiple times.
655 * Owned by caller, no references are kept when Compile() returns.
656 * \param script_data Arbitrary data associated with script. Using
657 * this has same effect as calling SetData(), but makes data available
658 * earlier (i.e. to compile event handlers).
659 * \return Compiled script object, bound to the context that was active
660 * when this function was called. When run it will always use this
661 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000662 */
663 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000664 ScriptOrigin* origin = NULL,
665 ScriptData* pre_data = NULL,
666 Handle<String> script_data = Handle<String>());
667
668 /**
669 * Compiles the specified script using the specified file name
670 * object (typically a string) as the script's origin.
671 *
672 * \param source Script source code.
673 * \param file_name File name to use as script's origin
674 * \param script_data Arbitrary data associated with script. Using
675 * this has same effect as calling SetData(), but makes data available
676 * earlier (i.e. to compile event handlers).
677 * \return Compiled script object, bound to the context that was active
678 * when this function was called. When run it will always use this
679 * context.
680 */
681 static Local<Script> Compile(Handle<String> source,
682 Handle<Value> file_name,
683 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000684
v8.team.kasperl727e9952008-09-02 14:56:44 +0000685 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000686 * Runs the script returning the resulting value. If the script is
687 * context independent (created using ::New) it will be run in the
688 * currently entered context. If it is context specific (created
689 * using ::Compile) it will be run in the context in which it was
690 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000691 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000693
694 /**
695 * Returns the script id value.
696 */
697 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000698
699 /**
700 * Associate an additional data object with the script. This is mainly used
701 * with the debugger as this data object is only available through the
702 * debugger API.
703 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000704 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705};
706
707
708/**
709 * An error message.
710 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000711class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000713 Local<String> Get() const;
714 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000716 /**
717 * Returns the resource name for the script from where the function causing
718 * the error originates.
719 */
ager@chromium.org32912102009-01-16 10:38:43 +0000720 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000722 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000723 * Returns the resource data for the script from where the function causing
724 * the error originates.
725 */
726 Handle<Value> GetScriptData() const;
727
728 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000729 * Exception stack trace. By default stack traces are not captured for
730 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
731 * to change this option.
732 */
733 Handle<StackTrace> GetStackTrace() const;
734
735 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000736 * Returns the number, 1-based, of the line where the error occurred.
737 */
ager@chromium.org32912102009-01-16 10:38:43 +0000738 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000740 /**
741 * Returns the index within the script of the first character where
742 * the error occurred.
743 */
ager@chromium.org32912102009-01-16 10:38:43 +0000744 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000745
746 /**
747 * Returns the index within the script of the last character where
748 * the error occurred.
749 */
ager@chromium.org32912102009-01-16 10:38:43 +0000750 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000751
752 /**
753 * Returns the index within the line of the first character where
754 * the error occurred.
755 */
ager@chromium.org32912102009-01-16 10:38:43 +0000756 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000757
758 /**
759 * Returns the index within the line of the last character where
760 * the error occurred.
761 */
ager@chromium.org32912102009-01-16 10:38:43 +0000762 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000763
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 // TODO(1245381): Print to a string instead of on a FILE.
765 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000766
767 static const int kNoLineNumberInfo = 0;
768 static const int kNoColumnInfo = 0;
769};
770
771
772/**
773 * Representation of a JavaScript stack trace. The information collected is a
774 * snapshot of the execution stack and the information remains valid after
775 * execution continues.
776 */
777class V8EXPORT StackTrace {
778 public:
779 /**
780 * Flags that determine what information is placed captured for each
781 * StackFrame when grabbing the current stack trace.
782 */
783 enum StackTraceOptions {
784 kLineNumber = 1,
785 kColumnOffset = 1 << 1 | kLineNumber,
786 kScriptName = 1 << 2,
787 kFunctionName = 1 << 3,
788 kIsEval = 1 << 4,
789 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000790 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000791 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000792 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000793 };
794
795 /**
796 * Returns a StackFrame at a particular index.
797 */
798 Local<StackFrame> GetFrame(uint32_t index) const;
799
800 /**
801 * Returns the number of StackFrames.
802 */
803 int GetFrameCount() const;
804
805 /**
806 * Returns StackTrace as a v8::Array that contains StackFrame objects.
807 */
808 Local<Array> AsArray();
809
810 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000811 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000812 *
813 * \param frame_limit The maximum number of stack frames we want to capture.
814 * \param options Enumerates the set of things we will capture for each
815 * StackFrame.
816 */
817 static Local<StackTrace> CurrentStackTrace(
818 int frame_limit,
819 StackTraceOptions options = kOverview);
820};
821
822
823/**
824 * A single JavaScript stack frame.
825 */
826class V8EXPORT StackFrame {
827 public:
828 /**
829 * Returns the number, 1-based, of the line for the associate function call.
830 * This method will return Message::kNoLineNumberInfo if it is unable to
831 * retrieve the line number, or if kLineNumber was not passed as an option
832 * when capturing the StackTrace.
833 */
834 int GetLineNumber() const;
835
836 /**
837 * Returns the 1-based column offset on the line for the associated function
838 * call.
839 * This method will return Message::kNoColumnInfo if it is unable to retrieve
840 * the column number, or if kColumnOffset was not passed as an option when
841 * capturing the StackTrace.
842 */
843 int GetColumn() const;
844
845 /**
846 * Returns the name of the resource that contains the script for the
847 * function for this StackFrame.
848 */
849 Local<String> GetScriptName() const;
850
851 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000852 * Returns the name of the resource that contains the script for the
853 * function for this StackFrame or sourceURL value if the script name
854 * is undefined and its source ends with //@ sourceURL=... string.
855 */
856 Local<String> GetScriptNameOrSourceURL() const;
857
858 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000859 * Returns the name of the function associated with this stack frame.
860 */
861 Local<String> GetFunctionName() const;
862
863 /**
864 * Returns whether or not the associated function is compiled via a call to
865 * eval().
866 */
867 bool IsEval() const;
868
869 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000870 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000871 * constructor via "new".
872 */
873 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874};
875
876
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000877// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878
879
880/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000881 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000883class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 /**
886 * Returns true if this value is the undefined value. See ECMA-262
887 * 4.3.10.
888 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000889 inline bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890
891 /**
892 * Returns true if this value is the null value. See ECMA-262
893 * 4.3.11.
894 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000895 inline bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
897 /**
898 * Returns true if this value is true.
899 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000900 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901
902 /**
903 * Returns true if this value is false.
904 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000905 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906
907 /**
908 * Returns true if this value is an instance of the String type.
909 * See ECMA-262 8.4.
910 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000911 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912
913 /**
914 * Returns true if this value is a function.
915 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000916 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
918 /**
919 * Returns true if this value is an array.
920 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000921 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922
v8.team.kasperl727e9952008-09-02 14:56:44 +0000923 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924 * Returns true if this value is an object.
925 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000926 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927
v8.team.kasperl727e9952008-09-02 14:56:44 +0000928 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 * Returns true if this value is boolean.
930 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000931 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000932
v8.team.kasperl727e9952008-09-02 14:56:44 +0000933 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934 * Returns true if this value is a number.
935 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000936 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937
v8.team.kasperl727e9952008-09-02 14:56:44 +0000938 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939 * Returns true if this value is external.
940 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000941 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942
v8.team.kasperl727e9952008-09-02 14:56:44 +0000943 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 * Returns true if this value is a 32-bit signed integer.
945 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000946 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000948 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000949 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000950 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000951 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000952
953 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000954 * Returns true if this value is a Date.
955 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000956 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000957
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000958 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000959 * Returns true if this value is a Boolean object.
960 */
961 V8EXPORT bool IsBooleanObject() const;
962
963 /**
964 * Returns true if this value is a Number object.
965 */
966 V8EXPORT bool IsNumberObject() const;
967
968 /**
969 * Returns true if this value is a String object.
970 */
971 V8EXPORT bool IsStringObject() const;
972
973 /**
974 * Returns true if this value is a NativeError.
975 */
976 V8EXPORT bool IsNativeError() const;
977
978 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000979 * Returns true if this value is a RegExp.
980 */
981 V8EXPORT bool IsRegExp() const;
982
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000983 V8EXPORT Local<Boolean> ToBoolean() const;
984 V8EXPORT Local<Number> ToNumber() const;
985 V8EXPORT Local<String> ToString() const;
986 V8EXPORT Local<String> ToDetailString() const;
987 V8EXPORT Local<Object> ToObject() const;
988 V8EXPORT Local<Integer> ToInteger() const;
989 V8EXPORT Local<Uint32> ToUint32() const;
990 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991
992 /**
993 * Attempts to convert a string to an array index.
994 * Returns an empty handle if the conversion fails.
995 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000996 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000998 V8EXPORT bool BooleanValue() const;
999 V8EXPORT double NumberValue() const;
1000 V8EXPORT int64_t IntegerValue() const;
1001 V8EXPORT uint32_t Uint32Value() const;
1002 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003
1004 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001005 V8EXPORT bool Equals(Handle<Value> that) const;
1006 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001007
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001008 private:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00001009 inline bool QuickIsUndefined() const;
1010 inline bool QuickIsNull() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001011 inline bool QuickIsString() const;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00001012 V8EXPORT bool FullIsUndefined() const;
1013 V8EXPORT bool FullIsNull() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001014 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015};
1016
1017
1018/**
1019 * The superclass of primitive values. See ECMA-262 4.3.2.
1020 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001021class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022
1023
1024/**
1025 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1026 * or false value.
1027 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001028class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001030 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031 static inline Handle<Boolean> New(bool value);
1032};
1033
1034
1035/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001036 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001038class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001040 enum Encoding {
1041 UNKNOWN_ENCODING = 0x1,
1042 TWO_BYTE_ENCODING = 0x0,
1043 ASCII_ENCODING = 0x4
1044 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001045 /**
1046 * Returns the number of characters in this string.
1047 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001048 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049
v8.team.kasperl727e9952008-09-02 14:56:44 +00001050 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001051 * Returns the number of bytes in the UTF-8 encoded
1052 * representation of this string.
1053 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001054 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001055
1056 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001057 * A fast conservative check for non-ASCII characters. May
1058 * return true even for ASCII strings, but if it returns
1059 * false you can be sure that all characters are in the range
1060 * 0-127.
1061 */
1062 V8EXPORT bool MayContainNonAscii() const;
1063
1064 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001065 * Write the contents of the string to an external buffer.
1066 * If no arguments are given, expects the buffer to be large
1067 * enough to hold the entire string and NULL terminator. Copies
1068 * the contents of the string and the NULL terminator into the
1069 * buffer.
1070 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001071 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1072 * before the end of the buffer.
1073 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001074 * Copies up to length characters into the output buffer.
1075 * Only null-terminates if there is enough space in the buffer.
1076 *
1077 * \param buffer The buffer into which the string will be copied.
1078 * \param start The starting position within the string at which
1079 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001080 * \param length The number of characters to copy from the string. For
1081 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001082 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001083 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001084 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001085 * \return The number of characters copied to the buffer excluding the null
1086 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001087 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001088 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001089 enum WriteOptions {
1090 NO_OPTIONS = 0,
1091 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001092 NO_NULL_TERMINATION = 2,
1093 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001094 };
1095
lrn@chromium.org34e60782011-09-15 07:25:40 +00001096 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001097 V8EXPORT int Write(uint16_t* buffer,
1098 int start = 0,
1099 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001100 int options = NO_OPTIONS) const;
1101 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001102 V8EXPORT int WriteAscii(char* buffer,
1103 int start = 0,
1104 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001105 int options = NO_OPTIONS) const;
1106 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001107 V8EXPORT int WriteUtf8(char* buffer,
1108 int length = -1,
1109 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001110 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111
v8.team.kasperl727e9952008-09-02 14:56:44 +00001112 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001113 * A zero length string.
1114 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001115 V8EXPORT static v8::Local<v8::String> Empty();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001116 inline static v8::Local<v8::String> Empty(Isolate* isolate);
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001117
1118 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001119 * Returns true if the string is external
1120 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001121 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122
v8.team.kasperl727e9952008-09-02 14:56:44 +00001123 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001124 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001125 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001126 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001127
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001128 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001129 public:
1130 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001131
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001132 protected:
1133 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001134
1135 /**
1136 * Internally V8 will call this Dispose method when the external string
1137 * resource is no longer needed. The default implementation will use the
1138 * delete operator. This method can be overridden in subclasses to
1139 * control how allocated external string resources are disposed.
1140 */
1141 virtual void Dispose() { delete this; }
1142
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001143 private:
1144 // Disallow copying and assigning.
1145 ExternalStringResourceBase(const ExternalStringResourceBase&);
1146 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001147
1148 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001149 };
1150
v8.team.kasperl727e9952008-09-02 14:56:44 +00001151 /**
1152 * An ExternalStringResource is a wrapper around a two-byte string
1153 * buffer that resides outside V8's heap. Implement an
1154 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001155 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001156 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001157 class V8EXPORT ExternalStringResource
1158 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 public:
1160 /**
1161 * Override the destructor to manage the life cycle of the underlying
1162 * buffer.
1163 */
1164 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001165
1166 /**
1167 * The string data from the underlying buffer.
1168 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001170
1171 /**
1172 * The length of the string. That is, the number of two-byte characters.
1173 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001175
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176 protected:
1177 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178 };
1179
1180 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001181 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001182 * string buffer that resides outside V8's heap. Implement an
1183 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001184 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001185 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001186 * UTF-8, which would require special treatment internally in the
1187 * engine and, in the case of UTF-8, do not allow efficient indexing.
1188 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001189 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001191 class V8EXPORT ExternalAsciiStringResource
1192 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193 public:
1194 /**
1195 * Override the destructor to manage the life cycle of the underlying
1196 * buffer.
1197 */
1198 virtual ~ExternalAsciiStringResource() {}
1199 /** The string data from the underlying buffer.*/
1200 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001201 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202 virtual size_t length() const = 0;
1203 protected:
1204 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 };
1206
1207 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001208 * If the string is an external string, return the ExternalStringResourceBase
1209 * regardless of the encoding, otherwise return NULL. The encoding of the
1210 * string is returned in encoding_out.
1211 */
1212 inline ExternalStringResourceBase* GetExternalStringResourceBase(
1213 Encoding* encoding_out) const;
1214
1215 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001216 * Get the ExternalStringResource for an external string. Returns
1217 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001219 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220
1221 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001222 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001223 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001225 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1226 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001228 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229
1230 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001231 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001233 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 * be careful to supply the length parameter.
1235 * If it is not given, the function calls
1236 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001237 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001239 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240
lrn@chromium.org34e60782011-09-15 07:25:40 +00001241 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001242 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243
1244 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001245 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246
v8.team.kasperl727e9952008-09-02 14:56:44 +00001247 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001248 * Creates a new string by concatenating the left and the right strings
1249 * passed in as parameters.
1250 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001251 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001252 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001253
1254 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001255 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001256 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001257 * resource will be disposed by calling its Dispose method. The caller of
1258 * this function should not otherwise delete or modify the resource. Neither
1259 * should the underlying buffer be deallocated or modified except through the
1260 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001261 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001262 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001263
ager@chromium.org6f10e412009-02-13 10:11:16 +00001264 /**
1265 * Associate an external string resource with this string by transforming it
1266 * in place so that existing references to this string in the JavaScript heap
1267 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001268 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001269 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001270 * The string is not modified if the operation fails. See NewExternal for
1271 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001272 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001273 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274
v8.team.kasperl727e9952008-09-02 14:56:44 +00001275 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001276 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001277 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001278 * resource will be disposed by calling its Dispose method. The caller of
1279 * this function should not otherwise delete or modify the resource. Neither
1280 * should the underlying buffer be deallocated or modified except through the
1281 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001282 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001283 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001284
ager@chromium.org6f10e412009-02-13 10:11:16 +00001285 /**
1286 * Associate an external string resource with this string by transforming it
1287 * in place so that existing references to this string in the JavaScript heap
1288 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001289 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001290 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001291 * The string is not modified if the operation fails. See NewExternal for
1292 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001293 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001294 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001296 /**
1297 * Returns true if this string can be made external.
1298 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001299 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001300
lrn@chromium.org34e60782011-09-15 07:25:40 +00001301 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001302 V8EXPORT static Local<String> NewUndetectable(const char* data,
1303 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304
lrn@chromium.org34e60782011-09-15 07:25:40 +00001305 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001306 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1307 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308
1309 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001310 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001311 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001312 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001313 * then the length() method returns 0 and the * operator returns
1314 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001315 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001316 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001317 public:
1318 explicit Utf8Value(Handle<v8::Value> obj);
1319 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001320 char* operator*() { return str_; }
1321 const char* operator*() const { return str_; }
1322 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001323 private:
1324 char* str_;
1325 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001326
1327 // Disallow copying and assigning.
1328 Utf8Value(const Utf8Value&);
1329 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001330 };
1331
1332 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001333 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001335 * If conversion to a string fails (eg. due to an exception in the toString()
1336 * method of the object) then the length() method returns 0 and the * operator
1337 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001339 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 public:
1341 explicit AsciiValue(Handle<v8::Value> obj);
1342 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001343 char* operator*() { return str_; }
1344 const char* operator*() const { return str_; }
1345 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 private:
1347 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001348 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001349
1350 // Disallow copying and assigning.
1351 AsciiValue(const AsciiValue&);
1352 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 };
1354
1355 /**
1356 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001357 * If conversion to a string fails (eg. due to an exception in the toString()
1358 * method of the object) then the length() method returns 0 and the * operator
1359 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001361 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 public:
1363 explicit Value(Handle<v8::Value> obj);
1364 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001365 uint16_t* operator*() { return str_; }
1366 const uint16_t* operator*() const { return str_; }
1367 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 private:
1369 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001370 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001371
1372 // Disallow copying and assigning.
1373 Value(const Value&);
1374 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001376
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001377 private:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001378 V8EXPORT void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1379 Encoding encoding) const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001380 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1381 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 number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001388class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001390 V8EXPORT double Value() const;
1391 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001392 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001394 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001395 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396};
1397
1398
1399/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001400 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001402class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001404 V8EXPORT static Local<Integer> New(int32_t value);
1405 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001406 V8EXPORT static Local<Integer> New(int32_t value, Isolate*);
1407 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001408 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001409 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001411 V8EXPORT Integer();
1412 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413};
1414
1415
1416/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001417 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001419class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001421 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001422 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001423 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424};
1425
1426
1427/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001428 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001429 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001430class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001432 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001434 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435};
1436
1437
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438enum PropertyAttribute {
1439 None = 0,
1440 ReadOnly = 1 << 0,
1441 DontEnum = 1 << 1,
1442 DontDelete = 1 << 2
1443};
1444
ager@chromium.org3811b432009-10-28 14:53:37 +00001445enum ExternalArrayType {
1446 kExternalByteArray = 1,
1447 kExternalUnsignedByteArray,
1448 kExternalShortArray,
1449 kExternalUnsignedShortArray,
1450 kExternalIntArray,
1451 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001452 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001453 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001454 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001455};
1456
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001458 * Accessor[Getter|Setter] are used as callback functions when
1459 * setting|getting a particular property. See Object and ObjectTemplate's
1460 * method SetAccessor.
1461 */
1462typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1463 const AccessorInfo& info);
1464
1465
1466typedef void (*AccessorSetter)(Local<String> property,
1467 Local<Value> value,
1468 const AccessorInfo& info);
1469
1470
1471/**
1472 * Access control specifications.
1473 *
1474 * Some accessors should be accessible across contexts. These
1475 * accessors have an explicit access control parameter which specifies
1476 * the kind of cross-context access that should be allowed.
1477 *
1478 * Additionally, for security, accessors can prohibit overwriting by
1479 * accessors defined in JavaScript. For objects that have such
1480 * accessors either locally or in their prototype chain it is not
1481 * possible to overwrite the accessor by using __defineGetter__ or
1482 * __defineSetter__ from JavaScript code.
1483 */
1484enum AccessControl {
1485 DEFAULT = 0,
1486 ALL_CAN_READ = 1,
1487 ALL_CAN_WRITE = 1 << 1,
1488 PROHIBITS_OVERWRITING = 1 << 2
1489};
1490
1491
1492/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001493 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001495class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001497 V8EXPORT bool Set(Handle<Value> key,
1498 Handle<Value> value,
1499 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001500
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001501 V8EXPORT bool Set(uint32_t index,
1502 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001503
ager@chromium.orge2902be2009-06-08 12:21:35 +00001504 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001505 // overriding accessors or read-only properties.
1506 //
1507 // Note that if the object has an interceptor the property will be set
1508 // locally, but since the interceptor takes precedence the local property
1509 // will only be returned if the interceptor doesn't return a value.
1510 //
1511 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001512 V8EXPORT bool ForceSet(Handle<Value> key,
1513 Handle<Value> value,
1514 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001515
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001516 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001518 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001519
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001520 /**
1521 * Gets the property attributes of a property which can be None or
1522 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1523 * None when the property doesn't exist.
1524 */
1525 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 // TODO(1245389): Replace the type-specific versions of these
1528 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001529 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001530
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001531 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001532
1533 // Delete a property on this object bypassing interceptors and
1534 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001535 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001536
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001537 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001538
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001539 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001541 V8EXPORT bool SetAccessor(Handle<String> name,
1542 AccessorGetter getter,
1543 AccessorSetter setter = 0,
1544 Handle<Value> data = Handle<Value>(),
1545 AccessControl settings = DEFAULT,
1546 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001547
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001549 * Returns an array containing the names of the enumerable properties
1550 * of this object, including properties from prototype objects. The
1551 * array returned by this method contains the same values as would
1552 * be enumerated by a for-in statement over this object.
1553 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001554 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001555
1556 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001557 * This function has the same functionality as GetPropertyNames but
1558 * the returned array doesn't contain the names of properties from
1559 * prototype objects.
1560 */
1561 V8EXPORT Local<Array> GetOwnPropertyNames();
1562
1563 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564 * Get the prototype object. This does not skip objects marked to
1565 * be skipped by __proto__ and it does not consult the security
1566 * handler.
1567 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001568 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569
1570 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001571 * Set the prototype object. This does not skip objects marked to
1572 * be skipped by __proto__ and it does not consult the security
1573 * handler.
1574 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001575 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001576
1577 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001578 * Finds an instance of the given function template in the prototype
1579 * chain.
1580 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001581 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1582 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001583
1584 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585 * Call builtin Object.prototype.toString on this object.
1586 * This is different from Value::ToString() that may call
1587 * user-defined toString function. This one does not.
1588 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001589 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001591 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001592 * Returns the function invoked as a constructor for this object.
1593 * May be the null value.
1594 */
1595 V8EXPORT Local<Value> GetConstructor();
1596
1597 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001598 * Returns the name of the function invoked as a constructor for this object.
1599 */
1600 V8EXPORT Local<String> GetConstructorName();
1601
kasper.lund212ac232008-07-16 07:07:30 +00001602 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001603 V8EXPORT int InternalFieldCount();
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00001604 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001605 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001606 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001607 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00001609 /** Gets a native pointer from an internal field. */
1610 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001611
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00001612 /** Sets a native pointer in an internal field. */
1613 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001614
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001616 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001617 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1618 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1619 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620
1621 /**
1622 * If result.IsEmpty() no real property was located in the prototype chain.
1623 * This means interceptors in the prototype chain are not called.
1624 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001625 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1626 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001627
1628 /**
1629 * If result.IsEmpty() no real property was located on the object or
1630 * in the prototype chain.
1631 * This means interceptors in the prototype chain are not called.
1632 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001633 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634
1635 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001636 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637
kasper.lund212ac232008-07-16 07:07:30 +00001638 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001639 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001641 /**
1642 * Turns on access check on the object if the object is an instance of
1643 * a template that has access check callbacks. If an object has no
1644 * access check info, the object cannot be accessed by anyone.
1645 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001646 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001647
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001648 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001649 * Returns the identity hash for this object. The current implementation
1650 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001651 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001652 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001653 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001654 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001655 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001656
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001657 /**
1658 * Access hidden properties on JavaScript objects. These properties are
1659 * hidden from the executing JavaScript and only accessible through the V8
1660 * C++ API. Hidden properties introduced by V8 internally (for example the
1661 * identity hash) are prefixed with "v8::".
1662 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001663 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1664 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1665 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001666
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001667 /**
1668 * Returns true if this is an instance of an api function (one
1669 * created from a function created from a function template) and has
1670 * been modified since it was created. Note that this method is
1671 * conservative and may return true for objects that haven't actually
1672 * been modified.
1673 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001674 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001675
1676 /**
1677 * Clone this object with a fast but shallow copy. Values will point
1678 * to the same values as the original object.
1679 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001680 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001682 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001683 * Returns the context in which the object was created.
1684 */
1685 V8EXPORT Local<Context> CreationContext();
1686
1687 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001688 * Set the backing store of the indexed properties to be managed by the
1689 * embedding layer. Access to the indexed properties will follow the rules
1690 * spelled out in CanvasPixelArray.
1691 * Note: The embedding program still owns the data and needs to ensure that
1692 * the backing store is preserved while V8 has a reference.
1693 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001694 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001695 V8EXPORT bool HasIndexedPropertiesInPixelData();
1696 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1697 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001698
ager@chromium.org3811b432009-10-28 14:53:37 +00001699 /**
1700 * Set the backing store of the indexed properties to be managed by the
1701 * embedding layer. Access to the indexed properties will follow the rules
1702 * spelled out for the CanvasArray subtypes in the WebGL specification.
1703 * Note: The embedding program still owns the data and needs to ensure that
1704 * the backing store is preserved while V8 has a reference.
1705 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001706 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1707 void* data,
1708 ExternalArrayType array_type,
1709 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001710 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1711 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1712 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1713 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001714
lrn@chromium.org1c092762011-05-09 09:42:16 +00001715 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001716 * Checks whether a callback is set by the
1717 * ObjectTemplate::SetCallAsFunctionHandler method.
1718 * When an Object is callable this method returns true.
1719 */
1720 V8EXPORT bool IsCallable();
1721
1722 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001723 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001724 * ObjectTemplate::SetCallAsFunctionHandler method.
1725 */
1726 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1727 int argc,
1728 Handle<Value> argv[]);
1729
1730 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001731 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001732 * ObjectTemplate::SetCallAsFunctionHandler method.
1733 * Note: This method behaves like the Function::NewInstance method.
1734 */
1735 V8EXPORT Local<Value> CallAsConstructor(int argc,
1736 Handle<Value> argv[]);
1737
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001738 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001739 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001742 V8EXPORT Object();
1743 V8EXPORT static void CheckCast(Value* obj);
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00001744 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1745 V8EXPORT void* SlowGetPointerFromInternalField(int index);
1746
1747 /**
1748 * If quick access to the internal field is possible this method
1749 * returns the value. Otherwise an empty handle is returned.
1750 */
1751 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752};
1753
1754
1755/**
1756 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1757 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001758class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001760 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761
ager@chromium.org3e875802009-06-29 08:26:34 +00001762 /**
1763 * Clones an element at index |index|. Returns an empty
1764 * handle if cloning fails (for any reason).
1765 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001766 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001767
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001768 /**
1769 * Creates a JavaScript array with the given length. If the length
1770 * is negative the returned array will have length 0.
1771 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001772 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001773
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001774 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001776 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001777 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778};
1779
1780
1781/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001782 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001784class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001786 V8EXPORT Local<Object> NewInstance() const;
1787 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1788 V8EXPORT Local<Value> Call(Handle<Object> recv,
1789 int argc,
1790 Handle<Value> argv[]);
1791 V8EXPORT void SetName(Handle<String> name);
1792 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001793
1794 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001795 * Name inferred from variable or property assignment of this function.
1796 * Used to facilitate debugging and profiling of JavaScript code written
1797 * in an OO style, where many functions are anonymous but are assigned
1798 * to object properties.
1799 */
1800 V8EXPORT Handle<Value> GetInferredName() const;
1801
1802 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001803 * Returns zero based line number of function body and
1804 * kLineOffsetNotFound if no information available.
1805 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001806 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001807 /**
1808 * Returns zero based column number of function body and
1809 * kLineOffsetNotFound if no information available.
1810 */
1811 V8EXPORT int GetScriptColumnNumber() const;
1812 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001813 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001814 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001815 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001816
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001818 V8EXPORT Function();
1819 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820};
1821
1822
1823/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001824 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1825 */
1826class Date : public Object {
1827 public:
1828 V8EXPORT static Local<Value> New(double time);
1829
1830 /**
1831 * A specialization of Value::NumberValue that is more efficient
1832 * because we know the structure of this object.
1833 */
1834 V8EXPORT double NumberValue() const;
1835
1836 static inline Date* Cast(v8::Value* obj);
1837
1838 /**
1839 * Notification that the embedder has changed the time zone,
1840 * daylight savings time, or other date / time configuration
1841 * parameters. V8 keeps a cache of various values used for
1842 * date / time computation. This notification will reset
1843 * those cached values for the current context so that date /
1844 * time configuration changes would be reflected in the Date
1845 * object.
1846 *
1847 * This API should not be called more than needed as it will
1848 * negatively impact the performance of date operations.
1849 */
1850 V8EXPORT static void DateTimeConfigurationChangeNotification();
1851
1852 private:
1853 V8EXPORT static void CheckCast(v8::Value* obj);
1854};
1855
1856
1857/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001858 * A Number object (ECMA-262, 4.3.21).
1859 */
1860class NumberObject : public Object {
1861 public:
1862 V8EXPORT static Local<Value> New(double value);
1863
1864 /**
1865 * Returns the Number held by the object.
1866 */
1867 V8EXPORT double NumberValue() const;
1868
1869 static inline NumberObject* Cast(v8::Value* obj);
1870
1871 private:
1872 V8EXPORT static void CheckCast(v8::Value* obj);
1873};
1874
1875
1876/**
1877 * A Boolean object (ECMA-262, 4.3.15).
1878 */
1879class BooleanObject : public Object {
1880 public:
1881 V8EXPORT static Local<Value> New(bool value);
1882
1883 /**
1884 * Returns the Boolean held by the object.
1885 */
1886 V8EXPORT bool BooleanValue() const;
1887
1888 static inline BooleanObject* Cast(v8::Value* obj);
1889
1890 private:
1891 V8EXPORT static void CheckCast(v8::Value* obj);
1892};
1893
1894
1895/**
1896 * A String object (ECMA-262, 4.3.18).
1897 */
1898class StringObject : public Object {
1899 public:
1900 V8EXPORT static Local<Value> New(Handle<String> value);
1901
1902 /**
1903 * Returns the String held by the object.
1904 */
1905 V8EXPORT Local<String> StringValue() const;
1906
1907 static inline StringObject* Cast(v8::Value* obj);
1908
1909 private:
1910 V8EXPORT static void CheckCast(v8::Value* obj);
1911};
1912
1913
1914/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001915 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1916 */
1917class RegExp : public Object {
1918 public:
1919 /**
1920 * Regular expression flag bits. They can be or'ed to enable a set
1921 * of flags.
1922 */
1923 enum Flags {
1924 kNone = 0,
1925 kGlobal = 1,
1926 kIgnoreCase = 2,
1927 kMultiline = 4
1928 };
1929
1930 /**
1931 * Creates a regular expression from the given pattern string and
1932 * the flags bit field. May throw a JavaScript exception as
1933 * described in ECMA-262, 15.10.4.1.
1934 *
1935 * For example,
1936 * RegExp::New(v8::String::New("foo"),
1937 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1938 * is equivalent to evaluating "/foo/gm".
1939 */
1940 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1941 Flags flags);
1942
1943 /**
1944 * Returns the value of the source property: a string representing
1945 * the regular expression.
1946 */
1947 V8EXPORT Local<String> GetSource() const;
1948
1949 /**
1950 * Returns the flags bit field.
1951 */
1952 V8EXPORT Flags GetFlags() const;
1953
1954 static inline RegExp* Cast(v8::Value* obj);
1955
1956 private:
1957 V8EXPORT static void CheckCast(v8::Value* obj);
1958};
1959
1960
1961/**
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00001962 * A JavaScript value that wraps a C++ void*. This type of value is
1963 * mainly used to associate C++ data structures with JavaScript
1964 * objects.
1965 *
1966 * The Wrap function V8 will return the most optimal Value object wrapping the
1967 * C++ void*. The type of the value is not guaranteed to be an External object
1968 * and no assumptions about its type should be made. To access the wrapped
1969 * value Unwrap should be used, all other operations on that object will lead
1970 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001972class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 public:
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00001974 V8EXPORT static Local<Value> Wrap(void* data);
1975 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001976
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001977 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001978 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001979 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 private:
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00001981 V8EXPORT External();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001982 V8EXPORT static void CheckCast(v8::Value* obj);
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00001983 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1984 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985};
1986
1987
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001988// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989
1990
1991/**
1992 * The superclass of object and function templates.
1993 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001994class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 public:
1996 /** Adds a property to each instance created by this template.*/
1997 void Set(Handle<String> name, Handle<Data> value,
1998 PropertyAttribute attributes = None);
1999 inline void Set(const char* name, Handle<Data> value);
2000 private:
2001 Template();
2002
2003 friend class ObjectTemplate;
2004 friend class FunctionTemplate;
2005};
2006
2007
2008/**
2009 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002010 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 * including the receiver, the number and values of arguments, and
2012 * the holder of the function.
2013 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002014class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015 public:
2016 inline int Length() const;
2017 inline Local<Value> operator[](int i) const;
2018 inline Local<Function> Callee() const;
2019 inline Local<Object> This() const;
2020 inline Local<Object> Holder() const;
2021 inline bool IsConstructCall() const;
2022 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002023 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002024
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002026 static const int kIsolateIndex = 0;
2027 static const int kDataIndex = -1;
2028 static const int kCalleeIndex = -2;
2029 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002032 inline Arguments(internal::Object** implicit_args,
2033 internal::Object** values,
2034 int length,
2035 bool is_construct_call);
2036 internal::Object** implicit_args_;
2037 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002039 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040};
2041
2042
2043/**
2044 * The information passed to an accessor callback about the context
2045 * of the property access.
2046 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002047class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002049 inline AccessorInfo(internal::Object** args)
2050 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002051 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 inline Local<Value> Data() const;
2053 inline Local<Object> This() const;
2054 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002057 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058};
2059
2060
2061typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2062
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 * NamedProperty[Getter|Setter] are used as interceptors on object.
2065 * See ObjectTemplate::SetNamedPropertyHandler.
2066 */
2067typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2068 const AccessorInfo& info);
2069
2070
2071/**
2072 * Returns the value if the setter intercepts the request.
2073 * Otherwise, returns an empty handle.
2074 */
2075typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2076 Local<Value> value,
2077 const AccessorInfo& info);
2078
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079/**
2080 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002081 * The result is an integer encoding property attributes (like v8::None,
2082 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002084typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2085 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086
2087
2088/**
2089 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002090 * The return value is true if the property could be deleted and false
2091 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 */
2093typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2094 const AccessorInfo& info);
2095
2096/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002097 * Returns an array containing the names of the properties the named
2098 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099 */
2100typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2101
v8.team.kasperl727e9952008-09-02 14:56:44 +00002102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002104 * Returns the value of the property if the getter intercepts the
2105 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 */
2107typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2108 const AccessorInfo& info);
2109
2110
2111/**
2112 * Returns the value if the setter intercepts the request.
2113 * Otherwise, returns an empty handle.
2114 */
2115typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2116 Local<Value> value,
2117 const AccessorInfo& info);
2118
2119
2120/**
2121 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002122 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002124typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2125 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126
2127/**
2128 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002129 * The return value is true if the property could be deleted and false
2130 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 */
2132typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2133 const AccessorInfo& info);
2134
v8.team.kasperl727e9952008-09-02 14:56:44 +00002135/**
2136 * Returns an array containing the indices of the properties the
2137 * indexed property getter intercepts.
2138 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2140
2141
2142/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002143 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144 */
2145enum AccessType {
2146 ACCESS_GET,
2147 ACCESS_SET,
2148 ACCESS_HAS,
2149 ACCESS_DELETE,
2150 ACCESS_KEYS
2151};
2152
v8.team.kasperl727e9952008-09-02 14:56:44 +00002153
2154/**
2155 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002156 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002157 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002158typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159 Local<Value> key,
2160 AccessType type,
2161 Local<Value> data);
2162
v8.team.kasperl727e9952008-09-02 14:56:44 +00002163
2164/**
2165 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002166 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002167 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002168typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 uint32_t index,
2170 AccessType type,
2171 Local<Value> data);
2172
2173
2174/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002175 * A FunctionTemplate is used to create functions at runtime. There
2176 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002177 * context. The lifetime of the created function is equal to the
2178 * lifetime of the context. So in case the embedder needs to create
2179 * temporary functions that can be collected using Scripts is
2180 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 *
2182 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002183 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002185 * A FunctionTemplate has a corresponding instance template which is
2186 * used to create object instances when the function is used as a
2187 * constructor. Properties added to the instance template are added to
2188 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 *
2190 * A FunctionTemplate can have a prototype template. The prototype template
2191 * is used to create the prototype object of the function.
2192 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002193 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002195 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2197 * t->Set("func_property", v8::Number::New(1));
2198 *
2199 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2200 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2201 * proto_t->Set("proto_const", v8::Number::New(2));
2202 *
2203 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2204 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2205 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2206 * instance_t->Set("instance_property", Number::New(3));
2207 *
2208 * v8::Local<v8::Function> function = t->GetFunction();
2209 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002210 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 *
2212 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002213 * and "instance" for the instance object created above. The function
2214 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002215 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002216 * \code
2217 * func_property in function == true;
2218 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002220 * function.prototype.proto_method() invokes 'InvokeCallback'
2221 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002223 * instance instanceof function == true;
2224 * instance.instance_accessor calls 'InstanceAccessorCallback'
2225 * instance.instance_property == 3;
2226 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002228 * A FunctionTemplate can inherit from another one by calling the
2229 * FunctionTemplate::Inherit method. The following graph illustrates
2230 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002232 * \code
2233 * FunctionTemplate Parent -> Parent() . prototype -> { }
2234 * ^ ^
2235 * | Inherit(Parent) | .__proto__
2236 * | |
2237 * FunctionTemplate Child -> Child() . prototype -> { }
2238 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002240 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2241 * object of the Child() function has __proto__ pointing to the
2242 * Parent() function's prototype object. An instance of the Child
2243 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002245 * Let Parent be the FunctionTemplate initialized in the previous
2246 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 * Local<FunctionTemplate> parent = t;
2250 * Local<FunctionTemplate> child = FunctionTemplate::New();
2251 * child->Inherit(parent);
2252 *
2253 * Local<Function> child_function = child->GetFunction();
2254 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002255 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002257 * The Child function and Child instance will have the following
2258 * properties:
2259 *
2260 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002262 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002264 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002266class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 public:
2268 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002269 static Local<FunctionTemplate> New(
2270 InvocationCallback callback = 0,
2271 Handle<Value> data = Handle<Value>(),
2272 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 /** Returns the unique function instance in the current execution context.*/
2274 Local<Function> GetFunction();
2275
v8.team.kasperl727e9952008-09-02 14:56:44 +00002276 /**
2277 * Set the call-handler callback for a FunctionTemplate. This
2278 * callback is called whenever the function created from this
2279 * FunctionTemplate is called.
2280 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 void SetCallHandler(InvocationCallback callback,
2282 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283
v8.team.kasperl727e9952008-09-02 14:56:44 +00002284 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 Local<ObjectTemplate> InstanceTemplate();
2286
2287 /** Causes the function template to inherit from a parent function template.*/
2288 void Inherit(Handle<FunctionTemplate> parent);
2289
2290 /**
2291 * A PrototypeTemplate is the template used to create the prototype object
2292 * of the function created by this template.
2293 */
2294 Local<ObjectTemplate> PrototypeTemplate();
2295
v8.team.kasperl727e9952008-09-02 14:56:44 +00002296
2297 /**
2298 * Set the class name of the FunctionTemplate. This is used for
2299 * printing objects created with the function created from the
2300 * FunctionTemplate as its constructor.
2301 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 void SetClassName(Handle<String> name);
2303
2304 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002305 * Determines whether the __proto__ accessor ignores instances of
2306 * the function template. If instances of the function template are
2307 * ignored, __proto__ skips all instances and instead returns the
2308 * next object in the prototype chain.
2309 *
2310 * Call with a value of true to make the __proto__ accessor ignore
2311 * instances of the function template. Call with a value of false
2312 * to make the __proto__ accessor not ignore instances of the
2313 * function template. By default, instances of a function template
2314 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315 */
2316 void SetHiddenPrototype(bool value);
2317
2318 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002319 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2320 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002321 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002322 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002323
2324 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002325 * Returns true if the given object is an instance of this function
2326 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327 */
2328 bool HasInstance(Handle<Value> object);
2329
2330 private:
2331 FunctionTemplate();
2332 void AddInstancePropertyAccessor(Handle<String> name,
2333 AccessorGetter getter,
2334 AccessorSetter setter,
2335 Handle<Value> data,
2336 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002337 PropertyAttribute attributes,
2338 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2340 NamedPropertySetter setter,
2341 NamedPropertyQuery query,
2342 NamedPropertyDeleter remover,
2343 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002344 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2346 IndexedPropertySetter setter,
2347 IndexedPropertyQuery query,
2348 IndexedPropertyDeleter remover,
2349 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002350 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2352 Handle<Value> data);
2353
2354 friend class Context;
2355 friend class ObjectTemplate;
2356};
2357
2358
2359/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002360 * An ObjectTemplate is used to create objects at runtime.
2361 *
2362 * Properties added to an ObjectTemplate are added to each object
2363 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002365class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002367 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370 /** Creates a new instance of this template.*/
2371 Local<Object> NewInstance();
2372
2373 /**
2374 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002375 *
2376 * Whenever the property with the given name is accessed on objects
2377 * created from this ObjectTemplate the getter and setter callbacks
2378 * are called instead of getting and setting the property directly
2379 * on the JavaScript object.
2380 *
2381 * \param name The name of the property for which an accessor is added.
2382 * \param getter The callback to invoke when getting the property.
2383 * \param setter The callback to invoke when setting the property.
2384 * \param data A piece of data that will be passed to the getter and setter
2385 * callbacks whenever they are invoked.
2386 * \param settings Access control settings for the accessor. This is a bit
2387 * field consisting of one of more of
2388 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2389 * The default is to not allow cross-context access.
2390 * ALL_CAN_READ means that all cross-context reads are allowed.
2391 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2392 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2393 * cross-context access.
2394 * \param attribute The attributes of the property for which an accessor
2395 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002396 * \param signature The signature describes valid receivers for the accessor
2397 * and is used to perform implicit instance checks against them. If the
2398 * receiver is incompatible (i.e. is not an instance of the constructor as
2399 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2400 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 */
2402 void SetAccessor(Handle<String> name,
2403 AccessorGetter getter,
2404 AccessorSetter setter = 0,
2405 Handle<Value> data = Handle<Value>(),
2406 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002407 PropertyAttribute attribute = None,
2408 Handle<AccessorSignature> signature =
2409 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410
2411 /**
2412 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002413 *
2414 * Whenever a named property is accessed on objects created from
2415 * this object template, the provided callback is invoked instead of
2416 * accessing the property directly on the JavaScript object.
2417 *
2418 * \param getter The callback to invoke when getting a property.
2419 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002420 * \param query The callback to invoke to check if a property is present,
2421 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002422 * \param deleter The callback to invoke when deleting a property.
2423 * \param enumerator The callback to invoke to enumerate all the named
2424 * properties of an object.
2425 * \param data A piece of data that will be passed to the callbacks
2426 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427 */
2428 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2429 NamedPropertySetter setter = 0,
2430 NamedPropertyQuery query = 0,
2431 NamedPropertyDeleter deleter = 0,
2432 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002433 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434
2435 /**
2436 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002437 *
2438 * Whenever an indexed property is accessed on objects created from
2439 * this object template, the provided callback is invoked instead of
2440 * accessing the property directly on the JavaScript object.
2441 *
2442 * \param getter The callback to invoke when getting a property.
2443 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002444 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002445 * \param deleter The callback to invoke when deleting a property.
2446 * \param enumerator The callback to invoke to enumerate all the indexed
2447 * properties of an object.
2448 * \param data A piece of data that will be passed to the callbacks
2449 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 */
2451 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2452 IndexedPropertySetter setter = 0,
2453 IndexedPropertyQuery query = 0,
2454 IndexedPropertyDeleter deleter = 0,
2455 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002456 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002457
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458 /**
2459 * Sets the callback to be used when calling instances created from
2460 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002461 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462 * function.
2463 */
2464 void SetCallAsFunctionHandler(InvocationCallback callback,
2465 Handle<Value> data = Handle<Value>());
2466
v8.team.kasperl727e9952008-09-02 14:56:44 +00002467 /**
2468 * Mark object instances of the template as undetectable.
2469 *
2470 * In many ways, undetectable objects behave as though they are not
2471 * there. They behave like 'undefined' in conditionals and when
2472 * printed. However, properties can be accessed and called as on
2473 * normal objects.
2474 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475 void MarkAsUndetectable();
2476
v8.team.kasperl727e9952008-09-02 14:56:44 +00002477 /**
2478 * Sets access check callbacks on the object template.
2479 *
2480 * When accessing properties on instances of this object template,
2481 * the access check callback will be called to determine whether or
2482 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002483 * The last parameter specifies whether access checks are turned
2484 * on by default on instances. If access checks are off by default,
2485 * they can be turned on on individual instances by calling
2486 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002487 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2489 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002490 Handle<Value> data = Handle<Value>(),
2491 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492
kasper.lund212ac232008-07-16 07:07:30 +00002493 /**
2494 * Gets the number of internal fields for objects generated from
2495 * this template.
2496 */
2497 int InternalFieldCount();
2498
2499 /**
2500 * Sets the number of internal fields for objects generated from
2501 * this template.
2502 */
2503 void SetInternalFieldCount(int value);
2504
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505 private:
2506 ObjectTemplate();
2507 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2508 friend class FunctionTemplate;
2509};
2510
2511
2512/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002513 * A Signature specifies which receivers and arguments are valid
2514 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002516class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517 public:
2518 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2519 Handle<FunctionTemplate>(),
2520 int argc = 0,
2521 Handle<FunctionTemplate> argv[] = 0);
2522 private:
2523 Signature();
2524};
2525
2526
2527/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002528 * An AccessorSignature specifies which receivers are valid parameters
2529 * to an accessor callback.
2530 */
2531class V8EXPORT AccessorSignature : public Data {
2532 public:
2533 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2534 Handle<FunctionTemplate>());
2535 private:
2536 AccessorSignature();
2537};
2538
2539
2540/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002541 * A utility for determining the type of objects based on the template
2542 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002544class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002545 public:
2546 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2547 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2548 int match(Handle<Value> value);
2549 private:
2550 TypeSwitch();
2551};
2552
2553
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002554// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002555
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002556class V8EXPORT ExternalAsciiStringResourceImpl
2557 : public String::ExternalAsciiStringResource {
2558 public:
2559 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2560 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2561 : data_(data), length_(length) {}
2562 const char* data() const { return data_; }
2563 size_t length() const { return length_; }
2564
2565 private:
2566 const char* data_;
2567 size_t length_;
2568};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569
2570/**
2571 * Ignore
2572 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002573class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002575 // Note that the strings passed into this constructor must live as long
2576 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002578 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002580 const char** deps = 0,
2581 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 virtual ~Extension() { }
2583 virtual v8::Handle<v8::FunctionTemplate>
2584 GetNativeFunction(v8::Handle<v8::String> name) {
2585 return v8::Handle<v8::FunctionTemplate>();
2586 }
2587
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002588 const char* name() const { return name_; }
2589 size_t source_length() const { return source_length_; }
2590 const String::ExternalAsciiStringResource* source() const {
2591 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592 int dependency_count() { return dep_count_; }
2593 const char** dependencies() { return deps_; }
2594 void set_auto_enable(bool value) { auto_enable_ = value; }
2595 bool auto_enable() { return auto_enable_; }
2596
2597 private:
2598 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002599 size_t source_length_; // expected to initialize before source_
2600 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601 int dep_count_;
2602 const char** deps_;
2603 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002604
2605 // Disallow copying and assigning.
2606 Extension(const Extension&);
2607 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608};
2609
2610
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002611void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612
2613
2614/**
2615 * Ignore
2616 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002617class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618 public:
2619 inline DeclareExtension(Extension* extension) {
2620 RegisterExtension(extension);
2621 }
2622};
2623
2624
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002625// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626
2627
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002628Handle<Primitive> V8EXPORT Undefined();
2629Handle<Primitive> V8EXPORT Null();
2630Handle<Boolean> V8EXPORT True();
2631Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002633inline Handle<Primitive> Undefined(Isolate* isolate);
2634inline Handle<Primitive> Null(Isolate* isolate);
2635inline Handle<Boolean> True(Isolate* isolate);
2636inline Handle<Boolean> False(Isolate* isolate);
2637
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638
2639/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002640 * A set of constraints that specifies the limits of the runtime's memory use.
2641 * You must set the heap size before initializing the VM - the size cannot be
2642 * adjusted after the VM is initialized.
2643 *
2644 * If you are using threads then you should hold the V8::Locker lock while
2645 * setting the stack limit and you must set a non-default stack limit separately
2646 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002648class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649 public:
2650 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002651 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002653 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002655 int max_executable_size() { return max_executable_size_; }
2656 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002657 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002658 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2660 private:
2661 int max_young_space_size_;
2662 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002663 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 uint32_t* stack_limit_;
2665};
2666
2667
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002668bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669
2670
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002671// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672
2673
2674typedef void (*FatalErrorCallback)(const char* location, const char* message);
2675
2676
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002677typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678
2679
2680/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002681 * Schedules an exception to be thrown when returning to JavaScript. When an
2682 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002684 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002685 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002686Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687
2688/**
2689 * Create new error objects by calling the corresponding error object
2690 * constructor with the message.
2691 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002692class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693 public:
2694 static Local<Value> RangeError(Handle<String> message);
2695 static Local<Value> ReferenceError(Handle<String> message);
2696 static Local<Value> SyntaxError(Handle<String> message);
2697 static Local<Value> TypeError(Handle<String> message);
2698 static Local<Value> Error(Handle<String> message);
2699};
2700
2701
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002702// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002704typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002706typedef void* (*CreateHistogramCallback)(const char* name,
2707 int min,
2708 int max,
2709 size_t buckets);
2710
2711typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2712
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002713// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002714 enum ObjectSpace {
2715 kObjectSpaceNewSpace = 1 << 0,
2716 kObjectSpaceOldPointerSpace = 1 << 1,
2717 kObjectSpaceOldDataSpace = 1 << 2,
2718 kObjectSpaceCodeSpace = 1 << 3,
2719 kObjectSpaceMapSpace = 1 << 4,
2720 kObjectSpaceLoSpace = 1 << 5,
2721
2722 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2723 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2724 kObjectSpaceLoSpace
2725 };
2726
2727 enum AllocationAction {
2728 kAllocationActionAllocate = 1 << 0,
2729 kAllocationActionFree = 1 << 1,
2730 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2731 };
2732
2733typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2734 AllocationAction action,
2735 int size);
2736
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002737// --- Leave Script Callback ---
2738typedef void (*CallCompletedCallback)();
2739
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002740// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2742 AccessType type,
2743 Local<Value> data);
2744
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002745// --- AllowCodeGenerationFromStrings callbacks ---
2746
2747/**
2748 * Callback to check if code generation from strings is allowed. See
2749 * Context::AllowCodeGenerationFromStrings.
2750 */
2751typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2752
2753// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002754
2755/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002756 * Applications can register callback functions which will be called
2757 * before and after a garbage collection. Allocations are not
2758 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002759 * objects (set or delete properties for example) since it is possible
2760 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002762enum GCType {
2763 kGCTypeScavenge = 1 << 0,
2764 kGCTypeMarkSweepCompact = 1 << 1,
2765 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2766};
2767
2768enum GCCallbackFlags {
2769 kNoGCCallbackFlags = 0,
2770 kGCCallbackFlagCompacted = 1 << 0
2771};
2772
2773typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2774typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2775
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776typedef void (*GCCallback)();
2777
2778
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002779/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002780 * Collection of V8 heap information.
2781 *
2782 * Instances of this class can be passed to v8::V8::HeapStatistics to
2783 * get heap statistics from V8.
2784 */
2785class V8EXPORT HeapStatistics {
2786 public:
2787 HeapStatistics();
2788 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002789 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00002790 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002791 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002792 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002793
2794 private:
2795 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002796 void set_total_heap_size_executable(size_t size) {
2797 total_heap_size_executable_ = size;
2798 }
danno@chromium.org72204d52012-10-31 10:02:10 +00002799 void set_total_physical_size(size_t size) {
2800 total_physical_size_ = size;
2801 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002802 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002803 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002804
2805 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002806 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00002807 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002808 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002809 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002810
2811 friend class V8;
2812};
2813
2814
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002815class RetainedObjectInfo;
2816
ager@chromium.org3811b432009-10-28 14:53:37 +00002817/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002818 * Isolate represents an isolated instance of the V8 engine. V8
2819 * isolates have completely separate states. Objects from one isolate
2820 * must not be used in other isolates. When V8 is initialized a
2821 * default isolate is implicitly created and entered. The embedder
2822 * can create additional isolates and use them in parallel in multiple
2823 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002824 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002825 */
2826class V8EXPORT Isolate {
2827 public:
2828 /**
2829 * Stack-allocated class which sets the isolate for all operations
2830 * executed within a local scope.
2831 */
2832 class V8EXPORT Scope {
2833 public:
2834 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2835 isolate->Enter();
2836 }
2837
2838 ~Scope() { isolate_->Exit(); }
2839
2840 private:
2841 Isolate* const isolate_;
2842
2843 // Prevent copying of Scope objects.
2844 Scope(const Scope&);
2845 Scope& operator=(const Scope&);
2846 };
2847
2848 /**
2849 * Creates a new isolate. Does not change the currently entered
2850 * isolate.
2851 *
2852 * When an isolate is no longer used its resources should be freed
2853 * by calling Dispose(). Using the delete operator is not allowed.
2854 */
2855 static Isolate* New();
2856
2857 /**
2858 * Returns the entered isolate for the current thread or NULL in
2859 * case there is no current isolate.
2860 */
2861 static Isolate* GetCurrent();
2862
2863 /**
2864 * Methods below this point require holding a lock (using Locker) in
2865 * a multi-threaded environment.
2866 */
2867
2868 /**
2869 * Sets this isolate as the entered one for the current thread.
2870 * Saves the previously entered one (if any), so that it can be
2871 * restored when exiting. Re-entering an isolate is allowed.
2872 */
2873 void Enter();
2874
2875 /**
2876 * Exits this isolate by restoring the previously entered one in the
2877 * current thread. The isolate may still stay the same, if it was
2878 * entered more than once.
2879 *
2880 * Requires: this == Isolate::GetCurrent().
2881 */
2882 void Exit();
2883
2884 /**
2885 * Disposes the isolate. The isolate must not be entered by any
2886 * thread to be disposable.
2887 */
2888 void Dispose();
2889
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002890 /**
2891 * Associate embedder-specific data with the isolate
2892 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002893 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002894
2895 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002896 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002897 * Returns NULL if SetData has never been called.
2898 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002899 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002900
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002901 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002902 Isolate();
2903 Isolate(const Isolate&);
2904 ~Isolate();
2905 Isolate& operator=(const Isolate&);
2906 void* operator new(size_t size);
2907 void operator delete(void*, size_t);
2908};
2909
2910
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002911class StartupData {
2912 public:
2913 enum CompressionAlgorithm {
2914 kUncompressed,
2915 kBZip2
2916 };
2917
2918 const char* data;
2919 int compressed_size;
2920 int raw_size;
2921};
2922
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002923
2924/**
2925 * A helper class for driving V8 startup data decompression. It is based on
2926 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2927 * for an embedder to use this class, instead, API functions can be used
2928 * directly.
2929 *
2930 * For an example of the class usage, see the "shell.cc" sample application.
2931 */
2932class V8EXPORT StartupDataDecompressor { // NOLINT
2933 public:
2934 StartupDataDecompressor();
2935 virtual ~StartupDataDecompressor();
2936 int Decompress();
2937
2938 protected:
2939 virtual int DecompressData(char* raw_data,
2940 int* raw_data_size,
2941 const char* compressed_data,
2942 int compressed_data_size) = 0;
2943
2944 private:
2945 char** raw_data;
2946};
2947
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002948
2949/**
2950 * EntropySource is used as a callback function when v8 needs a source
2951 * of entropy.
2952 */
2953typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2954
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002955
2956/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002957 * ReturnAddressLocationResolver is used as a callback function when v8 is
2958 * resolving the location of a return address on the stack. Profilers that
2959 * change the return address on the stack can use this to resolve the stack
2960 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002961 *
2962 * \param return_addr_location points to a location on stack where a machine
2963 * return address resides.
2964 * \returns either return_addr_location, or else a pointer to the profiler's
2965 * copy of the original return address.
2966 *
2967 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00002968 */
2969typedef uintptr_t (*ReturnAddressLocationResolver)(
2970 uintptr_t return_addr_location);
2971
2972
2973/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002974 * FunctionEntryHook is the type of the profile entry hook called at entry to
2975 * any generated function when function-level profiling is enabled.
2976 *
2977 * \param function the address of the function that's being entered.
2978 * \param return_addr_location points to a location on stack where the machine
2979 * return address resides. This can be used to identify the caller of
2980 * \p function, and/or modified to divert execution when \p function exits.
2981 *
2982 * \note the entry hook must not cause garbage collection.
2983 */
2984typedef void (*FunctionEntryHook)(uintptr_t function,
2985 uintptr_t return_addr_location);
2986
2987
2988/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002989 * A JIT code event is issued each time code is added, moved or removed.
2990 *
2991 * \note removal events are not currently issued.
2992 */
2993struct JitCodeEvent {
2994 enum EventType {
2995 CODE_ADDED,
2996 CODE_MOVED,
2997 CODE_REMOVED
2998 };
2999
3000 // Type of event.
3001 EventType type;
3002 // Start of the instructions.
3003 void* code_start;
3004 // Size of the instructions.
3005 size_t code_len;
3006
3007 union {
3008 // Only valid for CODE_ADDED.
3009 struct {
3010 // Name of the object associated with the code, note that the string is
3011 // not zero-terminated.
3012 const char* str;
3013 // Number of chars in str.
3014 size_t len;
3015 } name;
3016 // New location of instructions. Only valid for CODE_MOVED.
3017 void* new_code_start;
3018 };
3019};
3020
3021/**
3022 * Option flags passed to the SetJitCodeEventHandler function.
3023 */
3024enum JitCodeEventOptions {
3025 kJitCodeEventDefault = 0,
3026 // Generate callbacks for already existent code.
3027 kJitCodeEventEnumExisting = 1
3028};
3029
3030
3031/**
3032 * Callback function passed to SetJitCodeEventHandler.
3033 *
3034 * \param event code add, move or removal event.
3035 */
3036typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3037
3038
3039/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003040 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003041 */
3042class V8EXPORT ExternalResourceVisitor { // NOLINT
3043 public:
3044 virtual ~ExternalResourceVisitor() {}
3045 virtual void VisitExternalString(Handle<String> string) {}
3046};
3047
3048
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003049/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003050 * Interface for iterating through all the persistent handles in the heap.
3051 */
3052class V8EXPORT PersistentHandleVisitor { // NOLINT
3053 public:
3054 virtual ~PersistentHandleVisitor() {}
3055 virtual void VisitPersistentHandle(Persistent<Value> value,
3056 uint16_t class_id) {}
3057};
3058
3059
3060/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061 * Container class for static utility functions.
3062 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003063class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003064 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003065 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003066 static void SetFatalErrorHandler(FatalErrorCallback that);
3067
v8.team.kasperl727e9952008-09-02 14:56:44 +00003068 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003069 * Set the callback to invoke to check if code generation from
3070 * strings should be allowed.
3071 */
3072 static void SetAllowCodeGenerationFromStringsCallback(
3073 AllowCodeGenerationFromStringsCallback that);
3074
3075 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003076 * Ignore out-of-memory exceptions.
3077 *
3078 * V8 running out of memory is treated as a fatal error by default.
3079 * This means that the fatal error handler is called and that V8 is
3080 * terminated.
3081 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003082 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003083 * out-of-memory situation as a fatal error. This way, the contexts
3084 * that did not cause the out of memory problem might be able to
3085 * continue execution.
3086 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003087 static void IgnoreOutOfMemoryException();
3088
v8.team.kasperl727e9952008-09-02 14:56:44 +00003089 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003090 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003091 * fatal errors such as out-of-memory situations.
3092 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003093 static bool IsDead();
3094
3095 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003096 * The following 4 functions are to be used when V8 is built with
3097 * the 'compress_startup_data' flag enabled. In this case, the
3098 * embedder must decompress startup data prior to initializing V8.
3099 *
3100 * This is how interaction with V8 should look like:
3101 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3102 * v8::StartupData* compressed_data =
3103 * new v8::StartupData[compressed_data_count];
3104 * v8::V8::GetCompressedStartupData(compressed_data);
3105 * ... decompress data (compressed_data can be updated in-place) ...
3106 * v8::V8::SetDecompressedStartupData(compressed_data);
3107 * ... now V8 can be initialized
3108 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003109 *
3110 * A helper class StartupDataDecompressor is provided. It implements
3111 * the protocol of the interaction described above, and can be used in
3112 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003113 */
3114 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3115 static int GetCompressedStartupDataCount();
3116 static void GetCompressedStartupData(StartupData* compressed_data);
3117 static void SetDecompressedStartupData(StartupData* decompressed_data);
3118
3119 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003120 * Adds a message listener.
3121 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003122 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003123 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003124 */
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003125 static bool AddMessageListener(MessageCallback that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003126
3127 /**
3128 * Remove all message listeners from the specified callback function.
3129 */
3130 static void RemoveMessageListeners(MessageCallback that);
3131
3132 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003133 * Tells V8 to capture current stack trace when uncaught exception occurs
3134 * and report it to the message listeners. The option is off by default.
3135 */
3136 static void SetCaptureStackTraceForUncaughtExceptions(
3137 bool capture,
3138 int frame_limit = 10,
3139 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3140
3141 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003142 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003143 */
3144 static void SetFlagsFromString(const char* str, int length);
3145
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003146 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003147 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003148 */
3149 static void SetFlagsFromCommandLine(int* argc,
3150 char** argv,
3151 bool remove_flags);
3152
kasper.lund7276f142008-07-30 08:49:36 +00003153 /** Get the version string. */
3154 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003155
3156 /**
3157 * Enables the host application to provide a mechanism for recording
3158 * statistics counters.
3159 */
3160 static void SetCounterFunction(CounterLookupCallback);
3161
3162 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003163 * Enables the host application to provide a mechanism for recording
3164 * histograms. The CreateHistogram function returns a
3165 * histogram which will later be passed to the AddHistogramSample
3166 * function.
3167 */
3168 static void SetCreateHistogramFunction(CreateHistogramCallback);
3169 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3170
3171 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003172 * Enables the computation of a sliding window of states. The sliding
3173 * window information is recorded in statistics counters.
3174 */
3175 static void EnableSlidingStateWindow();
3176
3177 /** Callback function for reporting failed access checks.*/
3178 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3179
3180 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003181 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003182 * garbage collection. Allocations are not allowed in the
3183 * callback function, you therefore cannot manipulate objects (set
3184 * or delete properties for example) since it is possible such
3185 * operations will result in the allocation of objects. It is possible
3186 * to specify the GCType filter for your callback. But it is not possible to
3187 * register the same callback function two times with different
3188 * GCType filters.
3189 */
3190 static void AddGCPrologueCallback(
3191 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3192
3193 /**
3194 * This function removes callback which was installed by
3195 * AddGCPrologueCallback function.
3196 */
3197 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3198
3199 /**
3200 * The function is deprecated. Please use AddGCPrologueCallback instead.
3201 * Enables the host application to receive a notification before a
3202 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003203 * callback function, you therefore cannot manipulate objects (set
3204 * or delete properties for example) since it is possible such
3205 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003206 */
3207 static void SetGlobalGCPrologueCallback(GCCallback);
3208
3209 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003210 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003211 * garbage collection. Allocations are not allowed in the
3212 * callback function, you therefore cannot manipulate objects (set
3213 * or delete properties for example) since it is possible such
3214 * operations will result in the allocation of objects. It is possible
3215 * to specify the GCType filter for your callback. But it is not possible to
3216 * register the same callback function two times with different
3217 * GCType filters.
3218 */
3219 static void AddGCEpilogueCallback(
3220 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3221
3222 /**
3223 * This function removes callback which was installed by
3224 * AddGCEpilogueCallback function.
3225 */
3226 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3227
3228 /**
3229 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3230 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003231 * major garbage collection. Allocations are not allowed in the
3232 * callback function, you therefore cannot manipulate objects (set
3233 * or delete properties for example) since it is possible such
3234 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003235 */
3236 static void SetGlobalGCEpilogueCallback(GCCallback);
3237
3238 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003239 * Enables the host application to provide a mechanism to be notified
3240 * and perform custom logging when V8 Allocates Executable Memory.
3241 */
3242 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3243 ObjectSpace space,
3244 AllocationAction action);
3245
3246 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003247 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003248 */
3249 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3250
3251 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003252 * Adds a callback to notify the host application when a script finished
3253 * running. If a script re-enters the runtime during executing, the
3254 * CallCompletedCallback is only invoked when the outer-most script
3255 * execution ends. Executing scripts inside the callback do not trigger
3256 * further callbacks.
3257 */
3258 static void AddCallCompletedCallback(CallCompletedCallback callback);
3259
3260 /**
3261 * Removes callback that was installed by AddCallCompletedCallback.
3262 */
3263 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3264
3265 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003266 * Allows the host application to group objects together. If one
3267 * object in the group is alive, all objects in the group are alive.
3268 * After each garbage collection, object groups are removed. It is
3269 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003270 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003271 * wrapper objects. Object groups for all dependent handles need to
3272 * be provided for kGCTypeMarkSweepCompact collections, for all other
3273 * garbage collection types it is sufficient to provide object groups
3274 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003275 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003276 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003277 static void AddObjectGroup(Persistent<Value>* objects,
3278 size_t length,
3279 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280
3281 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003282 * Allows the host application to declare implicit references between
3283 * the objects: if |parent| is alive, all |children| are alive too.
3284 * After each garbage collection, all implicit references
3285 * are removed. It is intended to be used in the before-garbage-collection
3286 * callback function.
3287 */
3288 static void AddImplicitReferences(Persistent<Object> parent,
3289 Persistent<Value>* children,
3290 size_t length);
3291
3292 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003293 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003294 * initialize from scratch. This function is called implicitly if
3295 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003296 */
3297 static bool Initialize();
3298
kasper.lund7276f142008-07-30 08:49:36 +00003299 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003300 * Allows the host application to provide a callback which can be used
3301 * as a source of entropy for random number generators.
3302 */
3303 static void SetEntropySource(EntropySource source);
3304
3305 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003306 * Allows the host application to provide a callback that allows v8 to
3307 * cooperate with a profiler that rewrites return addresses on stack.
3308 */
3309 static void SetReturnAddressLocationResolver(
3310 ReturnAddressLocationResolver return_address_resolver);
3311
3312 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003313 * Allows the host application to provide the address of a function that's
3314 * invoked on entry to every V8-generated function.
3315 * Note that \p entry_hook is invoked at the very start of each
3316 * generated function.
3317 *
3318 * \param entry_hook a function that will be invoked on entry to every
3319 * V8-generated function.
3320 * \returns true on success on supported platforms, false on failure.
3321 * \note Setting a new entry hook function when one is already active will
3322 * fail.
3323 */
3324 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3325
3326 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003327 * Allows the host application to provide the address of a function that is
3328 * notified each time code is added, moved or removed.
3329 *
3330 * \param options options for the JIT code event handler.
3331 * \param event_handler the JIT code event handler, which will be invoked
3332 * each time code is added, moved or removed.
3333 * \note \p event_handler won't get notified of existent code.
3334 * \note since code removal notifications are not currently issued, the
3335 * \p event_handler may get notifications of code that overlaps earlier
3336 * code notifications. This happens when code areas are reused, and the
3337 * earlier overlapping code areas should therefore be discarded.
3338 * \note the events passed to \p event_handler and the strings they point to
3339 * are not guaranteed to live past each call. The \p event_handler must
3340 * copy strings and other parameters it needs to keep around.
3341 * \note the set of events declared in JitCodeEvent::EventType is expected to
3342 * grow over time, and the JitCodeEvent structure is expected to accrue
3343 * new members. The \p event_handler function must ignore event codes
3344 * it does not recognize to maintain future compatibility.
3345 */
3346 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3347 JitCodeEventHandler event_handler);
3348
3349 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003350 * Adjusts the amount of registered external memory. Used to give
3351 * V8 an indication of the amount of externally allocated memory
3352 * that is kept alive by JavaScript objects. V8 uses this to decide
3353 * when to perform global garbage collections. Registering
3354 * externally allocated memory will trigger global garbage
3355 * collections more often than otherwise in an attempt to garbage
3356 * collect the JavaScript objects keeping the externally allocated
3357 * memory alive.
3358 *
3359 * \param change_in_bytes the change in externally allocated memory
3360 * that is kept alive by JavaScript objects.
3361 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003362 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003363 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3364 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003365
iposva@chromium.org245aa852009-02-10 00:49:54 +00003366 /**
3367 * Suspends recording of tick samples in the profiler.
3368 * When the V8 profiling mode is enabled (usually via command line
3369 * switches) this function suspends recording of tick samples.
3370 * Profiling ticks are discarded until ResumeProfiler() is called.
3371 *
3372 * See also the --prof and --prof_auto command line switches to
3373 * enable V8 profiling.
3374 */
3375 static void PauseProfiler();
3376
3377 /**
3378 * Resumes recording of tick samples in the profiler.
3379 * See also PauseProfiler().
3380 */
3381 static void ResumeProfiler();
3382
ager@chromium.org41826e72009-03-30 13:30:57 +00003383 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003384 * Return whether profiler is currently paused.
3385 */
3386 static bool IsProfilerPaused();
3387
3388 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003389 * Retrieve the V8 thread id of the calling thread.
3390 *
3391 * The thread id for a thread should only be retrieved after the V8
3392 * lock has been acquired with a Locker object with that thread.
3393 */
3394 static int GetCurrentThreadId();
3395
3396 /**
3397 * Forcefully terminate execution of a JavaScript thread. This can
3398 * be used to terminate long-running scripts.
3399 *
3400 * TerminateExecution should only be called when then V8 lock has
3401 * been acquired with a Locker object. Therefore, in order to be
3402 * able to terminate long-running threads, preemption must be
3403 * enabled to allow the user of TerminateExecution to acquire the
3404 * lock.
3405 *
3406 * The termination is achieved by throwing an exception that is
3407 * uncatchable by JavaScript exception handlers. Termination
3408 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003409 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003410 * exception handler that catches an exception should check if that
3411 * exception is a termination exception and immediately return if
3412 * that is the case. Returning immediately in that case will
3413 * continue the propagation of the termination exception if needed.
3414 *
3415 * The thread id passed to TerminateExecution must have been
3416 * obtained by calling GetCurrentThreadId on the thread in question.
3417 *
3418 * \param thread_id The thread id of the thread to terminate.
3419 */
3420 static void TerminateExecution(int thread_id);
3421
3422 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003423 * Forcefully terminate the current thread of JavaScript execution
3424 * in the given isolate. If no isolate is provided, the default
3425 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003426 *
3427 * This method can be used by any thread even if that thread has not
3428 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003429 *
3430 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003431 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003432 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003433
3434 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003435 * Is V8 terminating JavaScript execution.
3436 *
3437 * Returns true if JavaScript execution is currently terminating
3438 * because of a call to TerminateExecution. In that case there are
3439 * still JavaScript frames on the stack and the termination
3440 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003441 *
3442 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003443 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003444 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003445
3446 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003447 * Releases any resources used by v8 and stops any utility threads
3448 * that may be running. Note that disposing v8 is permanent, it
3449 * cannot be reinitialized.
3450 *
3451 * It should generally not be necessary to dispose v8 before exiting
3452 * a process, this should happen automatically. It is only necessary
3453 * to use if the process needs the resources taken up by v8.
3454 */
3455 static bool Dispose();
3456
ager@chromium.org3811b432009-10-28 14:53:37 +00003457 /**
3458 * Get statistics about the heap memory usage.
3459 */
3460 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003461
3462 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003463 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003464 * heap. GC is not invoked prior to iterating, therefore there is no
3465 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003466 */
3467 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3468
3469 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003470 * Iterates through all the persistent handles in the current isolate's heap
3471 * that have class_ids.
3472 */
3473 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3474
3475 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003476 * Optional notification that the embedder is idle.
3477 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003478 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003479 * Returns true if the embedder should stop calling IdleNotification
3480 * until real work has been done. This indicates that V8 has done
3481 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003482 *
3483 * The hint argument specifies the amount of work to be done in the function
3484 * on scale from 1 to 1000. There is no guarantee that the actual work will
3485 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003486 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003487 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003488
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003489 /**
3490 * Optional notification that the system is running low on memory.
3491 * V8 uses these notifications to attempt to free memory.
3492 */
3493 static void LowMemoryNotification();
3494
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003495 /**
3496 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003497 * these notifications to guide the GC heuristic. Returns the number
3498 * of context disposals - including this one - since the last time
3499 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003500 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003501 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003502
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003503 private:
3504 V8();
3505
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003506 static internal::Object** GlobalizeReference(internal::Object** handle);
3507 static void DisposeGlobal(internal::Object** global_handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003508 static void DisposeGlobal(internal::Isolate* isolate,
3509 internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003510 static void MakeWeak(internal::Object** global_handle,
3511 void* data,
3512 WeakReferenceCallback);
3513 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003514 static void MarkIndependent(internal::Object** global_handle);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003515 static void MarkIndependent(internal::Isolate* isolate,
3516 internal::Object** global_handle);
3517 static void MarkPartiallyDependent(internal::Object** global_handle);
3518 static void MarkPartiallyDependent(internal::Isolate* isolate,
3519 internal::Object** global_handle);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003520 static bool IsGlobalIndependent(internal::Object** global_handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003521 static bool IsGlobalIndependent(internal::Isolate* isolate,
3522 internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003523 static bool IsGlobalNearDeath(internal::Object** global_handle);
3524 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003525 static void SetWrapperClassId(internal::Object** global_handle,
3526 uint16_t class_id);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003527 static uint16_t GetWrapperClassId(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003528
3529 template <class T> friend class Handle;
3530 template <class T> friend class Local;
3531 template <class T> friend class Persistent;
3532 friend class Context;
3533};
3534
3535
3536/**
3537 * An external exception handler.
3538 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003539class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003540 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003541 /**
3542 * Creates a new try/catch block and registers it with v8.
3543 */
3544 TryCatch();
3545
3546 /**
3547 * Unregisters and deletes this try/catch block.
3548 */
3549 ~TryCatch();
3550
3551 /**
3552 * Returns true if an exception has been caught by this try/catch block.
3553 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003554 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003555
3556 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003557 * For certain types of exceptions, it makes no sense to continue
3558 * execution.
3559 *
3560 * Currently, the only type of exception that can be caught by a
3561 * TryCatch handler and for which it does not make sense to continue
3562 * is termination exception. Such exceptions are thrown when the
3563 * TerminateExecution methods are called to terminate a long-running
3564 * script.
3565 *
3566 * If CanContinue returns false, the correct action is to perform
3567 * any C++ cleanup needed and then return.
3568 */
3569 bool CanContinue() const;
3570
3571 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003572 * Throws the exception caught by this TryCatch in a way that avoids
3573 * it being caught again by this same TryCatch. As with ThrowException
3574 * it is illegal to execute any JavaScript operations after calling
3575 * ReThrow; the caller must return immediately to where the exception
3576 * is caught.
3577 */
3578 Handle<Value> ReThrow();
3579
3580 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003581 * Returns the exception caught by this try/catch block. If no exception has
3582 * been caught an empty handle is returned.
3583 *
3584 * The returned handle is valid until this TryCatch block has been destroyed.
3585 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003586 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587
3588 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003589 * Returns the .stack property of the thrown object. If no .stack
3590 * property is present an empty handle is returned.
3591 */
3592 Local<Value> StackTrace() const;
3593
3594 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003595 * Returns the message associated with this exception. If there is
3596 * no message associated an empty handle is returned.
3597 *
3598 * The returned handle is valid until this TryCatch block has been
3599 * destroyed.
3600 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003601 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003602
3603 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003604 * Clears any exceptions that may have been caught by this try/catch block.
3605 * After this method has been called, HasCaught() will return false.
3606 *
3607 * It is not necessary to clear a try/catch block before using it again; if
3608 * another exception is thrown the previously caught exception will just be
3609 * overwritten. However, it is often a good idea since it makes it easier
3610 * to determine which operation threw a given exception.
3611 */
3612 void Reset();
3613
v8.team.kasperl727e9952008-09-02 14:56:44 +00003614 /**
3615 * Set verbosity of the external exception handler.
3616 *
3617 * By default, exceptions that are caught by an external exception
3618 * handler are not reported. Call SetVerbose with true on an
3619 * external exception handler to have exceptions caught by the
3620 * handler reported as if they were not caught.
3621 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003622 void SetVerbose(bool value);
3623
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003624 /**
3625 * Set whether or not this TryCatch should capture a Message object
3626 * which holds source information about where the exception
3627 * occurred. True by default.
3628 */
3629 void SetCaptureMessage(bool value);
3630
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003631 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003632 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003633 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003634 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003635 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003636 bool is_verbose_ : 1;
3637 bool can_continue_ : 1;
3638 bool capture_message_ : 1;
3639 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003640
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003641 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003642};
3643
3644
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003645// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003646
3647
3648/**
3649 * Ignore
3650 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003651class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003652 public:
3653 ExtensionConfiguration(int name_count, const char* names[])
3654 : name_count_(name_count), names_(names) { }
3655 private:
3656 friend class ImplementationUtilities;
3657 int name_count_;
3658 const char** names_;
3659};
3660
3661
3662/**
3663 * A sandboxed execution context with its own set of built-in objects
3664 * and functions.
3665 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003666class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003667 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003668 /**
3669 * Returns the global proxy object or global object itself for
3670 * detached contexts.
3671 *
3672 * Global proxy object is a thin wrapper whose prototype points to
3673 * actual context's global object with the properties like Object, etc.
3674 * This is done that way for security reasons (for more details see
3675 * https://wiki.mozilla.org/Gecko:SplitWindow).
3676 *
3677 * Please note that changes to global proxy object prototype most probably
3678 * would break VM---v8 expects only global object as a prototype of
3679 * global proxy object.
3680 *
3681 * If DetachGlobal() has been invoked, Global() would return actual global
3682 * object until global is reattached with ReattachGlobal().
3683 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003684 Local<Object> Global();
3685
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003686 /**
3687 * Detaches the global object from its context before
3688 * the global object can be reused to create a new context.
3689 */
3690 void DetachGlobal();
3691
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003692 /**
3693 * Reattaches a global object to a context. This can be used to
3694 * restore the connection between a global object and a context
3695 * after DetachGlobal has been called.
3696 *
3697 * \param global_object The global object to reattach to the
3698 * context. For this to work, the global object must be the global
3699 * object that was associated with this context before a call to
3700 * DetachGlobal.
3701 */
3702 void ReattachGlobal(Handle<Object> global_object);
3703
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003704 /** Creates a new context.
3705 *
3706 * Returns a persistent handle to the newly allocated context. This
3707 * persistent handle has to be disposed when the context is no
3708 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003709 *
3710 * \param extensions An optional extension configuration containing
3711 * the extensions to be installed in the newly created context.
3712 *
3713 * \param global_template An optional object template from which the
3714 * global object for the newly created context will be created.
3715 *
3716 * \param global_object An optional global object to be reused for
3717 * the newly created context. This global object must have been
3718 * created by a previous call to Context::New with the same global
3719 * template. The state of the global object will be completely reset
3720 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003721 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003722 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003723 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003724 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3725 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003726
kasper.lund44510672008-07-25 07:37:58 +00003727 /** Returns the last entered context. */
3728 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003729
kasper.lund44510672008-07-25 07:37:58 +00003730 /** Returns the context that is on the top of the stack. */
3731 static Local<Context> GetCurrent();
3732
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003733 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003734 * Returns the context of the calling JavaScript code. That is the
3735 * context of the top-most JavaScript frame. If there are no
3736 * JavaScript frames an empty handle is returned.
3737 */
3738 static Local<Context> GetCalling();
3739
3740 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003741 * Sets the security token for the context. To access an object in
3742 * another context, the security tokens must match.
3743 */
3744 void SetSecurityToken(Handle<Value> token);
3745
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003746 /** Restores the security token to the default value. */
3747 void UseDefaultSecurityToken();
3748
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003749 /** Returns the security token of this context.*/
3750 Handle<Value> GetSecurityToken();
3751
v8.team.kasperl727e9952008-09-02 14:56:44 +00003752 /**
3753 * Enter this context. After entering a context, all code compiled
3754 * and run is compiled and run in this context. If another context
3755 * is already entered, this old context is saved so it can be
3756 * restored when the new context is exited.
3757 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003758 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003759
3760 /**
3761 * Exit this context. Exiting the current context restores the
3762 * context that was in place when entering the current context.
3763 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764 void Exit();
3765
v8.team.kasperl727e9952008-09-02 14:56:44 +00003766 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003767 bool HasOutOfMemoryException();
3768
v8.team.kasperl727e9952008-09-02 14:56:44 +00003769 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003770 static bool InContext();
3771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772 /**
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00003773 * Associate an additional data object with the context. This is mainly used
3774 * with the debugger to provide additional information on the context through
3775 * the debugger API.
ager@chromium.org9085a012009-05-11 19:22:57 +00003776 */
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00003777 void SetData(Handle<Value> data);
3778 Local<Value> GetData();
ager@chromium.org9085a012009-05-11 19:22:57 +00003779
3780 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003781 * Control whether code generation from strings is allowed. Calling
3782 * this method with false will disable 'eval' and the 'Function'
3783 * constructor for code running in this context. If 'eval' or the
3784 * 'Function' constructor are used an exception will be thrown.
3785 *
3786 * If code generation from strings is not allowed the
3787 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3788 * set before blocking the call to 'eval' or the 'Function'
3789 * constructor. If that callback returns true, the call will be
3790 * allowed, otherwise an exception will be thrown. If no callback is
3791 * set an exception will be thrown.
3792 */
3793 void AllowCodeGenerationFromStrings(bool allow);
3794
3795 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003796 * Returns true if code generation from strings is allowed for the context.
3797 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3798 */
3799 bool IsCodeGenerationFromStringsAllowed();
3800
3801 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003802 * Sets the error description for the exception that is thrown when
3803 * code generation from strings is not allowed and 'eval' or the 'Function'
3804 * constructor are called.
3805 */
3806 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3807
3808 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809 * Stack-allocated class which sets the execution context for all
3810 * operations executed within a local scope.
3811 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003812 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003813 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003814 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003815 context_->Enter();
3816 }
3817 inline ~Scope() { context_->Exit(); }
3818 private:
3819 Handle<Context> context_;
3820 };
3821
3822 private:
3823 friend class Value;
3824 friend class Script;
3825 friend class Object;
3826 friend class Function;
3827};
3828
3829
3830/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003831 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003832 * is allowed to use any given V8 isolate. See Isolate class
3833 * comments. The definition of 'using V8 isolate' includes
3834 * accessing handles or holding onto object pointers obtained
3835 * from V8 handles while in the particular V8 isolate. It is up
3836 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003837 * constraint is not violated. In addition to any other synchronization
3838 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3839 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003841 * v8::Locker is a scoped lock object. While it's
3842 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003843 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3844 * locked by at most one thread at any time. In other words, the scope of a
3845 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003846 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003847 * Sample usage:
3848* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003849 * ...
3850 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003851 * v8::Locker locker(isolate);
3852 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003853 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003854 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003855 * ...
3856 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003857 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003858 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003859 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003860 * by destroying the v8::Locker object as above or by constructing a
3861 * v8::Unlocker object:
3862 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003863 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003864 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003865 * isolate->Exit();
3866 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003867 * ...
3868 * // Code not using V8 goes here while V8 can run in another thread.
3869 * ...
3870 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003871 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003872 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003873 *
3874 * The Unlocker object is intended for use in a long-running callback
3875 * from V8, where you want to release the V8 lock for other threads to
3876 * use.
3877 *
3878 * The v8::Locker is a recursive lock. That is, you can lock more than
3879 * once in a given thread. This can be useful if you have code that can
3880 * be called either from code that holds the lock or from code that does
3881 * not. The Unlocker is not recursive so you can not have several
3882 * Unlockers on the stack at once, and you can not use an Unlocker in a
3883 * thread that is not inside a Locker's scope.
3884 *
3885 * An unlocker will unlock several lockers if it has to and reinstate
3886 * the correct depth of locking on its destruction. eg.:
3887 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003888 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003889 * // V8 not locked.
3890 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003891 * v8::Locker locker(isolate);
3892 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003893 * // V8 locked.
3894 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003895 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896 * // V8 still locked (2 levels).
3897 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003898 * isolate->Exit();
3899 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003900 * // V8 not locked.
3901 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003902 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903 * // V8 locked again (2 levels).
3904 * }
3905 * // V8 still locked (1 level).
3906 * }
3907 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003908 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003909 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003910 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003912class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003913 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003914 /**
3915 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3916 */
3917 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003918 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003919 private:
3920 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003921};
3922
3923
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003924class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003925 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003926 /**
3927 * Initialize Locker for a given Isolate. NULL means default isolate.
3928 */
3929 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003931
3932 /**
3933 * Start preemption.
3934 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003935 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003936 * that will switch between multiple threads that are in contention
3937 * for the V8 lock.
3938 */
3939 static void StartPreemption(int every_n_ms);
3940
3941 /**
3942 * Stop preemption.
3943 */
3944 static void StopPreemption();
3945
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003946 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003947 * Returns whether or not the locker for a given isolate, or default isolate
3948 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003949 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003950 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003951
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003952 /**
3953 * Returns whether v8::Locker is being used by this V8 instance.
3954 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003955 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003956
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957 private:
3958 bool has_lock_;
3959 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003960 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003961
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003962 static bool active_;
3963
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003964 // Disallow copying and assigning.
3965 Locker(const Locker&);
3966 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003967};
3968
3969
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003970/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003971 * A struct for exporting HeapStats data from V8, using "push" model.
3972 */
3973struct HeapStatsUpdate;
3974
3975
3976/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003977 * An interface for exporting data from V8, using "push" model.
3978 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003979class V8EXPORT OutputStream { // NOLINT
3980 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003981 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003982 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003983 };
3984 enum WriteResult {
3985 kContinue = 0,
3986 kAbort = 1
3987 };
3988 virtual ~OutputStream() {}
3989 /** Notify about the end of stream. */
3990 virtual void EndOfStream() = 0;
3991 /** Get preferred output chunk size. Called only once. */
3992 virtual int GetChunkSize() { return 1024; }
3993 /** Get preferred output encoding. Called only once. */
3994 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3995 /**
3996 * Writes the next chunk of snapshot data into the stream. Writing
3997 * can be stopped by returning kAbort as function result. EndOfStream
3998 * will not be called in case writing was aborted.
3999 */
4000 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004001 /**
4002 * Writes the next chunk of heap stats data into the stream. Writing
4003 * can be stopped by returning kAbort as function result. EndOfStream
4004 * will not be called in case writing was aborted.
4005 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004006 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004007 return kAbort;
4008 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004009};
4010
4011
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004012/**
4013 * An interface for reporting progress and controlling long-running
4014 * activities.
4015 */
4016class V8EXPORT ActivityControl { // NOLINT
4017 public:
4018 enum ControlOption {
4019 kContinue = 0,
4020 kAbort = 1
4021 };
4022 virtual ~ActivityControl() {}
4023 /**
4024 * Notify about current progress. The activity can be stopped by
4025 * returning kAbort as the callback result.
4026 */
4027 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4028};
4029
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004030
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004031// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004033
4034namespace internal {
4035
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004036const int kApiPointerSize = sizeof(void*); // NOLINT
4037const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004038
4039// Tag information for HeapObject.
4040const int kHeapObjectTag = 1;
4041const int kHeapObjectTagSize = 2;
4042const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4043
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004044// Tag information for Smi.
4045const int kSmiTag = 0;
4046const int kSmiTagSize = 1;
4047const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4048
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004049template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004050
4051// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004052template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004053 static const int kSmiShiftSize = 0;
4054 static const int kSmiValueSize = 31;
4055 static inline int SmiToInt(internal::Object* value) {
4056 int shift_bits = kSmiTagSize + kSmiShiftSize;
4057 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4058 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4059 }
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004060
4061 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
4062 // with a plain reinterpret_cast.
4063 static const uintptr_t kEncodablePointerMask = 0x1;
4064 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004065};
4066
4067// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004068template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004069 static const int kSmiShiftSize = 31;
4070 static const int kSmiValueSize = 32;
4071 static inline int SmiToInt(internal::Object* value) {
4072 int shift_bits = kSmiTagSize + kSmiShiftSize;
4073 // Shift down and throw away top 32 bits.
4074 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4075 }
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004076
4077 // To maximize the range of pointers that can be encoded
4078 // in the available 32 bits, we require them to be 8 bytes aligned.
4079 // This gives 2 ^ (32 + 3) = 32G address space covered.
4080 // It might be not enough to cover stack allocated objects on some platforms.
4081 static const int kPointerAlignment = 3;
4082
4083 static const uintptr_t kEncodablePointerMask =
4084 ~(uintptr_t(0xffffffff) << kPointerAlignment);
4085
4086 static const int kPointerToSmiShift =
4087 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004088};
4089
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004090typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4091const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4092const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004093const uintptr_t kEncodablePointerMask =
4094 PlatformSmiTagging::kEncodablePointerMask;
4095const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004096
4097/**
4098 * This class exports constants and functionality from within v8 that
4099 * is necessary to implement inline functions in the v8 api. Don't
4100 * depend on functions and constants defined here.
4101 */
4102class Internals {
4103 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004104 // These values match non-compiler-dependent values defined within
4105 // the implementation of v8.
4106 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004107 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004108 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004109
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004110 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004111 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004112 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004113 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004114 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004115 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004116 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004117
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004118 static const int kIsolateStateOffset = 0;
4119 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4120 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4121 static const int kUndefinedValueRootIndex = 5;
4122 static const int kNullValueRootIndex = 7;
4123 static const int kTrueValueRootIndex = 8;
4124 static const int kFalseValueRootIndex = 9;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004125 static const int kEmptySymbolRootIndex = 118;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004126
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004127 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004128 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004129 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004130 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004131
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004132 static const int kUndefinedOddballKind = 5;
4133 static const int kNullOddballKind = 3;
4134
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004135 static inline bool HasHeapObjectTag(internal::Object* value) {
4136 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4137 kHeapObjectTag);
4138 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004139
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004140 static inline bool HasSmiTag(internal::Object* value) {
4141 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
4142 }
4143
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004144 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004145 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004146 }
4147
4148 static inline int GetInstanceType(internal::Object* obj) {
4149 typedef internal::Object O;
4150 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4151 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4152 }
4153
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004154 static inline int GetOddballKind(internal::Object* obj) {
4155 typedef internal::Object O;
4156 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4157 }
4158
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004159 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
4160 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
4161 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
4162 }
4163
4164 static inline void* GetExternalPointer(internal::Object* obj) {
4165 if (HasSmiTag(obj)) {
4166 return GetExternalPointerFromSmi(obj);
4167 } else if (GetInstanceType(obj) == kForeignType) {
4168 return ReadField<void*>(obj, kForeignAddressOffset);
4169 } else {
4170 return NULL;
4171 }
4172 }
4173
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004174 static inline bool IsExternalTwoByteString(int instance_type) {
4175 int representation = (instance_type & kFullStringRepresentationMask);
4176 return representation == kExternalTwoByteRepresentationTag;
4177 }
4178
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004179 static inline bool IsInitialized(v8::Isolate* isolate) {
4180 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4181 return *reinterpret_cast<int*>(addr) == 1;
4182 }
4183
4184 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
4185 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4186 kIsolateEmbedderDataOffset;
4187 *reinterpret_cast<void**>(addr) = data;
4188 }
4189
4190 static inline void* GetEmbedderData(v8::Isolate* isolate) {
4191 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4192 kIsolateEmbedderDataOffset;
4193 return *reinterpret_cast<void**>(addr);
4194 }
4195
4196 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
4197 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4198 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4199 }
4200
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004201 template <typename T>
4202 static inline T ReadField(Object* ptr, int offset) {
4203 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4204 return *reinterpret_cast<T*>(addr);
4205 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004206
4207 static inline bool CanCastToHeapObject(void* o) { return false; }
4208 static inline bool CanCastToHeapObject(Context* o) { return true; }
4209 static inline bool CanCastToHeapObject(String* o) { return true; }
4210 static inline bool CanCastToHeapObject(Object* o) { return true; }
4211 static inline bool CanCastToHeapObject(Message* o) { return true; }
4212 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
4213 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004214};
4215
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004216} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004217
4218
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004219template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004220Local<T>::Local() : Handle<T>() { }
4221
4222
4223template <class T>
4224Local<T> Local<T>::New(Handle<T> that) {
4225 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004226 T* that_ptr = *that;
4227 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4228 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4229 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4230 reinterpret_cast<internal::HeapObject*>(*p))));
4231 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004232 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4233}
4234
4235
4236template <class T>
4237Persistent<T> Persistent<T>::New(Handle<T> that) {
4238 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004239 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004240 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4241}
4242
4243
4244template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004245bool Persistent<T>::IsIndependent() const {
4246 if (this->IsEmpty()) return false;
4247 return V8::IsGlobalIndependent(reinterpret_cast<internal::Object**>(**this));
4248}
4249
4250
4251template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004252bool Persistent<T>::IsIndependent(Isolate* isolate) const {
4253 if (this->IsEmpty()) return false;
4254 return V8::IsGlobalIndependent(reinterpret_cast<internal::Isolate*>(isolate),
4255 reinterpret_cast<internal::Object**>(**this));
4256}
4257
4258
4259template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004260bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004261 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004262 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004263}
4264
4265
4266template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004267bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004268 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004269 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004270}
4271
4272
4273template <class T>
4274void Persistent<T>::Dispose() {
4275 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004276 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004277}
4278
4279
4280template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004281void Persistent<T>::Dispose(Isolate* isolate) {
4282 if (this->IsEmpty()) return;
4283 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4284 reinterpret_cast<internal::Object**>(**this));
4285}
4286
4287
4288template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004289Persistent<T>::Persistent() : Handle<T>() { }
4290
4291template <class T>
4292void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004293 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4294 parameters,
4295 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004296}
4297
4298template <class T>
4299void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004300 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004301}
4302
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004303template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004304void Persistent<T>::MarkIndependent() {
4305 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4306}
4307
4308template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004309void Persistent<T>::MarkIndependent(Isolate* isolate) {
4310 V8::MarkIndependent(reinterpret_cast<internal::Isolate*>(isolate),
4311 reinterpret_cast<internal::Object**>(**this));
4312}
4313
4314template <class T>
4315void Persistent<T>::MarkPartiallyDependent() {
4316 V8::MarkPartiallyDependent(reinterpret_cast<internal::Object**>(**this));
4317}
4318
4319template <class T>
4320void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
4321 V8::MarkPartiallyDependent(reinterpret_cast<internal::Isolate*>(isolate),
4322 reinterpret_cast<internal::Object**>(**this));
4323}
4324
4325template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004326void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4327 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4328}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004329
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004330template <class T>
4331uint16_t Persistent<T>::WrapperClassId() const {
4332 return V8::GetWrapperClassId(reinterpret_cast<internal::Object**>(**this));
4333}
4334
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004335Arguments::Arguments(internal::Object** implicit_args,
4336 internal::Object** values, int length,
4337 bool is_construct_call)
4338 : implicit_args_(implicit_args),
4339 values_(values),
4340 length_(length),
4341 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004342
4343
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004344Local<Value> Arguments::operator[](int i) const {
4345 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4346 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4347}
4348
4349
4350Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004351 return Local<Function>(reinterpret_cast<Function*>(
4352 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004353}
4354
4355
4356Local<Object> Arguments::This() const {
4357 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4358}
4359
4360
4361Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004362 return Local<Object>(reinterpret_cast<Object*>(
4363 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004364}
4365
4366
4367Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004368 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004369}
4370
4371
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004372Isolate* Arguments::GetIsolate() const {
4373 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4374}
4375
4376
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004377bool Arguments::IsConstructCall() const {
4378 return is_construct_call_;
4379}
4380
4381
4382int Arguments::Length() const {
4383 return length_;
4384}
4385
4386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004387template <class T>
4388Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004389 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4390 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004391 return Local<T>(reinterpret_cast<T*>(after));
4392}
4393
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004394Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004395 return resource_name_;
4396}
4397
4398
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004399Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004400 return resource_line_offset_;
4401}
4402
4403
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004404Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004405 return resource_column_offset_;
4406}
4407
4408
4409Handle<Boolean> Boolean::New(bool value) {
4410 return value ? True() : False();
4411}
4412
4413
4414void Template::Set(const char* name, v8::Handle<Data> value) {
4415 Set(v8::String::New(name), value);
4416}
4417
4418
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004419Local<Value> Object::GetInternalField(int index) {
4420#ifndef V8_ENABLE_CHECKS
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004421 Local<Value> quick_result = UncheckedGetInternalField(index);
4422 if (!quick_result.IsEmpty()) return quick_result;
4423#endif
4424 return CheckedGetInternalField(index);
4425}
4426
4427
4428Local<Value> Object::UncheckedGetInternalField(int index) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004429 typedef internal::Object O;
4430 typedef internal::Internals I;
4431 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004432 if (I::GetInstanceType(obj) == I::kJSObjectType) {
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004433 // If the object is a plain JSObject, which is the common case,
4434 // we know where to find the internal fields and can return the
4435 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004436 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004437 O* value = I::ReadField<O*>(obj, offset);
4438 O** result = HandleScope::CreateHandle(value);
4439 return Local<Value>(reinterpret_cast<Value*>(result));
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004440 } else {
4441 return Local<Value>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004442 }
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004443}
4444
4445
4446void* External::Unwrap(Handle<v8::Value> obj) {
4447#ifdef V8_ENABLE_CHECKS
4448 return FullUnwrap(obj);
4449#else
4450 return QuickUnwrap(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004451#endif
4452}
4453
4454
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004455void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4456 typedef internal::Object O;
4457 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
4458 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004459}
4460
4461
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004462void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004463 typedef internal::Object O;
4464 typedef internal::Internals I;
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004465
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004466 O* obj = *reinterpret_cast<O**>(this);
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004467
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004468 if (I::GetInstanceType(obj) == I::kJSObjectType) {
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004469 // If the object is a plain JSObject, which is the common case,
4470 // we know where to find the internal fields and can return the
4471 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004472 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004473 O* value = I::ReadField<O*>(obj, offset);
4474 return I::GetExternalPointer(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004475 }
svenpanne@chromium.orgfc2a4f42012-11-07 09:25:55 +00004476
4477 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004478}
4479
4480
4481String* String::Cast(v8::Value* value) {
4482#ifdef V8_ENABLE_CHECKS
4483 CheckCast(value);
4484#endif
4485 return static_cast<String*>(value);
4486}
4487
4488
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004489Local<String> String::Empty(Isolate* isolate) {
4490 typedef internal::Object* S;
4491 typedef internal::Internals I;
4492 if (!I::IsInitialized(isolate)) return Empty();
4493 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4494 return Local<String>(reinterpret_cast<String*>(slot));
4495}
4496
4497
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004498String::ExternalStringResource* String::GetExternalStringResource() const {
4499 typedef internal::Object O;
4500 typedef internal::Internals I;
4501 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004502 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004503 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004504 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4505 result = reinterpret_cast<String::ExternalStringResource*>(value);
4506 } else {
4507 result = NULL;
4508 }
4509#ifdef V8_ENABLE_CHECKS
4510 VerifyExternalStringResource(result);
4511#endif
4512 return result;
4513}
4514
4515
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004516String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4517 String::Encoding* encoding_out) const {
4518 typedef internal::Object O;
4519 typedef internal::Internals I;
4520 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4521 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4522 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4523 ExternalStringResourceBase* resource = NULL;
4524 if (type == I::kExternalAsciiRepresentationTag ||
4525 type == I::kExternalTwoByteRepresentationTag) {
4526 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4527 resource = static_cast<ExternalStringResourceBase*>(value);
4528 }
4529#ifdef V8_ENABLE_CHECKS
4530 VerifyExternalStringResourceBase(resource, *encoding_out);
4531#endif
4532 return resource;
4533}
4534
4535
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004536bool Value::IsUndefined() const {
4537#ifdef V8_ENABLE_CHECKS
4538 return FullIsUndefined();
4539#else
4540 return QuickIsUndefined();
4541#endif
4542}
4543
4544bool Value::QuickIsUndefined() const {
4545 typedef internal::Object O;
4546 typedef internal::Internals I;
4547 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4548 if (!I::HasHeapObjectTag(obj)) return false;
4549 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4550 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4551}
4552
4553
4554bool Value::IsNull() const {
4555#ifdef V8_ENABLE_CHECKS
4556 return FullIsNull();
4557#else
4558 return QuickIsNull();
4559#endif
4560}
4561
4562bool Value::QuickIsNull() const {
4563 typedef internal::Object O;
4564 typedef internal::Internals I;
4565 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4566 if (!I::HasHeapObjectTag(obj)) return false;
4567 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4568 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4569}
4570
4571
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004572bool Value::IsString() const {
4573#ifdef V8_ENABLE_CHECKS
4574 return FullIsString();
4575#else
4576 return QuickIsString();
4577#endif
4578}
4579
4580bool Value::QuickIsString() const {
4581 typedef internal::Object O;
4582 typedef internal::Internals I;
4583 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4584 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004585 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004586}
4587
4588
4589Number* Number::Cast(v8::Value* value) {
4590#ifdef V8_ENABLE_CHECKS
4591 CheckCast(value);
4592#endif
4593 return static_cast<Number*>(value);
4594}
4595
4596
4597Integer* Integer::Cast(v8::Value* value) {
4598#ifdef V8_ENABLE_CHECKS
4599 CheckCast(value);
4600#endif
4601 return static_cast<Integer*>(value);
4602}
4603
4604
4605Date* Date::Cast(v8::Value* value) {
4606#ifdef V8_ENABLE_CHECKS
4607 CheckCast(value);
4608#endif
4609 return static_cast<Date*>(value);
4610}
4611
4612
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004613StringObject* StringObject::Cast(v8::Value* value) {
4614#ifdef V8_ENABLE_CHECKS
4615 CheckCast(value);
4616#endif
4617 return static_cast<StringObject*>(value);
4618}
4619
4620
4621NumberObject* NumberObject::Cast(v8::Value* value) {
4622#ifdef V8_ENABLE_CHECKS
4623 CheckCast(value);
4624#endif
4625 return static_cast<NumberObject*>(value);
4626}
4627
4628
4629BooleanObject* BooleanObject::Cast(v8::Value* value) {
4630#ifdef V8_ENABLE_CHECKS
4631 CheckCast(value);
4632#endif
4633 return static_cast<BooleanObject*>(value);
4634}
4635
4636
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004637RegExp* RegExp::Cast(v8::Value* value) {
4638#ifdef V8_ENABLE_CHECKS
4639 CheckCast(value);
4640#endif
4641 return static_cast<RegExp*>(value);
4642}
4643
4644
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004645Object* Object::Cast(v8::Value* value) {
4646#ifdef V8_ENABLE_CHECKS
4647 CheckCast(value);
4648#endif
4649 return static_cast<Object*>(value);
4650}
4651
4652
4653Array* Array::Cast(v8::Value* value) {
4654#ifdef V8_ENABLE_CHECKS
4655 CheckCast(value);
4656#endif
4657 return static_cast<Array*>(value);
4658}
4659
4660
4661Function* Function::Cast(v8::Value* value) {
4662#ifdef V8_ENABLE_CHECKS
4663 CheckCast(value);
4664#endif
4665 return static_cast<Function*>(value);
4666}
4667
4668
4669External* External::Cast(v8::Value* value) {
4670#ifdef V8_ENABLE_CHECKS
4671 CheckCast(value);
4672#endif
4673 return static_cast<External*>(value);
4674}
4675
4676
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004677Isolate* AccessorInfo::GetIsolate() const {
4678 return *reinterpret_cast<Isolate**>(&args_[-3]);
4679}
4680
4681
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004682Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004683 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004684}
4685
4686
4687Local<Object> AccessorInfo::This() const {
4688 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4689}
4690
4691
4692Local<Object> AccessorInfo::Holder() const {
4693 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4694}
4695
4696
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004697Handle<Primitive> Undefined(Isolate* isolate) {
4698 typedef internal::Object* S;
4699 typedef internal::Internals I;
4700 if (!I::IsInitialized(isolate)) return Undefined();
4701 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4702 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4703}
4704
4705
4706Handle<Primitive> Null(Isolate* isolate) {
4707 typedef internal::Object* S;
4708 typedef internal::Internals I;
4709 if (!I::IsInitialized(isolate)) return Null();
4710 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4711 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4712}
4713
4714
4715Handle<Boolean> True(Isolate* isolate) {
4716 typedef internal::Object* S;
4717 typedef internal::Internals I;
4718 if (!I::IsInitialized(isolate)) return True();
4719 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4720 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4721}
4722
4723
4724Handle<Boolean> False(Isolate* isolate) {
4725 typedef internal::Object* S;
4726 typedef internal::Internals I;
4727 if (!I::IsInitialized(isolate)) return False();
4728 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4729 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4730}
4731
4732
4733void Isolate::SetData(void* data) {
4734 typedef internal::Internals I;
4735 I::SetEmbedderData(this, data);
4736}
4737
4738
4739void* Isolate::GetData() {
4740 typedef internal::Internals I;
4741 return I::GetEmbedderData(this);
4742}
4743
4744
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004745/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004746 * \example shell.cc
4747 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004748 * command-line and executes them.
4749 */
4750
4751
4752/**
4753 * \example process.cc
4754 */
4755
4756
4757} // namespace v8
4758
4759
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004760#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004761#undef TYPE_CHECK
4762
4763
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004764#endif // V8_H_