blob: 92d928b328822aaec30f9b2dc421aa2a42050d56 [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000065// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000066#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
67 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070#else
71#define V8EXPORT
72#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000075#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076
77#endif // _WIN32
78
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000080 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 */
82namespace v8 {
83
84class Context;
85class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000086class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087class Value;
88class Utils;
89class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000090class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091class Object;
92class Array;
93class Int32;
94class Uint32;
95class External;
96class Primitive;
97class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000098class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099class Integer;
100class Function;
101class Date;
102class ImplementationUtilities;
103class Signature;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000104class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105template <class T> class Handle;
106template <class T> class Local;
107template <class T> class Persistent;
108class FunctionTemplate;
109class ObjectTemplate;
110class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000111class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000112class StackTrace;
113class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000114class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000116namespace internal {
117
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000118class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000119class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000120class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121class HeapObject;
122class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000123}
124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000126// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127
128
129/**
130 * A weak reference callback function.
131 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000132 * This callback should either explicitly invoke Dispose on |object| if
133 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
134 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 * \param object the weak global object to be reclaimed by the garbage collector
136 * \param parameter the value passed in when making the weak global object
137 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000138typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 void* parameter);
140
141
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000142// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000144#define TYPE_CHECK(T, S) \
145 while (false) { \
146 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 }
148
149/**
150 * An object reference managed by the v8 garbage collector.
151 *
152 * All objects returned from v8 have to be tracked by the garbage
153 * collector so that it knows that the objects are still alive. Also,
154 * because the garbage collector may move objects, it is unsafe to
155 * point directly to an object. Instead, all objects are stored in
156 * handles which are known by the garbage collector and updated
157 * whenever an object moves. Handles should always be passed by value
158 * (except in cases like out-parameters) and they should never be
159 * allocated on the heap.
160 *
161 * There are two types of handles: local and persistent handles.
162 * Local handles are light-weight and transient and typically used in
163 * local operations. They are managed by HandleScopes. Persistent
164 * handles can be used when storing objects across several independent
165 * operations and have to be explicitly deallocated when they're no
166 * longer used.
167 *
168 * It is safe to extract the object stored in the handle by
169 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000170 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 * behind the scenes and the same rules apply to these values as to
172 * their handles.
173 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000174template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 /**
177 * Creates an empty handle.
178 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000179 inline Handle() : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
181 /**
182 * Creates a new handle for the specified value.
183 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000184 inline explicit Handle(T* val) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185
186 /**
187 * Creates a handle for the contents of the specified handle. This
188 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000189 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000191 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192 * between compatible handles, for instance assigning a
193 * Handle<String> to a variable declared as Handle<Value>, is legal
194 * because String is a subclass of Value.
195 */
196 template <class S> inline Handle(Handle<S> that)
197 : val_(reinterpret_cast<T*>(*that)) {
198 /**
199 * This check fails when trying to convert between incompatible
200 * handles. For example, converting from a Handle<String> to a
201 * Handle<Number>.
202 */
203 TYPE_CHECK(T, S);
204 }
205
206 /**
207 * Returns true if the handle is empty.
208 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000209 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 /**
212 * Sets the handle to be empty. IsEmpty() will then return true.
213 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000214 inline void Clear() { val_ = 0; }
215
216 inline T* operator->() const { return val_; }
217
218 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
220 /**
221 * Checks whether two handles are the same.
222 * Returns true if both are empty, or if the objects
223 * to which they refer are identical.
224 * The handles' references are not checked.
225 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000226 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000227 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
228 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 if (a == 0) return b == 0;
230 if (b == 0) return false;
231 return *a == *b;
232 }
233
234 /**
235 * Checks whether two handles are different.
236 * Returns true if only one of the handles is empty, or if
237 * the objects to which they refer are different.
238 * The handles' references are not checked.
239 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000240 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 return !operator==(that);
242 }
243
244 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000245#ifdef V8_ENABLE_CHECKS
246 // If we're going to perform the type check then we have to check
247 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000249#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 return Handle<T>(T::Cast(*that));
251 }
252
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000253 template <class S> inline Handle<S> As() {
254 return Handle<S>::Cast(*this);
255 }
256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 private:
258 T* val_;
259};
260
261
262/**
263 * A light-weight stack-allocated object handle. All operations
264 * that return objects from within v8 return them in local handles. They
265 * are created within HandleScopes, and all local handles allocated within a
266 * handle scope are destroyed when the handle scope is destroyed. Hence it
267 * is not necessary to explicitly deallocate local handles.
268 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000269template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000271 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 template <class S> inline Local(Local<S> that)
273 : Handle<T>(reinterpret_cast<T*>(*that)) {
274 /**
275 * This check fails when trying to convert between incompatible
276 * handles. For example, converting from a Handle<String> to a
277 * Handle<Number>.
278 */
279 TYPE_CHECK(T, S);
280 }
281 template <class S> inline Local(S* that) : Handle<T>(that) { }
282 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000283#ifdef V8_ENABLE_CHECKS
284 // If we're going to perform the type check then we have to check
285 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000287#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 return Local<T>(T::Cast(*that));
289 }
290
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000291 template <class S> inline Local<S> As() {
292 return Local<S>::Cast(*this);
293 }
294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 /** Create a local handle for the content of another handle.
296 * The referee is kept alive by the local handle even when
297 * the original handle is destroyed/disposed.
298 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000299 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300};
301
302
303/**
304 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000305 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306 * allocated, a Persistent handle remains valid until it is explicitly
307 * disposed.
308 *
309 * A persistent handle contains a reference to a storage cell within
310 * the v8 engine which holds an object value and which is updated by
311 * the garbage collector whenever the object is moved. A new storage
312 * cell can be created using Persistent::New and existing handles can
313 * be disposed using Persistent::Dispose. Since persistent handles
314 * are passed by value you may have many persistent handle objects
315 * that point to the same storage cell. For instance, if you pass a
316 * persistent handle as an argument to a function you will not get two
317 * different storage cells but rather two references to the same
318 * storage cell.
319 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000320template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 /**
323 * Creates an empty persistent handle that doesn't point to any
324 * storage cell.
325 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000326 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327
328 /**
329 * Creates a persistent handle for the same storage cell as the
330 * specified handle. This constructor allows you to pass persistent
331 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000332 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000334 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 * between compatible persistent handles, for instance assigning a
336 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000337 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 */
339 template <class S> inline Persistent(Persistent<S> that)
340 : Handle<T>(reinterpret_cast<T*>(*that)) {
341 /**
342 * This check fails when trying to convert between incompatible
343 * handles. For example, converting from a Handle<String> to a
344 * Handle<Number>.
345 */
346 TYPE_CHECK(T, S);
347 }
348
349 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
350
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000351 /**
352 * "Casts" a plain handle which is known to be a persistent handle
353 * to a persistent handle.
354 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 template <class S> explicit inline Persistent(Handle<S> that)
356 : Handle<T>(*that) { }
357
358 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000359#ifdef V8_ENABLE_CHECKS
360 // If we're going to perform the type check then we have to check
361 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000363#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 return Persistent<T>(T::Cast(*that));
365 }
366
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000367 template <class S> inline Persistent<S> As() {
368 return Persistent<S>::Cast(*this);
369 }
370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000372 * Creates a new persistent handle for an existing local or
373 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000375 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 /**
378 * Releases the storage cell referenced by this persistent handle.
379 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000380 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 * cell remain and IsEmpty will still return false.
382 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000383 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385 /**
386 * Make the reference to this object weak. When only weak handles
387 * refer to the object, the garbage collector will perform a
388 * callback to the given V8::WeakReferenceCallback function, passing
389 * it the object reference and the given parameters.
390 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000391 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392
393 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000394 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395
396 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000397 * Marks the reference to this object independent. Garbage collector
398 * is free to ignore any object groups containing this object.
399 * Weak callback for an independent handle should not
400 * assume that it will be preceded by a global GC prologue callback
401 * or followed by a global GC epilogue callback.
402 */
403 inline void MarkIndependent();
404
405 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 *Checks if the handle holds the only reference to an object.
407 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000408 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409
410 /**
411 * Returns true if the handle's reference is weak.
412 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000413 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000415 /**
416 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
417 * interface description in v8-profiler.h for details.
418 */
419 inline void SetWrapperClassId(uint16_t class_id);
420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421 private:
422 friend class ImplementationUtilities;
423 friend class ObjectTemplate;
424};
425
426
v8.team.kasperl727e9952008-09-02 14:56:44 +0000427 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 * A stack-allocated class that governs a number of local handles.
429 * After a handle scope has been created, all local handles will be
430 * allocated within that handle scope until either the handle scope is
431 * deleted or another handle scope is created. If there is already a
432 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000433 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 * new handles will again be allocated in the original handle scope.
435 *
436 * After the handle scope of a local handle has been deleted the
437 * garbage collector will no longer track the object stored in the
438 * handle and may deallocate it. The behavior of accessing a handle
439 * for which the handle scope has been deleted is undefined.
440 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000441class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000443 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000445 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446
447 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 * Closes the handle scope and returns the value as a handle in the
449 * previous scope, which is the new current scope after the call.
450 */
451 template <class T> Local<T> Close(Handle<T> value);
452
453 /**
454 * Counts the number of allocated handles.
455 */
456 static int NumberOfHandles();
457
458 /**
459 * Creates a new handle with the given value.
460 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000461 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462 // Faster version, uses HeapObject to obtain the current Isolate.
463 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464
465 private:
466 // Make it impossible to create heap-allocated or illegal handle
467 // scopes by disallowing certain operations.
468 HandleScope(const HandleScope&);
469 void operator=(const HandleScope&);
470 void* operator new(size_t size);
471 void operator delete(void*, size_t);
472
ager@chromium.org3811b432009-10-28 14:53:37 +0000473 // This Data class is accessible internally as HandleScopeData through a
474 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000475 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000477 internal::Object** next;
478 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000479 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000482 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 }
484 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000485
lrn@chromium.org303ada72010-10-27 09:33:13 +0000486 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000488 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000489 internal::Object** prev_next_;
490 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000492 // Allow for the active closing of HandleScopes which allows to pass a handle
493 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000495 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 friend class ImplementationUtilities;
498};
499
500
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000501// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502
503
504/**
505 * The superclass of values and API object templates.
506 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000507class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 private:
509 Data();
510};
511
512
513/**
514 * Pre-compilation data that can be associated with a script. This
515 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000516 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000517 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000519class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 public:
521 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000522
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000523 /**
524 * Pre-compiles the specified script (context-independent).
525 *
526 * \param input Pointer to UTF-8 script source code.
527 * \param length Length of UTF-8 script source code.
528 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000531 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000532 * Pre-compiles the specified script (context-independent).
533 *
534 * NOTE: Pre-compilation using this method cannot happen on another thread
535 * without using Lockers.
536 *
537 * \param source Script source code.
538 */
539 static ScriptData* PreCompile(Handle<String> source);
540
541 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000542 * Load previous pre-compilation data.
543 *
544 * \param data Pointer to data returned by a call to Data() of a previous
545 * ScriptData. Ownership is not transferred.
546 * \param length Length of data.
547 */
548 static ScriptData* New(const char* data, int length);
549
550 /**
551 * Returns the length of Data().
552 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000554
555 /**
556 * Returns a serialized representation of this ScriptData that can later be
557 * passed to New(). NOTE: Serialized data is platform-dependent.
558 */
559 virtual const char* Data() = 0;
560
561 /**
562 * Returns true if the source code could not be parsed.
563 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000564 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565};
566
567
568/**
569 * The origin, within a file, of a script.
570 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000571class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000573 inline ScriptOrigin(
574 Handle<Value> resource_name,
575 Handle<Integer> resource_line_offset = Handle<Integer>(),
576 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 : resource_name_(resource_name),
578 resource_line_offset_(resource_line_offset),
579 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000580 inline Handle<Value> ResourceName() const;
581 inline Handle<Integer> ResourceLineOffset() const;
582 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000584 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585 Handle<Integer> resource_line_offset_;
586 Handle<Integer> resource_column_offset_;
587};
588
589
590/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000591 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000593class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000596 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000597 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000598 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000599 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000600 * when New() returns
601 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
602 * using pre_data speeds compilation if it's done multiple times.
603 * Owned by caller, no references are kept when New() returns.
604 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000605 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000606 * available to compile event handlers.
607 * \return Compiled script object (context independent; when run it
608 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000610 static Local<Script> New(Handle<String> source,
611 ScriptOrigin* origin = NULL,
612 ScriptData* pre_data = NULL,
613 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614
mads.s.agercbaa0602008-08-14 13:41:48 +0000615 /**
616 * Compiles the specified script using the specified file name
617 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000618 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000619 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000620 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000621 * as the script's origin.
622 * \return Compiled script object (context independent; when run it
623 * will use the currently entered context).
624 */
625 static Local<Script> New(Handle<String> source,
626 Handle<Value> file_name);
627
628 /**
629 * Compiles the specified script (bound to current context).
630 *
631 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000632 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000633 * when Compile() returns
634 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
635 * using pre_data speeds compilation if it's done multiple times.
636 * Owned by caller, no references are kept when Compile() returns.
637 * \param script_data Arbitrary data associated with script. Using
638 * this has same effect as calling SetData(), but makes data available
639 * earlier (i.e. to compile event handlers).
640 * \return Compiled script object, bound to the context that was active
641 * when this function was called. When run it will always use this
642 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000643 */
644 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000645 ScriptOrigin* origin = NULL,
646 ScriptData* pre_data = NULL,
647 Handle<String> script_data = Handle<String>());
648
649 /**
650 * Compiles the specified script using the specified file name
651 * object (typically a string) as the script's origin.
652 *
653 * \param source Script source code.
654 * \param file_name File name to use as script's origin
655 * \param script_data Arbitrary data associated with script. Using
656 * this has same effect as calling SetData(), but makes data available
657 * earlier (i.e. to compile event handlers).
658 * \return Compiled script object, bound to the context that was active
659 * when this function was called. When run it will always use this
660 * context.
661 */
662 static Local<Script> Compile(Handle<String> source,
663 Handle<Value> file_name,
664 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000665
v8.team.kasperl727e9952008-09-02 14:56:44 +0000666 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000667 * Runs the script returning the resulting value. If the script is
668 * context independent (created using ::New) it will be run in the
669 * currently entered context. If it is context specific (created
670 * using ::Compile) it will be run in the context in which it was
671 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000672 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000674
675 /**
676 * Returns the script id value.
677 */
678 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000679
680 /**
681 * Associate an additional data object with the script. This is mainly used
682 * with the debugger as this data object is only available through the
683 * debugger API.
684 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000685 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686};
687
688
689/**
690 * An error message.
691 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000692class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000694 Local<String> Get() const;
695 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000697 /**
698 * Returns the resource name for the script from where the function causing
699 * the error originates.
700 */
ager@chromium.org32912102009-01-16 10:38:43 +0000701 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000703 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000704 * Returns the resource data for the script from where the function causing
705 * the error originates.
706 */
707 Handle<Value> GetScriptData() const;
708
709 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000710 * Exception stack trace. By default stack traces are not captured for
711 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
712 * to change this option.
713 */
714 Handle<StackTrace> GetStackTrace() const;
715
716 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000717 * Returns the number, 1-based, of the line where the error occurred.
718 */
ager@chromium.org32912102009-01-16 10:38:43 +0000719 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000721 /**
722 * Returns the index within the script of the first character where
723 * the error occurred.
724 */
ager@chromium.org32912102009-01-16 10:38:43 +0000725 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000726
727 /**
728 * Returns the index within the script of the last character where
729 * the error occurred.
730 */
ager@chromium.org32912102009-01-16 10:38:43 +0000731 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000732
733 /**
734 * Returns the index within the line of the first character where
735 * the error occurred.
736 */
ager@chromium.org32912102009-01-16 10:38:43 +0000737 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000738
739 /**
740 * Returns the index within the line of the last character where
741 * the error occurred.
742 */
ager@chromium.org32912102009-01-16 10:38:43 +0000743 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000744
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 // TODO(1245381): Print to a string instead of on a FILE.
746 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000747
748 static const int kNoLineNumberInfo = 0;
749 static const int kNoColumnInfo = 0;
750};
751
752
753/**
754 * Representation of a JavaScript stack trace. The information collected is a
755 * snapshot of the execution stack and the information remains valid after
756 * execution continues.
757 */
758class V8EXPORT StackTrace {
759 public:
760 /**
761 * Flags that determine what information is placed captured for each
762 * StackFrame when grabbing the current stack trace.
763 */
764 enum StackTraceOptions {
765 kLineNumber = 1,
766 kColumnOffset = 1 << 1 | kLineNumber,
767 kScriptName = 1 << 2,
768 kFunctionName = 1 << 3,
769 kIsEval = 1 << 4,
770 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000771 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000772 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000773 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000774 };
775
776 /**
777 * Returns a StackFrame at a particular index.
778 */
779 Local<StackFrame> GetFrame(uint32_t index) const;
780
781 /**
782 * Returns the number of StackFrames.
783 */
784 int GetFrameCount() const;
785
786 /**
787 * Returns StackTrace as a v8::Array that contains StackFrame objects.
788 */
789 Local<Array> AsArray();
790
791 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000792 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000793 *
794 * \param frame_limit The maximum number of stack frames we want to capture.
795 * \param options Enumerates the set of things we will capture for each
796 * StackFrame.
797 */
798 static Local<StackTrace> CurrentStackTrace(
799 int frame_limit,
800 StackTraceOptions options = kOverview);
801};
802
803
804/**
805 * A single JavaScript stack frame.
806 */
807class V8EXPORT StackFrame {
808 public:
809 /**
810 * Returns the number, 1-based, of the line for the associate function call.
811 * This method will return Message::kNoLineNumberInfo if it is unable to
812 * retrieve the line number, or if kLineNumber was not passed as an option
813 * when capturing the StackTrace.
814 */
815 int GetLineNumber() const;
816
817 /**
818 * Returns the 1-based column offset on the line for the associated function
819 * call.
820 * This method will return Message::kNoColumnInfo if it is unable to retrieve
821 * the column number, or if kColumnOffset was not passed as an option when
822 * capturing the StackTrace.
823 */
824 int GetColumn() const;
825
826 /**
827 * Returns the name of the resource that contains the script for the
828 * function for this StackFrame.
829 */
830 Local<String> GetScriptName() const;
831
832 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000833 * Returns the name of the resource that contains the script for the
834 * function for this StackFrame or sourceURL value if the script name
835 * is undefined and its source ends with //@ sourceURL=... string.
836 */
837 Local<String> GetScriptNameOrSourceURL() const;
838
839 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000840 * Returns the name of the function associated with this stack frame.
841 */
842 Local<String> GetFunctionName() const;
843
844 /**
845 * Returns whether or not the associated function is compiled via a call to
846 * eval().
847 */
848 bool IsEval() const;
849
850 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000851 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000852 * constructor via "new".
853 */
854 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855};
856
857
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000858// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859
860
861/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000862 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000864class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 /**
867 * Returns true if this value is the undefined value. See ECMA-262
868 * 4.3.10.
869 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000870 inline bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871
872 /**
873 * Returns true if this value is the null value. See ECMA-262
874 * 4.3.11.
875 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000876 inline bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877
878 /**
879 * Returns true if this value is true.
880 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000881 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882
883 /**
884 * Returns true if this value is false.
885 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000886 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887
888 /**
889 * Returns true if this value is an instance of the String type.
890 * See ECMA-262 8.4.
891 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000892 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893
894 /**
895 * Returns true if this value is a function.
896 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000897 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
899 /**
900 * Returns true if this value is an array.
901 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000902 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
v8.team.kasperl727e9952008-09-02 14:56:44 +0000904 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 * Returns true if this value is an object.
906 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000907 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908
v8.team.kasperl727e9952008-09-02 14:56:44 +0000909 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910 * Returns true if this value is boolean.
911 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000912 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000913
v8.team.kasperl727e9952008-09-02 14:56:44 +0000914 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 * Returns true if this value is a number.
916 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000917 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918
v8.team.kasperl727e9952008-09-02 14:56:44 +0000919 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 * Returns true if this value is external.
921 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000922 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923
v8.team.kasperl727e9952008-09-02 14:56:44 +0000924 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925 * Returns true if this value is a 32-bit signed integer.
926 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000927 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000929 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000930 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000931 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000932 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000933
934 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000935 * Returns true if this value is a Date.
936 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000937 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000938
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000939 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000940 * Returns true if this value is a Boolean object.
941 */
942 V8EXPORT bool IsBooleanObject() const;
943
944 /**
945 * Returns true if this value is a Number object.
946 */
947 V8EXPORT bool IsNumberObject() const;
948
949 /**
950 * Returns true if this value is a String object.
951 */
952 V8EXPORT bool IsStringObject() const;
953
954 /**
955 * Returns true if this value is a NativeError.
956 */
957 V8EXPORT bool IsNativeError() const;
958
959 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000960 * Returns true if this value is a RegExp.
961 */
962 V8EXPORT bool IsRegExp() const;
963
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000964 V8EXPORT Local<Boolean> ToBoolean() const;
965 V8EXPORT Local<Number> ToNumber() const;
966 V8EXPORT Local<String> ToString() const;
967 V8EXPORT Local<String> ToDetailString() const;
968 V8EXPORT Local<Object> ToObject() const;
969 V8EXPORT Local<Integer> ToInteger() const;
970 V8EXPORT Local<Uint32> ToUint32() const;
971 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972
973 /**
974 * Attempts to convert a string to an array index.
975 * Returns an empty handle if the conversion fails.
976 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000977 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000979 V8EXPORT bool BooleanValue() const;
980 V8EXPORT double NumberValue() const;
981 V8EXPORT int64_t IntegerValue() const;
982 V8EXPORT uint32_t Uint32Value() const;
983 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984
985 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000986 V8EXPORT bool Equals(Handle<Value> that) const;
987 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000988
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000989 private:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000990 inline bool QuickIsUndefined() const;
991 inline bool QuickIsNull() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000992 inline bool QuickIsString() const;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000993 V8EXPORT bool FullIsUndefined() const;
994 V8EXPORT bool FullIsNull() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000995 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996};
997
998
999/**
1000 * The superclass of primitive values. See ECMA-262 4.3.2.
1001 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001002class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003
1004
1005/**
1006 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1007 * or false value.
1008 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001009class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001011 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012 static inline Handle<Boolean> New(bool value);
1013};
1014
1015
1016/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001017 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001019class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001021 enum Encoding {
1022 UNKNOWN_ENCODING = 0x1,
1023 TWO_BYTE_ENCODING = 0x0,
1024 ASCII_ENCODING = 0x4
1025 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001026 /**
1027 * Returns the number of characters in this string.
1028 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001029 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030
v8.team.kasperl727e9952008-09-02 14:56:44 +00001031 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001032 * Returns the number of bytes in the UTF-8 encoded
1033 * representation of this string.
1034 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001035 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001036
1037 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001038 * A fast conservative check for non-ASCII characters. May
1039 * return true even for ASCII strings, but if it returns
1040 * false you can be sure that all characters are in the range
1041 * 0-127.
1042 */
1043 V8EXPORT bool MayContainNonAscii() const;
1044
1045 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001046 * Write the contents of the string to an external buffer.
1047 * If no arguments are given, expects the buffer to be large
1048 * enough to hold the entire string and NULL terminator. Copies
1049 * the contents of the string and the NULL terminator into the
1050 * buffer.
1051 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001052 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1053 * before the end of the buffer.
1054 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001055 * Copies up to length characters into the output buffer.
1056 * Only null-terminates if there is enough space in the buffer.
1057 *
1058 * \param buffer The buffer into which the string will be copied.
1059 * \param start The starting position within the string at which
1060 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001061 * \param length The number of characters to copy from the string. For
1062 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001063 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001064 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001065 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001066 * \return The number of characters copied to the buffer excluding the null
1067 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001068 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001069 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001070 enum WriteOptions {
1071 NO_OPTIONS = 0,
1072 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001073 NO_NULL_TERMINATION = 2,
1074 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001075 };
1076
lrn@chromium.org34e60782011-09-15 07:25:40 +00001077 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001078 V8EXPORT int Write(uint16_t* buffer,
1079 int start = 0,
1080 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001081 int options = NO_OPTIONS) const;
1082 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001083 V8EXPORT int WriteAscii(char* buffer,
1084 int start = 0,
1085 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001086 int options = NO_OPTIONS) const;
1087 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001088 V8EXPORT int WriteUtf8(char* buffer,
1089 int length = -1,
1090 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001091 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092
v8.team.kasperl727e9952008-09-02 14:56:44 +00001093 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001094 * A zero length string.
1095 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001096 V8EXPORT static v8::Local<v8::String> Empty();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001097 inline static v8::Local<v8::String> Empty(Isolate* isolate);
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001098
1099 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001100 * Returns true if the string is external
1101 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001102 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103
v8.team.kasperl727e9952008-09-02 14:56:44 +00001104 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001105 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001106 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001107 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001108
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001109 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001110 public:
1111 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001112
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001113 protected:
1114 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001115
1116 /**
1117 * Internally V8 will call this Dispose method when the external string
1118 * resource is no longer needed. The default implementation will use the
1119 * delete operator. This method can be overridden in subclasses to
1120 * control how allocated external string resources are disposed.
1121 */
1122 virtual void Dispose() { delete this; }
1123
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001124 private:
1125 // Disallow copying and assigning.
1126 ExternalStringResourceBase(const ExternalStringResourceBase&);
1127 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001128
1129 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001130 };
1131
v8.team.kasperl727e9952008-09-02 14:56:44 +00001132 /**
1133 * An ExternalStringResource is a wrapper around a two-byte string
1134 * buffer that resides outside V8's heap. Implement an
1135 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001136 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001137 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001138 class V8EXPORT ExternalStringResource
1139 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 public:
1141 /**
1142 * Override the destructor to manage the life cycle of the underlying
1143 * buffer.
1144 */
1145 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001146
1147 /**
1148 * The string data from the underlying buffer.
1149 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001151
1152 /**
1153 * The length of the string. That is, the number of two-byte characters.
1154 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001156
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 protected:
1158 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 };
1160
1161 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001162 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001163 * string buffer that resides outside V8's heap. Implement an
1164 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001165 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001166 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001167 * UTF-8, which would require special treatment internally in the
1168 * engine and, in the case of UTF-8, do not allow efficient indexing.
1169 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001170 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001172 class V8EXPORT ExternalAsciiStringResource
1173 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 public:
1175 /**
1176 * Override the destructor to manage the life cycle of the underlying
1177 * buffer.
1178 */
1179 virtual ~ExternalAsciiStringResource() {}
1180 /** The string data from the underlying buffer.*/
1181 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001182 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 virtual size_t length() const = 0;
1184 protected:
1185 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 };
1187
1188 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001189 * If the string is an external string, return the ExternalStringResourceBase
1190 * regardless of the encoding, otherwise return NULL. The encoding of the
1191 * string is returned in encoding_out.
1192 */
1193 inline ExternalStringResourceBase* GetExternalStringResourceBase(
1194 Encoding* encoding_out) const;
1195
1196 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001197 * Get the ExternalStringResource for an external string. Returns
1198 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001200 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201
1202 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001203 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001204 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001206 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1207 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001209 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210
1211 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001212 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001214 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215 * be careful to supply the length parameter.
1216 * If it is not given, the function calls
1217 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001218 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001219 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001220 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221
lrn@chromium.org34e60782011-09-15 07:25:40 +00001222 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001223 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224
1225 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001226 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227
v8.team.kasperl727e9952008-09-02 14:56:44 +00001228 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001229 * Creates a new string by concatenating the left and the right strings
1230 * passed in as parameters.
1231 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001232 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001233 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001234
1235 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001236 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001237 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001238 * resource will be disposed by calling its Dispose method. The caller of
1239 * this function should not otherwise delete or modify the resource. Neither
1240 * should the underlying buffer be deallocated or modified except through the
1241 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001242 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001243 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001244
ager@chromium.org6f10e412009-02-13 10:11:16 +00001245 /**
1246 * Associate an external string resource with this string by transforming it
1247 * in place so that existing references to this string in the JavaScript heap
1248 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001249 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001250 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001251 * The string is not modified if the operation fails. See NewExternal for
1252 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001253 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001254 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255
v8.team.kasperl727e9952008-09-02 14:56:44 +00001256 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001257 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001258 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001259 * resource will be disposed by calling its Dispose method. The caller of
1260 * this function should not otherwise delete or modify the resource. Neither
1261 * should the underlying buffer be deallocated or modified except through the
1262 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001263 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001264 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001265
ager@chromium.org6f10e412009-02-13 10:11:16 +00001266 /**
1267 * Associate an external string resource with this string by transforming it
1268 * in place so that existing references to this string in the JavaScript heap
1269 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001270 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001271 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001272 * The string is not modified if the operation fails. See NewExternal for
1273 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001274 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001275 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001277 /**
1278 * Returns true if this string can be made external.
1279 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001280 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001281
lrn@chromium.org34e60782011-09-15 07:25:40 +00001282 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001283 V8EXPORT static Local<String> NewUndetectable(const char* data,
1284 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285
lrn@chromium.org34e60782011-09-15 07:25:40 +00001286 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001287 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1288 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289
1290 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001291 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001292 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001293 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001294 * then the length() method returns 0 and the * operator returns
1295 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001296 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001297 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001298 public:
1299 explicit Utf8Value(Handle<v8::Value> obj);
1300 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001301 char* operator*() { return str_; }
1302 const char* operator*() const { return str_; }
1303 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001304 private:
1305 char* str_;
1306 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001307
1308 // Disallow copying and assigning.
1309 Utf8Value(const Utf8Value&);
1310 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001311 };
1312
1313 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001314 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001316 * If conversion to a string fails (eg. due to an exception in the toString()
1317 * method of the object) then the length() method returns 0 and the * operator
1318 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001320 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 public:
1322 explicit AsciiValue(Handle<v8::Value> obj);
1323 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001324 char* operator*() { return str_; }
1325 const char* operator*() const { return str_; }
1326 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 private:
1328 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001329 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001330
1331 // Disallow copying and assigning.
1332 AsciiValue(const AsciiValue&);
1333 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 };
1335
1336 /**
1337 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001338 * If conversion to a string fails (eg. due to an exception in the toString()
1339 * method of the object) then the length() method returns 0 and the * operator
1340 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001342 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 public:
1344 explicit Value(Handle<v8::Value> obj);
1345 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001346 uint16_t* operator*() { return str_; }
1347 const uint16_t* operator*() const { return str_; }
1348 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 private:
1350 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001351 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001352
1353 // Disallow copying and assigning.
1354 Value(const Value&);
1355 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001357
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001358 private:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001359 V8EXPORT void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1360 Encoding encoding) const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001361 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1362 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363};
1364
1365
1366/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001367 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001369class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001371 V8EXPORT double Value() const;
1372 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001373 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001375 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001376 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377};
1378
1379
1380/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001381 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001383class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001385 V8EXPORT static Local<Integer> New(int32_t value);
1386 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1387 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001388 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001390 V8EXPORT Integer();
1391 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392};
1393
1394
1395/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001396 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001398class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001400 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001402 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403};
1404
1405
1406/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001407 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001409class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001411 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001413 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414};
1415
1416
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417enum PropertyAttribute {
1418 None = 0,
1419 ReadOnly = 1 << 0,
1420 DontEnum = 1 << 1,
1421 DontDelete = 1 << 2
1422};
1423
ager@chromium.org3811b432009-10-28 14:53:37 +00001424enum ExternalArrayType {
1425 kExternalByteArray = 1,
1426 kExternalUnsignedByteArray,
1427 kExternalShortArray,
1428 kExternalUnsignedShortArray,
1429 kExternalIntArray,
1430 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001431 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001432 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001433 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001434};
1435
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001437 * Accessor[Getter|Setter] are used as callback functions when
1438 * setting|getting a particular property. See Object and ObjectTemplate's
1439 * method SetAccessor.
1440 */
1441typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1442 const AccessorInfo& info);
1443
1444
1445typedef void (*AccessorSetter)(Local<String> property,
1446 Local<Value> value,
1447 const AccessorInfo& info);
1448
1449
1450/**
1451 * Access control specifications.
1452 *
1453 * Some accessors should be accessible across contexts. These
1454 * accessors have an explicit access control parameter which specifies
1455 * the kind of cross-context access that should be allowed.
1456 *
1457 * Additionally, for security, accessors can prohibit overwriting by
1458 * accessors defined in JavaScript. For objects that have such
1459 * accessors either locally or in their prototype chain it is not
1460 * possible to overwrite the accessor by using __defineGetter__ or
1461 * __defineSetter__ from JavaScript code.
1462 */
1463enum AccessControl {
1464 DEFAULT = 0,
1465 ALL_CAN_READ = 1,
1466 ALL_CAN_WRITE = 1 << 1,
1467 PROHIBITS_OVERWRITING = 1 << 2
1468};
1469
1470
1471/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001472 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001474class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001476 V8EXPORT bool Set(Handle<Value> key,
1477 Handle<Value> value,
1478 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001479
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001480 V8EXPORT bool Set(uint32_t index,
1481 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001482
ager@chromium.orge2902be2009-06-08 12:21:35 +00001483 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001484 // overriding accessors or read-only properties.
1485 //
1486 // Note that if the object has an interceptor the property will be set
1487 // locally, but since the interceptor takes precedence the local property
1488 // will only be returned if the interceptor doesn't return a value.
1489 //
1490 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001491 V8EXPORT bool ForceSet(Handle<Value> key,
1492 Handle<Value> value,
1493 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001494
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001495 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001497 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001498
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001499 /**
1500 * Gets the property attributes of a property which can be None or
1501 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1502 * None when the property doesn't exist.
1503 */
1504 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1505
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 // TODO(1245389): Replace the type-specific versions of these
1507 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001508 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001509
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001510 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001511
1512 // Delete a property on this object bypassing interceptors and
1513 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001514 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001515
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001516 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001517
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001518 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001519
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001520 V8EXPORT bool SetAccessor(Handle<String> name,
1521 AccessorGetter getter,
1522 AccessorSetter setter = 0,
1523 Handle<Value> data = Handle<Value>(),
1524 AccessControl settings = DEFAULT,
1525 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001528 * Returns an array containing the names of the enumerable properties
1529 * of this object, including properties from prototype objects. The
1530 * array returned by this method contains the same values as would
1531 * be enumerated by a for-in statement over this object.
1532 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001533 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001534
1535 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001536 * This function has the same functionality as GetPropertyNames but
1537 * the returned array doesn't contain the names of properties from
1538 * prototype objects.
1539 */
1540 V8EXPORT Local<Array> GetOwnPropertyNames();
1541
1542 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543 * Get the prototype object. This does not skip objects marked to
1544 * be skipped by __proto__ and it does not consult the security
1545 * handler.
1546 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001547 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548
1549 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001550 * Set the prototype object. This does not skip objects marked to
1551 * be skipped by __proto__ and it does not consult the security
1552 * handler.
1553 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001554 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001555
1556 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001557 * Finds an instance of the given function template in the prototype
1558 * chain.
1559 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001560 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1561 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001562
1563 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564 * Call builtin Object.prototype.toString on this object.
1565 * This is different from Value::ToString() that may call
1566 * user-defined toString function. This one does not.
1567 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001568 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001570 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001571 * Returns the function invoked as a constructor for this object.
1572 * May be the null value.
1573 */
1574 V8EXPORT Local<Value> GetConstructor();
1575
1576 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001577 * Returns the name of the function invoked as a constructor for this object.
1578 */
1579 V8EXPORT Local<String> GetConstructorName();
1580
kasper.lund212ac232008-07-16 07:07:30 +00001581 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001582 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001583 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001584 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001585 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001586 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001588 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001589 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001590
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001591 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001592 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001593
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001595 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001596 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1597 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1598 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599
1600 /**
1601 * If result.IsEmpty() no real property was located in the prototype chain.
1602 * This means interceptors in the prototype chain are not called.
1603 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001604 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1605 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001606
1607 /**
1608 * If result.IsEmpty() no real property was located on the object or
1609 * in the prototype chain.
1610 * This means interceptors in the prototype chain are not called.
1611 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001612 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613
1614 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001615 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616
kasper.lund212ac232008-07-16 07:07:30 +00001617 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001618 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001620 /**
1621 * Turns on access check on the object if the object is an instance of
1622 * a template that has access check callbacks. If an object has no
1623 * access check info, the object cannot be accessed by anyone.
1624 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001625 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001626
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001627 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001628 * Returns the identity hash for this object. The current implementation
1629 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001630 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001631 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001632 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001633 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001634 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001635
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001636 /**
1637 * Access hidden properties on JavaScript objects. These properties are
1638 * hidden from the executing JavaScript and only accessible through the V8
1639 * C++ API. Hidden properties introduced by V8 internally (for example the
1640 * identity hash) are prefixed with "v8::".
1641 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001642 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1643 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1644 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001645
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001646 /**
1647 * Returns true if this is an instance of an api function (one
1648 * created from a function created from a function template) and has
1649 * been modified since it was created. Note that this method is
1650 * conservative and may return true for objects that haven't actually
1651 * been modified.
1652 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001653 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001654
1655 /**
1656 * Clone this object with a fast but shallow copy. Values will point
1657 * to the same values as the original object.
1658 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001659 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001661 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001662 * Returns the context in which the object was created.
1663 */
1664 V8EXPORT Local<Context> CreationContext();
1665
1666 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001667 * Set the backing store of the indexed properties to be managed by the
1668 * embedding layer. Access to the indexed properties will follow the rules
1669 * spelled out in CanvasPixelArray.
1670 * Note: The embedding program still owns the data and needs to ensure that
1671 * the backing store is preserved while V8 has a reference.
1672 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001673 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001674 V8EXPORT bool HasIndexedPropertiesInPixelData();
1675 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1676 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001677
ager@chromium.org3811b432009-10-28 14:53:37 +00001678 /**
1679 * Set the backing store of the indexed properties to be managed by the
1680 * embedding layer. Access to the indexed properties will follow the rules
1681 * spelled out for the CanvasArray subtypes in the WebGL specification.
1682 * Note: The embedding program still owns the data and needs to ensure that
1683 * the backing store is preserved while V8 has a reference.
1684 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001685 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1686 void* data,
1687 ExternalArrayType array_type,
1688 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001689 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1690 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1691 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1692 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001693
lrn@chromium.org1c092762011-05-09 09:42:16 +00001694 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001695 * Checks whether a callback is set by the
1696 * ObjectTemplate::SetCallAsFunctionHandler method.
1697 * When an Object is callable this method returns true.
1698 */
1699 V8EXPORT bool IsCallable();
1700
1701 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001702 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001703 * ObjectTemplate::SetCallAsFunctionHandler method.
1704 */
1705 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1706 int argc,
1707 Handle<Value> argv[]);
1708
1709 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001710 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001711 * ObjectTemplate::SetCallAsFunctionHandler method.
1712 * Note: This method behaves like the Function::NewInstance method.
1713 */
1714 V8EXPORT Local<Value> CallAsConstructor(int argc,
1715 Handle<Value> argv[]);
1716
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001717 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001718 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001719
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001721 V8EXPORT Object();
1722 V8EXPORT static void CheckCast(Value* obj);
1723 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1724 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001725
1726 /**
1727 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001728 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001729 */
1730 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731};
1732
1733
1734/**
1735 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1736 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001737class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001739 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740
ager@chromium.org3e875802009-06-29 08:26:34 +00001741 /**
1742 * Clones an element at index |index|. Returns an empty
1743 * handle if cloning fails (for any reason).
1744 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001745 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001746
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001747 /**
1748 * Creates a JavaScript array with the given length. If the length
1749 * is negative the returned array will have length 0.
1750 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001751 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001752
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001753 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001755 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001756 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757};
1758
1759
1760/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001761 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001763class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001765 V8EXPORT Local<Object> NewInstance() const;
1766 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1767 V8EXPORT Local<Value> Call(Handle<Object> recv,
1768 int argc,
1769 Handle<Value> argv[]);
1770 V8EXPORT void SetName(Handle<String> name);
1771 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001772
1773 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001774 * Name inferred from variable or property assignment of this function.
1775 * Used to facilitate debugging and profiling of JavaScript code written
1776 * in an OO style, where many functions are anonymous but are assigned
1777 * to object properties.
1778 */
1779 V8EXPORT Handle<Value> GetInferredName() const;
1780
1781 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001782 * Returns zero based line number of function body and
1783 * kLineOffsetNotFound if no information available.
1784 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001785 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001786 /**
1787 * Returns zero based column number of function body and
1788 * kLineOffsetNotFound if no information available.
1789 */
1790 V8EXPORT int GetScriptColumnNumber() const;
1791 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001792 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001793 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001794 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001795
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001797 V8EXPORT Function();
1798 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799};
1800
1801
1802/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001803 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1804 */
1805class Date : public Object {
1806 public:
1807 V8EXPORT static Local<Value> New(double time);
1808
1809 /**
1810 * A specialization of Value::NumberValue that is more efficient
1811 * because we know the structure of this object.
1812 */
1813 V8EXPORT double NumberValue() const;
1814
1815 static inline Date* Cast(v8::Value* obj);
1816
1817 /**
1818 * Notification that the embedder has changed the time zone,
1819 * daylight savings time, or other date / time configuration
1820 * parameters. V8 keeps a cache of various values used for
1821 * date / time computation. This notification will reset
1822 * those cached values for the current context so that date /
1823 * time configuration changes would be reflected in the Date
1824 * object.
1825 *
1826 * This API should not be called more than needed as it will
1827 * negatively impact the performance of date operations.
1828 */
1829 V8EXPORT static void DateTimeConfigurationChangeNotification();
1830
1831 private:
1832 V8EXPORT static void CheckCast(v8::Value* obj);
1833};
1834
1835
1836/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001837 * A Number object (ECMA-262, 4.3.21).
1838 */
1839class NumberObject : public Object {
1840 public:
1841 V8EXPORT static Local<Value> New(double value);
1842
1843 /**
1844 * Returns the Number held by the object.
1845 */
1846 V8EXPORT double NumberValue() const;
1847
1848 static inline NumberObject* Cast(v8::Value* obj);
1849
1850 private:
1851 V8EXPORT static void CheckCast(v8::Value* obj);
1852};
1853
1854
1855/**
1856 * A Boolean object (ECMA-262, 4.3.15).
1857 */
1858class BooleanObject : public Object {
1859 public:
1860 V8EXPORT static Local<Value> New(bool value);
1861
1862 /**
1863 * Returns the Boolean held by the object.
1864 */
1865 V8EXPORT bool BooleanValue() const;
1866
1867 static inline BooleanObject* Cast(v8::Value* obj);
1868
1869 private:
1870 V8EXPORT static void CheckCast(v8::Value* obj);
1871};
1872
1873
1874/**
1875 * A String object (ECMA-262, 4.3.18).
1876 */
1877class StringObject : public Object {
1878 public:
1879 V8EXPORT static Local<Value> New(Handle<String> value);
1880
1881 /**
1882 * Returns the String held by the object.
1883 */
1884 V8EXPORT Local<String> StringValue() const;
1885
1886 static inline StringObject* Cast(v8::Value* obj);
1887
1888 private:
1889 V8EXPORT static void CheckCast(v8::Value* obj);
1890};
1891
1892
1893/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001894 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1895 */
1896class RegExp : public Object {
1897 public:
1898 /**
1899 * Regular expression flag bits. They can be or'ed to enable a set
1900 * of flags.
1901 */
1902 enum Flags {
1903 kNone = 0,
1904 kGlobal = 1,
1905 kIgnoreCase = 2,
1906 kMultiline = 4
1907 };
1908
1909 /**
1910 * Creates a regular expression from the given pattern string and
1911 * the flags bit field. May throw a JavaScript exception as
1912 * described in ECMA-262, 15.10.4.1.
1913 *
1914 * For example,
1915 * RegExp::New(v8::String::New("foo"),
1916 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1917 * is equivalent to evaluating "/foo/gm".
1918 */
1919 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1920 Flags flags);
1921
1922 /**
1923 * Returns the value of the source property: a string representing
1924 * the regular expression.
1925 */
1926 V8EXPORT Local<String> GetSource() const;
1927
1928 /**
1929 * Returns the flags bit field.
1930 */
1931 V8EXPORT Flags GetFlags() const;
1932
1933 static inline RegExp* Cast(v8::Value* obj);
1934
1935 private:
1936 V8EXPORT static void CheckCast(v8::Value* obj);
1937};
1938
1939
1940/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001941 * A JavaScript value that wraps a C++ void*. This type of value is
1942 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001944 *
1945 * The Wrap function V8 will return the most optimal Value object wrapping the
1946 * C++ void*. The type of the value is not guaranteed to be an External object
1947 * and no assumptions about its type should be made. To access the wrapped
1948 * value Unwrap should be used, all other operations on that object will lead
1949 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001951class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001953 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001954 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001955
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001956 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001957 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001958 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001960 V8EXPORT External();
1961 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001962 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001963 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964};
1965
1966
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001967// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968
1969
1970/**
1971 * The superclass of object and function templates.
1972 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001973class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974 public:
1975 /** Adds a property to each instance created by this template.*/
1976 void Set(Handle<String> name, Handle<Data> value,
1977 PropertyAttribute attributes = None);
1978 inline void Set(const char* name, Handle<Data> value);
1979 private:
1980 Template();
1981
1982 friend class ObjectTemplate;
1983 friend class FunctionTemplate;
1984};
1985
1986
1987/**
1988 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001989 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 * including the receiver, the number and values of arguments, and
1991 * the holder of the function.
1992 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001993class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 public:
1995 inline int Length() const;
1996 inline Local<Value> operator[](int i) const;
1997 inline Local<Function> Callee() const;
1998 inline Local<Object> This() const;
1999 inline Local<Object> Holder() const;
2000 inline bool IsConstructCall() const;
2001 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002002 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002003
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002005 static const int kIsolateIndex = 0;
2006 static const int kDataIndex = -1;
2007 static const int kCalleeIndex = -2;
2008 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002009
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002011 inline Arguments(internal::Object** implicit_args,
2012 internal::Object** values,
2013 int length,
2014 bool is_construct_call);
2015 internal::Object** implicit_args_;
2016 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002018 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019};
2020
2021
2022/**
2023 * The information passed to an accessor callback about the context
2024 * of the property access.
2025 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002026class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002028 inline AccessorInfo(internal::Object** args)
2029 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002030 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 inline Local<Value> Data() const;
2032 inline Local<Object> This() const;
2033 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002034
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002036 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037};
2038
2039
2040typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2041
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 * NamedProperty[Getter|Setter] are used as interceptors on object.
2044 * See ObjectTemplate::SetNamedPropertyHandler.
2045 */
2046typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2047 const AccessorInfo& info);
2048
2049
2050/**
2051 * Returns the value if the setter intercepts the request.
2052 * Otherwise, returns an empty handle.
2053 */
2054typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2055 Local<Value> value,
2056 const AccessorInfo& info);
2057
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058/**
2059 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002060 * The result is an integer encoding property attributes (like v8::None,
2061 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002063typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2064 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065
2066
2067/**
2068 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002069 * The return value is true if the property could be deleted and false
2070 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071 */
2072typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2073 const AccessorInfo& info);
2074
2075/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002076 * Returns an array containing the names of the properties the named
2077 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 */
2079typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2080
v8.team.kasperl727e9952008-09-02 14:56:44 +00002081
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002083 * Returns the value of the property if the getter intercepts the
2084 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 */
2086typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2087 const AccessorInfo& info);
2088
2089
2090/**
2091 * Returns the value if the setter intercepts the request.
2092 * Otherwise, returns an empty handle.
2093 */
2094typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2095 Local<Value> value,
2096 const AccessorInfo& info);
2097
2098
2099/**
2100 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002101 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002103typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2104 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105
2106/**
2107 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002108 * The return value is true if the property could be deleted and false
2109 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 */
2111typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2112 const AccessorInfo& info);
2113
v8.team.kasperl727e9952008-09-02 14:56:44 +00002114/**
2115 * Returns an array containing the indices of the properties the
2116 * indexed property getter intercepts.
2117 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2119
2120
2121/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002122 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 */
2124enum AccessType {
2125 ACCESS_GET,
2126 ACCESS_SET,
2127 ACCESS_HAS,
2128 ACCESS_DELETE,
2129 ACCESS_KEYS
2130};
2131
v8.team.kasperl727e9952008-09-02 14:56:44 +00002132
2133/**
2134 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002135 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002136 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002137typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138 Local<Value> key,
2139 AccessType type,
2140 Local<Value> data);
2141
v8.team.kasperl727e9952008-09-02 14:56:44 +00002142
2143/**
2144 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002145 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002146 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002147typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 uint32_t index,
2149 AccessType type,
2150 Local<Value> data);
2151
2152
2153/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002154 * A FunctionTemplate is used to create functions at runtime. There
2155 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002156 * context. The lifetime of the created function is equal to the
2157 * lifetime of the context. So in case the embedder needs to create
2158 * temporary functions that can be collected using Scripts is
2159 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160 *
2161 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002162 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002163 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002164 * A FunctionTemplate has a corresponding instance template which is
2165 * used to create object instances when the function is used as a
2166 * constructor. Properties added to the instance template are added to
2167 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168 *
2169 * A FunctionTemplate can have a prototype template. The prototype template
2170 * is used to create the prototype object of the function.
2171 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002174 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2176 * t->Set("func_property", v8::Number::New(1));
2177 *
2178 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2179 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2180 * proto_t->Set("proto_const", v8::Number::New(2));
2181 *
2182 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2183 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2184 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2185 * instance_t->Set("instance_property", Number::New(3));
2186 *
2187 * v8::Local<v8::Function> function = t->GetFunction();
2188 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002189 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002190 *
2191 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002192 * and "instance" for the instance object created above. The function
2193 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002195 * \code
2196 * func_property in function == true;
2197 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002199 * function.prototype.proto_method() invokes 'InvokeCallback'
2200 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002202 * instance instanceof function == true;
2203 * instance.instance_accessor calls 'InstanceAccessorCallback'
2204 * instance.instance_property == 3;
2205 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002207 * A FunctionTemplate can inherit from another one by calling the
2208 * FunctionTemplate::Inherit method. The following graph illustrates
2209 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002211 * \code
2212 * FunctionTemplate Parent -> Parent() . prototype -> { }
2213 * ^ ^
2214 * | Inherit(Parent) | .__proto__
2215 * | |
2216 * FunctionTemplate Child -> Child() . prototype -> { }
2217 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002219 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2220 * object of the Child() function has __proto__ pointing to the
2221 * Parent() function's prototype object. An instance of the Child
2222 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002224 * Let Parent be the FunctionTemplate initialized in the previous
2225 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002227 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 * Local<FunctionTemplate> parent = t;
2229 * Local<FunctionTemplate> child = FunctionTemplate::New();
2230 * child->Inherit(parent);
2231 *
2232 * Local<Function> child_function = child->GetFunction();
2233 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002234 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002236 * The Child function and Child instance will have the following
2237 * properties:
2238 *
2239 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002241 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002243 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002245class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246 public:
2247 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248 static Local<FunctionTemplate> New(
2249 InvocationCallback callback = 0,
2250 Handle<Value> data = Handle<Value>(),
2251 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 /** Returns the unique function instance in the current execution context.*/
2253 Local<Function> GetFunction();
2254
v8.team.kasperl727e9952008-09-02 14:56:44 +00002255 /**
2256 * Set the call-handler callback for a FunctionTemplate. This
2257 * callback is called whenever the function created from this
2258 * FunctionTemplate is called.
2259 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 void SetCallHandler(InvocationCallback callback,
2261 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262
v8.team.kasperl727e9952008-09-02 14:56:44 +00002263 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 Local<ObjectTemplate> InstanceTemplate();
2265
2266 /** Causes the function template to inherit from a parent function template.*/
2267 void Inherit(Handle<FunctionTemplate> parent);
2268
2269 /**
2270 * A PrototypeTemplate is the template used to create the prototype object
2271 * of the function created by this template.
2272 */
2273 Local<ObjectTemplate> PrototypeTemplate();
2274
v8.team.kasperl727e9952008-09-02 14:56:44 +00002275
2276 /**
2277 * Set the class name of the FunctionTemplate. This is used for
2278 * printing objects created with the function created from the
2279 * FunctionTemplate as its constructor.
2280 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 void SetClassName(Handle<String> name);
2282
2283 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002284 * Determines whether the __proto__ accessor ignores instances of
2285 * the function template. If instances of the function template are
2286 * ignored, __proto__ skips all instances and instead returns the
2287 * next object in the prototype chain.
2288 *
2289 * Call with a value of true to make the __proto__ accessor ignore
2290 * instances of the function template. Call with a value of false
2291 * to make the __proto__ accessor not ignore instances of the
2292 * function template. By default, instances of a function template
2293 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 */
2295 void SetHiddenPrototype(bool value);
2296
2297 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002298 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2299 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002300 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002301 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002302
2303 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002304 * Returns true if the given object is an instance of this function
2305 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 */
2307 bool HasInstance(Handle<Value> object);
2308
2309 private:
2310 FunctionTemplate();
2311 void AddInstancePropertyAccessor(Handle<String> name,
2312 AccessorGetter getter,
2313 AccessorSetter setter,
2314 Handle<Value> data,
2315 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002316 PropertyAttribute attributes,
2317 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2319 NamedPropertySetter setter,
2320 NamedPropertyQuery query,
2321 NamedPropertyDeleter remover,
2322 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002323 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2325 IndexedPropertySetter setter,
2326 IndexedPropertyQuery query,
2327 IndexedPropertyDeleter remover,
2328 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002329 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2331 Handle<Value> data);
2332
2333 friend class Context;
2334 friend class ObjectTemplate;
2335};
2336
2337
2338/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002339 * An ObjectTemplate is used to create objects at runtime.
2340 *
2341 * Properties added to an ObjectTemplate are added to each object
2342 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002344class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002346 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002348
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 /** Creates a new instance of this template.*/
2350 Local<Object> NewInstance();
2351
2352 /**
2353 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002354 *
2355 * Whenever the property with the given name is accessed on objects
2356 * created from this ObjectTemplate the getter and setter callbacks
2357 * are called instead of getting and setting the property directly
2358 * on the JavaScript object.
2359 *
2360 * \param name The name of the property for which an accessor is added.
2361 * \param getter The callback to invoke when getting the property.
2362 * \param setter The callback to invoke when setting the property.
2363 * \param data A piece of data that will be passed to the getter and setter
2364 * callbacks whenever they are invoked.
2365 * \param settings Access control settings for the accessor. This is a bit
2366 * field consisting of one of more of
2367 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2368 * The default is to not allow cross-context access.
2369 * ALL_CAN_READ means that all cross-context reads are allowed.
2370 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2371 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2372 * cross-context access.
2373 * \param attribute The attributes of the property for which an accessor
2374 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002375 * \param signature The signature describes valid receivers for the accessor
2376 * and is used to perform implicit instance checks against them. If the
2377 * receiver is incompatible (i.e. is not an instance of the constructor as
2378 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2379 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380 */
2381 void SetAccessor(Handle<String> name,
2382 AccessorGetter getter,
2383 AccessorSetter setter = 0,
2384 Handle<Value> data = Handle<Value>(),
2385 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002386 PropertyAttribute attribute = None,
2387 Handle<AccessorSignature> signature =
2388 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389
2390 /**
2391 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002392 *
2393 * Whenever a named property is accessed on objects created from
2394 * this object template, the provided callback is invoked instead of
2395 * accessing the property directly on the JavaScript object.
2396 *
2397 * \param getter The callback to invoke when getting a property.
2398 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002399 * \param query The callback to invoke to check if a property is present,
2400 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002401 * \param deleter The callback to invoke when deleting a property.
2402 * \param enumerator The callback to invoke to enumerate all the named
2403 * properties of an object.
2404 * \param data A piece of data that will be passed to the callbacks
2405 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406 */
2407 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2408 NamedPropertySetter setter = 0,
2409 NamedPropertyQuery query = 0,
2410 NamedPropertyDeleter deleter = 0,
2411 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002412 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413
2414 /**
2415 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002416 *
2417 * Whenever an indexed property is accessed on objects created from
2418 * this object template, the provided callback is invoked instead of
2419 * accessing the property directly on the JavaScript object.
2420 *
2421 * \param getter The callback to invoke when getting a property.
2422 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002423 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002424 * \param deleter The callback to invoke when deleting a property.
2425 * \param enumerator The callback to invoke to enumerate all the indexed
2426 * properties of an object.
2427 * \param data A piece of data that will be passed to the callbacks
2428 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 */
2430 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2431 IndexedPropertySetter setter = 0,
2432 IndexedPropertyQuery query = 0,
2433 IndexedPropertyDeleter deleter = 0,
2434 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002435 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002436
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437 /**
2438 * Sets the callback to be used when calling instances created from
2439 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002440 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441 * function.
2442 */
2443 void SetCallAsFunctionHandler(InvocationCallback callback,
2444 Handle<Value> data = Handle<Value>());
2445
v8.team.kasperl727e9952008-09-02 14:56:44 +00002446 /**
2447 * Mark object instances of the template as undetectable.
2448 *
2449 * In many ways, undetectable objects behave as though they are not
2450 * there. They behave like 'undefined' in conditionals and when
2451 * printed. However, properties can be accessed and called as on
2452 * normal objects.
2453 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002454 void MarkAsUndetectable();
2455
v8.team.kasperl727e9952008-09-02 14:56:44 +00002456 /**
2457 * Sets access check callbacks on the object template.
2458 *
2459 * When accessing properties on instances of this object template,
2460 * the access check callback will be called to determine whether or
2461 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002462 * The last parameter specifies whether access checks are turned
2463 * on by default on instances. If access checks are off by default,
2464 * they can be turned on on individual instances by calling
2465 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002466 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2468 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002469 Handle<Value> data = Handle<Value>(),
2470 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471
kasper.lund212ac232008-07-16 07:07:30 +00002472 /**
2473 * Gets the number of internal fields for objects generated from
2474 * this template.
2475 */
2476 int InternalFieldCount();
2477
2478 /**
2479 * Sets the number of internal fields for objects generated from
2480 * this template.
2481 */
2482 void SetInternalFieldCount(int value);
2483
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002484 private:
2485 ObjectTemplate();
2486 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2487 friend class FunctionTemplate;
2488};
2489
2490
2491/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002492 * A Signature specifies which receivers and arguments are valid
2493 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002495class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496 public:
2497 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2498 Handle<FunctionTemplate>(),
2499 int argc = 0,
2500 Handle<FunctionTemplate> argv[] = 0);
2501 private:
2502 Signature();
2503};
2504
2505
2506/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002507 * An AccessorSignature specifies which receivers are valid parameters
2508 * to an accessor callback.
2509 */
2510class V8EXPORT AccessorSignature : public Data {
2511 public:
2512 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2513 Handle<FunctionTemplate>());
2514 private:
2515 AccessorSignature();
2516};
2517
2518
2519/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002520 * A utility for determining the type of objects based on the template
2521 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002523class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002524 public:
2525 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2526 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2527 int match(Handle<Value> value);
2528 private:
2529 TypeSwitch();
2530};
2531
2532
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002533// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002535class V8EXPORT ExternalAsciiStringResourceImpl
2536 : public String::ExternalAsciiStringResource {
2537 public:
2538 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2539 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2540 : data_(data), length_(length) {}
2541 const char* data() const { return data_; }
2542 size_t length() const { return length_; }
2543
2544 private:
2545 const char* data_;
2546 size_t length_;
2547};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548
2549/**
2550 * Ignore
2551 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002552class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002554 // Note that the strings passed into this constructor must live as long
2555 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002557 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002559 const char** deps = 0,
2560 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561 virtual ~Extension() { }
2562 virtual v8::Handle<v8::FunctionTemplate>
2563 GetNativeFunction(v8::Handle<v8::String> name) {
2564 return v8::Handle<v8::FunctionTemplate>();
2565 }
2566
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002567 const char* name() const { return name_; }
2568 size_t source_length() const { return source_length_; }
2569 const String::ExternalAsciiStringResource* source() const {
2570 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 int dependency_count() { return dep_count_; }
2572 const char** dependencies() { return deps_; }
2573 void set_auto_enable(bool value) { auto_enable_ = value; }
2574 bool auto_enable() { return auto_enable_; }
2575
2576 private:
2577 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002578 size_t source_length_; // expected to initialize before source_
2579 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580 int dep_count_;
2581 const char** deps_;
2582 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002583
2584 // Disallow copying and assigning.
2585 Extension(const Extension&);
2586 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587};
2588
2589
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002590void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591
2592
2593/**
2594 * Ignore
2595 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002596class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597 public:
2598 inline DeclareExtension(Extension* extension) {
2599 RegisterExtension(extension);
2600 }
2601};
2602
2603
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002604// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002605
2606
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002607Handle<Primitive> V8EXPORT Undefined();
2608Handle<Primitive> V8EXPORT Null();
2609Handle<Boolean> V8EXPORT True();
2610Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002612inline Handle<Primitive> Undefined(Isolate* isolate);
2613inline Handle<Primitive> Null(Isolate* isolate);
2614inline Handle<Boolean> True(Isolate* isolate);
2615inline Handle<Boolean> False(Isolate* isolate);
2616
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617
2618/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002619 * A set of constraints that specifies the limits of the runtime's memory use.
2620 * You must set the heap size before initializing the VM - the size cannot be
2621 * adjusted after the VM is initialized.
2622 *
2623 * If you are using threads then you should hold the V8::Locker lock while
2624 * setting the stack limit and you must set a non-default stack limit separately
2625 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002627class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 public:
2629 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002630 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002632 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002634 int max_executable_size() { return max_executable_size_; }
2635 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002636 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002637 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2639 private:
2640 int max_young_space_size_;
2641 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002642 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643 uint32_t* stack_limit_;
2644};
2645
2646
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002647bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648
2649
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002650// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651
2652
2653typedef void (*FatalErrorCallback)(const char* location, const char* message);
2654
2655
2656typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2657
2658
2659/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002660 * Schedules an exception to be thrown when returning to JavaScript. When an
2661 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002663 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002665Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666
2667/**
2668 * Create new error objects by calling the corresponding error object
2669 * constructor with the message.
2670 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002671class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672 public:
2673 static Local<Value> RangeError(Handle<String> message);
2674 static Local<Value> ReferenceError(Handle<String> message);
2675 static Local<Value> SyntaxError(Handle<String> message);
2676 static Local<Value> TypeError(Handle<String> message);
2677 static Local<Value> Error(Handle<String> message);
2678};
2679
2680
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002681// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002683typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002685typedef void* (*CreateHistogramCallback)(const char* name,
2686 int min,
2687 int max,
2688 size_t buckets);
2689
2690typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2691
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002692// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002693 enum ObjectSpace {
2694 kObjectSpaceNewSpace = 1 << 0,
2695 kObjectSpaceOldPointerSpace = 1 << 1,
2696 kObjectSpaceOldDataSpace = 1 << 2,
2697 kObjectSpaceCodeSpace = 1 << 3,
2698 kObjectSpaceMapSpace = 1 << 4,
2699 kObjectSpaceLoSpace = 1 << 5,
2700
2701 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2702 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2703 kObjectSpaceLoSpace
2704 };
2705
2706 enum AllocationAction {
2707 kAllocationActionAllocate = 1 << 0,
2708 kAllocationActionFree = 1 << 1,
2709 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2710 };
2711
2712typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2713 AllocationAction action,
2714 int size);
2715
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002716// --- Leave Script Callback ---
2717typedef void (*CallCompletedCallback)();
2718
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002719// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2721 AccessType type,
2722 Local<Value> data);
2723
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002724// --- AllowCodeGenerationFromStrings callbacks ---
2725
2726/**
2727 * Callback to check if code generation from strings is allowed. See
2728 * Context::AllowCodeGenerationFromStrings.
2729 */
2730typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2731
2732// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733
2734/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002735 * Applications can register callback functions which will be called
2736 * before and after a garbage collection. Allocations are not
2737 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002738 * objects (set or delete properties for example) since it is possible
2739 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002741enum GCType {
2742 kGCTypeScavenge = 1 << 0,
2743 kGCTypeMarkSweepCompact = 1 << 1,
2744 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2745};
2746
2747enum GCCallbackFlags {
2748 kNoGCCallbackFlags = 0,
2749 kGCCallbackFlagCompacted = 1 << 0
2750};
2751
2752typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2753typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2754
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002755typedef void (*GCCallback)();
2756
2757
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002759 * Collection of V8 heap information.
2760 *
2761 * Instances of this class can be passed to v8::V8::HeapStatistics to
2762 * get heap statistics from V8.
2763 */
2764class V8EXPORT HeapStatistics {
2765 public:
2766 HeapStatistics();
2767 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002768 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002769 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002770 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002771
2772 private:
2773 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002774 void set_total_heap_size_executable(size_t size) {
2775 total_heap_size_executable_ = size;
2776 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002777 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002778 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002779
2780 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002781 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002782 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002783 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002784
2785 friend class V8;
2786};
2787
2788
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002789class RetainedObjectInfo;
2790
ager@chromium.org3811b432009-10-28 14:53:37 +00002791/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002792 * Isolate represents an isolated instance of the V8 engine. V8
2793 * isolates have completely separate states. Objects from one isolate
2794 * must not be used in other isolates. When V8 is initialized a
2795 * default isolate is implicitly created and entered. The embedder
2796 * can create additional isolates and use them in parallel in multiple
2797 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002798 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002799 */
2800class V8EXPORT Isolate {
2801 public:
2802 /**
2803 * Stack-allocated class which sets the isolate for all operations
2804 * executed within a local scope.
2805 */
2806 class V8EXPORT Scope {
2807 public:
2808 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2809 isolate->Enter();
2810 }
2811
2812 ~Scope() { isolate_->Exit(); }
2813
2814 private:
2815 Isolate* const isolate_;
2816
2817 // Prevent copying of Scope objects.
2818 Scope(const Scope&);
2819 Scope& operator=(const Scope&);
2820 };
2821
2822 /**
2823 * Creates a new isolate. Does not change the currently entered
2824 * isolate.
2825 *
2826 * When an isolate is no longer used its resources should be freed
2827 * by calling Dispose(). Using the delete operator is not allowed.
2828 */
2829 static Isolate* New();
2830
2831 /**
2832 * Returns the entered isolate for the current thread or NULL in
2833 * case there is no current isolate.
2834 */
2835 static Isolate* GetCurrent();
2836
2837 /**
2838 * Methods below this point require holding a lock (using Locker) in
2839 * a multi-threaded environment.
2840 */
2841
2842 /**
2843 * Sets this isolate as the entered one for the current thread.
2844 * Saves the previously entered one (if any), so that it can be
2845 * restored when exiting. Re-entering an isolate is allowed.
2846 */
2847 void Enter();
2848
2849 /**
2850 * Exits this isolate by restoring the previously entered one in the
2851 * current thread. The isolate may still stay the same, if it was
2852 * entered more than once.
2853 *
2854 * Requires: this == Isolate::GetCurrent().
2855 */
2856 void Exit();
2857
2858 /**
2859 * Disposes the isolate. The isolate must not be entered by any
2860 * thread to be disposable.
2861 */
2862 void Dispose();
2863
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002864 /**
2865 * Associate embedder-specific data with the isolate
2866 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002867 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002868
2869 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002870 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002871 * Returns NULL if SetData has never been called.
2872 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002873 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002874
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002875 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002876 Isolate();
2877 Isolate(const Isolate&);
2878 ~Isolate();
2879 Isolate& operator=(const Isolate&);
2880 void* operator new(size_t size);
2881 void operator delete(void*, size_t);
2882};
2883
2884
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002885class StartupData {
2886 public:
2887 enum CompressionAlgorithm {
2888 kUncompressed,
2889 kBZip2
2890 };
2891
2892 const char* data;
2893 int compressed_size;
2894 int raw_size;
2895};
2896
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002897
2898/**
2899 * A helper class for driving V8 startup data decompression. It is based on
2900 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2901 * for an embedder to use this class, instead, API functions can be used
2902 * directly.
2903 *
2904 * For an example of the class usage, see the "shell.cc" sample application.
2905 */
2906class V8EXPORT StartupDataDecompressor { // NOLINT
2907 public:
2908 StartupDataDecompressor();
2909 virtual ~StartupDataDecompressor();
2910 int Decompress();
2911
2912 protected:
2913 virtual int DecompressData(char* raw_data,
2914 int* raw_data_size,
2915 const char* compressed_data,
2916 int compressed_data_size) = 0;
2917
2918 private:
2919 char** raw_data;
2920};
2921
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002922
2923/**
2924 * EntropySource is used as a callback function when v8 needs a source
2925 * of entropy.
2926 */
2927typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2928
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002929
2930/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002931 * ReturnAddressLocationResolver is used as a callback function when v8 is
2932 * resolving the location of a return address on the stack. Profilers that
2933 * change the return address on the stack can use this to resolve the stack
2934 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002935 *
2936 * \param return_addr_location points to a location on stack where a machine
2937 * return address resides.
2938 * \returns either return_addr_location, or else a pointer to the profiler's
2939 * copy of the original return address.
2940 *
2941 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00002942 */
2943typedef uintptr_t (*ReturnAddressLocationResolver)(
2944 uintptr_t return_addr_location);
2945
2946
2947/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002948 * FunctionEntryHook is the type of the profile entry hook called at entry to
2949 * any generated function when function-level profiling is enabled.
2950 *
2951 * \param function the address of the function that's being entered.
2952 * \param return_addr_location points to a location on stack where the machine
2953 * return address resides. This can be used to identify the caller of
2954 * \p function, and/or modified to divert execution when \p function exits.
2955 *
2956 * \note the entry hook must not cause garbage collection.
2957 */
2958typedef void (*FunctionEntryHook)(uintptr_t function,
2959 uintptr_t return_addr_location);
2960
2961
2962/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002963 * A JIT code event is issued each time code is added, moved or removed.
2964 *
2965 * \note removal events are not currently issued.
2966 */
2967struct JitCodeEvent {
2968 enum EventType {
2969 CODE_ADDED,
2970 CODE_MOVED,
2971 CODE_REMOVED
2972 };
2973
2974 // Type of event.
2975 EventType type;
2976 // Start of the instructions.
2977 void* code_start;
2978 // Size of the instructions.
2979 size_t code_len;
2980
2981 union {
2982 // Only valid for CODE_ADDED.
2983 struct {
2984 // Name of the object associated with the code, note that the string is
2985 // not zero-terminated.
2986 const char* str;
2987 // Number of chars in str.
2988 size_t len;
2989 } name;
2990 // New location of instructions. Only valid for CODE_MOVED.
2991 void* new_code_start;
2992 };
2993};
2994
2995/**
2996 * Option flags passed to the SetJitCodeEventHandler function.
2997 */
2998enum JitCodeEventOptions {
2999 kJitCodeEventDefault = 0,
3000 // Generate callbacks for already existent code.
3001 kJitCodeEventEnumExisting = 1
3002};
3003
3004
3005/**
3006 * Callback function passed to SetJitCodeEventHandler.
3007 *
3008 * \param event code add, move or removal event.
3009 */
3010typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3011
3012
3013/**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003014 * Interface for iterating though all external resources in the heap.
3015 */
3016class V8EXPORT ExternalResourceVisitor { // NOLINT
3017 public:
3018 virtual ~ExternalResourceVisitor() {}
3019 virtual void VisitExternalString(Handle<String> string) {}
3020};
3021
3022
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003023/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003024 * Container class for static utility functions.
3025 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003026class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003027 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003028 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003029 static void SetFatalErrorHandler(FatalErrorCallback that);
3030
v8.team.kasperl727e9952008-09-02 14:56:44 +00003031 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003032 * Set the callback to invoke to check if code generation from
3033 * strings should be allowed.
3034 */
3035 static void SetAllowCodeGenerationFromStringsCallback(
3036 AllowCodeGenerationFromStringsCallback that);
3037
3038 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003039 * Ignore out-of-memory exceptions.
3040 *
3041 * V8 running out of memory is treated as a fatal error by default.
3042 * This means that the fatal error handler is called and that V8 is
3043 * terminated.
3044 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003045 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003046 * out-of-memory situation as a fatal error. This way, the contexts
3047 * that did not cause the out of memory problem might be able to
3048 * continue execution.
3049 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050 static void IgnoreOutOfMemoryException();
3051
v8.team.kasperl727e9952008-09-02 14:56:44 +00003052 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003053 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003054 * fatal errors such as out-of-memory situations.
3055 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056 static bool IsDead();
3057
3058 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003059 * The following 4 functions are to be used when V8 is built with
3060 * the 'compress_startup_data' flag enabled. In this case, the
3061 * embedder must decompress startup data prior to initializing V8.
3062 *
3063 * This is how interaction with V8 should look like:
3064 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3065 * v8::StartupData* compressed_data =
3066 * new v8::StartupData[compressed_data_count];
3067 * v8::V8::GetCompressedStartupData(compressed_data);
3068 * ... decompress data (compressed_data can be updated in-place) ...
3069 * v8::V8::SetDecompressedStartupData(compressed_data);
3070 * ... now V8 can be initialized
3071 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003072 *
3073 * A helper class StartupDataDecompressor is provided. It implements
3074 * the protocol of the interaction described above, and can be used in
3075 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003076 */
3077 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3078 static int GetCompressedStartupDataCount();
3079 static void GetCompressedStartupData(StartupData* compressed_data);
3080 static void SetDecompressedStartupData(StartupData* decompressed_data);
3081
3082 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003083 * Adds a message listener.
3084 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003085 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003086 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003087 */
3088 static bool AddMessageListener(MessageCallback that,
3089 Handle<Value> data = Handle<Value>());
3090
3091 /**
3092 * Remove all message listeners from the specified callback function.
3093 */
3094 static void RemoveMessageListeners(MessageCallback that);
3095
3096 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003097 * Tells V8 to capture current stack trace when uncaught exception occurs
3098 * and report it to the message listeners. The option is off by default.
3099 */
3100 static void SetCaptureStackTraceForUncaughtExceptions(
3101 bool capture,
3102 int frame_limit = 10,
3103 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3104
3105 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003106 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003107 */
3108 static void SetFlagsFromString(const char* str, int length);
3109
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003110 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003111 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003112 */
3113 static void SetFlagsFromCommandLine(int* argc,
3114 char** argv,
3115 bool remove_flags);
3116
kasper.lund7276f142008-07-30 08:49:36 +00003117 /** Get the version string. */
3118 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003119
3120 /**
3121 * Enables the host application to provide a mechanism for recording
3122 * statistics counters.
3123 */
3124 static void SetCounterFunction(CounterLookupCallback);
3125
3126 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003127 * Enables the host application to provide a mechanism for recording
3128 * histograms. The CreateHistogram function returns a
3129 * histogram which will later be passed to the AddHistogramSample
3130 * function.
3131 */
3132 static void SetCreateHistogramFunction(CreateHistogramCallback);
3133 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3134
3135 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003136 * Enables the computation of a sliding window of states. The sliding
3137 * window information is recorded in statistics counters.
3138 */
3139 static void EnableSlidingStateWindow();
3140
3141 /** Callback function for reporting failed access checks.*/
3142 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3143
3144 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003145 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003146 * garbage collection. Allocations are not allowed in the
3147 * callback function, you therefore cannot manipulate objects (set
3148 * or delete properties for example) since it is possible such
3149 * operations will result in the allocation of objects. It is possible
3150 * to specify the GCType filter for your callback. But it is not possible to
3151 * register the same callback function two times with different
3152 * GCType filters.
3153 */
3154 static void AddGCPrologueCallback(
3155 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3156
3157 /**
3158 * This function removes callback which was installed by
3159 * AddGCPrologueCallback function.
3160 */
3161 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3162
3163 /**
3164 * The function is deprecated. Please use AddGCPrologueCallback instead.
3165 * Enables the host application to receive a notification before a
3166 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003167 * callback function, you therefore cannot manipulate objects (set
3168 * or delete properties for example) since it is possible such
3169 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003170 */
3171 static void SetGlobalGCPrologueCallback(GCCallback);
3172
3173 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003174 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003175 * garbage collection. Allocations are not allowed in the
3176 * callback function, you therefore cannot manipulate objects (set
3177 * or delete properties for example) since it is possible such
3178 * operations will result in the allocation of objects. It is possible
3179 * to specify the GCType filter for your callback. But it is not possible to
3180 * register the same callback function two times with different
3181 * GCType filters.
3182 */
3183 static void AddGCEpilogueCallback(
3184 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3185
3186 /**
3187 * This function removes callback which was installed by
3188 * AddGCEpilogueCallback function.
3189 */
3190 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3191
3192 /**
3193 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3194 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003195 * major garbage collection. Allocations are not allowed in the
3196 * callback function, you therefore cannot manipulate objects (set
3197 * or delete properties for example) since it is possible such
3198 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003199 */
3200 static void SetGlobalGCEpilogueCallback(GCCallback);
3201
3202 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003203 * Enables the host application to provide a mechanism to be notified
3204 * and perform custom logging when V8 Allocates Executable Memory.
3205 */
3206 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3207 ObjectSpace space,
3208 AllocationAction action);
3209
3210 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003211 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003212 */
3213 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3214
3215 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003216 * Adds a callback to notify the host application when a script finished
3217 * running. If a script re-enters the runtime during executing, the
3218 * CallCompletedCallback is only invoked when the outer-most script
3219 * execution ends. Executing scripts inside the callback do not trigger
3220 * further callbacks.
3221 */
3222 static void AddCallCompletedCallback(CallCompletedCallback callback);
3223
3224 /**
3225 * Removes callback that was installed by AddCallCompletedCallback.
3226 */
3227 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3228
3229 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003230 * Allows the host application to group objects together. If one
3231 * object in the group is alive, all objects in the group are alive.
3232 * After each garbage collection, object groups are removed. It is
3233 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003234 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00003235 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003236 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003237 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003238 static void AddObjectGroup(Persistent<Value>* objects,
3239 size_t length,
3240 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241
3242 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003243 * Allows the host application to declare implicit references between
3244 * the objects: if |parent| is alive, all |children| are alive too.
3245 * After each garbage collection, all implicit references
3246 * are removed. It is intended to be used in the before-garbage-collection
3247 * callback function.
3248 */
3249 static void AddImplicitReferences(Persistent<Object> parent,
3250 Persistent<Value>* children,
3251 size_t length);
3252
3253 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003254 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003255 * initialize from scratch. This function is called implicitly if
3256 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257 */
3258 static bool Initialize();
3259
kasper.lund7276f142008-07-30 08:49:36 +00003260 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003261 * Allows the host application to provide a callback which can be used
3262 * as a source of entropy for random number generators.
3263 */
3264 static void SetEntropySource(EntropySource source);
3265
3266 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003267 * Allows the host application to provide a callback that allows v8 to
3268 * cooperate with a profiler that rewrites return addresses on stack.
3269 */
3270 static void SetReturnAddressLocationResolver(
3271 ReturnAddressLocationResolver return_address_resolver);
3272
3273 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003274 * Allows the host application to provide the address of a function that's
3275 * invoked on entry to every V8-generated function.
3276 * Note that \p entry_hook is invoked at the very start of each
3277 * generated function.
3278 *
3279 * \param entry_hook a function that will be invoked on entry to every
3280 * V8-generated function.
3281 * \returns true on success on supported platforms, false on failure.
3282 * \note Setting a new entry hook function when one is already active will
3283 * fail.
3284 */
3285 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3286
3287 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003288 * Allows the host application to provide the address of a function that is
3289 * notified each time code is added, moved or removed.
3290 *
3291 * \param options options for the JIT code event handler.
3292 * \param event_handler the JIT code event handler, which will be invoked
3293 * each time code is added, moved or removed.
3294 * \note \p event_handler won't get notified of existent code.
3295 * \note since code removal notifications are not currently issued, the
3296 * \p event_handler may get notifications of code that overlaps earlier
3297 * code notifications. This happens when code areas are reused, and the
3298 * earlier overlapping code areas should therefore be discarded.
3299 * \note the events passed to \p event_handler and the strings they point to
3300 * are not guaranteed to live past each call. The \p event_handler must
3301 * copy strings and other parameters it needs to keep around.
3302 * \note the set of events declared in JitCodeEvent::EventType is expected to
3303 * grow over time, and the JitCodeEvent structure is expected to accrue
3304 * new members. The \p event_handler function must ignore event codes
3305 * it does not recognize to maintain future compatibility.
3306 */
3307 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3308 JitCodeEventHandler event_handler);
3309
3310 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003311 * Adjusts the amount of registered external memory. Used to give
3312 * V8 an indication of the amount of externally allocated memory
3313 * that is kept alive by JavaScript objects. V8 uses this to decide
3314 * when to perform global garbage collections. Registering
3315 * externally allocated memory will trigger global garbage
3316 * collections more often than otherwise in an attempt to garbage
3317 * collect the JavaScript objects keeping the externally allocated
3318 * memory alive.
3319 *
3320 * \param change_in_bytes the change in externally allocated memory
3321 * that is kept alive by JavaScript objects.
3322 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003323 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003324 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3325 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003326
iposva@chromium.org245aa852009-02-10 00:49:54 +00003327 /**
3328 * Suspends recording of tick samples in the profiler.
3329 * When the V8 profiling mode is enabled (usually via command line
3330 * switches) this function suspends recording of tick samples.
3331 * Profiling ticks are discarded until ResumeProfiler() is called.
3332 *
3333 * See also the --prof and --prof_auto command line switches to
3334 * enable V8 profiling.
3335 */
3336 static void PauseProfiler();
3337
3338 /**
3339 * Resumes recording of tick samples in the profiler.
3340 * See also PauseProfiler().
3341 */
3342 static void ResumeProfiler();
3343
ager@chromium.org41826e72009-03-30 13:30:57 +00003344 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003345 * Return whether profiler is currently paused.
3346 */
3347 static bool IsProfilerPaused();
3348
3349 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003350 * Retrieve the V8 thread id of the calling thread.
3351 *
3352 * The thread id for a thread should only be retrieved after the V8
3353 * lock has been acquired with a Locker object with that thread.
3354 */
3355 static int GetCurrentThreadId();
3356
3357 /**
3358 * Forcefully terminate execution of a JavaScript thread. This can
3359 * be used to terminate long-running scripts.
3360 *
3361 * TerminateExecution should only be called when then V8 lock has
3362 * been acquired with a Locker object. Therefore, in order to be
3363 * able to terminate long-running threads, preemption must be
3364 * enabled to allow the user of TerminateExecution to acquire the
3365 * lock.
3366 *
3367 * The termination is achieved by throwing an exception that is
3368 * uncatchable by JavaScript exception handlers. Termination
3369 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003370 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003371 * exception handler that catches an exception should check if that
3372 * exception is a termination exception and immediately return if
3373 * that is the case. Returning immediately in that case will
3374 * continue the propagation of the termination exception if needed.
3375 *
3376 * The thread id passed to TerminateExecution must have been
3377 * obtained by calling GetCurrentThreadId on the thread in question.
3378 *
3379 * \param thread_id The thread id of the thread to terminate.
3380 */
3381 static void TerminateExecution(int thread_id);
3382
3383 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003384 * Forcefully terminate the current thread of JavaScript execution
3385 * in the given isolate. If no isolate is provided, the default
3386 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003387 *
3388 * This method can be used by any thread even if that thread has not
3389 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003390 *
3391 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003392 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003393 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003394
3395 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003396 * Is V8 terminating JavaScript execution.
3397 *
3398 * Returns true if JavaScript execution is currently terminating
3399 * because of a call to TerminateExecution. In that case there are
3400 * still JavaScript frames on the stack and the termination
3401 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003402 *
3403 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003404 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003405 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003406
3407 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003408 * Releases any resources used by v8 and stops any utility threads
3409 * that may be running. Note that disposing v8 is permanent, it
3410 * cannot be reinitialized.
3411 *
3412 * It should generally not be necessary to dispose v8 before exiting
3413 * a process, this should happen automatically. It is only necessary
3414 * to use if the process needs the resources taken up by v8.
3415 */
3416 static bool Dispose();
3417
ager@chromium.org3811b432009-10-28 14:53:37 +00003418 /**
3419 * Get statistics about the heap memory usage.
3420 */
3421 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003422
3423 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003424 * Iterates through all external resources referenced from current isolate
3425 * heap. This method is not expected to be used except for debugging purposes
3426 * and may be quite slow.
3427 */
3428 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3429
3430 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003431 * Optional notification that the embedder is idle.
3432 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003433 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003434 * Returns true if the embedder should stop calling IdleNotification
3435 * until real work has been done. This indicates that V8 has done
3436 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003437 *
3438 * The hint argument specifies the amount of work to be done in the function
3439 * on scale from 1 to 1000. There is no guarantee that the actual work will
3440 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003441 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003442 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003443
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003444 /**
3445 * Optional notification that the system is running low on memory.
3446 * V8 uses these notifications to attempt to free memory.
3447 */
3448 static void LowMemoryNotification();
3449
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003450 /**
3451 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003452 * these notifications to guide the GC heuristic. Returns the number
3453 * of context disposals - including this one - since the last time
3454 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003455 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003456 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003457
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003458 private:
3459 V8();
3460
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003461 static internal::Object** GlobalizeReference(internal::Object** handle);
3462 static void DisposeGlobal(internal::Object** global_handle);
3463 static void MakeWeak(internal::Object** global_handle,
3464 void* data,
3465 WeakReferenceCallback);
3466 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003467 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003468 static bool IsGlobalNearDeath(internal::Object** global_handle);
3469 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003470 static void SetWrapperClassId(internal::Object** global_handle,
3471 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003472
3473 template <class T> friend class Handle;
3474 template <class T> friend class Local;
3475 template <class T> friend class Persistent;
3476 friend class Context;
3477};
3478
3479
3480/**
3481 * An external exception handler.
3482 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003483class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003484 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003485 /**
3486 * Creates a new try/catch block and registers it with v8.
3487 */
3488 TryCatch();
3489
3490 /**
3491 * Unregisters and deletes this try/catch block.
3492 */
3493 ~TryCatch();
3494
3495 /**
3496 * Returns true if an exception has been caught by this try/catch block.
3497 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003498 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499
3500 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003501 * For certain types of exceptions, it makes no sense to continue
3502 * execution.
3503 *
3504 * Currently, the only type of exception that can be caught by a
3505 * TryCatch handler and for which it does not make sense to continue
3506 * is termination exception. Such exceptions are thrown when the
3507 * TerminateExecution methods are called to terminate a long-running
3508 * script.
3509 *
3510 * If CanContinue returns false, the correct action is to perform
3511 * any C++ cleanup needed and then return.
3512 */
3513 bool CanContinue() const;
3514
3515 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003516 * Throws the exception caught by this TryCatch in a way that avoids
3517 * it being caught again by this same TryCatch. As with ThrowException
3518 * it is illegal to execute any JavaScript operations after calling
3519 * ReThrow; the caller must return immediately to where the exception
3520 * is caught.
3521 */
3522 Handle<Value> ReThrow();
3523
3524 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003525 * Returns the exception caught by this try/catch block. If no exception has
3526 * been caught an empty handle is returned.
3527 *
3528 * The returned handle is valid until this TryCatch block has been destroyed.
3529 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003530 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003531
3532 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003533 * Returns the .stack property of the thrown object. If no .stack
3534 * property is present an empty handle is returned.
3535 */
3536 Local<Value> StackTrace() const;
3537
3538 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003539 * Returns the message associated with this exception. If there is
3540 * no message associated an empty handle is returned.
3541 *
3542 * The returned handle is valid until this TryCatch block has been
3543 * destroyed.
3544 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003545 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003546
3547 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003548 * Clears any exceptions that may have been caught by this try/catch block.
3549 * After this method has been called, HasCaught() will return false.
3550 *
3551 * It is not necessary to clear a try/catch block before using it again; if
3552 * another exception is thrown the previously caught exception will just be
3553 * overwritten. However, it is often a good idea since it makes it easier
3554 * to determine which operation threw a given exception.
3555 */
3556 void Reset();
3557
v8.team.kasperl727e9952008-09-02 14:56:44 +00003558 /**
3559 * Set verbosity of the external exception handler.
3560 *
3561 * By default, exceptions that are caught by an external exception
3562 * handler are not reported. Call SetVerbose with true on an
3563 * external exception handler to have exceptions caught by the
3564 * handler reported as if they were not caught.
3565 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003566 void SetVerbose(bool value);
3567
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003568 /**
3569 * Set whether or not this TryCatch should capture a Message object
3570 * which holds source information about where the exception
3571 * occurred. True by default.
3572 */
3573 void SetCaptureMessage(bool value);
3574
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003575 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003576 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003577 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003578 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003579 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003580 bool is_verbose_ : 1;
3581 bool can_continue_ : 1;
3582 bool capture_message_ : 1;
3583 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003584
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003585 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586};
3587
3588
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003589// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003590
3591
3592/**
3593 * Ignore
3594 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003595class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596 public:
3597 ExtensionConfiguration(int name_count, const char* names[])
3598 : name_count_(name_count), names_(names) { }
3599 private:
3600 friend class ImplementationUtilities;
3601 int name_count_;
3602 const char** names_;
3603};
3604
3605
3606/**
3607 * A sandboxed execution context with its own set of built-in objects
3608 * and functions.
3609 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003610class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003611 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003612 /**
3613 * Returns the global proxy object or global object itself for
3614 * detached contexts.
3615 *
3616 * Global proxy object is a thin wrapper whose prototype points to
3617 * actual context's global object with the properties like Object, etc.
3618 * This is done that way for security reasons (for more details see
3619 * https://wiki.mozilla.org/Gecko:SplitWindow).
3620 *
3621 * Please note that changes to global proxy object prototype most probably
3622 * would break VM---v8 expects only global object as a prototype of
3623 * global proxy object.
3624 *
3625 * If DetachGlobal() has been invoked, Global() would return actual global
3626 * object until global is reattached with ReattachGlobal().
3627 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628 Local<Object> Global();
3629
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003630 /**
3631 * Detaches the global object from its context before
3632 * the global object can be reused to create a new context.
3633 */
3634 void DetachGlobal();
3635
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003636 /**
3637 * Reattaches a global object to a context. This can be used to
3638 * restore the connection between a global object and a context
3639 * after DetachGlobal has been called.
3640 *
3641 * \param global_object The global object to reattach to the
3642 * context. For this to work, the global object must be the global
3643 * object that was associated with this context before a call to
3644 * DetachGlobal.
3645 */
3646 void ReattachGlobal(Handle<Object> global_object);
3647
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003648 /** Creates a new context.
3649 *
3650 * Returns a persistent handle to the newly allocated context. This
3651 * persistent handle has to be disposed when the context is no
3652 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003653 *
3654 * \param extensions An optional extension configuration containing
3655 * the extensions to be installed in the newly created context.
3656 *
3657 * \param global_template An optional object template from which the
3658 * global object for the newly created context will be created.
3659 *
3660 * \param global_object An optional global object to be reused for
3661 * the newly created context. This global object must have been
3662 * created by a previous call to Context::New with the same global
3663 * template. The state of the global object will be completely reset
3664 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003665 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003666 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003667 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003668 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3669 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003670
kasper.lund44510672008-07-25 07:37:58 +00003671 /** Returns the last entered context. */
3672 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003673
kasper.lund44510672008-07-25 07:37:58 +00003674 /** Returns the context that is on the top of the stack. */
3675 static Local<Context> GetCurrent();
3676
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003677 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003678 * Returns the context of the calling JavaScript code. That is the
3679 * context of the top-most JavaScript frame. If there are no
3680 * JavaScript frames an empty handle is returned.
3681 */
3682 static Local<Context> GetCalling();
3683
3684 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003685 * Sets the security token for the context. To access an object in
3686 * another context, the security tokens must match.
3687 */
3688 void SetSecurityToken(Handle<Value> token);
3689
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003690 /** Restores the security token to the default value. */
3691 void UseDefaultSecurityToken();
3692
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003693 /** Returns the security token of this context.*/
3694 Handle<Value> GetSecurityToken();
3695
v8.team.kasperl727e9952008-09-02 14:56:44 +00003696 /**
3697 * Enter this context. After entering a context, all code compiled
3698 * and run is compiled and run in this context. If another context
3699 * is already entered, this old context is saved so it can be
3700 * restored when the new context is exited.
3701 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003702 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003703
3704 /**
3705 * Exit this context. Exiting the current context restores the
3706 * context that was in place when entering the current context.
3707 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003708 void Exit();
3709
v8.team.kasperl727e9952008-09-02 14:56:44 +00003710 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711 bool HasOutOfMemoryException();
3712
v8.team.kasperl727e9952008-09-02 14:56:44 +00003713 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003714 static bool InContext();
3715
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003717 * Associate an additional data object with the context. This is mainly used
3718 * with the debugger to provide additional information on the context through
3719 * the debugger API.
3720 */
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003721 void SetData(Handle<Value> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003722 Local<Value> GetData();
3723
3724 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003725 * Control whether code generation from strings is allowed. Calling
3726 * this method with false will disable 'eval' and the 'Function'
3727 * constructor for code running in this context. If 'eval' or the
3728 * 'Function' constructor are used an exception will be thrown.
3729 *
3730 * If code generation from strings is not allowed the
3731 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3732 * set before blocking the call to 'eval' or the 'Function'
3733 * constructor. If that callback returns true, the call will be
3734 * allowed, otherwise an exception will be thrown. If no callback is
3735 * set an exception will be thrown.
3736 */
3737 void AllowCodeGenerationFromStrings(bool allow);
3738
3739 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003740 * Returns true if code generation from strings is allowed for the context.
3741 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3742 */
3743 bool IsCodeGenerationFromStringsAllowed();
3744
3745 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003746 * Sets the error description for the exception that is thrown when
3747 * code generation from strings is not allowed and 'eval' or the 'Function'
3748 * constructor are called.
3749 */
3750 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3751
3752 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003753 * Stack-allocated class which sets the execution context for all
3754 * operations executed within a local scope.
3755 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003756 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003757 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003758 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003759 context_->Enter();
3760 }
3761 inline ~Scope() { context_->Exit(); }
3762 private:
3763 Handle<Context> context_;
3764 };
3765
3766 private:
3767 friend class Value;
3768 friend class Script;
3769 friend class Object;
3770 friend class Function;
3771};
3772
3773
3774/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003775 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003776 * is allowed to use any given V8 isolate. See Isolate class
3777 * comments. The definition of 'using V8 isolate' includes
3778 * accessing handles or holding onto object pointers obtained
3779 * from V8 handles while in the particular V8 isolate. It is up
3780 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003781 * constraint is not violated. In addition to any other synchronization
3782 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3783 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003785 * v8::Locker is a scoped lock object. While it's
3786 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003787 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3788 * locked by at most one thread at any time. In other words, the scope of a
3789 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003790 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003791 * Sample usage:
3792* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003793 * ...
3794 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003795 * v8::Locker locker(isolate);
3796 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003797 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003798 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003799 * ...
3800 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003801 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003802 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003803 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804 * by destroying the v8::Locker object as above or by constructing a
3805 * v8::Unlocker object:
3806 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003807 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003808 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003809 * isolate->Exit();
3810 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003811 * ...
3812 * // Code not using V8 goes here while V8 can run in another thread.
3813 * ...
3814 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003815 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003816 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003817 *
3818 * The Unlocker object is intended for use in a long-running callback
3819 * from V8, where you want to release the V8 lock for other threads to
3820 * use.
3821 *
3822 * The v8::Locker is a recursive lock. That is, you can lock more than
3823 * once in a given thread. This can be useful if you have code that can
3824 * be called either from code that holds the lock or from code that does
3825 * not. The Unlocker is not recursive so you can not have several
3826 * Unlockers on the stack at once, and you can not use an Unlocker in a
3827 * thread that is not inside a Locker's scope.
3828 *
3829 * An unlocker will unlock several lockers if it has to and reinstate
3830 * the correct depth of locking on its destruction. eg.:
3831 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003832 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833 * // V8 not locked.
3834 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003835 * v8::Locker locker(isolate);
3836 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003837 * // V8 locked.
3838 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003839 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840 * // V8 still locked (2 levels).
3841 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003842 * isolate->Exit();
3843 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003844 * // V8 not locked.
3845 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003846 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003847 * // V8 locked again (2 levels).
3848 * }
3849 * // V8 still locked (1 level).
3850 * }
3851 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003852 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003853 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003854 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003855 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003856class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003857 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003858 /**
3859 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3860 */
3861 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003862 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003863 private:
3864 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003865};
3866
3867
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003868class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003870 /**
3871 * Initialize Locker for a given Isolate. NULL means default isolate.
3872 */
3873 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003874 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003875
3876 /**
3877 * Start preemption.
3878 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003879 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003880 * that will switch between multiple threads that are in contention
3881 * for the V8 lock.
3882 */
3883 static void StartPreemption(int every_n_ms);
3884
3885 /**
3886 * Stop preemption.
3887 */
3888 static void StopPreemption();
3889
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003890 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003891 * Returns whether or not the locker for a given isolate, or default isolate
3892 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003893 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003894 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003895
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003896 /**
3897 * Returns whether v8::Locker is being used by this V8 instance.
3898 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003899 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003900
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003901 private:
3902 bool has_lock_;
3903 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003904 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003905
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003906 static bool active_;
3907
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003908 // Disallow copying and assigning.
3909 Locker(const Locker&);
3910 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911};
3912
3913
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003914/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003915 * A struct for exporting HeapStats data from V8, using "push" model.
3916 */
3917struct HeapStatsUpdate;
3918
3919
3920/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003921 * An interface for exporting data from V8, using "push" model.
3922 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003923class V8EXPORT OutputStream { // NOLINT
3924 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003925 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003926 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003927 };
3928 enum WriteResult {
3929 kContinue = 0,
3930 kAbort = 1
3931 };
3932 virtual ~OutputStream() {}
3933 /** Notify about the end of stream. */
3934 virtual void EndOfStream() = 0;
3935 /** Get preferred output chunk size. Called only once. */
3936 virtual int GetChunkSize() { return 1024; }
3937 /** Get preferred output encoding. Called only once. */
3938 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3939 /**
3940 * Writes the next chunk of snapshot data into the stream. Writing
3941 * can be stopped by returning kAbort as function result. EndOfStream
3942 * will not be called in case writing was aborted.
3943 */
3944 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003945 /**
3946 * Writes the next chunk of heap stats data into the stream. Writing
3947 * can be stopped by returning kAbort as function result. EndOfStream
3948 * will not be called in case writing was aborted.
3949 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003950 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00003951 return kAbort;
3952 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003953};
3954
3955
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003956/**
3957 * An interface for reporting progress and controlling long-running
3958 * activities.
3959 */
3960class V8EXPORT ActivityControl { // NOLINT
3961 public:
3962 enum ControlOption {
3963 kContinue = 0,
3964 kAbort = 1
3965 };
3966 virtual ~ActivityControl() {}
3967 /**
3968 * Notify about current progress. The activity can be stopped by
3969 * returning kAbort as the callback result.
3970 */
3971 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3972};
3973
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003974
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003975// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003976
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003977
3978namespace internal {
3979
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003980const int kApiPointerSize = sizeof(void*); // NOLINT
3981const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003982
3983// Tag information for HeapObject.
3984const int kHeapObjectTag = 1;
3985const int kHeapObjectTagSize = 2;
3986const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3987
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003988// Tag information for Smi.
3989const int kSmiTag = 0;
3990const int kSmiTagSize = 1;
3991const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3992
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003993template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003994
3995// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003996template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003997 static const int kSmiShiftSize = 0;
3998 static const int kSmiValueSize = 31;
3999 static inline int SmiToInt(internal::Object* value) {
4000 int shift_bits = kSmiTagSize + kSmiShiftSize;
4001 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4002 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4003 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004004
4005 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
4006 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004007 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004008 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004009};
4010
4011// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004012template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004013 static const int kSmiShiftSize = 31;
4014 static const int kSmiValueSize = 32;
4015 static inline int SmiToInt(internal::Object* value) {
4016 int shift_bits = kSmiTagSize + kSmiShiftSize;
4017 // Shift down and throw away top 32 bits.
4018 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4019 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004020
4021 // To maximize the range of pointers that can be encoded
4022 // in the available 32 bits, we require them to be 8 bytes aligned.
4023 // This gives 2 ^ (32 + 3) = 32G address space covered.
4024 // It might be not enough to cover stack allocated objects on some platforms.
4025 static const int kPointerAlignment = 3;
4026
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004027 static const uintptr_t kEncodablePointerMask =
4028 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004029
4030 static const int kPointerToSmiShift =
4031 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004032};
4033
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004034typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4035const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4036const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004037const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004038 PlatformSmiTagging::kEncodablePointerMask;
4039const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004040
4041/**
4042 * This class exports constants and functionality from within v8 that
4043 * is necessary to implement inline functions in the v8 api. Don't
4044 * depend on functions and constants defined here.
4045 */
4046class Internals {
4047 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004048 // These values match non-compiler-dependent values defined within
4049 // the implementation of v8.
4050 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004051 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004052 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004053
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004054 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004055 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004056 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004057 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004058 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004059 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004060 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004061
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004062 static const int kIsolateStateOffset = 0;
4063 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4064 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4065 static const int kUndefinedValueRootIndex = 5;
4066 static const int kNullValueRootIndex = 7;
4067 static const int kTrueValueRootIndex = 8;
4068 static const int kFalseValueRootIndex = 9;
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00004069 static const int kEmptySymbolRootIndex = 116;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004070
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004071 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004072 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004073 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004074 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004075
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004076 static const int kUndefinedOddballKind = 5;
4077 static const int kNullOddballKind = 3;
4078
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004079 static inline bool HasHeapObjectTag(internal::Object* value) {
4080 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4081 kHeapObjectTag);
4082 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004083
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004084 static inline bool HasSmiTag(internal::Object* value) {
4085 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
4086 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004087
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004088 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004089 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004090 }
4091
4092 static inline int GetInstanceType(internal::Object* obj) {
4093 typedef internal::Object O;
4094 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4095 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4096 }
4097
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004098 static inline int GetOddballKind(internal::Object* obj) {
4099 typedef internal::Object O;
4100 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4101 }
4102
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004103 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004104 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004105 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
4106 }
4107
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004108 static inline void* GetExternalPointer(internal::Object* obj) {
4109 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004110 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004111 } else if (GetInstanceType(obj) == kForeignType) {
4112 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004113 } else {
4114 return NULL;
4115 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004116 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004117
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004118 static inline bool IsExternalTwoByteString(int instance_type) {
4119 int representation = (instance_type & kFullStringRepresentationMask);
4120 return representation == kExternalTwoByteRepresentationTag;
4121 }
4122
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004123 static inline bool IsInitialized(v8::Isolate* isolate) {
4124 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4125 return *reinterpret_cast<int*>(addr) == 1;
4126 }
4127
4128 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
4129 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4130 kIsolateEmbedderDataOffset;
4131 *reinterpret_cast<void**>(addr) = data;
4132 }
4133
4134 static inline void* GetEmbedderData(v8::Isolate* isolate) {
4135 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4136 kIsolateEmbedderDataOffset;
4137 return *reinterpret_cast<void**>(addr);
4138 }
4139
4140 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
4141 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4142 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4143 }
4144
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004145 template <typename T>
4146 static inline T ReadField(Object* ptr, int offset) {
4147 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4148 return *reinterpret_cast<T*>(addr);
4149 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004150
4151 static inline bool CanCastToHeapObject(void* o) { return false; }
4152 static inline bool CanCastToHeapObject(Context* o) { return true; }
4153 static inline bool CanCastToHeapObject(String* o) { return true; }
4154 static inline bool CanCastToHeapObject(Object* o) { return true; }
4155 static inline bool CanCastToHeapObject(Message* o) { return true; }
4156 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
4157 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004158};
4159
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004160} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004161
4162
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004163template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004164Local<T>::Local() : Handle<T>() { }
4165
4166
4167template <class T>
4168Local<T> Local<T>::New(Handle<T> that) {
4169 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004170 T* that_ptr = *that;
4171 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4172 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4173 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4174 reinterpret_cast<internal::HeapObject*>(*p))));
4175 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004176 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4177}
4178
4179
4180template <class T>
4181Persistent<T> Persistent<T>::New(Handle<T> that) {
4182 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004183 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004184 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4185}
4186
4187
4188template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004189bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004190 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004191 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004192}
4193
4194
4195template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004196bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004197 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004198 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004199}
4200
4201
4202template <class T>
4203void Persistent<T>::Dispose() {
4204 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004205 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004206}
4207
4208
4209template <class T>
4210Persistent<T>::Persistent() : Handle<T>() { }
4211
4212template <class T>
4213void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004214 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4215 parameters,
4216 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004217}
4218
4219template <class T>
4220void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004221 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222}
4223
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004224template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004225void Persistent<T>::MarkIndependent() {
4226 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4227}
4228
4229template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004230void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4231 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4232}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004233
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004234Arguments::Arguments(internal::Object** implicit_args,
4235 internal::Object** values, int length,
4236 bool is_construct_call)
4237 : implicit_args_(implicit_args),
4238 values_(values),
4239 length_(length),
4240 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004241
4242
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004243Local<Value> Arguments::operator[](int i) const {
4244 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4245 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4246}
4247
4248
4249Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004250 return Local<Function>(reinterpret_cast<Function*>(
4251 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004252}
4253
4254
4255Local<Object> Arguments::This() const {
4256 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4257}
4258
4259
4260Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004261 return Local<Object>(reinterpret_cast<Object*>(
4262 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004263}
4264
4265
4266Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004267 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004268}
4269
4270
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004271Isolate* Arguments::GetIsolate() const {
4272 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4273}
4274
4275
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004276bool Arguments::IsConstructCall() const {
4277 return is_construct_call_;
4278}
4279
4280
4281int Arguments::Length() const {
4282 return length_;
4283}
4284
4285
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004286template <class T>
4287Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004288 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4289 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004290 return Local<T>(reinterpret_cast<T*>(after));
4291}
4292
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004293Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004294 return resource_name_;
4295}
4296
4297
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004298Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004299 return resource_line_offset_;
4300}
4301
4302
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004303Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004304 return resource_column_offset_;
4305}
4306
4307
4308Handle<Boolean> Boolean::New(bool value) {
4309 return value ? True() : False();
4310}
4311
4312
4313void Template::Set(const char* name, v8::Handle<Data> value) {
4314 Set(v8::String::New(name), value);
4315}
4316
4317
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004318Local<Value> Object::GetInternalField(int index) {
4319#ifndef V8_ENABLE_CHECKS
4320 Local<Value> quick_result = UncheckedGetInternalField(index);
4321 if (!quick_result.IsEmpty()) return quick_result;
4322#endif
4323 return CheckedGetInternalField(index);
4324}
4325
4326
4327Local<Value> Object::UncheckedGetInternalField(int index) {
4328 typedef internal::Object O;
4329 typedef internal::Internals I;
4330 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004331 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004332 // If the object is a plain JSObject, which is the common case,
4333 // we know where to find the internal fields and can return the
4334 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004335 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004336 O* value = I::ReadField<O*>(obj, offset);
4337 O** result = HandleScope::CreateHandle(value);
4338 return Local<Value>(reinterpret_cast<Value*>(result));
4339 } else {
4340 return Local<Value>();
4341 }
4342}
4343
4344
4345void* External::Unwrap(Handle<v8::Value> obj) {
4346#ifdef V8_ENABLE_CHECKS
4347 return FullUnwrap(obj);
4348#else
4349 return QuickUnwrap(obj);
4350#endif
4351}
4352
4353
4354void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4355 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004356 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004357 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004358}
4359
4360
4361void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004362 typedef internal::Object O;
4363 typedef internal::Internals I;
4364
4365 O* obj = *reinterpret_cast<O**>(this);
4366
4367 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4368 // If the object is a plain JSObject, which is the common case,
4369 // we know where to find the internal fields and can return the
4370 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004371 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004372 O* value = I::ReadField<O*>(obj, offset);
4373 return I::GetExternalPointer(value);
4374 }
4375
4376 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004377}
4378
4379
4380String* String::Cast(v8::Value* value) {
4381#ifdef V8_ENABLE_CHECKS
4382 CheckCast(value);
4383#endif
4384 return static_cast<String*>(value);
4385}
4386
4387
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004388Local<String> String::Empty(Isolate* isolate) {
4389 typedef internal::Object* S;
4390 typedef internal::Internals I;
4391 if (!I::IsInitialized(isolate)) return Empty();
4392 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4393 return Local<String>(reinterpret_cast<String*>(slot));
4394}
4395
4396
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004397String::ExternalStringResource* String::GetExternalStringResource() const {
4398 typedef internal::Object O;
4399 typedef internal::Internals I;
4400 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004401 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004402 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004403 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4404 result = reinterpret_cast<String::ExternalStringResource*>(value);
4405 } else {
4406 result = NULL;
4407 }
4408#ifdef V8_ENABLE_CHECKS
4409 VerifyExternalStringResource(result);
4410#endif
4411 return result;
4412}
4413
4414
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004415String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4416 String::Encoding* encoding_out) const {
4417 typedef internal::Object O;
4418 typedef internal::Internals I;
4419 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4420 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4421 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4422 ExternalStringResourceBase* resource = NULL;
4423 if (type == I::kExternalAsciiRepresentationTag ||
4424 type == I::kExternalTwoByteRepresentationTag) {
4425 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4426 resource = static_cast<ExternalStringResourceBase*>(value);
4427 }
4428#ifdef V8_ENABLE_CHECKS
4429 VerifyExternalStringResourceBase(resource, *encoding_out);
4430#endif
4431 return resource;
4432}
4433
4434
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004435bool Value::IsUndefined() const {
4436#ifdef V8_ENABLE_CHECKS
4437 return FullIsUndefined();
4438#else
4439 return QuickIsUndefined();
4440#endif
4441}
4442
4443bool Value::QuickIsUndefined() const {
4444 typedef internal::Object O;
4445 typedef internal::Internals I;
4446 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4447 if (!I::HasHeapObjectTag(obj)) return false;
4448 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4449 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4450}
4451
4452
4453bool Value::IsNull() const {
4454#ifdef V8_ENABLE_CHECKS
4455 return FullIsNull();
4456#else
4457 return QuickIsNull();
4458#endif
4459}
4460
4461bool Value::QuickIsNull() const {
4462 typedef internal::Object O;
4463 typedef internal::Internals I;
4464 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4465 if (!I::HasHeapObjectTag(obj)) return false;
4466 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4467 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4468}
4469
4470
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004471bool Value::IsString() const {
4472#ifdef V8_ENABLE_CHECKS
4473 return FullIsString();
4474#else
4475 return QuickIsString();
4476#endif
4477}
4478
4479bool Value::QuickIsString() const {
4480 typedef internal::Object O;
4481 typedef internal::Internals I;
4482 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4483 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004484 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004485}
4486
4487
4488Number* Number::Cast(v8::Value* value) {
4489#ifdef V8_ENABLE_CHECKS
4490 CheckCast(value);
4491#endif
4492 return static_cast<Number*>(value);
4493}
4494
4495
4496Integer* Integer::Cast(v8::Value* value) {
4497#ifdef V8_ENABLE_CHECKS
4498 CheckCast(value);
4499#endif
4500 return static_cast<Integer*>(value);
4501}
4502
4503
4504Date* Date::Cast(v8::Value* value) {
4505#ifdef V8_ENABLE_CHECKS
4506 CheckCast(value);
4507#endif
4508 return static_cast<Date*>(value);
4509}
4510
4511
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004512StringObject* StringObject::Cast(v8::Value* value) {
4513#ifdef V8_ENABLE_CHECKS
4514 CheckCast(value);
4515#endif
4516 return static_cast<StringObject*>(value);
4517}
4518
4519
4520NumberObject* NumberObject::Cast(v8::Value* value) {
4521#ifdef V8_ENABLE_CHECKS
4522 CheckCast(value);
4523#endif
4524 return static_cast<NumberObject*>(value);
4525}
4526
4527
4528BooleanObject* BooleanObject::Cast(v8::Value* value) {
4529#ifdef V8_ENABLE_CHECKS
4530 CheckCast(value);
4531#endif
4532 return static_cast<BooleanObject*>(value);
4533}
4534
4535
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004536RegExp* RegExp::Cast(v8::Value* value) {
4537#ifdef V8_ENABLE_CHECKS
4538 CheckCast(value);
4539#endif
4540 return static_cast<RegExp*>(value);
4541}
4542
4543
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004544Object* Object::Cast(v8::Value* value) {
4545#ifdef V8_ENABLE_CHECKS
4546 CheckCast(value);
4547#endif
4548 return static_cast<Object*>(value);
4549}
4550
4551
4552Array* Array::Cast(v8::Value* value) {
4553#ifdef V8_ENABLE_CHECKS
4554 CheckCast(value);
4555#endif
4556 return static_cast<Array*>(value);
4557}
4558
4559
4560Function* Function::Cast(v8::Value* value) {
4561#ifdef V8_ENABLE_CHECKS
4562 CheckCast(value);
4563#endif
4564 return static_cast<Function*>(value);
4565}
4566
4567
4568External* External::Cast(v8::Value* value) {
4569#ifdef V8_ENABLE_CHECKS
4570 CheckCast(value);
4571#endif
4572 return static_cast<External*>(value);
4573}
4574
4575
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004576Isolate* AccessorInfo::GetIsolate() const {
4577 return *reinterpret_cast<Isolate**>(&args_[-3]);
4578}
4579
4580
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004581Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004582 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004583}
4584
4585
4586Local<Object> AccessorInfo::This() const {
4587 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4588}
4589
4590
4591Local<Object> AccessorInfo::Holder() const {
4592 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4593}
4594
4595
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004596Handle<Primitive> Undefined(Isolate* isolate) {
4597 typedef internal::Object* S;
4598 typedef internal::Internals I;
4599 if (!I::IsInitialized(isolate)) return Undefined();
4600 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4601 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4602}
4603
4604
4605Handle<Primitive> Null(Isolate* isolate) {
4606 typedef internal::Object* S;
4607 typedef internal::Internals I;
4608 if (!I::IsInitialized(isolate)) return Null();
4609 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4610 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4611}
4612
4613
4614Handle<Boolean> True(Isolate* isolate) {
4615 typedef internal::Object* S;
4616 typedef internal::Internals I;
4617 if (!I::IsInitialized(isolate)) return True();
4618 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4619 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4620}
4621
4622
4623Handle<Boolean> False(Isolate* isolate) {
4624 typedef internal::Object* S;
4625 typedef internal::Internals I;
4626 if (!I::IsInitialized(isolate)) return False();
4627 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4628 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4629}
4630
4631
4632void Isolate::SetData(void* data) {
4633 typedef internal::Internals I;
4634 I::SetEmbedderData(this, data);
4635}
4636
4637
4638void* Isolate::GetData() {
4639 typedef internal::Internals I;
4640 return I::GetEmbedderData(this);
4641}
4642
4643
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004644/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004645 * \example shell.cc
4646 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004647 * command-line and executes them.
4648 */
4649
4650
4651/**
4652 * \example process.cc
4653 */
4654
4655
4656} // namespace v8
4657
4658
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004659#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004660#undef TYPE_CHECK
4661
4662
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004663#endif // V8_H_