blob: 73b7fbe4c439f523f43621783767d380a3d1202c [file] [log] [blame]
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001// Copyright 2011 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
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000065// Setup for Linux shared library export. There is no need to distinguish
66// between building or using the V8 shared library, but we should not
67// export symbols when we are building a static library.
68#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000070#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000071#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000072#endif // defined(__GNUC__) && (__GNUC__ >= 4)
73
74#endif // _WIN32
75
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000077 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 */
79namespace v8 {
80
81class Context;
82class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000083class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084class Value;
85class Utils;
86class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000087class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088class Object;
89class Array;
90class Int32;
91class Uint32;
92class External;
93class Primitive;
94class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000095class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096class Integer;
97class Function;
98class Date;
99class ImplementationUtilities;
100class Signature;
101template <class T> class Handle;
102template <class T> class Local;
103template <class T> class Persistent;
104class FunctionTemplate;
105class ObjectTemplate;
106class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000107class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000108class StackTrace;
109class StackFrame;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000111namespace internal {
112
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000113class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000114class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000115class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000116class HeapObject;
117class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000118}
119
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000121// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122
123
124/**
125 * A weak reference callback function.
126 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000127 * This callback should either explicitly invoke Dispose on |object| if
128 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
129 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 * \param object the weak global object to be reclaimed by the garbage collector
131 * \param parameter the value passed in when making the weak global object
132 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000133typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 void* parameter);
135
136
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000137// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000139#define TYPE_CHECK(T, S) \
140 while (false) { \
141 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 }
143
144/**
145 * An object reference managed by the v8 garbage collector.
146 *
147 * All objects returned from v8 have to be tracked by the garbage
148 * collector so that it knows that the objects are still alive. Also,
149 * because the garbage collector may move objects, it is unsafe to
150 * point directly to an object. Instead, all objects are stored in
151 * handles which are known by the garbage collector and updated
152 * whenever an object moves. Handles should always be passed by value
153 * (except in cases like out-parameters) and they should never be
154 * allocated on the heap.
155 *
156 * There are two types of handles: local and persistent handles.
157 * Local handles are light-weight and transient and typically used in
158 * local operations. They are managed by HandleScopes. Persistent
159 * handles can be used when storing objects across several independent
160 * operations and have to be explicitly deallocated when they're no
161 * longer used.
162 *
163 * It is safe to extract the object stored in the handle by
164 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000165 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 * behind the scenes and the same rules apply to these values as to
167 * their handles.
168 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000169template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 /**
172 * Creates an empty handle.
173 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000174 inline Handle() : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175
176 /**
177 * Creates a new handle for the specified value.
178 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000179 inline explicit Handle(T* val) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
181 /**
182 * Creates a handle for the contents of the specified handle. This
183 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000184 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000186 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 * between compatible handles, for instance assigning a
188 * Handle<String> to a variable declared as Handle<Value>, is legal
189 * because String is a subclass of Value.
190 */
191 template <class S> inline Handle(Handle<S> that)
192 : val_(reinterpret_cast<T*>(*that)) {
193 /**
194 * This check fails when trying to convert between incompatible
195 * handles. For example, converting from a Handle<String> to a
196 * Handle<Number>.
197 */
198 TYPE_CHECK(T, S);
199 }
200
201 /**
202 * Returns true if the handle is empty.
203 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000204 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 /**
207 * Sets the handle to be empty. IsEmpty() will then return true.
208 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000209 inline void Clear() { val_ = 0; }
210
211 inline T* operator->() const { return val_; }
212
213 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214
215 /**
216 * Checks whether two handles are the same.
217 * Returns true if both are empty, or if the objects
218 * to which they refer are identical.
219 * The handles' references are not checked.
220 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000221 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000222 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
223 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 if (a == 0) return b == 0;
225 if (b == 0) return false;
226 return *a == *b;
227 }
228
229 /**
230 * Checks whether two handles are different.
231 * Returns true if only one of the handles is empty, or if
232 * the objects to which they refer are different.
233 * The handles' references are not checked.
234 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000235 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236 return !operator==(that);
237 }
238
239 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000240#ifdef V8_ENABLE_CHECKS
241 // If we're going to perform the type check then we have to check
242 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000244#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245 return Handle<T>(T::Cast(*that));
246 }
247
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000248 template <class S> inline Handle<S> As() {
249 return Handle<S>::Cast(*this);
250 }
251
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252 private:
253 T* val_;
254};
255
256
257/**
258 * A light-weight stack-allocated object handle. All operations
259 * that return objects from within v8 return them in local handles. They
260 * are created within HandleScopes, and all local handles allocated within a
261 * handle scope are destroyed when the handle scope is destroyed. Hence it
262 * is not necessary to explicitly deallocate local handles.
263 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000264template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000266 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 template <class S> inline Local(Local<S> that)
268 : Handle<T>(reinterpret_cast<T*>(*that)) {
269 /**
270 * This check fails when trying to convert between incompatible
271 * handles. For example, converting from a Handle<String> to a
272 * Handle<Number>.
273 */
274 TYPE_CHECK(T, S);
275 }
276 template <class S> inline Local(S* that) : Handle<T>(that) { }
277 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000278#ifdef V8_ENABLE_CHECKS
279 // If we're going to perform the type check then we have to check
280 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000282#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 return Local<T>(T::Cast(*that));
284 }
285
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000286 template <class S> inline Local<S> As() {
287 return Local<S>::Cast(*this);
288 }
289
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 /** Create a local handle for the content of another handle.
291 * The referee is kept alive by the local handle even when
292 * the original handle is destroyed/disposed.
293 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000294 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295};
296
297
298/**
299 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000300 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301 * allocated, a Persistent handle remains valid until it is explicitly
302 * disposed.
303 *
304 * A persistent handle contains a reference to a storage cell within
305 * the v8 engine which holds an object value and which is updated by
306 * the garbage collector whenever the object is moved. A new storage
307 * cell can be created using Persistent::New and existing handles can
308 * be disposed using Persistent::Dispose. Since persistent handles
309 * are passed by value you may have many persistent handle objects
310 * that point to the same storage cell. For instance, if you pass a
311 * persistent handle as an argument to a function you will not get two
312 * different storage cells but rather two references to the same
313 * storage cell.
314 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000315template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 /**
318 * Creates an empty persistent handle that doesn't point to any
319 * storage cell.
320 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000321 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322
323 /**
324 * Creates a persistent handle for the same storage cell as the
325 * specified handle. This constructor allows you to pass persistent
326 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000327 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000329 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 * between compatible persistent handles, for instance assigning a
331 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000332 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 */
334 template <class S> inline Persistent(Persistent<S> that)
335 : Handle<T>(reinterpret_cast<T*>(*that)) {
336 /**
337 * This check fails when trying to convert between incompatible
338 * handles. For example, converting from a Handle<String> to a
339 * Handle<Number>.
340 */
341 TYPE_CHECK(T, S);
342 }
343
344 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
345
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000346 /**
347 * "Casts" a plain handle which is known to be a persistent handle
348 * to a persistent handle.
349 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 template <class S> explicit inline Persistent(Handle<S> that)
351 : Handle<T>(*that) { }
352
353 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000354#ifdef V8_ENABLE_CHECKS
355 // If we're going to perform the type check then we have to check
356 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000358#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 return Persistent<T>(T::Cast(*that));
360 }
361
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000362 template <class S> inline Persistent<S> As() {
363 return Persistent<S>::Cast(*this);
364 }
365
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000367 * Creates a new persistent handle for an existing local or
368 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000370 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371
372 /**
373 * Releases the storage cell referenced by this persistent handle.
374 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000375 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376 * cell remain and IsEmpty will still return false.
377 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000378 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379
380 /**
381 * Make the reference to this object weak. When only weak handles
382 * refer to the object, the garbage collector will perform a
383 * callback to the given V8::WeakReferenceCallback function, passing
384 * it the object reference and the given parameters.
385 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000386 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387
388 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000389 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390
391 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000392 * Marks the reference to this object independent. Garbage collector
393 * is free to ignore any object groups containing this object.
394 * Weak callback for an independent handle should not
395 * assume that it will be preceded by a global GC prologue callback
396 * or followed by a global GC epilogue callback.
397 */
398 inline void MarkIndependent();
399
400 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 *Checks if the handle holds the only reference to an object.
402 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000403 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404
405 /**
406 * Returns true if the handle's reference is weak.
407 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000408 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000410 /**
411 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
412 * interface description in v8-profiler.h for details.
413 */
414 inline void SetWrapperClassId(uint16_t class_id);
415
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 private:
417 friend class ImplementationUtilities;
418 friend class ObjectTemplate;
419};
420
421
v8.team.kasperl727e9952008-09-02 14:56:44 +0000422 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 * A stack-allocated class that governs a number of local handles.
424 * After a handle scope has been created, all local handles will be
425 * allocated within that handle scope until either the handle scope is
426 * deleted or another handle scope is created. If there is already a
427 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000428 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 * new handles will again be allocated in the original handle scope.
430 *
431 * After the handle scope of a local handle has been deleted the
432 * garbage collector will no longer track the object stored in the
433 * handle and may deallocate it. The behavior of accessing a handle
434 * for which the handle scope has been deleted is undefined.
435 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000436class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000438 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000440 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441
442 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443 * Closes the handle scope and returns the value as a handle in the
444 * previous scope, which is the new current scope after the call.
445 */
446 template <class T> Local<T> Close(Handle<T> value);
447
448 /**
449 * Counts the number of allocated handles.
450 */
451 static int NumberOfHandles();
452
453 /**
454 * Creates a new handle with the given value.
455 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000456 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000457 // Faster version, uses HeapObject to obtain the current Isolate.
458 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459
460 private:
461 // Make it impossible to create heap-allocated or illegal handle
462 // scopes by disallowing certain operations.
463 HandleScope(const HandleScope&);
464 void operator=(const HandleScope&);
465 void* operator new(size_t size);
466 void operator delete(void*, size_t);
467
ager@chromium.org3811b432009-10-28 14:53:37 +0000468 // This Data class is accessible internally as HandleScopeData through a
469 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000470 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000472 internal::Object** next;
473 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000474 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000477 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 }
479 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000480
lrn@chromium.org303ada72010-10-27 09:33:13 +0000481 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000483 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000484 internal::Object** prev_next_;
485 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000487 // Allow for the active closing of HandleScopes which allows to pass a handle
488 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000490 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 friend class ImplementationUtilities;
493};
494
495
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000496// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497
498
499/**
500 * The superclass of values and API object templates.
501 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000502class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 private:
504 Data();
505};
506
507
508/**
509 * Pre-compilation data that can be associated with a script. This
510 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000511 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000512 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000514class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 public:
516 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000517
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000518 /**
519 * Pre-compiles the specified script (context-independent).
520 *
521 * \param input Pointer to UTF-8 script source code.
522 * \param length Length of UTF-8 script source code.
523 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000526 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000527 * Pre-compiles the specified script (context-independent).
528 *
529 * NOTE: Pre-compilation using this method cannot happen on another thread
530 * without using Lockers.
531 *
532 * \param source Script source code.
533 */
534 static ScriptData* PreCompile(Handle<String> source);
535
536 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000537 * Load previous pre-compilation data.
538 *
539 * \param data Pointer to data returned by a call to Data() of a previous
540 * ScriptData. Ownership is not transferred.
541 * \param length Length of data.
542 */
543 static ScriptData* New(const char* data, int length);
544
545 /**
546 * Returns the length of Data().
547 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000549
550 /**
551 * Returns a serialized representation of this ScriptData that can later be
552 * passed to New(). NOTE: Serialized data is platform-dependent.
553 */
554 virtual const char* Data() = 0;
555
556 /**
557 * Returns true if the source code could not be parsed.
558 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000559 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560};
561
562
563/**
564 * The origin, within a file, of a script.
565 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000566class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000568 inline ScriptOrigin(
569 Handle<Value> resource_name,
570 Handle<Integer> resource_line_offset = Handle<Integer>(),
571 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 : resource_name_(resource_name),
573 resource_line_offset_(resource_line_offset),
574 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000575 inline Handle<Value> ResourceName() const;
576 inline Handle<Integer> ResourceLineOffset() const;
577 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000579 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 Handle<Integer> resource_line_offset_;
581 Handle<Integer> resource_column_offset_;
582};
583
584
585/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000586 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000588class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000591 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000592 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000593 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000594 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000595 * when New() returns
596 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
597 * using pre_data speeds compilation if it's done multiple times.
598 * Owned by caller, no references are kept when New() returns.
599 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000600 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000601 * available to compile event handlers.
602 * \return Compiled script object (context independent; when run it
603 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000605 static Local<Script> New(Handle<String> source,
606 ScriptOrigin* origin = NULL,
607 ScriptData* pre_data = NULL,
608 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609
mads.s.agercbaa0602008-08-14 13:41:48 +0000610 /**
611 * Compiles the specified script using the specified file name
612 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000613 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000614 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000615 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000616 * as the script's origin.
617 * \return Compiled script object (context independent; when run it
618 * will use the currently entered context).
619 */
620 static Local<Script> New(Handle<String> source,
621 Handle<Value> file_name);
622
623 /**
624 * Compiles the specified script (bound to current context).
625 *
626 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000627 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000628 * when Compile() returns
629 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
630 * using pre_data speeds compilation if it's done multiple times.
631 * Owned by caller, no references are kept when Compile() returns.
632 * \param script_data Arbitrary data associated with script. Using
633 * this has same effect as calling SetData(), but makes data available
634 * earlier (i.e. to compile event handlers).
635 * \return Compiled script object, bound to the context that was active
636 * when this function was called. When run it will always use this
637 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000638 */
639 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000640 ScriptOrigin* origin = NULL,
641 ScriptData* pre_data = NULL,
642 Handle<String> script_data = Handle<String>());
643
644 /**
645 * Compiles the specified script using the specified file name
646 * object (typically a string) as the script's origin.
647 *
648 * \param source Script source code.
649 * \param file_name File name to use as script's origin
650 * \param script_data Arbitrary data associated with script. Using
651 * this has same effect as calling SetData(), but makes data available
652 * earlier (i.e. to compile event handlers).
653 * \return Compiled script object, bound to the context that was active
654 * when this function was called. When run it will always use this
655 * context.
656 */
657 static Local<Script> Compile(Handle<String> source,
658 Handle<Value> file_name,
659 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000660
v8.team.kasperl727e9952008-09-02 14:56:44 +0000661 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000662 * Runs the script returning the resulting value. If the script is
663 * context independent (created using ::New) it will be run in the
664 * currently entered context. If it is context specific (created
665 * using ::Compile) it will be run in the context in which it was
666 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000667 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000669
670 /**
671 * Returns the script id value.
672 */
673 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000674
675 /**
676 * Associate an additional data object with the script. This is mainly used
677 * with the debugger as this data object is only available through the
678 * debugger API.
679 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000680 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681};
682
683
684/**
685 * An error message.
686 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000687class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000689 Local<String> Get() const;
690 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000692 /**
693 * Returns the resource name for the script from where the function causing
694 * the error originates.
695 */
ager@chromium.org32912102009-01-16 10:38:43 +0000696 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000698 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000699 * Returns the resource data for the script from where the function causing
700 * the error originates.
701 */
702 Handle<Value> GetScriptData() const;
703
704 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000705 * Exception stack trace. By default stack traces are not captured for
706 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
707 * to change this option.
708 */
709 Handle<StackTrace> GetStackTrace() const;
710
711 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000712 * Returns the number, 1-based, of the line where the error occurred.
713 */
ager@chromium.org32912102009-01-16 10:38:43 +0000714 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000716 /**
717 * Returns the index within the script of the first character where
718 * the error occurred.
719 */
ager@chromium.org32912102009-01-16 10:38:43 +0000720 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000721
722 /**
723 * Returns the index within the script of the last character where
724 * the error occurred.
725 */
ager@chromium.org32912102009-01-16 10:38:43 +0000726 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000727
728 /**
729 * Returns the index within the line of the first character where
730 * the error occurred.
731 */
ager@chromium.org32912102009-01-16 10:38:43 +0000732 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000733
734 /**
735 * Returns the index within the line of the last character where
736 * the error occurred.
737 */
ager@chromium.org32912102009-01-16 10:38:43 +0000738 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000739
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740 // TODO(1245381): Print to a string instead of on a FILE.
741 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000742
743 static const int kNoLineNumberInfo = 0;
744 static const int kNoColumnInfo = 0;
745};
746
747
748/**
749 * Representation of a JavaScript stack trace. The information collected is a
750 * snapshot of the execution stack and the information remains valid after
751 * execution continues.
752 */
753class V8EXPORT StackTrace {
754 public:
755 /**
756 * Flags that determine what information is placed captured for each
757 * StackFrame when grabbing the current stack trace.
758 */
759 enum StackTraceOptions {
760 kLineNumber = 1,
761 kColumnOffset = 1 << 1 | kLineNumber,
762 kScriptName = 1 << 2,
763 kFunctionName = 1 << 3,
764 kIsEval = 1 << 4,
765 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000766 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000767 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000768 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000769 };
770
771 /**
772 * Returns a StackFrame at a particular index.
773 */
774 Local<StackFrame> GetFrame(uint32_t index) const;
775
776 /**
777 * Returns the number of StackFrames.
778 */
779 int GetFrameCount() const;
780
781 /**
782 * Returns StackTrace as a v8::Array that contains StackFrame objects.
783 */
784 Local<Array> AsArray();
785
786 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000787 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000788 *
789 * \param frame_limit The maximum number of stack frames we want to capture.
790 * \param options Enumerates the set of things we will capture for each
791 * StackFrame.
792 */
793 static Local<StackTrace> CurrentStackTrace(
794 int frame_limit,
795 StackTraceOptions options = kOverview);
796};
797
798
799/**
800 * A single JavaScript stack frame.
801 */
802class V8EXPORT StackFrame {
803 public:
804 /**
805 * Returns the number, 1-based, of the line for the associate function call.
806 * This method will return Message::kNoLineNumberInfo if it is unable to
807 * retrieve the line number, or if kLineNumber was not passed as an option
808 * when capturing the StackTrace.
809 */
810 int GetLineNumber() const;
811
812 /**
813 * Returns the 1-based column offset on the line for the associated function
814 * call.
815 * This method will return Message::kNoColumnInfo if it is unable to retrieve
816 * the column number, or if kColumnOffset was not passed as an option when
817 * capturing the StackTrace.
818 */
819 int GetColumn() const;
820
821 /**
822 * Returns the name of the resource that contains the script for the
823 * function for this StackFrame.
824 */
825 Local<String> GetScriptName() const;
826
827 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000828 * Returns the name of the resource that contains the script for the
829 * function for this StackFrame or sourceURL value if the script name
830 * is undefined and its source ends with //@ sourceURL=... string.
831 */
832 Local<String> GetScriptNameOrSourceURL() const;
833
834 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000835 * Returns the name of the function associated with this stack frame.
836 */
837 Local<String> GetFunctionName() const;
838
839 /**
840 * Returns whether or not the associated function is compiled via a call to
841 * eval().
842 */
843 bool IsEval() const;
844
845 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000846 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000847 * constructor via "new".
848 */
849 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850};
851
852
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000853// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854
855
856/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000857 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000859class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861 /**
862 * Returns true if this value is the undefined value. See ECMA-262
863 * 4.3.10.
864 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000865 V8EXPORT bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866
867 /**
868 * Returns true if this value is the null value. See ECMA-262
869 * 4.3.11.
870 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000871 V8EXPORT bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
873 /**
874 * Returns true if this value is true.
875 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000876 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877
878 /**
879 * Returns true if this value is false.
880 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000881 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882
883 /**
884 * Returns true if this value is an instance of the String type.
885 * See ECMA-262 8.4.
886 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000887 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888
889 /**
890 * Returns true if this value is a function.
891 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000892 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893
894 /**
895 * Returns true if this value is an array.
896 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000897 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
v8.team.kasperl727e9952008-09-02 14:56:44 +0000899 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900 * Returns true if this value is an object.
901 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000902 V8EXPORT bool IsObject() 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 boolean.
906 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000907 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +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 a number.
911 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000912 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913
v8.team.kasperl727e9952008-09-02 14:56:44 +0000914 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 * Returns true if this value is external.
916 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000917 V8EXPORT bool IsExternal() 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 a 32-bit signed integer.
921 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000922 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000924 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000925 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000926 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000927 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000928
929 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000930 * Returns true if this value is a Date.
931 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000932 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000933
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000934 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000935 * Returns true if this value is a Boolean object.
936 */
937 V8EXPORT bool IsBooleanObject() const;
938
939 /**
940 * Returns true if this value is a Number object.
941 */
942 V8EXPORT bool IsNumberObject() const;
943
944 /**
945 * Returns true if this value is a String object.
946 */
947 V8EXPORT bool IsStringObject() const;
948
949 /**
950 * Returns true if this value is a NativeError.
951 */
952 V8EXPORT bool IsNativeError() const;
953
954 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000955 * Returns true if this value is a RegExp.
956 */
957 V8EXPORT bool IsRegExp() const;
958
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000959 V8EXPORT Local<Boolean> ToBoolean() const;
960 V8EXPORT Local<Number> ToNumber() const;
961 V8EXPORT Local<String> ToString() const;
962 V8EXPORT Local<String> ToDetailString() const;
963 V8EXPORT Local<Object> ToObject() const;
964 V8EXPORT Local<Integer> ToInteger() const;
965 V8EXPORT Local<Uint32> ToUint32() const;
966 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967
968 /**
969 * Attempts to convert a string to an array index.
970 * Returns an empty handle if the conversion fails.
971 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000972 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000974 V8EXPORT bool BooleanValue() const;
975 V8EXPORT double NumberValue() const;
976 V8EXPORT int64_t IntegerValue() const;
977 V8EXPORT uint32_t Uint32Value() const;
978 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979
980 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000981 V8EXPORT bool Equals(Handle<Value> that) const;
982 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000983
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000984 private:
985 inline bool QuickIsString() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000986 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987};
988
989
990/**
991 * The superclass of primitive values. See ECMA-262 4.3.2.
992 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000993class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994
995
996/**
997 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
998 * or false value.
999 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001000class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001002 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 static inline Handle<Boolean> New(bool value);
1004};
1005
1006
1007/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001008 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001010class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001012 /**
1013 * Returns the number of characters in this string.
1014 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001015 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016
v8.team.kasperl727e9952008-09-02 14:56:44 +00001017 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001018 * Returns the number of bytes in the UTF-8 encoded
1019 * representation of this string.
1020 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001021 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001022
1023 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001024 * Write the contents of the string to an external buffer.
1025 * If no arguments are given, expects the buffer to be large
1026 * enough to hold the entire string and NULL terminator. Copies
1027 * the contents of the string and the NULL terminator into the
1028 * buffer.
1029 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001030 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1031 * before the end of the buffer.
1032 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001033 * Copies up to length characters into the output buffer.
1034 * Only null-terminates if there is enough space in the buffer.
1035 *
1036 * \param buffer The buffer into which the string will be copied.
1037 * \param start The starting position within the string at which
1038 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001039 * \param length The number of characters to copy from the string. For
1040 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001041 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001042 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001043 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001044 * \return The number of characters copied to the buffer excluding the null
1045 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001046 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001047 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001048 enum WriteOptions {
1049 NO_OPTIONS = 0,
1050 HINT_MANY_WRITES_EXPECTED = 1,
1051 NO_NULL_TERMINATION = 2
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001052 };
1053
lrn@chromium.org34e60782011-09-15 07:25:40 +00001054 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001055 V8EXPORT int Write(uint16_t* buffer,
1056 int start = 0,
1057 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001058 int options = NO_OPTIONS) const;
1059 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001060 V8EXPORT int WriteAscii(char* buffer,
1061 int start = 0,
1062 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001063 int options = NO_OPTIONS) const;
1064 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001065 V8EXPORT int WriteUtf8(char* buffer,
1066 int length = -1,
1067 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001068 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069
v8.team.kasperl727e9952008-09-02 14:56:44 +00001070 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001071 * A zero length string.
1072 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001073 V8EXPORT static v8::Local<v8::String> Empty();
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001074
1075 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001076 * Returns true if the string is external
1077 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001078 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079
v8.team.kasperl727e9952008-09-02 14:56:44 +00001080 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001081 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001082 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001083 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001084
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001085 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001086 public:
1087 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001088
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001089 protected:
1090 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001091
1092 /**
1093 * Internally V8 will call this Dispose method when the external string
1094 * resource is no longer needed. The default implementation will use the
1095 * delete operator. This method can be overridden in subclasses to
1096 * control how allocated external string resources are disposed.
1097 */
1098 virtual void Dispose() { delete this; }
1099
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001100 private:
1101 // Disallow copying and assigning.
1102 ExternalStringResourceBase(const ExternalStringResourceBase&);
1103 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001104
1105 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001106 };
1107
v8.team.kasperl727e9952008-09-02 14:56:44 +00001108 /**
1109 * An ExternalStringResource is a wrapper around a two-byte string
1110 * buffer that resides outside V8's heap. Implement an
1111 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001112 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001113 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001114 class V8EXPORT ExternalStringResource
1115 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 public:
1117 /**
1118 * Override the destructor to manage the life cycle of the underlying
1119 * buffer.
1120 */
1121 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001122
1123 /**
1124 * The string data from the underlying buffer.
1125 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001127
1128 /**
1129 * The length of the string. That is, the number of two-byte characters.
1130 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 protected:
1134 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 };
1136
1137 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001138 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001139 * string buffer that resides outside V8's heap. Implement an
1140 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001141 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001142 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001143 * UTF-8, which would require special treatment internally in the
1144 * engine and, in the case of UTF-8, do not allow efficient indexing.
1145 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001146 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001148 class V8EXPORT ExternalAsciiStringResource
1149 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 public:
1151 /**
1152 * Override the destructor to manage the life cycle of the underlying
1153 * buffer.
1154 */
1155 virtual ~ExternalAsciiStringResource() {}
1156 /** The string data from the underlying buffer.*/
1157 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001158 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 virtual size_t length() const = 0;
1160 protected:
1161 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162 };
1163
1164 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001165 * Get the ExternalStringResource for an external string. Returns
1166 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001168 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169
1170 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001171 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001172 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001174 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1175 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001177 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178
1179 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001180 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001182 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 * be careful to supply the length parameter.
1184 * If it is not given, the function calls
1185 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001186 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001188 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189
lrn@chromium.org34e60782011-09-15 07:25:40 +00001190 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001191 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192
1193 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001194 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195
v8.team.kasperl727e9952008-09-02 14:56:44 +00001196 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001197 * Creates a new string by concatenating the left and the right strings
1198 * passed in as parameters.
1199 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001200 V8EXPORT static Local<String> Concat(Handle<String> left,
1201 Handle<String>right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001202
1203 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001204 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001205 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001206 * resource will be disposed by calling its Dispose method. The caller of
1207 * this function should not otherwise delete or modify the resource. Neither
1208 * should the underlying buffer be deallocated or modified except through the
1209 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001210 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001211 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001212
ager@chromium.org6f10e412009-02-13 10:11:16 +00001213 /**
1214 * Associate an external string resource with this string by transforming it
1215 * in place so that existing references to this string in the JavaScript heap
1216 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001217 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001218 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001219 * The string is not modified if the operation fails. See NewExternal for
1220 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001221 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001222 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223
v8.team.kasperl727e9952008-09-02 14:56:44 +00001224 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001225 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001226 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001227 * resource will be disposed by calling its Dispose method. The caller of
1228 * this function should not otherwise delete or modify the resource. Neither
1229 * should the underlying buffer be deallocated or modified except through the
1230 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001231 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001232 V8EXPORT static Local<String> NewExternal(
1233 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001234
ager@chromium.org6f10e412009-02-13 10:11:16 +00001235 /**
1236 * Associate an external string resource with this string by transforming it
1237 * in place so that existing references to this string in the JavaScript heap
1238 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001239 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001240 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001241 * The string is not modified if the operation fails. See NewExternal for
1242 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001243 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001244 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001246 /**
1247 * Returns true if this string can be made external.
1248 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001249 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001250
lrn@chromium.org34e60782011-09-15 07:25:40 +00001251 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001252 V8EXPORT static Local<String> NewUndetectable(const char* data,
1253 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254
lrn@chromium.org34e60782011-09-15 07:25:40 +00001255 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001256 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1257 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258
1259 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001260 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001261 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001262 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001263 * then the length() method returns 0 and the * operator returns
1264 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001265 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001266 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001267 public:
1268 explicit Utf8Value(Handle<v8::Value> obj);
1269 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001270 char* operator*() { return str_; }
1271 const char* operator*() const { return str_; }
1272 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001273 private:
1274 char* str_;
1275 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001276
1277 // Disallow copying and assigning.
1278 Utf8Value(const Utf8Value&);
1279 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001280 };
1281
1282 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001283 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001285 * If conversion to a string fails (eg. due to an exception in the toString()
1286 * method of the object) then the length() method returns 0 and the * operator
1287 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001289 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 public:
1291 explicit AsciiValue(Handle<v8::Value> obj);
1292 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001293 char* operator*() { return str_; }
1294 const char* operator*() const { return str_; }
1295 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 private:
1297 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001298 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001299
1300 // Disallow copying and assigning.
1301 AsciiValue(const AsciiValue&);
1302 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 };
1304
1305 /**
1306 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001307 * If conversion to a string fails (eg. due to an exception in the toString()
1308 * method of the object) then the length() method returns 0 and the * operator
1309 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001311 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 public:
1313 explicit Value(Handle<v8::Value> obj);
1314 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001315 uint16_t* operator*() { return str_; }
1316 const uint16_t* operator*() const { return str_; }
1317 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 private:
1319 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001320 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001321
1322 // Disallow copying and assigning.
1323 Value(const Value&);
1324 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001326
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001327 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001328 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1329 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330};
1331
1332
1333/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001334 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001335 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001336class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001338 V8EXPORT double Value() const;
1339 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001340 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001342 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001343 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344};
1345
1346
1347/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001348 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001350class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001352 V8EXPORT static Local<Integer> New(int32_t value);
1353 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1354 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001355 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001357 V8EXPORT Integer();
1358 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359};
1360
1361
1362/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001363 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001365class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001367 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001369 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370};
1371
1372
1373/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001374 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001376class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001378 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001380 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381};
1382
1383
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384enum PropertyAttribute {
1385 None = 0,
1386 ReadOnly = 1 << 0,
1387 DontEnum = 1 << 1,
1388 DontDelete = 1 << 2
1389};
1390
ager@chromium.org3811b432009-10-28 14:53:37 +00001391enum ExternalArrayType {
1392 kExternalByteArray = 1,
1393 kExternalUnsignedByteArray,
1394 kExternalShortArray,
1395 kExternalUnsignedShortArray,
1396 kExternalIntArray,
1397 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001398 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001399 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001400 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001401};
1402
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001404 * Accessor[Getter|Setter] are used as callback functions when
1405 * setting|getting a particular property. See Object and ObjectTemplate's
1406 * method SetAccessor.
1407 */
1408typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1409 const AccessorInfo& info);
1410
1411
1412typedef void (*AccessorSetter)(Local<String> property,
1413 Local<Value> value,
1414 const AccessorInfo& info);
1415
1416
1417/**
1418 * Access control specifications.
1419 *
1420 * Some accessors should be accessible across contexts. These
1421 * accessors have an explicit access control parameter which specifies
1422 * the kind of cross-context access that should be allowed.
1423 *
1424 * Additionally, for security, accessors can prohibit overwriting by
1425 * accessors defined in JavaScript. For objects that have such
1426 * accessors either locally or in their prototype chain it is not
1427 * possible to overwrite the accessor by using __defineGetter__ or
1428 * __defineSetter__ from JavaScript code.
1429 */
1430enum AccessControl {
1431 DEFAULT = 0,
1432 ALL_CAN_READ = 1,
1433 ALL_CAN_WRITE = 1 << 1,
1434 PROHIBITS_OVERWRITING = 1 << 2
1435};
1436
1437
1438/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001439 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001441class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001443 V8EXPORT bool Set(Handle<Value> key,
1444 Handle<Value> value,
1445 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001446
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001447 V8EXPORT bool Set(uint32_t index,
1448 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001449
ager@chromium.orge2902be2009-06-08 12:21:35 +00001450 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001451 // overriding accessors or read-only properties.
1452 //
1453 // Note that if the object has an interceptor the property will be set
1454 // locally, but since the interceptor takes precedence the local property
1455 // will only be returned if the interceptor doesn't return a value.
1456 //
1457 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001458 V8EXPORT bool ForceSet(Handle<Value> key,
1459 Handle<Value> value,
1460 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001461
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001462 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001464 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001465
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001466 /**
1467 * Gets the property attributes of a property which can be None or
1468 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1469 * None when the property doesn't exist.
1470 */
1471 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1472
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 // TODO(1245389): Replace the type-specific versions of these
1474 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001475 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001476
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001477 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001478
1479 // Delete a property on this object bypassing interceptors and
1480 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001481 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001482
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001483 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001484
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001485 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001487 V8EXPORT bool SetAccessor(Handle<String> name,
1488 AccessorGetter getter,
1489 AccessorSetter setter = 0,
1490 Handle<Value> data = Handle<Value>(),
1491 AccessControl settings = DEFAULT,
1492 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001493
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001495 * Returns an array containing the names of the enumerable properties
1496 * of this object, including properties from prototype objects. The
1497 * array returned by this method contains the same values as would
1498 * be enumerated by a for-in statement over this object.
1499 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001500 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001501
1502 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001503 * This function has the same functionality as GetPropertyNames but
1504 * the returned array doesn't contain the names of properties from
1505 * prototype objects.
1506 */
1507 V8EXPORT Local<Array> GetOwnPropertyNames();
1508
1509 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001510 * Get the prototype object. This does not skip objects marked to
1511 * be skipped by __proto__ and it does not consult the security
1512 * handler.
1513 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001514 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515
1516 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001517 * Set the prototype object. This does not skip objects marked to
1518 * be skipped by __proto__ and it does not consult the security
1519 * handler.
1520 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001521 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001522
1523 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001524 * Finds an instance of the given function template in the prototype
1525 * chain.
1526 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001527 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1528 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001529
1530 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531 * Call builtin Object.prototype.toString on this object.
1532 * This is different from Value::ToString() that may call
1533 * user-defined toString function. This one does not.
1534 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001535 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001537 /**
1538 * Returns the name of the function invoked as a constructor for this object.
1539 */
1540 V8EXPORT Local<String> GetConstructorName();
1541
kasper.lund212ac232008-07-16 07:07:30 +00001542 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001543 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001544 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001545 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001546 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001547 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001549 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001550 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001551
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001552 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001553 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001554
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001556 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001557 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1558 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1559 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560
1561 /**
1562 * If result.IsEmpty() no real property was located in the prototype chain.
1563 * This means interceptors in the prototype chain are not called.
1564 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001565 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1566 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001567
1568 /**
1569 * If result.IsEmpty() no real property was located on the object or
1570 * in the prototype chain.
1571 * This means interceptors in the prototype chain are not called.
1572 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001573 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574
1575 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001576 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577
kasper.lund212ac232008-07-16 07:07:30 +00001578 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001579 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001581 /**
1582 * Turns on access check on the object if the object is an instance of
1583 * a template that has access check callbacks. If an object has no
1584 * access check info, the object cannot be accessed by anyone.
1585 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001586 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001587
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001588 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001589 * Returns the identity hash for this object. The current implementation
1590 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001591 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001592 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001593 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001594 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001595 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001596
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001597 /**
1598 * Access hidden properties on JavaScript objects. These properties are
1599 * hidden from the executing JavaScript and only accessible through the V8
1600 * C++ API. Hidden properties introduced by V8 internally (for example the
1601 * identity hash) are prefixed with "v8::".
1602 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001603 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1604 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1605 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001606
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001607 /**
1608 * Returns true if this is an instance of an api function (one
1609 * created from a function created from a function template) and has
1610 * been modified since it was created. Note that this method is
1611 * conservative and may return true for objects that haven't actually
1612 * been modified.
1613 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001614 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001615
1616 /**
1617 * Clone this object with a fast but shallow copy. Values will point
1618 * to the same values as the original object.
1619 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001620 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001622 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001623 * Returns the context in which the object was created.
1624 */
1625 V8EXPORT Local<Context> CreationContext();
1626
1627 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001628 * Set the backing store of the indexed properties to be managed by the
1629 * embedding layer. Access to the indexed properties will follow the rules
1630 * spelled out in CanvasPixelArray.
1631 * Note: The embedding program still owns the data and needs to ensure that
1632 * the backing store is preserved while V8 has a reference.
1633 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001634 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001635 V8EXPORT bool HasIndexedPropertiesInPixelData();
1636 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1637 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001638
ager@chromium.org3811b432009-10-28 14:53:37 +00001639 /**
1640 * Set the backing store of the indexed properties to be managed by the
1641 * embedding layer. Access to the indexed properties will follow the rules
1642 * spelled out for the CanvasArray subtypes in the WebGL specification.
1643 * Note: The embedding program still owns the data and needs to ensure that
1644 * the backing store is preserved while V8 has a reference.
1645 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001646 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1647 void* data,
1648 ExternalArrayType array_type,
1649 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001650 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1651 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1652 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1653 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001654
lrn@chromium.org1c092762011-05-09 09:42:16 +00001655 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001656 * Checks whether a callback is set by the
1657 * ObjectTemplate::SetCallAsFunctionHandler method.
1658 * When an Object is callable this method returns true.
1659 */
1660 V8EXPORT bool IsCallable();
1661
1662 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001663 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001664 * ObjectTemplate::SetCallAsFunctionHandler method.
1665 */
1666 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1667 int argc,
1668 Handle<Value> argv[]);
1669
1670 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001671 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001672 * ObjectTemplate::SetCallAsFunctionHandler method.
1673 * Note: This method behaves like the Function::NewInstance method.
1674 */
1675 V8EXPORT Local<Value> CallAsConstructor(int argc,
1676 Handle<Value> argv[]);
1677
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001678 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001679 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001680
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001682 V8EXPORT Object();
1683 V8EXPORT static void CheckCast(Value* obj);
1684 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1685 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001686
1687 /**
1688 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001689 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001690 */
1691 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692};
1693
1694
1695/**
1696 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1697 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001698class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001700 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701
ager@chromium.org3e875802009-06-29 08:26:34 +00001702 /**
1703 * Clones an element at index |index|. Returns an empty
1704 * handle if cloning fails (for any reason).
1705 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001706 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001707
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001708 /**
1709 * Creates a JavaScript array with the given length. If the length
1710 * is negative the returned array will have length 0.
1711 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001712 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001713
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001714 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001716 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001717 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718};
1719
1720
1721/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001722 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001724class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001726 V8EXPORT Local<Object> NewInstance() const;
1727 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1728 V8EXPORT Local<Value> Call(Handle<Object> recv,
1729 int argc,
1730 Handle<Value> argv[]);
1731 V8EXPORT void SetName(Handle<String> name);
1732 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001733
1734 /**
1735 * Returns zero based line number of function body and
1736 * kLineOffsetNotFound if no information available.
1737 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001738 V8EXPORT int GetScriptLineNumber() const;
1739 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001740 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001741 V8EXPORT static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001743 V8EXPORT Function();
1744 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745};
1746
1747
1748/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001749 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1750 */
1751class Date : public Object {
1752 public:
1753 V8EXPORT static Local<Value> New(double time);
1754
1755 /**
1756 * A specialization of Value::NumberValue that is more efficient
1757 * because we know the structure of this object.
1758 */
1759 V8EXPORT double NumberValue() const;
1760
1761 static inline Date* Cast(v8::Value* obj);
1762
1763 /**
1764 * Notification that the embedder has changed the time zone,
1765 * daylight savings time, or other date / time configuration
1766 * parameters. V8 keeps a cache of various values used for
1767 * date / time computation. This notification will reset
1768 * those cached values for the current context so that date /
1769 * time configuration changes would be reflected in the Date
1770 * object.
1771 *
1772 * This API should not be called more than needed as it will
1773 * negatively impact the performance of date operations.
1774 */
1775 V8EXPORT static void DateTimeConfigurationChangeNotification();
1776
1777 private:
1778 V8EXPORT static void CheckCast(v8::Value* obj);
1779};
1780
1781
1782/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001783 * A Number object (ECMA-262, 4.3.21).
1784 */
1785class NumberObject : public Object {
1786 public:
1787 V8EXPORT static Local<Value> New(double value);
1788
1789 /**
1790 * Returns the Number held by the object.
1791 */
1792 V8EXPORT double NumberValue() const;
1793
1794 static inline NumberObject* Cast(v8::Value* obj);
1795
1796 private:
1797 V8EXPORT static void CheckCast(v8::Value* obj);
1798};
1799
1800
1801/**
1802 * A Boolean object (ECMA-262, 4.3.15).
1803 */
1804class BooleanObject : public Object {
1805 public:
1806 V8EXPORT static Local<Value> New(bool value);
1807
1808 /**
1809 * Returns the Boolean held by the object.
1810 */
1811 V8EXPORT bool BooleanValue() const;
1812
1813 static inline BooleanObject* Cast(v8::Value* obj);
1814
1815 private:
1816 V8EXPORT static void CheckCast(v8::Value* obj);
1817};
1818
1819
1820/**
1821 * A String object (ECMA-262, 4.3.18).
1822 */
1823class StringObject : public Object {
1824 public:
1825 V8EXPORT static Local<Value> New(Handle<String> value);
1826
1827 /**
1828 * Returns the String held by the object.
1829 */
1830 V8EXPORT Local<String> StringValue() const;
1831
1832 static inline StringObject* Cast(v8::Value* obj);
1833
1834 private:
1835 V8EXPORT static void CheckCast(v8::Value* obj);
1836};
1837
1838
1839/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001840 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1841 */
1842class RegExp : public Object {
1843 public:
1844 /**
1845 * Regular expression flag bits. They can be or'ed to enable a set
1846 * of flags.
1847 */
1848 enum Flags {
1849 kNone = 0,
1850 kGlobal = 1,
1851 kIgnoreCase = 2,
1852 kMultiline = 4
1853 };
1854
1855 /**
1856 * Creates a regular expression from the given pattern string and
1857 * the flags bit field. May throw a JavaScript exception as
1858 * described in ECMA-262, 15.10.4.1.
1859 *
1860 * For example,
1861 * RegExp::New(v8::String::New("foo"),
1862 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1863 * is equivalent to evaluating "/foo/gm".
1864 */
1865 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1866 Flags flags);
1867
1868 /**
1869 * Returns the value of the source property: a string representing
1870 * the regular expression.
1871 */
1872 V8EXPORT Local<String> GetSource() const;
1873
1874 /**
1875 * Returns the flags bit field.
1876 */
1877 V8EXPORT Flags GetFlags() const;
1878
1879 static inline RegExp* Cast(v8::Value* obj);
1880
1881 private:
1882 V8EXPORT static void CheckCast(v8::Value* obj);
1883};
1884
1885
1886/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001887 * A JavaScript value that wraps a C++ void*. This type of value is
1888 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001890 *
1891 * The Wrap function V8 will return the most optimal Value object wrapping the
1892 * C++ void*. The type of the value is not guaranteed to be an External object
1893 * and no assumptions about its type should be made. To access the wrapped
1894 * value Unwrap should be used, all other operations on that object will lead
1895 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001897class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001899 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001900 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001901
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001902 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001903 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001904 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001906 V8EXPORT External();
1907 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001908 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001909 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910};
1911
1912
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001913// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914
1915
1916/**
1917 * The superclass of object and function templates.
1918 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001919class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920 public:
1921 /** Adds a property to each instance created by this template.*/
1922 void Set(Handle<String> name, Handle<Data> value,
1923 PropertyAttribute attributes = None);
1924 inline void Set(const char* name, Handle<Data> value);
1925 private:
1926 Template();
1927
1928 friend class ObjectTemplate;
1929 friend class FunctionTemplate;
1930};
1931
1932
1933/**
1934 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001935 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 * including the receiver, the number and values of arguments, and
1937 * the holder of the function.
1938 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001939class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940 public:
1941 inline int Length() const;
1942 inline Local<Value> operator[](int i) const;
1943 inline Local<Function> Callee() const;
1944 inline Local<Object> This() const;
1945 inline Local<Object> Holder() const;
1946 inline bool IsConstructCall() const;
1947 inline Local<Value> Data() const;
1948 private:
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001949 static const int kDataIndex = 0;
1950 static const int kCalleeIndex = -1;
1951 static const int kHolderIndex = -2;
1952
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001954 inline Arguments(internal::Object** implicit_args,
1955 internal::Object** values,
1956 int length,
1957 bool is_construct_call);
1958 internal::Object** implicit_args_;
1959 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001961 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962};
1963
1964
1965/**
1966 * The information passed to an accessor callback about the context
1967 * of the property access.
1968 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001969class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001971 inline AccessorInfo(internal::Object** args)
1972 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 inline Local<Value> Data() const;
1974 inline Local<Object> This() const;
1975 inline Local<Object> Holder() const;
1976 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001977 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978};
1979
1980
1981typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1982
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001984 * NamedProperty[Getter|Setter] are used as interceptors on object.
1985 * See ObjectTemplate::SetNamedPropertyHandler.
1986 */
1987typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1988 const AccessorInfo& info);
1989
1990
1991/**
1992 * Returns the value if the setter intercepts the request.
1993 * Otherwise, returns an empty handle.
1994 */
1995typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1996 Local<Value> value,
1997 const AccessorInfo& info);
1998
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999/**
2000 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002001 * The result is an integer encoding property attributes (like v8::None,
2002 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002004typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2005 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006
2007
2008/**
2009 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002010 * The return value is true if the property could be deleted and false
2011 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 */
2013typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2014 const AccessorInfo& info);
2015
2016/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002017 * Returns an array containing the names of the properties the named
2018 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 */
2020typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2021
v8.team.kasperl727e9952008-09-02 14:56:44 +00002022
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002024 * Returns the value of the property if the getter intercepts the
2025 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 */
2027typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2028 const AccessorInfo& info);
2029
2030
2031/**
2032 * Returns the value if the setter intercepts the request.
2033 * Otherwise, returns an empty handle.
2034 */
2035typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2036 Local<Value> value,
2037 const AccessorInfo& info);
2038
2039
2040/**
2041 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002042 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002044typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2045 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046
2047/**
2048 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002049 * The return value is true if the property could be deleted and false
2050 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 */
2052typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2053 const AccessorInfo& info);
2054
v8.team.kasperl727e9952008-09-02 14:56:44 +00002055/**
2056 * Returns an array containing the indices of the properties the
2057 * indexed property getter intercepts.
2058 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2060
2061
2062/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002063 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 */
2065enum AccessType {
2066 ACCESS_GET,
2067 ACCESS_SET,
2068 ACCESS_HAS,
2069 ACCESS_DELETE,
2070 ACCESS_KEYS
2071};
2072
v8.team.kasperl727e9952008-09-02 14:56:44 +00002073
2074/**
2075 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002076 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002077 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002078typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 Local<Value> key,
2080 AccessType type,
2081 Local<Value> data);
2082
v8.team.kasperl727e9952008-09-02 14:56:44 +00002083
2084/**
2085 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002086 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002087 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002088typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 uint32_t index,
2090 AccessType type,
2091 Local<Value> data);
2092
2093
2094/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002095 * A FunctionTemplate is used to create functions at runtime. There
2096 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002097 * context. The lifetime of the created function is equal to the
2098 * lifetime of the context. So in case the embedder needs to create
2099 * temporary functions that can be collected using Scripts is
2100 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 *
2102 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002103 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002105 * A FunctionTemplate has a corresponding instance template which is
2106 * used to create object instances when the function is used as a
2107 * constructor. Properties added to the instance template are added to
2108 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 *
2110 * A FunctionTemplate can have a prototype template. The prototype template
2111 * is used to create the prototype object of the function.
2112 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002113 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002115 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2117 * t->Set("func_property", v8::Number::New(1));
2118 *
2119 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2120 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2121 * proto_t->Set("proto_const", v8::Number::New(2));
2122 *
2123 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2124 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2125 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2126 * instance_t->Set("instance_property", Number::New(3));
2127 *
2128 * v8::Local<v8::Function> function = t->GetFunction();
2129 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002130 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 *
2132 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002133 * and "instance" for the instance object created above. The function
2134 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002136 * \code
2137 * func_property in function == true;
2138 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002140 * function.prototype.proto_method() invokes 'InvokeCallback'
2141 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002143 * instance instanceof function == true;
2144 * instance.instance_accessor calls 'InstanceAccessorCallback'
2145 * instance.instance_property == 3;
2146 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002148 * A FunctionTemplate can inherit from another one by calling the
2149 * FunctionTemplate::Inherit method. The following graph illustrates
2150 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002152 * \code
2153 * FunctionTemplate Parent -> Parent() . prototype -> { }
2154 * ^ ^
2155 * | Inherit(Parent) | .__proto__
2156 * | |
2157 * FunctionTemplate Child -> Child() . prototype -> { }
2158 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002160 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2161 * object of the Child() function has __proto__ pointing to the
2162 * Parent() function's prototype object. An instance of the Child
2163 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002165 * Let Parent be the FunctionTemplate initialized in the previous
2166 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002168 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 * Local<FunctionTemplate> parent = t;
2170 * Local<FunctionTemplate> child = FunctionTemplate::New();
2171 * child->Inherit(parent);
2172 *
2173 * Local<Function> child_function = child->GetFunction();
2174 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002175 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002177 * The Child function and Child instance will have the following
2178 * properties:
2179 *
2180 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002182 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002184 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002186class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 public:
2188 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002189 static Local<FunctionTemplate> New(
2190 InvocationCallback callback = 0,
2191 Handle<Value> data = Handle<Value>(),
2192 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 /** Returns the unique function instance in the current execution context.*/
2194 Local<Function> GetFunction();
2195
v8.team.kasperl727e9952008-09-02 14:56:44 +00002196 /**
2197 * Set the call-handler callback for a FunctionTemplate. This
2198 * callback is called whenever the function created from this
2199 * FunctionTemplate is called.
2200 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 void SetCallHandler(InvocationCallback callback,
2202 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203
v8.team.kasperl727e9952008-09-02 14:56:44 +00002204 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 Local<ObjectTemplate> InstanceTemplate();
2206
2207 /** Causes the function template to inherit from a parent function template.*/
2208 void Inherit(Handle<FunctionTemplate> parent);
2209
2210 /**
2211 * A PrototypeTemplate is the template used to create the prototype object
2212 * of the function created by this template.
2213 */
2214 Local<ObjectTemplate> PrototypeTemplate();
2215
v8.team.kasperl727e9952008-09-02 14:56:44 +00002216
2217 /**
2218 * Set the class name of the FunctionTemplate. This is used for
2219 * printing objects created with the function created from the
2220 * FunctionTemplate as its constructor.
2221 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 void SetClassName(Handle<String> name);
2223
2224 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002225 * Determines whether the __proto__ accessor ignores instances of
2226 * the function template. If instances of the function template are
2227 * ignored, __proto__ skips all instances and instead returns the
2228 * next object in the prototype chain.
2229 *
2230 * Call with a value of true to make the __proto__ accessor ignore
2231 * instances of the function template. Call with a value of false
2232 * to make the __proto__ accessor not ignore instances of the
2233 * function template. By default, instances of a function template
2234 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 */
2236 void SetHiddenPrototype(bool value);
2237
2238 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002239 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2240 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002241 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002242 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002243
2244 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002245 * Returns true if the given object is an instance of this function
2246 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 */
2248 bool HasInstance(Handle<Value> object);
2249
2250 private:
2251 FunctionTemplate();
2252 void AddInstancePropertyAccessor(Handle<String> name,
2253 AccessorGetter getter,
2254 AccessorSetter setter,
2255 Handle<Value> data,
2256 AccessControl settings,
2257 PropertyAttribute attributes);
2258 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2259 NamedPropertySetter setter,
2260 NamedPropertyQuery query,
2261 NamedPropertyDeleter remover,
2262 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002263 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2265 IndexedPropertySetter setter,
2266 IndexedPropertyQuery query,
2267 IndexedPropertyDeleter remover,
2268 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002269 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2271 Handle<Value> data);
2272
2273 friend class Context;
2274 friend class ObjectTemplate;
2275};
2276
2277
2278/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002279 * An ObjectTemplate is used to create objects at runtime.
2280 *
2281 * Properties added to an ObjectTemplate are added to each object
2282 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002284class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002286 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002287 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 /** Creates a new instance of this template.*/
2290 Local<Object> NewInstance();
2291
2292 /**
2293 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002294 *
2295 * Whenever the property with the given name is accessed on objects
2296 * created from this ObjectTemplate the getter and setter callbacks
2297 * are called instead of getting and setting the property directly
2298 * on the JavaScript object.
2299 *
2300 * \param name The name of the property for which an accessor is added.
2301 * \param getter The callback to invoke when getting the property.
2302 * \param setter The callback to invoke when setting the property.
2303 * \param data A piece of data that will be passed to the getter and setter
2304 * callbacks whenever they are invoked.
2305 * \param settings Access control settings for the accessor. This is a bit
2306 * field consisting of one of more of
2307 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2308 * The default is to not allow cross-context access.
2309 * ALL_CAN_READ means that all cross-context reads are allowed.
2310 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2311 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2312 * cross-context access.
2313 * \param attribute The attributes of the property for which an accessor
2314 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315 */
2316 void SetAccessor(Handle<String> name,
2317 AccessorGetter getter,
2318 AccessorSetter setter = 0,
2319 Handle<Value> data = Handle<Value>(),
2320 AccessControl settings = DEFAULT,
2321 PropertyAttribute attribute = None);
2322
2323 /**
2324 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002325 *
2326 * Whenever a named property is accessed on objects created from
2327 * this object template, the provided callback is invoked instead of
2328 * accessing the property directly on the JavaScript object.
2329 *
2330 * \param getter The callback to invoke when getting a property.
2331 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002332 * \param query The callback to invoke to check if a property is present,
2333 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002334 * \param deleter The callback to invoke when deleting a property.
2335 * \param enumerator The callback to invoke to enumerate all the named
2336 * properties of an object.
2337 * \param data A piece of data that will be passed to the callbacks
2338 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339 */
2340 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2341 NamedPropertySetter setter = 0,
2342 NamedPropertyQuery query = 0,
2343 NamedPropertyDeleter deleter = 0,
2344 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002345 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346
2347 /**
2348 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002349 *
2350 * Whenever an indexed property is accessed on objects created from
2351 * this object template, the provided callback is invoked instead of
2352 * accessing the property directly on the JavaScript object.
2353 *
2354 * \param getter The callback to invoke when getting a property.
2355 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002356 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002357 * \param deleter The callback to invoke when deleting a property.
2358 * \param enumerator The callback to invoke to enumerate all the indexed
2359 * properties of an object.
2360 * \param data A piece of data that will be passed to the callbacks
2361 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362 */
2363 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2364 IndexedPropertySetter setter = 0,
2365 IndexedPropertyQuery query = 0,
2366 IndexedPropertyDeleter deleter = 0,
2367 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002368 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370 /**
2371 * Sets the callback to be used when calling instances created from
2372 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002373 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 * function.
2375 */
2376 void SetCallAsFunctionHandler(InvocationCallback callback,
2377 Handle<Value> data = Handle<Value>());
2378
v8.team.kasperl727e9952008-09-02 14:56:44 +00002379 /**
2380 * Mark object instances of the template as undetectable.
2381 *
2382 * In many ways, undetectable objects behave as though they are not
2383 * there. They behave like 'undefined' in conditionals and when
2384 * printed. However, properties can be accessed and called as on
2385 * normal objects.
2386 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387 void MarkAsUndetectable();
2388
v8.team.kasperl727e9952008-09-02 14:56:44 +00002389 /**
2390 * Sets access check callbacks on the object template.
2391 *
2392 * When accessing properties on instances of this object template,
2393 * the access check callback will be called to determine whether or
2394 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002395 * The last parameter specifies whether access checks are turned
2396 * on by default on instances. If access checks are off by default,
2397 * they can be turned on on individual instances by calling
2398 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002399 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002400 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2401 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002402 Handle<Value> data = Handle<Value>(),
2403 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002404
kasper.lund212ac232008-07-16 07:07:30 +00002405 /**
2406 * Gets the number of internal fields for objects generated from
2407 * this template.
2408 */
2409 int InternalFieldCount();
2410
2411 /**
2412 * Sets the number of internal fields for objects generated from
2413 * this template.
2414 */
2415 void SetInternalFieldCount(int value);
2416
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417 private:
2418 ObjectTemplate();
2419 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2420 friend class FunctionTemplate;
2421};
2422
2423
2424/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002425 * A Signature specifies which receivers and arguments a function can
2426 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002428class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 public:
2430 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2431 Handle<FunctionTemplate>(),
2432 int argc = 0,
2433 Handle<FunctionTemplate> argv[] = 0);
2434 private:
2435 Signature();
2436};
2437
2438
2439/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002440 * A utility for determining the type of objects based on the template
2441 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002443class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 public:
2445 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2446 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2447 int match(Handle<Value> value);
2448 private:
2449 TypeSwitch();
2450};
2451
2452
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002453// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002454
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002455class V8EXPORT ExternalAsciiStringResourceImpl
2456 : public String::ExternalAsciiStringResource {
2457 public:
2458 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2459 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2460 : data_(data), length_(length) {}
2461 const char* data() const { return data_; }
2462 size_t length() const { return length_; }
2463
2464 private:
2465 const char* data_;
2466 size_t length_;
2467};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468
2469/**
2470 * Ignore
2471 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002472class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002474 // Note that the strings passed into this constructor must live as long
2475 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476 Extension(const char* name,
2477 const char* source = 0,
2478 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002479 const char** deps = 0,
2480 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481 virtual ~Extension() { }
2482 virtual v8::Handle<v8::FunctionTemplate>
2483 GetNativeFunction(v8::Handle<v8::String> name) {
2484 return v8::Handle<v8::FunctionTemplate>();
2485 }
2486
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002487 const char* name() const { return name_; }
2488 size_t source_length() const { return source_length_; }
2489 const String::ExternalAsciiStringResource* source() const {
2490 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491 int dependency_count() { return dep_count_; }
2492 const char** dependencies() { return deps_; }
2493 void set_auto_enable(bool value) { auto_enable_ = value; }
2494 bool auto_enable() { return auto_enable_; }
2495
2496 private:
2497 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002498 size_t source_length_; // expected to initialize before source_
2499 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500 int dep_count_;
2501 const char** deps_;
2502 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002503
2504 // Disallow copying and assigning.
2505 Extension(const Extension&);
2506 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507};
2508
2509
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002510void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511
2512
2513/**
2514 * Ignore
2515 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002516class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517 public:
2518 inline DeclareExtension(Extension* extension) {
2519 RegisterExtension(extension);
2520 }
2521};
2522
2523
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002524// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525
2526
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002527Handle<Primitive> V8EXPORT Undefined();
2528Handle<Primitive> V8EXPORT Null();
2529Handle<Boolean> V8EXPORT True();
2530Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531
2532
2533/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002534 * A set of constraints that specifies the limits of the runtime's memory use.
2535 * You must set the heap size before initializing the VM - the size cannot be
2536 * adjusted after the VM is initialized.
2537 *
2538 * If you are using threads then you should hold the V8::Locker lock while
2539 * setting the stack limit and you must set a non-default stack limit separately
2540 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002542class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543 public:
2544 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002545 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002547 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002549 int max_executable_size() { return max_executable_size_; }
2550 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002551 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002552 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2554 private:
2555 int max_young_space_size_;
2556 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002557 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558 uint32_t* stack_limit_;
2559};
2560
2561
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002562bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563
2564
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002565// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002566
2567
2568typedef void (*FatalErrorCallback)(const char* location, const char* message);
2569
2570
2571typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2572
2573
2574/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002575 * Schedules an exception to be thrown when returning to JavaScript. When an
2576 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002578 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002580Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581
2582/**
2583 * Create new error objects by calling the corresponding error object
2584 * constructor with the message.
2585 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002586class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587 public:
2588 static Local<Value> RangeError(Handle<String> message);
2589 static Local<Value> ReferenceError(Handle<String> message);
2590 static Local<Value> SyntaxError(Handle<String> message);
2591 static Local<Value> TypeError(Handle<String> message);
2592 static Local<Value> Error(Handle<String> message);
2593};
2594
2595
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002596// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002598typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002600typedef void* (*CreateHistogramCallback)(const char* name,
2601 int min,
2602 int max,
2603 size_t buckets);
2604
2605typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2606
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002607// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002608 enum ObjectSpace {
2609 kObjectSpaceNewSpace = 1 << 0,
2610 kObjectSpaceOldPointerSpace = 1 << 1,
2611 kObjectSpaceOldDataSpace = 1 << 2,
2612 kObjectSpaceCodeSpace = 1 << 3,
2613 kObjectSpaceMapSpace = 1 << 4,
2614 kObjectSpaceLoSpace = 1 << 5,
2615
2616 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2617 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2618 kObjectSpaceLoSpace
2619 };
2620
2621 enum AllocationAction {
2622 kAllocationActionAllocate = 1 << 0,
2623 kAllocationActionFree = 1 << 1,
2624 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2625 };
2626
2627typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2628 AllocationAction action,
2629 int size);
2630
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002631// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2633 AccessType type,
2634 Local<Value> data);
2635
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002636// --- AllowCodeGenerationFromStrings callbacks ---
2637
2638/**
2639 * Callback to check if code generation from strings is allowed. See
2640 * Context::AllowCodeGenerationFromStrings.
2641 */
2642typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2643
2644// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645
2646/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002647 * Applications can register callback functions which will be called
2648 * before and after a garbage collection. Allocations are not
2649 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002650 * objects (set or delete properties for example) since it is possible
2651 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002653enum GCType {
2654 kGCTypeScavenge = 1 << 0,
2655 kGCTypeMarkSweepCompact = 1 << 1,
2656 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2657};
2658
2659enum GCCallbackFlags {
2660 kNoGCCallbackFlags = 0,
2661 kGCCallbackFlagCompacted = 1 << 0
2662};
2663
2664typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2665typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2666
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667typedef void (*GCCallback)();
2668
2669
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002671 * Collection of V8 heap information.
2672 *
2673 * Instances of this class can be passed to v8::V8::HeapStatistics to
2674 * get heap statistics from V8.
2675 */
2676class V8EXPORT HeapStatistics {
2677 public:
2678 HeapStatistics();
2679 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002680 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002681 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002682 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002683
2684 private:
2685 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002686 void set_total_heap_size_executable(size_t size) {
2687 total_heap_size_executable_ = size;
2688 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002689 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002690 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002691
2692 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002693 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002694 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002695 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002696
2697 friend class V8;
2698};
2699
2700
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002701class RetainedObjectInfo;
2702
ager@chromium.org3811b432009-10-28 14:53:37 +00002703/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002704 * Isolate represents an isolated instance of the V8 engine. V8
2705 * isolates have completely separate states. Objects from one isolate
2706 * must not be used in other isolates. When V8 is initialized a
2707 * default isolate is implicitly created and entered. The embedder
2708 * can create additional isolates and use them in parallel in multiple
2709 * threads. An isolate can be entered by at most one thread at any
2710 * given time. The Locker/Unlocker API can be used to synchronize.
2711 */
2712class V8EXPORT Isolate {
2713 public:
2714 /**
2715 * Stack-allocated class which sets the isolate for all operations
2716 * executed within a local scope.
2717 */
2718 class V8EXPORT Scope {
2719 public:
2720 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2721 isolate->Enter();
2722 }
2723
2724 ~Scope() { isolate_->Exit(); }
2725
2726 private:
2727 Isolate* const isolate_;
2728
2729 // Prevent copying of Scope objects.
2730 Scope(const Scope&);
2731 Scope& operator=(const Scope&);
2732 };
2733
2734 /**
2735 * Creates a new isolate. Does not change the currently entered
2736 * isolate.
2737 *
2738 * When an isolate is no longer used its resources should be freed
2739 * by calling Dispose(). Using the delete operator is not allowed.
2740 */
2741 static Isolate* New();
2742
2743 /**
2744 * Returns the entered isolate for the current thread or NULL in
2745 * case there is no current isolate.
2746 */
2747 static Isolate* GetCurrent();
2748
2749 /**
2750 * Methods below this point require holding a lock (using Locker) in
2751 * a multi-threaded environment.
2752 */
2753
2754 /**
2755 * Sets this isolate as the entered one for the current thread.
2756 * Saves the previously entered one (if any), so that it can be
2757 * restored when exiting. Re-entering an isolate is allowed.
2758 */
2759 void Enter();
2760
2761 /**
2762 * Exits this isolate by restoring the previously entered one in the
2763 * current thread. The isolate may still stay the same, if it was
2764 * entered more than once.
2765 *
2766 * Requires: this == Isolate::GetCurrent().
2767 */
2768 void Exit();
2769
2770 /**
2771 * Disposes the isolate. The isolate must not be entered by any
2772 * thread to be disposable.
2773 */
2774 void Dispose();
2775
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002776 /**
2777 * Associate embedder-specific data with the isolate
2778 */
2779 void SetData(void* data);
2780
2781 /**
2782 * Retrive embedder-specific data from the isolate.
2783 * Returns NULL if SetData has never been called.
2784 */
2785 void* GetData();
2786
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002787 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002788 Isolate();
2789 Isolate(const Isolate&);
2790 ~Isolate();
2791 Isolate& operator=(const Isolate&);
2792 void* operator new(size_t size);
2793 void operator delete(void*, size_t);
2794};
2795
2796
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002797class StartupData {
2798 public:
2799 enum CompressionAlgorithm {
2800 kUncompressed,
2801 kBZip2
2802 };
2803
2804 const char* data;
2805 int compressed_size;
2806 int raw_size;
2807};
2808
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002809
2810/**
2811 * A helper class for driving V8 startup data decompression. It is based on
2812 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2813 * for an embedder to use this class, instead, API functions can be used
2814 * directly.
2815 *
2816 * For an example of the class usage, see the "shell.cc" sample application.
2817 */
2818class V8EXPORT StartupDataDecompressor { // NOLINT
2819 public:
2820 StartupDataDecompressor();
2821 virtual ~StartupDataDecompressor();
2822 int Decompress();
2823
2824 protected:
2825 virtual int DecompressData(char* raw_data,
2826 int* raw_data_size,
2827 const char* compressed_data,
2828 int compressed_data_size) = 0;
2829
2830 private:
2831 char** raw_data;
2832};
2833
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002834
2835/**
2836 * EntropySource is used as a callback function when v8 needs a source
2837 * of entropy.
2838 */
2839typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2840
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002841/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002842 * Container class for static utility functions.
2843 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002844class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002845 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002846 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847 static void SetFatalErrorHandler(FatalErrorCallback that);
2848
v8.team.kasperl727e9952008-09-02 14:56:44 +00002849 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002850 * Set the callback to invoke to check if code generation from
2851 * strings should be allowed.
2852 */
2853 static void SetAllowCodeGenerationFromStringsCallback(
2854 AllowCodeGenerationFromStringsCallback that);
2855
2856 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002857 * Ignore out-of-memory exceptions.
2858 *
2859 * V8 running out of memory is treated as a fatal error by default.
2860 * This means that the fatal error handler is called and that V8 is
2861 * terminated.
2862 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002863 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002864 * out-of-memory situation as a fatal error. This way, the contexts
2865 * that did not cause the out of memory problem might be able to
2866 * continue execution.
2867 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868 static void IgnoreOutOfMemoryException();
2869
v8.team.kasperl727e9952008-09-02 14:56:44 +00002870 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002871 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002872 * fatal errors such as out-of-memory situations.
2873 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874 static bool IsDead();
2875
2876 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002877 * The following 4 functions are to be used when V8 is built with
2878 * the 'compress_startup_data' flag enabled. In this case, the
2879 * embedder must decompress startup data prior to initializing V8.
2880 *
2881 * This is how interaction with V8 should look like:
2882 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2883 * v8::StartupData* compressed_data =
2884 * new v8::StartupData[compressed_data_count];
2885 * v8::V8::GetCompressedStartupData(compressed_data);
2886 * ... decompress data (compressed_data can be updated in-place) ...
2887 * v8::V8::SetDecompressedStartupData(compressed_data);
2888 * ... now V8 can be initialized
2889 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002890 *
2891 * A helper class StartupDataDecompressor is provided. It implements
2892 * the protocol of the interaction described above, and can be used in
2893 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002894 */
2895 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2896 static int GetCompressedStartupDataCount();
2897 static void GetCompressedStartupData(StartupData* compressed_data);
2898 static void SetDecompressedStartupData(StartupData* decompressed_data);
2899
2900 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002901 * Adds a message listener.
2902 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002903 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00002904 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002905 */
2906 static bool AddMessageListener(MessageCallback that,
2907 Handle<Value> data = Handle<Value>());
2908
2909 /**
2910 * Remove all message listeners from the specified callback function.
2911 */
2912 static void RemoveMessageListeners(MessageCallback that);
2913
2914 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002915 * Tells V8 to capture current stack trace when uncaught exception occurs
2916 * and report it to the message listeners. The option is off by default.
2917 */
2918 static void SetCaptureStackTraceForUncaughtExceptions(
2919 bool capture,
2920 int frame_limit = 10,
2921 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2922
2923 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002924 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002925 */
2926 static void SetFlagsFromString(const char* str, int length);
2927
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002928 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002929 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002930 */
2931 static void SetFlagsFromCommandLine(int* argc,
2932 char** argv,
2933 bool remove_flags);
2934
kasper.lund7276f142008-07-30 08:49:36 +00002935 /** Get the version string. */
2936 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937
2938 /**
2939 * Enables the host application to provide a mechanism for recording
2940 * statistics counters.
2941 */
2942 static void SetCounterFunction(CounterLookupCallback);
2943
2944 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002945 * Enables the host application to provide a mechanism for recording
2946 * histograms. The CreateHistogram function returns a
2947 * histogram which will later be passed to the AddHistogramSample
2948 * function.
2949 */
2950 static void SetCreateHistogramFunction(CreateHistogramCallback);
2951 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2952
2953 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002954 * Enables the computation of a sliding window of states. The sliding
2955 * window information is recorded in statistics counters.
2956 */
2957 static void EnableSlidingStateWindow();
2958
2959 /** Callback function for reporting failed access checks.*/
2960 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2961
2962 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002963 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002964 * garbage collection. Allocations are not allowed in the
2965 * callback function, you therefore cannot manipulate objects (set
2966 * or delete properties for example) since it is possible such
2967 * operations will result in the allocation of objects. It is possible
2968 * to specify the GCType filter for your callback. But it is not possible to
2969 * register the same callback function two times with different
2970 * GCType filters.
2971 */
2972 static void AddGCPrologueCallback(
2973 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2974
2975 /**
2976 * This function removes callback which was installed by
2977 * AddGCPrologueCallback function.
2978 */
2979 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2980
2981 /**
2982 * The function is deprecated. Please use AddGCPrologueCallback instead.
2983 * Enables the host application to receive a notification before a
2984 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002985 * callback function, you therefore cannot manipulate objects (set
2986 * or delete properties for example) since it is possible such
2987 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988 */
2989 static void SetGlobalGCPrologueCallback(GCCallback);
2990
2991 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002992 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002993 * garbage collection. Allocations are not allowed in the
2994 * callback function, you therefore cannot manipulate objects (set
2995 * or delete properties for example) since it is possible such
2996 * operations will result in the allocation of objects. It is possible
2997 * to specify the GCType filter for your callback. But it is not possible to
2998 * register the same callback function two times with different
2999 * GCType filters.
3000 */
3001 static void AddGCEpilogueCallback(
3002 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3003
3004 /**
3005 * This function removes callback which was installed by
3006 * AddGCEpilogueCallback function.
3007 */
3008 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3009
3010 /**
3011 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3012 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003013 * major garbage collection. Allocations are not allowed in the
3014 * callback function, you therefore cannot manipulate objects (set
3015 * or delete properties for example) since it is possible such
3016 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003017 */
3018 static void SetGlobalGCEpilogueCallback(GCCallback);
3019
3020 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003021 * Enables the host application to provide a mechanism to be notified
3022 * and perform custom logging when V8 Allocates Executable Memory.
3023 */
3024 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3025 ObjectSpace space,
3026 AllocationAction action);
3027
3028 /**
3029 * This function removes callback which was installed by
3030 * AddMemoryAllocationCallback function.
3031 */
3032 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3033
3034 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003035 * Allows the host application to group objects together. If one
3036 * object in the group is alive, all objects in the group are alive.
3037 * After each garbage collection, object groups are removed. It is
3038 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003039 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00003040 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003041 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003042 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003043 static void AddObjectGroup(Persistent<Value>* objects,
3044 size_t length,
3045 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046
3047 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003048 * Allows the host application to declare implicit references between
3049 * the objects: if |parent| is alive, all |children| are alive too.
3050 * After each garbage collection, all implicit references
3051 * are removed. It is intended to be used in the before-garbage-collection
3052 * callback function.
3053 */
3054 static void AddImplicitReferences(Persistent<Object> parent,
3055 Persistent<Value>* children,
3056 size_t length);
3057
3058 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003059 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003060 * initialize from scratch. This function is called implicitly if
3061 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003062 */
3063 static bool Initialize();
3064
kasper.lund7276f142008-07-30 08:49:36 +00003065 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003066 * Allows the host application to provide a callback which can be used
3067 * as a source of entropy for random number generators.
3068 */
3069 static void SetEntropySource(EntropySource source);
3070
3071 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003072 * Adjusts the amount of registered external memory. Used to give
3073 * V8 an indication of the amount of externally allocated memory
3074 * that is kept alive by JavaScript objects. V8 uses this to decide
3075 * when to perform global garbage collections. Registering
3076 * externally allocated memory will trigger global garbage
3077 * collections more often than otherwise in an attempt to garbage
3078 * collect the JavaScript objects keeping the externally allocated
3079 * memory alive.
3080 *
3081 * \param change_in_bytes the change in externally allocated memory
3082 * that is kept alive by JavaScript objects.
3083 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003084 */
3085 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
3086
iposva@chromium.org245aa852009-02-10 00:49:54 +00003087 /**
3088 * Suspends recording of tick samples in the profiler.
3089 * When the V8 profiling mode is enabled (usually via command line
3090 * switches) this function suspends recording of tick samples.
3091 * Profiling ticks are discarded until ResumeProfiler() is called.
3092 *
3093 * See also the --prof and --prof_auto command line switches to
3094 * enable V8 profiling.
3095 */
3096 static void PauseProfiler();
3097
3098 /**
3099 * Resumes recording of tick samples in the profiler.
3100 * See also PauseProfiler().
3101 */
3102 static void ResumeProfiler();
3103
ager@chromium.org41826e72009-03-30 13:30:57 +00003104 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003105 * Return whether profiler is currently paused.
3106 */
3107 static bool IsProfilerPaused();
3108
3109 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003110 * Retrieve the V8 thread id of the calling thread.
3111 *
3112 * The thread id for a thread should only be retrieved after the V8
3113 * lock has been acquired with a Locker object with that thread.
3114 */
3115 static int GetCurrentThreadId();
3116
3117 /**
3118 * Forcefully terminate execution of a JavaScript thread. This can
3119 * be used to terminate long-running scripts.
3120 *
3121 * TerminateExecution should only be called when then V8 lock has
3122 * been acquired with a Locker object. Therefore, in order to be
3123 * able to terminate long-running threads, preemption must be
3124 * enabled to allow the user of TerminateExecution to acquire the
3125 * lock.
3126 *
3127 * The termination is achieved by throwing an exception that is
3128 * uncatchable by JavaScript exception handlers. Termination
3129 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003130 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003131 * exception handler that catches an exception should check if that
3132 * exception is a termination exception and immediately return if
3133 * that is the case. Returning immediately in that case will
3134 * continue the propagation of the termination exception if needed.
3135 *
3136 * The thread id passed to TerminateExecution must have been
3137 * obtained by calling GetCurrentThreadId on the thread in question.
3138 *
3139 * \param thread_id The thread id of the thread to terminate.
3140 */
3141 static void TerminateExecution(int thread_id);
3142
3143 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003144 * Forcefully terminate the current thread of JavaScript execution
3145 * in the given isolate. If no isolate is provided, the default
3146 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003147 *
3148 * This method can be used by any thread even if that thread has not
3149 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003150 *
3151 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003152 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003153 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003154
3155 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003156 * Is V8 terminating JavaScript execution.
3157 *
3158 * Returns true if JavaScript execution is currently terminating
3159 * because of a call to TerminateExecution. In that case there are
3160 * still JavaScript frames on the stack and the termination
3161 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003162 *
3163 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003164 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003165 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003166
3167 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003168 * Releases any resources used by v8 and stops any utility threads
3169 * that may be running. Note that disposing v8 is permanent, it
3170 * cannot be reinitialized.
3171 *
3172 * It should generally not be necessary to dispose v8 before exiting
3173 * a process, this should happen automatically. It is only necessary
3174 * to use if the process needs the resources taken up by v8.
3175 */
3176 static bool Dispose();
3177
ager@chromium.org3811b432009-10-28 14:53:37 +00003178 /**
3179 * Get statistics about the heap memory usage.
3180 */
3181 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003182
3183 /**
3184 * Optional notification that the embedder is idle.
3185 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003186 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003187 * Returns true if the embedder should stop calling IdleNotification
3188 * until real work has been done. This indicates that V8 has done
3189 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003190 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003191 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003192
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003193 /**
3194 * Optional notification that the system is running low on memory.
3195 * V8 uses these notifications to attempt to free memory.
3196 */
3197 static void LowMemoryNotification();
3198
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003199 /**
3200 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003201 * these notifications to guide the GC heuristic. Returns the number
3202 * of context disposals - including this one - since the last time
3203 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003204 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003205 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003206
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207 private:
3208 V8();
3209
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003210 static internal::Object** GlobalizeReference(internal::Object** handle);
3211 static void DisposeGlobal(internal::Object** global_handle);
3212 static void MakeWeak(internal::Object** global_handle,
3213 void* data,
3214 WeakReferenceCallback);
3215 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003216 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003217 static bool IsGlobalNearDeath(internal::Object** global_handle);
3218 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003219 static void SetWrapperClassId(internal::Object** global_handle,
3220 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003221
3222 template <class T> friend class Handle;
3223 template <class T> friend class Local;
3224 template <class T> friend class Persistent;
3225 friend class Context;
3226};
3227
3228
3229/**
3230 * An external exception handler.
3231 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003232class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003233 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003234 /**
3235 * Creates a new try/catch block and registers it with v8.
3236 */
3237 TryCatch();
3238
3239 /**
3240 * Unregisters and deletes this try/catch block.
3241 */
3242 ~TryCatch();
3243
3244 /**
3245 * Returns true if an exception has been caught by this try/catch block.
3246 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003247 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003248
3249 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003250 * For certain types of exceptions, it makes no sense to continue
3251 * execution.
3252 *
3253 * Currently, the only type of exception that can be caught by a
3254 * TryCatch handler and for which it does not make sense to continue
3255 * is termination exception. Such exceptions are thrown when the
3256 * TerminateExecution methods are called to terminate a long-running
3257 * script.
3258 *
3259 * If CanContinue returns false, the correct action is to perform
3260 * any C++ cleanup needed and then return.
3261 */
3262 bool CanContinue() const;
3263
3264 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003265 * Throws the exception caught by this TryCatch in a way that avoids
3266 * it being caught again by this same TryCatch. As with ThrowException
3267 * it is illegal to execute any JavaScript operations after calling
3268 * ReThrow; the caller must return immediately to where the exception
3269 * is caught.
3270 */
3271 Handle<Value> ReThrow();
3272
3273 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274 * Returns the exception caught by this try/catch block. If no exception has
3275 * been caught an empty handle is returned.
3276 *
3277 * The returned handle is valid until this TryCatch block has been destroyed.
3278 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003279 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280
3281 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003282 * Returns the .stack property of the thrown object. If no .stack
3283 * property is present an empty handle is returned.
3284 */
3285 Local<Value> StackTrace() const;
3286
3287 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003288 * Returns the message associated with this exception. If there is
3289 * no message associated an empty handle is returned.
3290 *
3291 * The returned handle is valid until this TryCatch block has been
3292 * destroyed.
3293 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003294 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003295
3296 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003297 * Clears any exceptions that may have been caught by this try/catch block.
3298 * After this method has been called, HasCaught() will return false.
3299 *
3300 * It is not necessary to clear a try/catch block before using it again; if
3301 * another exception is thrown the previously caught exception will just be
3302 * overwritten. However, it is often a good idea since it makes it easier
3303 * to determine which operation threw a given exception.
3304 */
3305 void Reset();
3306
v8.team.kasperl727e9952008-09-02 14:56:44 +00003307 /**
3308 * Set verbosity of the external exception handler.
3309 *
3310 * By default, exceptions that are caught by an external exception
3311 * handler are not reported. Call SetVerbose with true on an
3312 * external exception handler to have exceptions caught by the
3313 * handler reported as if they were not caught.
3314 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003315 void SetVerbose(bool value);
3316
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003317 /**
3318 * Set whether or not this TryCatch should capture a Message object
3319 * which holds source information about where the exception
3320 * occurred. True by default.
3321 */
3322 void SetCaptureMessage(bool value);
3323
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003324 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003325 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003326 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003327 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003328 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003329 bool is_verbose_ : 1;
3330 bool can_continue_ : 1;
3331 bool capture_message_ : 1;
3332 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003333
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003334 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003335};
3336
3337
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003338// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003339
3340
3341/**
3342 * Ignore
3343 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003344class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003345 public:
3346 ExtensionConfiguration(int name_count, const char* names[])
3347 : name_count_(name_count), names_(names) { }
3348 private:
3349 friend class ImplementationUtilities;
3350 int name_count_;
3351 const char** names_;
3352};
3353
3354
3355/**
3356 * A sandboxed execution context with its own set of built-in objects
3357 * and functions.
3358 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003359class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003360 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003361 /**
3362 * Returns the global proxy object or global object itself for
3363 * detached contexts.
3364 *
3365 * Global proxy object is a thin wrapper whose prototype points to
3366 * actual context's global object with the properties like Object, etc.
3367 * This is done that way for security reasons (for more details see
3368 * https://wiki.mozilla.org/Gecko:SplitWindow).
3369 *
3370 * Please note that changes to global proxy object prototype most probably
3371 * would break VM---v8 expects only global object as a prototype of
3372 * global proxy object.
3373 *
3374 * If DetachGlobal() has been invoked, Global() would return actual global
3375 * object until global is reattached with ReattachGlobal().
3376 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003377 Local<Object> Global();
3378
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003379 /**
3380 * Detaches the global object from its context before
3381 * the global object can be reused to create a new context.
3382 */
3383 void DetachGlobal();
3384
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003385 /**
3386 * Reattaches a global object to a context. This can be used to
3387 * restore the connection between a global object and a context
3388 * after DetachGlobal has been called.
3389 *
3390 * \param global_object The global object to reattach to the
3391 * context. For this to work, the global object must be the global
3392 * object that was associated with this context before a call to
3393 * DetachGlobal.
3394 */
3395 void ReattachGlobal(Handle<Object> global_object);
3396
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003397 /** Creates a new context.
3398 *
3399 * Returns a persistent handle to the newly allocated context. This
3400 * persistent handle has to be disposed when the context is no
3401 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003402 *
3403 * \param extensions An optional extension configuration containing
3404 * the extensions to be installed in the newly created context.
3405 *
3406 * \param global_template An optional object template from which the
3407 * global object for the newly created context will be created.
3408 *
3409 * \param global_object An optional global object to be reused for
3410 * the newly created context. This global object must have been
3411 * created by a previous call to Context::New with the same global
3412 * template. The state of the global object will be completely reset
3413 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003414 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003415 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003416 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003417 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3418 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419
kasper.lund44510672008-07-25 07:37:58 +00003420 /** Returns the last entered context. */
3421 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003422
kasper.lund44510672008-07-25 07:37:58 +00003423 /** Returns the context that is on the top of the stack. */
3424 static Local<Context> GetCurrent();
3425
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003426 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003427 * Returns the context of the calling JavaScript code. That is the
3428 * context of the top-most JavaScript frame. If there are no
3429 * JavaScript frames an empty handle is returned.
3430 */
3431 static Local<Context> GetCalling();
3432
3433 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003434 * Sets the security token for the context. To access an object in
3435 * another context, the security tokens must match.
3436 */
3437 void SetSecurityToken(Handle<Value> token);
3438
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003439 /** Restores the security token to the default value. */
3440 void UseDefaultSecurityToken();
3441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003442 /** Returns the security token of this context.*/
3443 Handle<Value> GetSecurityToken();
3444
v8.team.kasperl727e9952008-09-02 14:56:44 +00003445 /**
3446 * Enter this context. After entering a context, all code compiled
3447 * and run is compiled and run in this context. If another context
3448 * is already entered, this old context is saved so it can be
3449 * restored when the new context is exited.
3450 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003451 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003452
3453 /**
3454 * Exit this context. Exiting the current context restores the
3455 * context that was in place when entering the current context.
3456 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003457 void Exit();
3458
v8.team.kasperl727e9952008-09-02 14:56:44 +00003459 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003460 bool HasOutOfMemoryException();
3461
v8.team.kasperl727e9952008-09-02 14:56:44 +00003462 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003463 static bool InContext();
3464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003465 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003466 * Associate an additional data object with the context. This is mainly used
3467 * with the debugger to provide additional information on the context through
3468 * the debugger API.
3469 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003470 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003471 Local<Value> GetData();
3472
3473 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003474 * Control whether code generation from strings is allowed. Calling
3475 * this method with false will disable 'eval' and the 'Function'
3476 * constructor for code running in this context. If 'eval' or the
3477 * 'Function' constructor are used an exception will be thrown.
3478 *
3479 * If code generation from strings is not allowed the
3480 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3481 * set before blocking the call to 'eval' or the 'Function'
3482 * constructor. If that callback returns true, the call will be
3483 * allowed, otherwise an exception will be thrown. If no callback is
3484 * set an exception will be thrown.
3485 */
3486 void AllowCodeGenerationFromStrings(bool allow);
3487
3488 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003489 * Stack-allocated class which sets the execution context for all
3490 * operations executed within a local scope.
3491 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003492 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003493 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003494 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003495 context_->Enter();
3496 }
3497 inline ~Scope() { context_->Exit(); }
3498 private:
3499 Handle<Context> context_;
3500 };
3501
3502 private:
3503 friend class Value;
3504 friend class Script;
3505 friend class Object;
3506 friend class Function;
3507};
3508
3509
3510/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003511 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003512 * is allowed to use any given V8 isolate. See Isolate class
3513 * comments. The definition of 'using V8 isolate' includes
3514 * accessing handles or holding onto object pointers obtained
3515 * from V8 handles while in the particular V8 isolate. It is up
3516 * to the user of V8 to ensure (perhaps with locking) that this
3517 * constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003519 * v8::Locker is a scoped lock object. While it's
3520 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003521 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3522 * locked by at most one thread at any time. In other words, the scope of a
3523 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003524 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003525 * Sample usage:
3526* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003527 * ...
3528 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003529 * v8::Locker locker(isolate);
3530 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003531 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003532 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003533 * ...
3534 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003535 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003536 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003537 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003538 * by destroying the v8::Locker object as above or by constructing a
3539 * v8::Unlocker object:
3540 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003541 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003542 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003543 * isolate->Exit();
3544 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003545 * ...
3546 * // Code not using V8 goes here while V8 can run in another thread.
3547 * ...
3548 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003549 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003550 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551 *
3552 * The Unlocker object is intended for use in a long-running callback
3553 * from V8, where you want to release the V8 lock for other threads to
3554 * use.
3555 *
3556 * The v8::Locker is a recursive lock. That is, you can lock more than
3557 * once in a given thread. This can be useful if you have code that can
3558 * be called either from code that holds the lock or from code that does
3559 * not. The Unlocker is not recursive so you can not have several
3560 * Unlockers on the stack at once, and you can not use an Unlocker in a
3561 * thread that is not inside a Locker's scope.
3562 *
3563 * An unlocker will unlock several lockers if it has to and reinstate
3564 * the correct depth of locking on its destruction. eg.:
3565 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003566 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003567 * // V8 not locked.
3568 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003569 * v8::Locker locker(isolate);
3570 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003571 * // V8 locked.
3572 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003573 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003574 * // V8 still locked (2 levels).
3575 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003576 * isolate->Exit();
3577 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003578 * // V8 not locked.
3579 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003580 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003581 * // V8 locked again (2 levels).
3582 * }
3583 * // V8 still locked (1 level).
3584 * }
3585 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003586 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003587 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003588 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003589 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003590class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003592 /**
3593 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3594 */
3595 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003597 private:
3598 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003599};
3600
3601
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003602class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003604 /**
3605 * Initialize Locker for a given Isolate. NULL means default isolate.
3606 */
3607 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003609
3610 /**
3611 * Start preemption.
3612 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003613 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003614 * that will switch between multiple threads that are in contention
3615 * for the V8 lock.
3616 */
3617 static void StartPreemption(int every_n_ms);
3618
3619 /**
3620 * Stop preemption.
3621 */
3622 static void StopPreemption();
3623
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003624 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003625 * Returns whether or not the locker for a given isolate, or default isolate
3626 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003627 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003628 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003629
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003630 /**
3631 * Returns whether v8::Locker is being used by this V8 instance.
3632 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003633 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003635 private:
3636 bool has_lock_;
3637 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003638 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003639
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003640 static bool active_;
3641
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003642 // Disallow copying and assigning.
3643 Locker(const Locker&);
3644 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003645};
3646
3647
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003648/**
3649 * An interface for exporting data from V8, using "push" model.
3650 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003651class V8EXPORT OutputStream { // NOLINT
3652 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003653 enum OutputEncoding {
3654 kAscii = 0 // 7-bit ASCII.
3655 };
3656 enum WriteResult {
3657 kContinue = 0,
3658 kAbort = 1
3659 };
3660 virtual ~OutputStream() {}
3661 /** Notify about the end of stream. */
3662 virtual void EndOfStream() = 0;
3663 /** Get preferred output chunk size. Called only once. */
3664 virtual int GetChunkSize() { return 1024; }
3665 /** Get preferred output encoding. Called only once. */
3666 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3667 /**
3668 * Writes the next chunk of snapshot data into the stream. Writing
3669 * can be stopped by returning kAbort as function result. EndOfStream
3670 * will not be called in case writing was aborted.
3671 */
3672 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
3673};
3674
3675
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003676/**
3677 * An interface for reporting progress and controlling long-running
3678 * activities.
3679 */
3680class V8EXPORT ActivityControl { // NOLINT
3681 public:
3682 enum ControlOption {
3683 kContinue = 0,
3684 kAbort = 1
3685 };
3686 virtual ~ActivityControl() {}
3687 /**
3688 * Notify about current progress. The activity can be stopped by
3689 * returning kAbort as the callback result.
3690 */
3691 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3692};
3693
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003694
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003695// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003696
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003697
3698namespace internal {
3699
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003700static const int kApiPointerSize = sizeof(void*); // NOLINT
3701static const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003702
3703// Tag information for HeapObject.
3704const int kHeapObjectTag = 1;
3705const int kHeapObjectTagSize = 2;
3706const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3707
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003708// Tag information for Smi.
3709const int kSmiTag = 0;
3710const int kSmiTagSize = 1;
3711const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3712
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003713template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003714
3715// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003716template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003717 static const int kSmiShiftSize = 0;
3718 static const int kSmiValueSize = 31;
3719 static inline int SmiToInt(internal::Object* value) {
3720 int shift_bits = kSmiTagSize + kSmiShiftSize;
3721 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3722 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3723 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003724
3725 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3726 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003727 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003728 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003729};
3730
3731// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003732template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003733 static const int kSmiShiftSize = 31;
3734 static const int kSmiValueSize = 32;
3735 static inline int SmiToInt(internal::Object* value) {
3736 int shift_bits = kSmiTagSize + kSmiShiftSize;
3737 // Shift down and throw away top 32 bits.
3738 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3739 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003740
3741 // To maximize the range of pointers that can be encoded
3742 // in the available 32 bits, we require them to be 8 bytes aligned.
3743 // This gives 2 ^ (32 + 3) = 32G address space covered.
3744 // It might be not enough to cover stack allocated objects on some platforms.
3745 static const int kPointerAlignment = 3;
3746
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003747 static const uintptr_t kEncodablePointerMask =
3748 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003749
3750 static const int kPointerToSmiShift =
3751 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003752};
3753
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003754typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3755const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3756const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003757const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003758 PlatformSmiTagging::kEncodablePointerMask;
3759const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003760
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003761template <size_t ptr_size> struct InternalConstants;
3762
3763// Internal constants for 32-bit systems.
3764template <> struct InternalConstants<4> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003765 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003766};
3767
3768// Internal constants for 64-bit systems.
3769template <> struct InternalConstants<8> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003770 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003771};
3772
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003773/**
3774 * This class exports constants and functionality from within v8 that
3775 * is necessary to implement inline functions in the v8 api. Don't
3776 * depend on functions and constants defined here.
3777 */
3778class Internals {
3779 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003780 // These values match non-compiler-dependent values defined within
3781 // the implementation of v8.
3782 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003783 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003784 static const int kStringResourceOffset =
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003785 InternalConstants<kApiPointerSize>::kStringResourceOffset;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003786
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003787 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003788 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003789 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003790 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003791
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003792 static const int kJSObjectType = 0xa6;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003793 static const int kFirstNonstringType = 0x80;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003794 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003795
3796 static inline bool HasHeapObjectTag(internal::Object* value) {
3797 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3798 kHeapObjectTag);
3799 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003800
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003801 static inline bool HasSmiTag(internal::Object* value) {
3802 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3803 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003804
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003805 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003806 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003807 }
3808
3809 static inline int GetInstanceType(internal::Object* obj) {
3810 typedef internal::Object O;
3811 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3812 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3813 }
3814
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003815 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003816 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003817 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3818 }
3819
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003820 static inline void* GetExternalPointer(internal::Object* obj) {
3821 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003822 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003823 } else if (GetInstanceType(obj) == kForeignType) {
3824 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003825 } else {
3826 return NULL;
3827 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003828 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003829
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003830 static inline bool IsExternalTwoByteString(int instance_type) {
3831 int representation = (instance_type & kFullStringRepresentationMask);
3832 return representation == kExternalTwoByteRepresentationTag;
3833 }
3834
3835 template <typename T>
3836 static inline T ReadField(Object* ptr, int offset) {
3837 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3838 return *reinterpret_cast<T*>(addr);
3839 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003840
3841 static inline bool CanCastToHeapObject(void* o) { return false; }
3842 static inline bool CanCastToHeapObject(Context* o) { return true; }
3843 static inline bool CanCastToHeapObject(String* o) { return true; }
3844 static inline bool CanCastToHeapObject(Object* o) { return true; }
3845 static inline bool CanCastToHeapObject(Message* o) { return true; }
3846 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
3847 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003848};
3849
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003850} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003851
3852
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003853template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003854Local<T>::Local() : Handle<T>() { }
3855
3856
3857template <class T>
3858Local<T> Local<T>::New(Handle<T> that) {
3859 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003860 T* that_ptr = *that;
3861 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
3862 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
3863 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
3864 reinterpret_cast<internal::HeapObject*>(*p))));
3865 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003866 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3867}
3868
3869
3870template <class T>
3871Persistent<T> Persistent<T>::New(Handle<T> that) {
3872 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003873 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003874 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3875}
3876
3877
3878template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003879bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003880 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003881 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882}
3883
3884
3885template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003886bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003887 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003888 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003889}
3890
3891
3892template <class T>
3893void Persistent<T>::Dispose() {
3894 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003895 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896}
3897
3898
3899template <class T>
3900Persistent<T>::Persistent() : Handle<T>() { }
3901
3902template <class T>
3903void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003904 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3905 parameters,
3906 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907}
3908
3909template <class T>
3910void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003911 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003912}
3913
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003914template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003915void Persistent<T>::MarkIndependent() {
3916 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
3917}
3918
3919template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003920void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
3921 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
3922}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003923
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003924Arguments::Arguments(internal::Object** implicit_args,
3925 internal::Object** values, int length,
3926 bool is_construct_call)
3927 : implicit_args_(implicit_args),
3928 values_(values),
3929 length_(length),
3930 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003931
3932
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003933Local<Value> Arguments::operator[](int i) const {
3934 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3935 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3936}
3937
3938
3939Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003940 return Local<Function>(reinterpret_cast<Function*>(
3941 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003942}
3943
3944
3945Local<Object> Arguments::This() const {
3946 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3947}
3948
3949
3950Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003951 return Local<Object>(reinterpret_cast<Object*>(
3952 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953}
3954
3955
3956Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003957 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003958}
3959
3960
3961bool Arguments::IsConstructCall() const {
3962 return is_construct_call_;
3963}
3964
3965
3966int Arguments::Length() const {
3967 return length_;
3968}
3969
3970
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003971template <class T>
3972Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003973 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3974 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003975 return Local<T>(reinterpret_cast<T*>(after));
3976}
3977
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003978Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003979 return resource_name_;
3980}
3981
3982
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003983Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984 return resource_line_offset_;
3985}
3986
3987
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003988Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003989 return resource_column_offset_;
3990}
3991
3992
3993Handle<Boolean> Boolean::New(bool value) {
3994 return value ? True() : False();
3995}
3996
3997
3998void Template::Set(const char* name, v8::Handle<Data> value) {
3999 Set(v8::String::New(name), value);
4000}
4001
4002
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004003Local<Value> Object::GetInternalField(int index) {
4004#ifndef V8_ENABLE_CHECKS
4005 Local<Value> quick_result = UncheckedGetInternalField(index);
4006 if (!quick_result.IsEmpty()) return quick_result;
4007#endif
4008 return CheckedGetInternalField(index);
4009}
4010
4011
4012Local<Value> Object::UncheckedGetInternalField(int index) {
4013 typedef internal::Object O;
4014 typedef internal::Internals I;
4015 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004016 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004017 // If the object is a plain JSObject, which is the common case,
4018 // we know where to find the internal fields and can return the
4019 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004020 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004021 O* value = I::ReadField<O*>(obj, offset);
4022 O** result = HandleScope::CreateHandle(value);
4023 return Local<Value>(reinterpret_cast<Value*>(result));
4024 } else {
4025 return Local<Value>();
4026 }
4027}
4028
4029
4030void* External::Unwrap(Handle<v8::Value> obj) {
4031#ifdef V8_ENABLE_CHECKS
4032 return FullUnwrap(obj);
4033#else
4034 return QuickUnwrap(obj);
4035#endif
4036}
4037
4038
4039void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4040 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004041 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004042 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004043}
4044
4045
4046void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004047 typedef internal::Object O;
4048 typedef internal::Internals I;
4049
4050 O* obj = *reinterpret_cast<O**>(this);
4051
4052 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4053 // If the object is a plain JSObject, which is the common case,
4054 // we know where to find the internal fields and can return the
4055 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004056 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004057 O* value = I::ReadField<O*>(obj, offset);
4058 return I::GetExternalPointer(value);
4059 }
4060
4061 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004062}
4063
4064
4065String* String::Cast(v8::Value* value) {
4066#ifdef V8_ENABLE_CHECKS
4067 CheckCast(value);
4068#endif
4069 return static_cast<String*>(value);
4070}
4071
4072
4073String::ExternalStringResource* String::GetExternalStringResource() const {
4074 typedef internal::Object O;
4075 typedef internal::Internals I;
4076 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004077 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004078 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004079 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4080 result = reinterpret_cast<String::ExternalStringResource*>(value);
4081 } else {
4082 result = NULL;
4083 }
4084#ifdef V8_ENABLE_CHECKS
4085 VerifyExternalStringResource(result);
4086#endif
4087 return result;
4088}
4089
4090
4091bool Value::IsString() const {
4092#ifdef V8_ENABLE_CHECKS
4093 return FullIsString();
4094#else
4095 return QuickIsString();
4096#endif
4097}
4098
4099bool Value::QuickIsString() const {
4100 typedef internal::Object O;
4101 typedef internal::Internals I;
4102 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4103 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004104 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004105}
4106
4107
4108Number* Number::Cast(v8::Value* value) {
4109#ifdef V8_ENABLE_CHECKS
4110 CheckCast(value);
4111#endif
4112 return static_cast<Number*>(value);
4113}
4114
4115
4116Integer* Integer::Cast(v8::Value* value) {
4117#ifdef V8_ENABLE_CHECKS
4118 CheckCast(value);
4119#endif
4120 return static_cast<Integer*>(value);
4121}
4122
4123
4124Date* Date::Cast(v8::Value* value) {
4125#ifdef V8_ENABLE_CHECKS
4126 CheckCast(value);
4127#endif
4128 return static_cast<Date*>(value);
4129}
4130
4131
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004132StringObject* StringObject::Cast(v8::Value* value) {
4133#ifdef V8_ENABLE_CHECKS
4134 CheckCast(value);
4135#endif
4136 return static_cast<StringObject*>(value);
4137}
4138
4139
4140NumberObject* NumberObject::Cast(v8::Value* value) {
4141#ifdef V8_ENABLE_CHECKS
4142 CheckCast(value);
4143#endif
4144 return static_cast<NumberObject*>(value);
4145}
4146
4147
4148BooleanObject* BooleanObject::Cast(v8::Value* value) {
4149#ifdef V8_ENABLE_CHECKS
4150 CheckCast(value);
4151#endif
4152 return static_cast<BooleanObject*>(value);
4153}
4154
4155
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004156RegExp* RegExp::Cast(v8::Value* value) {
4157#ifdef V8_ENABLE_CHECKS
4158 CheckCast(value);
4159#endif
4160 return static_cast<RegExp*>(value);
4161}
4162
4163
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004164Object* Object::Cast(v8::Value* value) {
4165#ifdef V8_ENABLE_CHECKS
4166 CheckCast(value);
4167#endif
4168 return static_cast<Object*>(value);
4169}
4170
4171
4172Array* Array::Cast(v8::Value* value) {
4173#ifdef V8_ENABLE_CHECKS
4174 CheckCast(value);
4175#endif
4176 return static_cast<Array*>(value);
4177}
4178
4179
4180Function* Function::Cast(v8::Value* value) {
4181#ifdef V8_ENABLE_CHECKS
4182 CheckCast(value);
4183#endif
4184 return static_cast<Function*>(value);
4185}
4186
4187
4188External* External::Cast(v8::Value* value) {
4189#ifdef V8_ENABLE_CHECKS
4190 CheckCast(value);
4191#endif
4192 return static_cast<External*>(value);
4193}
4194
4195
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004196Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004197 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004198}
4199
4200
4201Local<Object> AccessorInfo::This() const {
4202 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4203}
4204
4205
4206Local<Object> AccessorInfo::Holder() const {
4207 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4208}
4209
4210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004211/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004212 * \example shell.cc
4213 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004214 * command-line and executes them.
4215 */
4216
4217
4218/**
4219 * \example process.cc
4220 */
4221
4222
4223} // namespace v8
4224
4225
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004226#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004227#undef TYPE_CHECK
4228
4229
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004230#endif // V8_H_