blob: d31ef5405cfaf5c0fb7bbd419b73c50b715ca3a9 [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000065// Setup for Linux shared library export.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000066#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000067#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000068#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000069#else
70#define V8EXPORT
71#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000072#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000073#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000074#endif // defined(__GNUC__) && (__GNUC__ >= 4)
75
76#endif // _WIN32
77
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000079 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080 */
81namespace v8 {
82
83class Context;
84class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000085class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086class Value;
87class Utils;
88class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000089class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090class Object;
91class Array;
92class Int32;
93class Uint32;
94class External;
95class Primitive;
96class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000097class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098class Integer;
99class Function;
100class Date;
101class ImplementationUtilities;
102class Signature;
103template <class T> class Handle;
104template <class T> class Local;
105template <class T> class Persistent;
106class FunctionTemplate;
107class ObjectTemplate;
108class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000109class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000110class StackTrace;
111class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000112class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000114namespace internal {
115
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000116class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000117class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000118class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000119class HeapObject;
120class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000121}
122
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000124// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125
126
127/**
128 * A weak reference callback function.
129 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000130 * This callback should either explicitly invoke Dispose on |object| if
131 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
132 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 * \param object the weak global object to be reclaimed by the garbage collector
134 * \param parameter the value passed in when making the weak global object
135 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000136typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 void* parameter);
138
139
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000140// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000142#define TYPE_CHECK(T, S) \
143 while (false) { \
144 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 }
146
147/**
148 * An object reference managed by the v8 garbage collector.
149 *
150 * All objects returned from v8 have to be tracked by the garbage
151 * collector so that it knows that the objects are still alive. Also,
152 * because the garbage collector may move objects, it is unsafe to
153 * point directly to an object. Instead, all objects are stored in
154 * handles which are known by the garbage collector and updated
155 * whenever an object moves. Handles should always be passed by value
156 * (except in cases like out-parameters) and they should never be
157 * allocated on the heap.
158 *
159 * There are two types of handles: local and persistent handles.
160 * Local handles are light-weight and transient and typically used in
161 * local operations. They are managed by HandleScopes. Persistent
162 * handles can be used when storing objects across several independent
163 * operations and have to be explicitly deallocated when they're no
164 * longer used.
165 *
166 * It is safe to extract the object stored in the handle by
167 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000168 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 * behind the scenes and the same rules apply to these values as to
170 * their handles.
171 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000172template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 /**
175 * Creates an empty handle.
176 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000177 inline Handle() : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178
179 /**
180 * Creates a new handle for the specified value.
181 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000182 inline explicit Handle(T* val) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183
184 /**
185 * Creates a handle for the contents of the specified handle. This
186 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000187 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000189 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 * between compatible handles, for instance assigning a
191 * Handle<String> to a variable declared as Handle<Value>, is legal
192 * because String is a subclass of Value.
193 */
194 template <class S> inline Handle(Handle<S> that)
195 : val_(reinterpret_cast<T*>(*that)) {
196 /**
197 * This check fails when trying to convert between incompatible
198 * handles. For example, converting from a Handle<String> to a
199 * Handle<Number>.
200 */
201 TYPE_CHECK(T, S);
202 }
203
204 /**
205 * Returns true if the handle is empty.
206 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000207 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209 /**
210 * Sets the handle to be empty. IsEmpty() will then return true.
211 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000212 inline void Clear() { val_ = 0; }
213
214 inline T* operator->() const { return val_; }
215
216 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217
218 /**
219 * Checks whether two handles are the same.
220 * Returns true if both are empty, or if the objects
221 * to which they refer are identical.
222 * The handles' references are not checked.
223 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000224 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000225 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
226 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227 if (a == 0) return b == 0;
228 if (b == 0) return false;
229 return *a == *b;
230 }
231
232 /**
233 * Checks whether two handles are different.
234 * Returns true if only one of the handles is empty, or if
235 * the objects to which they refer are different.
236 * The handles' references are not checked.
237 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000238 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 return !operator==(that);
240 }
241
242 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000243#ifdef V8_ENABLE_CHECKS
244 // If we're going to perform the type check then we have to check
245 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000247#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 return Handle<T>(T::Cast(*that));
249 }
250
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000251 template <class S> inline Handle<S> As() {
252 return Handle<S>::Cast(*this);
253 }
254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 private:
256 T* val_;
257};
258
259
260/**
261 * A light-weight stack-allocated object handle. All operations
262 * that return objects from within v8 return them in local handles. They
263 * are created within HandleScopes, and all local handles allocated within a
264 * handle scope are destroyed when the handle scope is destroyed. Hence it
265 * is not necessary to explicitly deallocate local handles.
266 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000267template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000269 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 template <class S> inline Local(Local<S> that)
271 : Handle<T>(reinterpret_cast<T*>(*that)) {
272 /**
273 * This check fails when trying to convert between incompatible
274 * handles. For example, converting from a Handle<String> to a
275 * Handle<Number>.
276 */
277 TYPE_CHECK(T, S);
278 }
279 template <class S> inline Local(S* that) : Handle<T>(that) { }
280 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000281#ifdef V8_ENABLE_CHECKS
282 // If we're going to perform the type check then we have to check
283 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000285#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 return Local<T>(T::Cast(*that));
287 }
288
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000289 template <class S> inline Local<S> As() {
290 return Local<S>::Cast(*this);
291 }
292
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 /** Create a local handle for the content of another handle.
294 * The referee is kept alive by the local handle even when
295 * the original handle is destroyed/disposed.
296 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000297 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298};
299
300
301/**
302 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000303 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 * allocated, a Persistent handle remains valid until it is explicitly
305 * disposed.
306 *
307 * A persistent handle contains a reference to a storage cell within
308 * the v8 engine which holds an object value and which is updated by
309 * the garbage collector whenever the object is moved. A new storage
310 * cell can be created using Persistent::New and existing handles can
311 * be disposed using Persistent::Dispose. Since persistent handles
312 * are passed by value you may have many persistent handle objects
313 * that point to the same storage cell. For instance, if you pass a
314 * persistent handle as an argument to a function you will not get two
315 * different storage cells but rather two references to the same
316 * storage cell.
317 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000318template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 /**
321 * Creates an empty persistent handle that doesn't point to any
322 * storage cell.
323 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000324 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325
326 /**
327 * Creates a persistent handle for the same storage cell as the
328 * specified handle. This constructor allows you to pass persistent
329 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000330 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000332 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 * between compatible persistent handles, for instance assigning a
334 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000335 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 */
337 template <class S> inline Persistent(Persistent<S> that)
338 : Handle<T>(reinterpret_cast<T*>(*that)) {
339 /**
340 * This check fails when trying to convert between incompatible
341 * handles. For example, converting from a Handle<String> to a
342 * Handle<Number>.
343 */
344 TYPE_CHECK(T, S);
345 }
346
347 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
348
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000349 /**
350 * "Casts" a plain handle which is known to be a persistent handle
351 * to a persistent handle.
352 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 template <class S> explicit inline Persistent(Handle<S> that)
354 : Handle<T>(*that) { }
355
356 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000357#ifdef V8_ENABLE_CHECKS
358 // If we're going to perform the type check then we have to check
359 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000361#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 return Persistent<T>(T::Cast(*that));
363 }
364
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000365 template <class S> inline Persistent<S> As() {
366 return Persistent<S>::Cast(*this);
367 }
368
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000370 * Creates a new persistent handle for an existing local or
371 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000373 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374
375 /**
376 * Releases the storage cell referenced by this persistent handle.
377 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000378 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 * cell remain and IsEmpty will still return false.
380 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000381 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382
383 /**
384 * Make the reference to this object weak. When only weak handles
385 * refer to the object, the garbage collector will perform a
386 * callback to the given V8::WeakReferenceCallback function, passing
387 * it the object reference and the given parameters.
388 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000389 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390
391 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000392 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393
394 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000395 * Marks the reference to this object independent. Garbage collector
396 * is free to ignore any object groups containing this object.
397 * Weak callback for an independent handle should not
398 * assume that it will be preceded by a global GC prologue callback
399 * or followed by a global GC epilogue callback.
400 */
401 inline void MarkIndependent();
402
403 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404 *Checks if the handle holds the only reference to an object.
405 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000406 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407
408 /**
409 * Returns true if the handle's reference is weak.
410 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000411 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000413 /**
414 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
415 * interface description in v8-profiler.h for details.
416 */
417 inline void SetWrapperClassId(uint16_t class_id);
418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 private:
420 friend class ImplementationUtilities;
421 friend class ObjectTemplate;
422};
423
424
v8.team.kasperl727e9952008-09-02 14:56:44 +0000425 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 * A stack-allocated class that governs a number of local handles.
427 * After a handle scope has been created, all local handles will be
428 * allocated within that handle scope until either the handle scope is
429 * deleted or another handle scope is created. If there is already a
430 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000431 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 * new handles will again be allocated in the original handle scope.
433 *
434 * After the handle scope of a local handle has been deleted the
435 * garbage collector will no longer track the object stored in the
436 * handle and may deallocate it. The behavior of accessing a handle
437 * for which the handle scope has been deleted is undefined.
438 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000439class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000441 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000443 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444
445 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446 * Closes the handle scope and returns the value as a handle in the
447 * previous scope, which is the new current scope after the call.
448 */
449 template <class T> Local<T> Close(Handle<T> value);
450
451 /**
452 * Counts the number of allocated handles.
453 */
454 static int NumberOfHandles();
455
456 /**
457 * Creates a new handle with the given value.
458 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000459 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000460 // Faster version, uses HeapObject to obtain the current Isolate.
461 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462
463 private:
464 // Make it impossible to create heap-allocated or illegal handle
465 // scopes by disallowing certain operations.
466 HandleScope(const HandleScope&);
467 void operator=(const HandleScope&);
468 void* operator new(size_t size);
469 void operator delete(void*, size_t);
470
ager@chromium.org3811b432009-10-28 14:53:37 +0000471 // This Data class is accessible internally as HandleScopeData through a
472 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000473 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000475 internal::Object** next;
476 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000477 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000480 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 }
482 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000483
lrn@chromium.org303ada72010-10-27 09:33:13 +0000484 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000486 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000487 internal::Object** prev_next_;
488 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000490 // Allow for the active closing of HandleScopes which allows to pass a handle
491 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000493 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 friend class ImplementationUtilities;
496};
497
498
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000499// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500
501
502/**
503 * The superclass of values and API object templates.
504 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000505class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506 private:
507 Data();
508};
509
510
511/**
512 * Pre-compilation data that can be associated with a script. This
513 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000514 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000515 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000517class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 public:
519 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000520
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000521 /**
522 * Pre-compiles the specified script (context-independent).
523 *
524 * \param input Pointer to UTF-8 script source code.
525 * \param length Length of UTF-8 script source code.
526 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000529 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000530 * Pre-compiles the specified script (context-independent).
531 *
532 * NOTE: Pre-compilation using this method cannot happen on another thread
533 * without using Lockers.
534 *
535 * \param source Script source code.
536 */
537 static ScriptData* PreCompile(Handle<String> source);
538
539 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000540 * Load previous pre-compilation data.
541 *
542 * \param data Pointer to data returned by a call to Data() of a previous
543 * ScriptData. Ownership is not transferred.
544 * \param length Length of data.
545 */
546 static ScriptData* New(const char* data, int length);
547
548 /**
549 * Returns the length of Data().
550 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000552
553 /**
554 * Returns a serialized representation of this ScriptData that can later be
555 * passed to New(). NOTE: Serialized data is platform-dependent.
556 */
557 virtual const char* Data() = 0;
558
559 /**
560 * Returns true if the source code could not be parsed.
561 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000562 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563};
564
565
566/**
567 * The origin, within a file, of a script.
568 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000569class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000571 inline ScriptOrigin(
572 Handle<Value> resource_name,
573 Handle<Integer> resource_line_offset = Handle<Integer>(),
574 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 : resource_name_(resource_name),
576 resource_line_offset_(resource_line_offset),
577 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000578 inline Handle<Value> ResourceName() const;
579 inline Handle<Integer> ResourceLineOffset() const;
580 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000582 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 Handle<Integer> resource_line_offset_;
584 Handle<Integer> resource_column_offset_;
585};
586
587
588/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000589 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000591class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000594 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000595 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000596 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000597 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000598 * when New() returns
599 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
600 * using pre_data speeds compilation if it's done multiple times.
601 * Owned by caller, no references are kept when New() returns.
602 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000603 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000604 * available to compile event handlers.
605 * \return Compiled script object (context independent; when run it
606 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000608 static Local<Script> New(Handle<String> source,
609 ScriptOrigin* origin = NULL,
610 ScriptData* pre_data = NULL,
611 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612
mads.s.agercbaa0602008-08-14 13:41:48 +0000613 /**
614 * Compiles the specified script using the specified file name
615 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000616 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000617 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000618 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000619 * as the script's origin.
620 * \return Compiled script object (context independent; when run it
621 * will use the currently entered context).
622 */
623 static Local<Script> New(Handle<String> source,
624 Handle<Value> file_name);
625
626 /**
627 * Compiles the specified script (bound to current context).
628 *
629 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000630 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000631 * when Compile() returns
632 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
633 * using pre_data speeds compilation if it's done multiple times.
634 * Owned by caller, no references are kept when Compile() returns.
635 * \param script_data Arbitrary data associated with script. Using
636 * this has same effect as calling SetData(), but makes data available
637 * earlier (i.e. to compile event handlers).
638 * \return Compiled script object, bound to the context that was active
639 * when this function was called. When run it will always use this
640 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000641 */
642 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000643 ScriptOrigin* origin = NULL,
644 ScriptData* pre_data = NULL,
645 Handle<String> script_data = Handle<String>());
646
647 /**
648 * Compiles the specified script using the specified file name
649 * object (typically a string) as the script's origin.
650 *
651 * \param source Script source code.
652 * \param file_name File name to use as script's origin
653 * \param script_data Arbitrary data associated with script. Using
654 * this has same effect as calling SetData(), but makes data available
655 * earlier (i.e. to compile event handlers).
656 * \return Compiled script object, bound to the context that was active
657 * when this function was called. When run it will always use this
658 * context.
659 */
660 static Local<Script> Compile(Handle<String> source,
661 Handle<Value> file_name,
662 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000663
v8.team.kasperl727e9952008-09-02 14:56:44 +0000664 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000665 * Runs the script returning the resulting value. If the script is
666 * context independent (created using ::New) it will be run in the
667 * currently entered context. If it is context specific (created
668 * using ::Compile) it will be run in the context in which it was
669 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000670 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000672
673 /**
674 * Returns the script id value.
675 */
676 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000677
678 /**
679 * Associate an additional data object with the script. This is mainly used
680 * with the debugger as this data object is only available through the
681 * debugger API.
682 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000683 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000684};
685
686
687/**
688 * An error message.
689 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000690class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000692 Local<String> Get() const;
693 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000695 /**
696 * Returns the resource name for the script from where the function causing
697 * the error originates.
698 */
ager@chromium.org32912102009-01-16 10:38:43 +0000699 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000701 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000702 * Returns the resource data for the script from where the function causing
703 * the error originates.
704 */
705 Handle<Value> GetScriptData() const;
706
707 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000708 * Exception stack trace. By default stack traces are not captured for
709 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
710 * to change this option.
711 */
712 Handle<StackTrace> GetStackTrace() const;
713
714 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000715 * Returns the number, 1-based, of the line where the error occurred.
716 */
ager@chromium.org32912102009-01-16 10:38:43 +0000717 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000719 /**
720 * Returns the index within the script of the first character where
721 * the error occurred.
722 */
ager@chromium.org32912102009-01-16 10:38:43 +0000723 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000724
725 /**
726 * Returns the index within the script of the last character where
727 * the error occurred.
728 */
ager@chromium.org32912102009-01-16 10:38:43 +0000729 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000730
731 /**
732 * Returns the index within the line of the first character where
733 * the error occurred.
734 */
ager@chromium.org32912102009-01-16 10:38:43 +0000735 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000736
737 /**
738 * Returns the index within the line of the last character where
739 * the error occurred.
740 */
ager@chromium.org32912102009-01-16 10:38:43 +0000741 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000742
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743 // TODO(1245381): Print to a string instead of on a FILE.
744 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000745
746 static const int kNoLineNumberInfo = 0;
747 static const int kNoColumnInfo = 0;
748};
749
750
751/**
752 * Representation of a JavaScript stack trace. The information collected is a
753 * snapshot of the execution stack and the information remains valid after
754 * execution continues.
755 */
756class V8EXPORT StackTrace {
757 public:
758 /**
759 * Flags that determine what information is placed captured for each
760 * StackFrame when grabbing the current stack trace.
761 */
762 enum StackTraceOptions {
763 kLineNumber = 1,
764 kColumnOffset = 1 << 1 | kLineNumber,
765 kScriptName = 1 << 2,
766 kFunctionName = 1 << 3,
767 kIsEval = 1 << 4,
768 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000769 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000770 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000771 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000772 };
773
774 /**
775 * Returns a StackFrame at a particular index.
776 */
777 Local<StackFrame> GetFrame(uint32_t index) const;
778
779 /**
780 * Returns the number of StackFrames.
781 */
782 int GetFrameCount() const;
783
784 /**
785 * Returns StackTrace as a v8::Array that contains StackFrame objects.
786 */
787 Local<Array> AsArray();
788
789 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000790 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000791 *
792 * \param frame_limit The maximum number of stack frames we want to capture.
793 * \param options Enumerates the set of things we will capture for each
794 * StackFrame.
795 */
796 static Local<StackTrace> CurrentStackTrace(
797 int frame_limit,
798 StackTraceOptions options = kOverview);
799};
800
801
802/**
803 * A single JavaScript stack frame.
804 */
805class V8EXPORT StackFrame {
806 public:
807 /**
808 * Returns the number, 1-based, of the line for the associate function call.
809 * This method will return Message::kNoLineNumberInfo if it is unable to
810 * retrieve the line number, or if kLineNumber was not passed as an option
811 * when capturing the StackTrace.
812 */
813 int GetLineNumber() const;
814
815 /**
816 * Returns the 1-based column offset on the line for the associated function
817 * call.
818 * This method will return Message::kNoColumnInfo if it is unable to retrieve
819 * the column number, or if kColumnOffset was not passed as an option when
820 * capturing the StackTrace.
821 */
822 int GetColumn() const;
823
824 /**
825 * Returns the name of the resource that contains the script for the
826 * function for this StackFrame.
827 */
828 Local<String> GetScriptName() const;
829
830 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000831 * Returns the name of the resource that contains the script for the
832 * function for this StackFrame or sourceURL value if the script name
833 * is undefined and its source ends with //@ sourceURL=... string.
834 */
835 Local<String> GetScriptNameOrSourceURL() const;
836
837 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000838 * Returns the name of the function associated with this stack frame.
839 */
840 Local<String> GetFunctionName() const;
841
842 /**
843 * Returns whether or not the associated function is compiled via a call to
844 * eval().
845 */
846 bool IsEval() const;
847
848 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000849 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000850 * constructor via "new".
851 */
852 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853};
854
855
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000856// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857
858
859/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000860 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000862class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864 /**
865 * Returns true if this value is the undefined value. See ECMA-262
866 * 4.3.10.
867 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000868 inline bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
870 /**
871 * Returns true if this value is the null value. See ECMA-262
872 * 4.3.11.
873 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000874 inline bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875
876 /**
877 * Returns true if this value is true.
878 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000879 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880
881 /**
882 * Returns true if this value is false.
883 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000884 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885
886 /**
887 * Returns true if this value is an instance of the String type.
888 * See ECMA-262 8.4.
889 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000890 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891
892 /**
893 * Returns true if this value is a function.
894 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000895 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
897 /**
898 * Returns true if this value is an array.
899 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000900 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901
v8.team.kasperl727e9952008-09-02 14:56:44 +0000902 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 * Returns true if this value is an object.
904 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000905 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906
v8.team.kasperl727e9952008-09-02 14:56:44 +0000907 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 * Returns true if this value is boolean.
909 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000910 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000911
v8.team.kasperl727e9952008-09-02 14:56:44 +0000912 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 * Returns true if this value is a number.
914 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000915 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916
v8.team.kasperl727e9952008-09-02 14:56:44 +0000917 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918 * Returns true if this value is external.
919 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000920 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921
v8.team.kasperl727e9952008-09-02 14:56:44 +0000922 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923 * Returns true if this value is a 32-bit signed integer.
924 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000925 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000927 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000928 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000929 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000930 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000931
932 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000933 * Returns true if this value is a Date.
934 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000935 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000936
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000937 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000938 * Returns true if this value is a Boolean object.
939 */
940 V8EXPORT bool IsBooleanObject() const;
941
942 /**
943 * Returns true if this value is a Number object.
944 */
945 V8EXPORT bool IsNumberObject() const;
946
947 /**
948 * Returns true if this value is a String object.
949 */
950 V8EXPORT bool IsStringObject() const;
951
952 /**
953 * Returns true if this value is a NativeError.
954 */
955 V8EXPORT bool IsNativeError() const;
956
957 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000958 * Returns true if this value is a RegExp.
959 */
960 V8EXPORT bool IsRegExp() const;
961
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000962 V8EXPORT Local<Boolean> ToBoolean() const;
963 V8EXPORT Local<Number> ToNumber() const;
964 V8EXPORT Local<String> ToString() const;
965 V8EXPORT Local<String> ToDetailString() const;
966 V8EXPORT Local<Object> ToObject() const;
967 V8EXPORT Local<Integer> ToInteger() const;
968 V8EXPORT Local<Uint32> ToUint32() const;
969 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970
971 /**
972 * Attempts to convert a string to an array index.
973 * Returns an empty handle if the conversion fails.
974 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000975 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000977 V8EXPORT bool BooleanValue() const;
978 V8EXPORT double NumberValue() const;
979 V8EXPORT int64_t IntegerValue() const;
980 V8EXPORT uint32_t Uint32Value() const;
981 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982
983 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000984 V8EXPORT bool Equals(Handle<Value> that) const;
985 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000986
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000987 private:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000988 inline bool QuickIsUndefined() const;
989 inline bool QuickIsNull() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000990 inline bool QuickIsString() const;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000991 V8EXPORT bool FullIsUndefined() const;
992 V8EXPORT bool FullIsNull() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000993 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994};
995
996
997/**
998 * The superclass of primitive values. See ECMA-262 4.3.2.
999 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001000class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001
1002
1003/**
1004 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1005 * or false value.
1006 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001007class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001009 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 static inline Handle<Boolean> New(bool value);
1011};
1012
1013
1014/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001015 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001017class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001019 /**
1020 * Returns the number of characters in this string.
1021 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001022 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023
v8.team.kasperl727e9952008-09-02 14:56:44 +00001024 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001025 * Returns the number of bytes in the UTF-8 encoded
1026 * representation of this string.
1027 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001028 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001029
1030 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001031 * A fast conservative check for non-ASCII characters. May
1032 * return true even for ASCII strings, but if it returns
1033 * false you can be sure that all characters are in the range
1034 * 0-127.
1035 */
1036 V8EXPORT bool MayContainNonAscii() const;
1037
1038 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001039 * Write the contents of the string to an external buffer.
1040 * If no arguments are given, expects the buffer to be large
1041 * enough to hold the entire string and NULL terminator. Copies
1042 * the contents of the string and the NULL terminator into the
1043 * buffer.
1044 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001045 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1046 * before the end of the buffer.
1047 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001048 * Copies up to length characters into the output buffer.
1049 * Only null-terminates if there is enough space in the buffer.
1050 *
1051 * \param buffer The buffer into which the string will be copied.
1052 * \param start The starting position within the string at which
1053 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001054 * \param length The number of characters to copy from the string. For
1055 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001056 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001057 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001058 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001059 * \return The number of characters copied to the buffer excluding the null
1060 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001061 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001062 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001063 enum WriteOptions {
1064 NO_OPTIONS = 0,
1065 HINT_MANY_WRITES_EXPECTED = 1,
1066 NO_NULL_TERMINATION = 2
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001067 };
1068
lrn@chromium.org34e60782011-09-15 07:25:40 +00001069 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001070 V8EXPORT int Write(uint16_t* buffer,
1071 int start = 0,
1072 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001073 int options = NO_OPTIONS) const;
1074 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001075 V8EXPORT int WriteAscii(char* buffer,
1076 int start = 0,
1077 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001078 int options = NO_OPTIONS) const;
1079 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001080 V8EXPORT int WriteUtf8(char* buffer,
1081 int length = -1,
1082 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001083 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084
v8.team.kasperl727e9952008-09-02 14:56:44 +00001085 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001086 * A zero length string.
1087 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001088 V8EXPORT static v8::Local<v8::String> Empty();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001089 inline static v8::Local<v8::String> Empty(Isolate* isolate);
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001090
1091 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001092 * Returns true if the string is external
1093 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001094 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095
v8.team.kasperl727e9952008-09-02 14:56:44 +00001096 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001097 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001098 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001099 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001100
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001101 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001102 public:
1103 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001104
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001105 protected:
1106 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001107
1108 /**
1109 * Internally V8 will call this Dispose method when the external string
1110 * resource is no longer needed. The default implementation will use the
1111 * delete operator. This method can be overridden in subclasses to
1112 * control how allocated external string resources are disposed.
1113 */
1114 virtual void Dispose() { delete this; }
1115
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001116 private:
1117 // Disallow copying and assigning.
1118 ExternalStringResourceBase(const ExternalStringResourceBase&);
1119 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001120
1121 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001122 };
1123
v8.team.kasperl727e9952008-09-02 14:56:44 +00001124 /**
1125 * An ExternalStringResource is a wrapper around a two-byte string
1126 * buffer that resides outside V8's heap. Implement an
1127 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001128 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001129 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001130 class V8EXPORT ExternalStringResource
1131 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132 public:
1133 /**
1134 * Override the destructor to manage the life cycle of the underlying
1135 * buffer.
1136 */
1137 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001138
1139 /**
1140 * The string data from the underlying buffer.
1141 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001143
1144 /**
1145 * The length of the string. That is, the number of two-byte characters.
1146 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149 protected:
1150 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151 };
1152
1153 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001154 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001155 * string buffer that resides outside V8's heap. Implement an
1156 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001157 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001158 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001159 * UTF-8, which would require special treatment internally in the
1160 * engine and, in the case of UTF-8, do not allow efficient indexing.
1161 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001162 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001164 class V8EXPORT ExternalAsciiStringResource
1165 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166 public:
1167 /**
1168 * Override the destructor to manage the life cycle of the underlying
1169 * buffer.
1170 */
1171 virtual ~ExternalAsciiStringResource() {}
1172 /** The string data from the underlying buffer.*/
1173 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001174 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 virtual size_t length() const = 0;
1176 protected:
1177 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178 };
1179
1180 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001181 * Get the ExternalStringResource for an external string. Returns
1182 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001184 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185
1186 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001187 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001188 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001190 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1191 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001193 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194
1195 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001196 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001198 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 * be careful to supply the length parameter.
1200 * If it is not given, the function calls
1201 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001202 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001204 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205
lrn@chromium.org34e60782011-09-15 07:25:40 +00001206 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001207 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208
1209 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001210 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211
v8.team.kasperl727e9952008-09-02 14:56:44 +00001212 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001213 * Creates a new string by concatenating the left and the right strings
1214 * passed in as parameters.
1215 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001216 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001217 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001218
1219 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001220 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001221 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001222 * resource will be disposed by calling its Dispose method. The caller of
1223 * this function should not otherwise delete or modify the resource. Neither
1224 * should the underlying buffer be deallocated or modified except through the
1225 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001226 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001227 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001228
ager@chromium.org6f10e412009-02-13 10:11:16 +00001229 /**
1230 * Associate an external string resource with this string by transforming it
1231 * in place so that existing references to this string in the JavaScript heap
1232 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001233 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001234 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001235 * The string is not modified if the operation fails. See NewExternal for
1236 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001237 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001238 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239
v8.team.kasperl727e9952008-09-02 14:56:44 +00001240 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001241 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001242 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001243 * resource will be disposed by calling its Dispose method. The caller of
1244 * this function should not otherwise delete or modify the resource. Neither
1245 * should the underlying buffer be deallocated or modified except through the
1246 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001247 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001248 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001249
ager@chromium.org6f10e412009-02-13 10:11:16 +00001250 /**
1251 * Associate an external string resource with this string by transforming it
1252 * in place so that existing references to this string in the JavaScript heap
1253 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001254 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001255 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001256 * The string is not modified if the operation fails. See NewExternal for
1257 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001258 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001259 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001261 /**
1262 * Returns true if this string can be made external.
1263 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001264 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001265
lrn@chromium.org34e60782011-09-15 07:25:40 +00001266 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001267 V8EXPORT static Local<String> NewUndetectable(const char* data,
1268 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269
lrn@chromium.org34e60782011-09-15 07:25:40 +00001270 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001271 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1272 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273
1274 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001275 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001276 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001277 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001278 * then the length() method returns 0 and the * operator returns
1279 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001280 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001281 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001282 public:
1283 explicit Utf8Value(Handle<v8::Value> obj);
1284 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001285 char* operator*() { return str_; }
1286 const char* operator*() const { return str_; }
1287 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001288 private:
1289 char* str_;
1290 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001291
1292 // Disallow copying and assigning.
1293 Utf8Value(const Utf8Value&);
1294 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001295 };
1296
1297 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001298 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001300 * If conversion to a string fails (eg. due to an exception in the toString()
1301 * method of the object) then the length() method returns 0 and the * operator
1302 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001304 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 public:
1306 explicit AsciiValue(Handle<v8::Value> obj);
1307 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001308 char* operator*() { return str_; }
1309 const char* operator*() const { return str_; }
1310 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 private:
1312 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001313 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001314
1315 // Disallow copying and assigning.
1316 AsciiValue(const AsciiValue&);
1317 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 };
1319
1320 /**
1321 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001322 * If conversion to a string fails (eg. due to an exception in the toString()
1323 * method of the object) then the length() method returns 0 and the * operator
1324 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001326 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 public:
1328 explicit Value(Handle<v8::Value> obj);
1329 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001330 uint16_t* operator*() { return str_; }
1331 const uint16_t* operator*() const { return str_; }
1332 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 private:
1334 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001335 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001336
1337 // Disallow copying and assigning.
1338 Value(const Value&);
1339 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001341
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001342 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001343 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1344 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345};
1346
1347
1348/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001349 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001351class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001353 V8EXPORT double Value() const;
1354 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001355 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001357 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001358 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 signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001365class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001367 V8EXPORT static Local<Integer> New(int32_t value);
1368 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1369 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001370 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001372 V8EXPORT Integer();
1373 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374};
1375
1376
1377/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001378 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001380class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001382 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001384 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385};
1386
1387
1388/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001389 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001391class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001393 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001395 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396};
1397
1398
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399enum PropertyAttribute {
1400 None = 0,
1401 ReadOnly = 1 << 0,
1402 DontEnum = 1 << 1,
1403 DontDelete = 1 << 2
1404};
1405
ager@chromium.org3811b432009-10-28 14:53:37 +00001406enum ExternalArrayType {
1407 kExternalByteArray = 1,
1408 kExternalUnsignedByteArray,
1409 kExternalShortArray,
1410 kExternalUnsignedShortArray,
1411 kExternalIntArray,
1412 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001413 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001414 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001415 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001416};
1417
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001419 * Accessor[Getter|Setter] are used as callback functions when
1420 * setting|getting a particular property. See Object and ObjectTemplate's
1421 * method SetAccessor.
1422 */
1423typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1424 const AccessorInfo& info);
1425
1426
1427typedef void (*AccessorSetter)(Local<String> property,
1428 Local<Value> value,
1429 const AccessorInfo& info);
1430
1431
1432/**
1433 * Access control specifications.
1434 *
1435 * Some accessors should be accessible across contexts. These
1436 * accessors have an explicit access control parameter which specifies
1437 * the kind of cross-context access that should be allowed.
1438 *
1439 * Additionally, for security, accessors can prohibit overwriting by
1440 * accessors defined in JavaScript. For objects that have such
1441 * accessors either locally or in their prototype chain it is not
1442 * possible to overwrite the accessor by using __defineGetter__ or
1443 * __defineSetter__ from JavaScript code.
1444 */
1445enum AccessControl {
1446 DEFAULT = 0,
1447 ALL_CAN_READ = 1,
1448 ALL_CAN_WRITE = 1 << 1,
1449 PROHIBITS_OVERWRITING = 1 << 2
1450};
1451
1452
1453/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001454 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001456class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001458 V8EXPORT bool Set(Handle<Value> key,
1459 Handle<Value> value,
1460 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001461
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001462 V8EXPORT bool Set(uint32_t index,
1463 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001464
ager@chromium.orge2902be2009-06-08 12:21:35 +00001465 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001466 // overriding accessors or read-only properties.
1467 //
1468 // Note that if the object has an interceptor the property will be set
1469 // locally, but since the interceptor takes precedence the local property
1470 // will only be returned if the interceptor doesn't return a value.
1471 //
1472 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001473 V8EXPORT bool ForceSet(Handle<Value> key,
1474 Handle<Value> value,
1475 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001476
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001477 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001479 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001480
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001481 /**
1482 * Gets the property attributes of a property which can be None or
1483 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1484 * None when the property doesn't exist.
1485 */
1486 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1487
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 // TODO(1245389): Replace the type-specific versions of these
1489 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001490 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001491
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001492 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001493
1494 // Delete a property on this object bypassing interceptors and
1495 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001496 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001497
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001498 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001499
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001500 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001502 V8EXPORT bool SetAccessor(Handle<String> name,
1503 AccessorGetter getter,
1504 AccessorSetter setter = 0,
1505 Handle<Value> data = Handle<Value>(),
1506 AccessControl settings = DEFAULT,
1507 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001508
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001510 * Returns an array containing the names of the enumerable properties
1511 * of this object, including properties from prototype objects. The
1512 * array returned by this method contains the same values as would
1513 * be enumerated by a for-in statement over this object.
1514 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001515 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001516
1517 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001518 * This function has the same functionality as GetPropertyNames but
1519 * the returned array doesn't contain the names of properties from
1520 * prototype objects.
1521 */
1522 V8EXPORT Local<Array> GetOwnPropertyNames();
1523
1524 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525 * Get the prototype object. This does not skip objects marked to
1526 * be skipped by __proto__ and it does not consult the security
1527 * handler.
1528 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001529 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530
1531 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001532 * Set the prototype object. This does not skip objects marked to
1533 * be skipped by __proto__ and it does not consult the security
1534 * handler.
1535 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001536 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001537
1538 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001539 * Finds an instance of the given function template in the prototype
1540 * chain.
1541 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001542 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1543 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001544
1545 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001546 * Call builtin Object.prototype.toString on this object.
1547 * This is different from Value::ToString() that may call
1548 * user-defined toString function. This one does not.
1549 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001550 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001552 /**
1553 * Returns the name of the function invoked as a constructor for this object.
1554 */
1555 V8EXPORT Local<String> GetConstructorName();
1556
kasper.lund212ac232008-07-16 07:07:30 +00001557 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001558 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001559 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001560 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001561 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001562 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001564 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001565 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001566
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001567 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001568 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001569
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001571 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001572 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1573 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1574 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575
1576 /**
1577 * If result.IsEmpty() no real property was located in the prototype chain.
1578 * This means interceptors in the prototype chain are not called.
1579 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001580 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1581 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001582
1583 /**
1584 * If result.IsEmpty() no real property was located on the object or
1585 * in the prototype chain.
1586 * This means interceptors in the prototype chain are not called.
1587 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001588 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589
1590 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001591 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592
kasper.lund212ac232008-07-16 07:07:30 +00001593 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001594 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001595
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001596 /**
1597 * Turns on access check on the object if the object is an instance of
1598 * a template that has access check callbacks. If an object has no
1599 * access check info, the object cannot be accessed by anyone.
1600 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001601 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001602
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001603 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001604 * Returns the identity hash for this object. The current implementation
1605 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001606 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001607 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001608 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001609 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001610 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001611
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001612 /**
1613 * Access hidden properties on JavaScript objects. These properties are
1614 * hidden from the executing JavaScript and only accessible through the V8
1615 * C++ API. Hidden properties introduced by V8 internally (for example the
1616 * identity hash) are prefixed with "v8::".
1617 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001618 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1619 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1620 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001621
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001622 /**
1623 * Returns true if this is an instance of an api function (one
1624 * created from a function created from a function template) and has
1625 * been modified since it was created. Note that this method is
1626 * conservative and may return true for objects that haven't actually
1627 * been modified.
1628 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001629 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001630
1631 /**
1632 * Clone this object with a fast but shallow copy. Values will point
1633 * to the same values as the original object.
1634 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001635 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001637 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001638 * Returns the context in which the object was created.
1639 */
1640 V8EXPORT Local<Context> CreationContext();
1641
1642 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001643 * Set the backing store of the indexed properties to be managed by the
1644 * embedding layer. Access to the indexed properties will follow the rules
1645 * spelled out in CanvasPixelArray.
1646 * Note: The embedding program still owns the data and needs to ensure that
1647 * the backing store is preserved while V8 has a reference.
1648 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001649 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001650 V8EXPORT bool HasIndexedPropertiesInPixelData();
1651 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1652 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001653
ager@chromium.org3811b432009-10-28 14:53:37 +00001654 /**
1655 * Set the backing store of the indexed properties to be managed by the
1656 * embedding layer. Access to the indexed properties will follow the rules
1657 * spelled out for the CanvasArray subtypes in the WebGL specification.
1658 * Note: The embedding program still owns the data and needs to ensure that
1659 * the backing store is preserved while V8 has a reference.
1660 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001661 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1662 void* data,
1663 ExternalArrayType array_type,
1664 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001665 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1666 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1667 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1668 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001669
lrn@chromium.org1c092762011-05-09 09:42:16 +00001670 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001671 * Checks whether a callback is set by the
1672 * ObjectTemplate::SetCallAsFunctionHandler method.
1673 * When an Object is callable this method returns true.
1674 */
1675 V8EXPORT bool IsCallable();
1676
1677 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001678 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001679 * ObjectTemplate::SetCallAsFunctionHandler method.
1680 */
1681 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1682 int argc,
1683 Handle<Value> argv[]);
1684
1685 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001686 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001687 * ObjectTemplate::SetCallAsFunctionHandler method.
1688 * Note: This method behaves like the Function::NewInstance method.
1689 */
1690 V8EXPORT Local<Value> CallAsConstructor(int argc,
1691 Handle<Value> argv[]);
1692
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001693 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001694 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001695
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001697 V8EXPORT Object();
1698 V8EXPORT static void CheckCast(Value* obj);
1699 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1700 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001701
1702 /**
1703 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001704 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001705 */
1706 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707};
1708
1709
1710/**
1711 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1712 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001713class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001715 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716
ager@chromium.org3e875802009-06-29 08:26:34 +00001717 /**
1718 * Clones an element at index |index|. Returns an empty
1719 * handle if cloning fails (for any reason).
1720 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001721 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001722
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001723 /**
1724 * Creates a JavaScript array with the given length. If the length
1725 * is negative the returned array will have length 0.
1726 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001727 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001728
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001729 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001731 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001732 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733};
1734
1735
1736/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001737 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001739class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001741 V8EXPORT Local<Object> NewInstance() const;
1742 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1743 V8EXPORT Local<Value> Call(Handle<Object> recv,
1744 int argc,
1745 Handle<Value> argv[]);
1746 V8EXPORT void SetName(Handle<String> name);
1747 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001748
1749 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001750 * Name inferred from variable or property assignment of this function.
1751 * Used to facilitate debugging and profiling of JavaScript code written
1752 * in an OO style, where many functions are anonymous but are assigned
1753 * to object properties.
1754 */
1755 V8EXPORT Handle<Value> GetInferredName() const;
1756
1757 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001758 * Returns zero based line number of function body and
1759 * kLineOffsetNotFound if no information available.
1760 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001761 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001762 /**
1763 * Returns zero based column number of function body and
1764 * kLineOffsetNotFound if no information available.
1765 */
1766 V8EXPORT int GetScriptColumnNumber() const;
1767 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001768 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001769 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001770 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001773 V8EXPORT Function();
1774 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775};
1776
1777
1778/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001779 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1780 */
1781class Date : public Object {
1782 public:
1783 V8EXPORT static Local<Value> New(double time);
1784
1785 /**
1786 * A specialization of Value::NumberValue that is more efficient
1787 * because we know the structure of this object.
1788 */
1789 V8EXPORT double NumberValue() const;
1790
1791 static inline Date* Cast(v8::Value* obj);
1792
1793 /**
1794 * Notification that the embedder has changed the time zone,
1795 * daylight savings time, or other date / time configuration
1796 * parameters. V8 keeps a cache of various values used for
1797 * date / time computation. This notification will reset
1798 * those cached values for the current context so that date /
1799 * time configuration changes would be reflected in the Date
1800 * object.
1801 *
1802 * This API should not be called more than needed as it will
1803 * negatively impact the performance of date operations.
1804 */
1805 V8EXPORT static void DateTimeConfigurationChangeNotification();
1806
1807 private:
1808 V8EXPORT static void CheckCast(v8::Value* obj);
1809};
1810
1811
1812/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001813 * A Number object (ECMA-262, 4.3.21).
1814 */
1815class NumberObject : public Object {
1816 public:
1817 V8EXPORT static Local<Value> New(double value);
1818
1819 /**
1820 * Returns the Number held by the object.
1821 */
1822 V8EXPORT double NumberValue() const;
1823
1824 static inline NumberObject* Cast(v8::Value* obj);
1825
1826 private:
1827 V8EXPORT static void CheckCast(v8::Value* obj);
1828};
1829
1830
1831/**
1832 * A Boolean object (ECMA-262, 4.3.15).
1833 */
1834class BooleanObject : public Object {
1835 public:
1836 V8EXPORT static Local<Value> New(bool value);
1837
1838 /**
1839 * Returns the Boolean held by the object.
1840 */
1841 V8EXPORT bool BooleanValue() const;
1842
1843 static inline BooleanObject* Cast(v8::Value* obj);
1844
1845 private:
1846 V8EXPORT static void CheckCast(v8::Value* obj);
1847};
1848
1849
1850/**
1851 * A String object (ECMA-262, 4.3.18).
1852 */
1853class StringObject : public Object {
1854 public:
1855 V8EXPORT static Local<Value> New(Handle<String> value);
1856
1857 /**
1858 * Returns the String held by the object.
1859 */
1860 V8EXPORT Local<String> StringValue() const;
1861
1862 static inline StringObject* Cast(v8::Value* obj);
1863
1864 private:
1865 V8EXPORT static void CheckCast(v8::Value* obj);
1866};
1867
1868
1869/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001870 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1871 */
1872class RegExp : public Object {
1873 public:
1874 /**
1875 * Regular expression flag bits. They can be or'ed to enable a set
1876 * of flags.
1877 */
1878 enum Flags {
1879 kNone = 0,
1880 kGlobal = 1,
1881 kIgnoreCase = 2,
1882 kMultiline = 4
1883 };
1884
1885 /**
1886 * Creates a regular expression from the given pattern string and
1887 * the flags bit field. May throw a JavaScript exception as
1888 * described in ECMA-262, 15.10.4.1.
1889 *
1890 * For example,
1891 * RegExp::New(v8::String::New("foo"),
1892 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1893 * is equivalent to evaluating "/foo/gm".
1894 */
1895 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1896 Flags flags);
1897
1898 /**
1899 * Returns the value of the source property: a string representing
1900 * the regular expression.
1901 */
1902 V8EXPORT Local<String> GetSource() const;
1903
1904 /**
1905 * Returns the flags bit field.
1906 */
1907 V8EXPORT Flags GetFlags() const;
1908
1909 static inline RegExp* Cast(v8::Value* obj);
1910
1911 private:
1912 V8EXPORT static void CheckCast(v8::Value* obj);
1913};
1914
1915
1916/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001917 * A JavaScript value that wraps a C++ void*. This type of value is
1918 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001920 *
1921 * The Wrap function V8 will return the most optimal Value object wrapping the
1922 * C++ void*. The type of the value is not guaranteed to be an External object
1923 * and no assumptions about its type should be made. To access the wrapped
1924 * value Unwrap should be used, all other operations on that object will lead
1925 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001927class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001929 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001930 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001931
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001932 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001933 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001934 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001936 V8EXPORT External();
1937 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001938 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001939 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940};
1941
1942
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001943// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944
1945
1946/**
1947 * The superclass of object and function templates.
1948 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001949class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 public:
1951 /** Adds a property to each instance created by this template.*/
1952 void Set(Handle<String> name, Handle<Data> value,
1953 PropertyAttribute attributes = None);
1954 inline void Set(const char* name, Handle<Data> value);
1955 private:
1956 Template();
1957
1958 friend class ObjectTemplate;
1959 friend class FunctionTemplate;
1960};
1961
1962
1963/**
1964 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001965 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966 * including the receiver, the number and values of arguments, and
1967 * the holder of the function.
1968 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001969class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 public:
1971 inline int Length() const;
1972 inline Local<Value> operator[](int i) const;
1973 inline Local<Function> Callee() const;
1974 inline Local<Object> This() const;
1975 inline Local<Object> Holder() const;
1976 inline bool IsConstructCall() const;
1977 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001978 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00001979
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001981 static const int kIsolateIndex = 0;
1982 static const int kDataIndex = -1;
1983 static const int kCalleeIndex = -2;
1984 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001985
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001987 inline Arguments(internal::Object** implicit_args,
1988 internal::Object** values,
1989 int length,
1990 bool is_construct_call);
1991 internal::Object** implicit_args_;
1992 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001994 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995};
1996
1997
1998/**
1999 * The information passed to an accessor callback about the context
2000 * of the property access.
2001 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002002class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002004 inline AccessorInfo(internal::Object** args)
2005 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002006 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 inline Local<Value> Data() const;
2008 inline Local<Object> This() const;
2009 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002010
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002012 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013};
2014
2015
2016typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2017
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 * NamedProperty[Getter|Setter] are used as interceptors on object.
2020 * See ObjectTemplate::SetNamedPropertyHandler.
2021 */
2022typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2023 const AccessorInfo& info);
2024
2025
2026/**
2027 * Returns the value if the setter intercepts the request.
2028 * Otherwise, returns an empty handle.
2029 */
2030typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2031 Local<Value> value,
2032 const AccessorInfo& info);
2033
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034/**
2035 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002036 * The result is an integer encoding property attributes (like v8::None,
2037 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002039typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2040 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041
2042
2043/**
2044 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002045 * The return value is true if the property could be deleted and false
2046 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 */
2048typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2049 const AccessorInfo& info);
2050
2051/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002052 * Returns an array containing the names of the properties the named
2053 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 */
2055typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2056
v8.team.kasperl727e9952008-09-02 14:56:44 +00002057
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002059 * Returns the value of the property if the getter intercepts the
2060 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 */
2062typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2063 const AccessorInfo& info);
2064
2065
2066/**
2067 * Returns the value if the setter intercepts the request.
2068 * Otherwise, returns an empty handle.
2069 */
2070typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2071 Local<Value> value,
2072 const AccessorInfo& info);
2073
2074
2075/**
2076 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002077 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002079typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2080 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081
2082/**
2083 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002084 * The return value is true if the property could be deleted and false
2085 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 */
2087typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2088 const AccessorInfo& info);
2089
v8.team.kasperl727e9952008-09-02 14:56:44 +00002090/**
2091 * Returns an array containing the indices of the properties the
2092 * indexed property getter intercepts.
2093 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2095
2096
2097/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002098 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099 */
2100enum AccessType {
2101 ACCESS_GET,
2102 ACCESS_SET,
2103 ACCESS_HAS,
2104 ACCESS_DELETE,
2105 ACCESS_KEYS
2106};
2107
v8.team.kasperl727e9952008-09-02 14:56:44 +00002108
2109/**
2110 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002111 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002112 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002113typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114 Local<Value> key,
2115 AccessType type,
2116 Local<Value> data);
2117
v8.team.kasperl727e9952008-09-02 14:56:44 +00002118
2119/**
2120 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002121 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002122 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002123typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124 uint32_t index,
2125 AccessType type,
2126 Local<Value> data);
2127
2128
2129/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002130 * A FunctionTemplate is used to create functions at runtime. There
2131 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002132 * context. The lifetime of the created function is equal to the
2133 * lifetime of the context. So in case the embedder needs to create
2134 * temporary functions that can be collected using Scripts is
2135 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 *
2137 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002138 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002140 * A FunctionTemplate has a corresponding instance template which is
2141 * used to create object instances when the function is used as a
2142 * constructor. Properties added to the instance template are added to
2143 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144 *
2145 * A FunctionTemplate can have a prototype template. The prototype template
2146 * is used to create the prototype object of the function.
2147 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002148 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002150 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2152 * t->Set("func_property", v8::Number::New(1));
2153 *
2154 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2155 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2156 * proto_t->Set("proto_const", v8::Number::New(2));
2157 *
2158 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2159 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2160 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2161 * instance_t->Set("instance_property", Number::New(3));
2162 *
2163 * v8::Local<v8::Function> function = t->GetFunction();
2164 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002165 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 *
2167 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002168 * and "instance" for the instance object created above. The function
2169 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002171 * \code
2172 * func_property in function == true;
2173 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002175 * function.prototype.proto_method() invokes 'InvokeCallback'
2176 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002178 * instance instanceof function == true;
2179 * instance.instance_accessor calls 'InstanceAccessorCallback'
2180 * instance.instance_property == 3;
2181 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002183 * A FunctionTemplate can inherit from another one by calling the
2184 * FunctionTemplate::Inherit method. The following graph illustrates
2185 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002187 * \code
2188 * FunctionTemplate Parent -> Parent() . prototype -> { }
2189 * ^ ^
2190 * | Inherit(Parent) | .__proto__
2191 * | |
2192 * FunctionTemplate Child -> Child() . prototype -> { }
2193 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002195 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2196 * object of the Child() function has __proto__ pointing to the
2197 * Parent() function's prototype object. An instance of the Child
2198 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002200 * Let Parent be the FunctionTemplate initialized in the previous
2201 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002203 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 * Local<FunctionTemplate> parent = t;
2205 * Local<FunctionTemplate> child = FunctionTemplate::New();
2206 * child->Inherit(parent);
2207 *
2208 * Local<Function> child_function = child->GetFunction();
2209 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002210 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002212 * The Child function and Child instance will have the following
2213 * properties:
2214 *
2215 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002217 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002219 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002221class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 public:
2223 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002224 static Local<FunctionTemplate> New(
2225 InvocationCallback callback = 0,
2226 Handle<Value> data = Handle<Value>(),
2227 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 /** Returns the unique function instance in the current execution context.*/
2229 Local<Function> GetFunction();
2230
v8.team.kasperl727e9952008-09-02 14:56:44 +00002231 /**
2232 * Set the call-handler callback for a FunctionTemplate. This
2233 * callback is called whenever the function created from this
2234 * FunctionTemplate is called.
2235 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 void SetCallHandler(InvocationCallback callback,
2237 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238
v8.team.kasperl727e9952008-09-02 14:56:44 +00002239 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240 Local<ObjectTemplate> InstanceTemplate();
2241
2242 /** Causes the function template to inherit from a parent function template.*/
2243 void Inherit(Handle<FunctionTemplate> parent);
2244
2245 /**
2246 * A PrototypeTemplate is the template used to create the prototype object
2247 * of the function created by this template.
2248 */
2249 Local<ObjectTemplate> PrototypeTemplate();
2250
v8.team.kasperl727e9952008-09-02 14:56:44 +00002251
2252 /**
2253 * Set the class name of the FunctionTemplate. This is used for
2254 * printing objects created with the function created from the
2255 * FunctionTemplate as its constructor.
2256 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257 void SetClassName(Handle<String> name);
2258
2259 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002260 * Determines whether the __proto__ accessor ignores instances of
2261 * the function template. If instances of the function template are
2262 * ignored, __proto__ skips all instances and instead returns the
2263 * next object in the prototype chain.
2264 *
2265 * Call with a value of true to make the __proto__ accessor ignore
2266 * instances of the function template. Call with a value of false
2267 * to make the __proto__ accessor not ignore instances of the
2268 * function template. By default, instances of a function template
2269 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 */
2271 void SetHiddenPrototype(bool value);
2272
2273 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002274 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2275 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002276 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002277 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002278
2279 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002280 * Returns true if the given object is an instance of this function
2281 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282 */
2283 bool HasInstance(Handle<Value> object);
2284
2285 private:
2286 FunctionTemplate();
2287 void AddInstancePropertyAccessor(Handle<String> name,
2288 AccessorGetter getter,
2289 AccessorSetter setter,
2290 Handle<Value> data,
2291 AccessControl settings,
2292 PropertyAttribute attributes);
2293 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2294 NamedPropertySetter setter,
2295 NamedPropertyQuery query,
2296 NamedPropertyDeleter remover,
2297 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002298 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2300 IndexedPropertySetter setter,
2301 IndexedPropertyQuery query,
2302 IndexedPropertyDeleter remover,
2303 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002304 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2306 Handle<Value> data);
2307
2308 friend class Context;
2309 friend class ObjectTemplate;
2310};
2311
2312
2313/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002314 * An ObjectTemplate is used to create objects at runtime.
2315 *
2316 * Properties added to an ObjectTemplate are added to each object
2317 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002319class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002321 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002323
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324 /** Creates a new instance of this template.*/
2325 Local<Object> NewInstance();
2326
2327 /**
2328 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002329 *
2330 * Whenever the property with the given name is accessed on objects
2331 * created from this ObjectTemplate the getter and setter callbacks
2332 * are called instead of getting and setting the property directly
2333 * on the JavaScript object.
2334 *
2335 * \param name The name of the property for which an accessor is added.
2336 * \param getter The callback to invoke when getting the property.
2337 * \param setter The callback to invoke when setting the property.
2338 * \param data A piece of data that will be passed to the getter and setter
2339 * callbacks whenever they are invoked.
2340 * \param settings Access control settings for the accessor. This is a bit
2341 * field consisting of one of more of
2342 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2343 * The default is to not allow cross-context access.
2344 * ALL_CAN_READ means that all cross-context reads are allowed.
2345 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2346 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2347 * cross-context access.
2348 * \param attribute The attributes of the property for which an accessor
2349 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 */
2351 void SetAccessor(Handle<String> name,
2352 AccessorGetter getter,
2353 AccessorSetter setter = 0,
2354 Handle<Value> data = Handle<Value>(),
2355 AccessControl settings = DEFAULT,
2356 PropertyAttribute attribute = None);
2357
2358 /**
2359 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002360 *
2361 * Whenever a named property is accessed on objects created from
2362 * this object template, the provided callback is invoked instead of
2363 * accessing the property directly on the JavaScript object.
2364 *
2365 * \param getter The callback to invoke when getting a property.
2366 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002367 * \param query The callback to invoke to check if a property is present,
2368 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002369 * \param deleter The callback to invoke when deleting a property.
2370 * \param enumerator The callback to invoke to enumerate all the named
2371 * properties of an object.
2372 * \param data A piece of data that will be passed to the callbacks
2373 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 */
2375 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2376 NamedPropertySetter setter = 0,
2377 NamedPropertyQuery query = 0,
2378 NamedPropertyDeleter deleter = 0,
2379 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002380 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381
2382 /**
2383 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002384 *
2385 * Whenever an indexed property is accessed on objects created from
2386 * this object template, the provided callback is invoked instead of
2387 * accessing the property directly on the JavaScript object.
2388 *
2389 * \param getter The callback to invoke when getting a property.
2390 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002391 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002392 * \param deleter The callback to invoke when deleting a property.
2393 * \param enumerator The callback to invoke to enumerate all the indexed
2394 * properties of an object.
2395 * \param data A piece of data that will be passed to the callbacks
2396 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 */
2398 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2399 IndexedPropertySetter setter = 0,
2400 IndexedPropertyQuery query = 0,
2401 IndexedPropertyDeleter deleter = 0,
2402 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002403 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002404
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405 /**
2406 * Sets the callback to be used when calling instances created from
2407 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002408 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002409 * function.
2410 */
2411 void SetCallAsFunctionHandler(InvocationCallback callback,
2412 Handle<Value> data = Handle<Value>());
2413
v8.team.kasperl727e9952008-09-02 14:56:44 +00002414 /**
2415 * Mark object instances of the template as undetectable.
2416 *
2417 * In many ways, undetectable objects behave as though they are not
2418 * there. They behave like 'undefined' in conditionals and when
2419 * printed. However, properties can be accessed and called as on
2420 * normal objects.
2421 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 void MarkAsUndetectable();
2423
v8.team.kasperl727e9952008-09-02 14:56:44 +00002424 /**
2425 * Sets access check callbacks on the object template.
2426 *
2427 * When accessing properties on instances of this object template,
2428 * the access check callback will be called to determine whether or
2429 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002430 * The last parameter specifies whether access checks are turned
2431 * on by default on instances. If access checks are off by default,
2432 * they can be turned on on individual instances by calling
2433 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002434 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002435 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2436 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002437 Handle<Value> data = Handle<Value>(),
2438 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439
kasper.lund212ac232008-07-16 07:07:30 +00002440 /**
2441 * Gets the number of internal fields for objects generated from
2442 * this template.
2443 */
2444 int InternalFieldCount();
2445
2446 /**
2447 * Sets the number of internal fields for objects generated from
2448 * this template.
2449 */
2450 void SetInternalFieldCount(int value);
2451
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 private:
2453 ObjectTemplate();
2454 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2455 friend class FunctionTemplate;
2456};
2457
2458
2459/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002460 * A Signature specifies which receivers and arguments a function can
2461 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002463class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464 public:
2465 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2466 Handle<FunctionTemplate>(),
2467 int argc = 0,
2468 Handle<FunctionTemplate> argv[] = 0);
2469 private:
2470 Signature();
2471};
2472
2473
2474/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002475 * A utility for determining the type of objects based on the template
2476 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002478class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479 public:
2480 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2481 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2482 int match(Handle<Value> value);
2483 private:
2484 TypeSwitch();
2485};
2486
2487
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002488// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002490class V8EXPORT ExternalAsciiStringResourceImpl
2491 : public String::ExternalAsciiStringResource {
2492 public:
2493 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2494 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2495 : data_(data), length_(length) {}
2496 const char* data() const { return data_; }
2497 size_t length() const { return length_; }
2498
2499 private:
2500 const char* data_;
2501 size_t length_;
2502};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503
2504/**
2505 * Ignore
2506 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002507class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002509 // Note that the strings passed into this constructor must live as long
2510 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511 Extension(const char* name,
2512 const char* source = 0,
2513 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002514 const char** deps = 0,
2515 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 virtual ~Extension() { }
2517 virtual v8::Handle<v8::FunctionTemplate>
2518 GetNativeFunction(v8::Handle<v8::String> name) {
2519 return v8::Handle<v8::FunctionTemplate>();
2520 }
2521
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002522 const char* name() const { return name_; }
2523 size_t source_length() const { return source_length_; }
2524 const String::ExternalAsciiStringResource* source() const {
2525 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002526 int dependency_count() { return dep_count_; }
2527 const char** dependencies() { return deps_; }
2528 void set_auto_enable(bool value) { auto_enable_ = value; }
2529 bool auto_enable() { return auto_enable_; }
2530
2531 private:
2532 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002533 size_t source_length_; // expected to initialize before source_
2534 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 int dep_count_;
2536 const char** deps_;
2537 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002538
2539 // Disallow copying and assigning.
2540 Extension(const Extension&);
2541 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002542};
2543
2544
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002545void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546
2547
2548/**
2549 * Ignore
2550 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002551class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552 public:
2553 inline DeclareExtension(Extension* extension) {
2554 RegisterExtension(extension);
2555 }
2556};
2557
2558
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002559// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002560
2561
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002562Handle<Primitive> V8EXPORT Undefined();
2563Handle<Primitive> V8EXPORT Null();
2564Handle<Boolean> V8EXPORT True();
2565Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002566
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002567inline Handle<Primitive> Undefined(Isolate* isolate);
2568inline Handle<Primitive> Null(Isolate* isolate);
2569inline Handle<Boolean> True(Isolate* isolate);
2570inline Handle<Boolean> False(Isolate* isolate);
2571
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572
2573/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002574 * A set of constraints that specifies the limits of the runtime's memory use.
2575 * You must set the heap size before initializing the VM - the size cannot be
2576 * adjusted after the VM is initialized.
2577 *
2578 * If you are using threads then you should hold the V8::Locker lock while
2579 * setting the stack limit and you must set a non-default stack limit separately
2580 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002582class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583 public:
2584 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002585 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002586 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002587 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002589 int max_executable_size() { return max_executable_size_; }
2590 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002591 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002592 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2594 private:
2595 int max_young_space_size_;
2596 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002597 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002598 uint32_t* stack_limit_;
2599};
2600
2601
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002602bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603
2604
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002605// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002606
2607
2608typedef void (*FatalErrorCallback)(const char* location, const char* message);
2609
2610
2611typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2612
2613
2614/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002615 * Schedules an exception to be thrown when returning to JavaScript. When an
2616 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002618 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002620Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621
2622/**
2623 * Create new error objects by calling the corresponding error object
2624 * constructor with the message.
2625 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002626class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 public:
2628 static Local<Value> RangeError(Handle<String> message);
2629 static Local<Value> ReferenceError(Handle<String> message);
2630 static Local<Value> SyntaxError(Handle<String> message);
2631 static Local<Value> TypeError(Handle<String> message);
2632 static Local<Value> Error(Handle<String> message);
2633};
2634
2635
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002636// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002638typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002640typedef void* (*CreateHistogramCallback)(const char* name,
2641 int min,
2642 int max,
2643 size_t buckets);
2644
2645typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2646
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002647// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002648 enum ObjectSpace {
2649 kObjectSpaceNewSpace = 1 << 0,
2650 kObjectSpaceOldPointerSpace = 1 << 1,
2651 kObjectSpaceOldDataSpace = 1 << 2,
2652 kObjectSpaceCodeSpace = 1 << 3,
2653 kObjectSpaceMapSpace = 1 << 4,
2654 kObjectSpaceLoSpace = 1 << 5,
2655
2656 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2657 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2658 kObjectSpaceLoSpace
2659 };
2660
2661 enum AllocationAction {
2662 kAllocationActionAllocate = 1 << 0,
2663 kAllocationActionFree = 1 << 1,
2664 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2665 };
2666
2667typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2668 AllocationAction action,
2669 int size);
2670
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002671// --- Leave Script Callback ---
2672typedef void (*CallCompletedCallback)();
2673
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002674// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2676 AccessType type,
2677 Local<Value> data);
2678
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002679// --- AllowCodeGenerationFromStrings callbacks ---
2680
2681/**
2682 * Callback to check if code generation from strings is allowed. See
2683 * Context::AllowCodeGenerationFromStrings.
2684 */
2685typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2686
2687// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688
2689/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002690 * Applications can register callback functions which will be called
2691 * before and after a garbage collection. Allocations are not
2692 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002693 * objects (set or delete properties for example) since it is possible
2694 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002696enum GCType {
2697 kGCTypeScavenge = 1 << 0,
2698 kGCTypeMarkSweepCompact = 1 << 1,
2699 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2700};
2701
2702enum GCCallbackFlags {
2703 kNoGCCallbackFlags = 0,
2704 kGCCallbackFlagCompacted = 1 << 0
2705};
2706
2707typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2708typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2709
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710typedef void (*GCCallback)();
2711
2712
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002714 * Collection of V8 heap information.
2715 *
2716 * Instances of this class can be passed to v8::V8::HeapStatistics to
2717 * get heap statistics from V8.
2718 */
2719class V8EXPORT HeapStatistics {
2720 public:
2721 HeapStatistics();
2722 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002723 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002724 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002725 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002726
2727 private:
2728 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002729 void set_total_heap_size_executable(size_t size) {
2730 total_heap_size_executable_ = size;
2731 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002732 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002733 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002734
2735 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002736 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002737 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002738 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002739
2740 friend class V8;
2741};
2742
2743
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002744class RetainedObjectInfo;
2745
ager@chromium.org3811b432009-10-28 14:53:37 +00002746/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002747 * Isolate represents an isolated instance of the V8 engine. V8
2748 * isolates have completely separate states. Objects from one isolate
2749 * must not be used in other isolates. When V8 is initialized a
2750 * default isolate is implicitly created and entered. The embedder
2751 * can create additional isolates and use them in parallel in multiple
2752 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002753 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002754 */
2755class V8EXPORT Isolate {
2756 public:
2757 /**
2758 * Stack-allocated class which sets the isolate for all operations
2759 * executed within a local scope.
2760 */
2761 class V8EXPORT Scope {
2762 public:
2763 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2764 isolate->Enter();
2765 }
2766
2767 ~Scope() { isolate_->Exit(); }
2768
2769 private:
2770 Isolate* const isolate_;
2771
2772 // Prevent copying of Scope objects.
2773 Scope(const Scope&);
2774 Scope& operator=(const Scope&);
2775 };
2776
2777 /**
2778 * Creates a new isolate. Does not change the currently entered
2779 * isolate.
2780 *
2781 * When an isolate is no longer used its resources should be freed
2782 * by calling Dispose(). Using the delete operator is not allowed.
2783 */
2784 static Isolate* New();
2785
2786 /**
2787 * Returns the entered isolate for the current thread or NULL in
2788 * case there is no current isolate.
2789 */
2790 static Isolate* GetCurrent();
2791
2792 /**
2793 * Methods below this point require holding a lock (using Locker) in
2794 * a multi-threaded environment.
2795 */
2796
2797 /**
2798 * Sets this isolate as the entered one for the current thread.
2799 * Saves the previously entered one (if any), so that it can be
2800 * restored when exiting. Re-entering an isolate is allowed.
2801 */
2802 void Enter();
2803
2804 /**
2805 * Exits this isolate by restoring the previously entered one in the
2806 * current thread. The isolate may still stay the same, if it was
2807 * entered more than once.
2808 *
2809 * Requires: this == Isolate::GetCurrent().
2810 */
2811 void Exit();
2812
2813 /**
2814 * Disposes the isolate. The isolate must not be entered by any
2815 * thread to be disposable.
2816 */
2817 void Dispose();
2818
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002819 /**
2820 * Associate embedder-specific data with the isolate
2821 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002822 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002823
2824 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002825 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002826 * Returns NULL if SetData has never been called.
2827 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002828 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002829
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002830 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002831 Isolate();
2832 Isolate(const Isolate&);
2833 ~Isolate();
2834 Isolate& operator=(const Isolate&);
2835 void* operator new(size_t size);
2836 void operator delete(void*, size_t);
2837};
2838
2839
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002840class StartupData {
2841 public:
2842 enum CompressionAlgorithm {
2843 kUncompressed,
2844 kBZip2
2845 };
2846
2847 const char* data;
2848 int compressed_size;
2849 int raw_size;
2850};
2851
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002852
2853/**
2854 * A helper class for driving V8 startup data decompression. It is based on
2855 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2856 * for an embedder to use this class, instead, API functions can be used
2857 * directly.
2858 *
2859 * For an example of the class usage, see the "shell.cc" sample application.
2860 */
2861class V8EXPORT StartupDataDecompressor { // NOLINT
2862 public:
2863 StartupDataDecompressor();
2864 virtual ~StartupDataDecompressor();
2865 int Decompress();
2866
2867 protected:
2868 virtual int DecompressData(char* raw_data,
2869 int* raw_data_size,
2870 const char* compressed_data,
2871 int compressed_data_size) = 0;
2872
2873 private:
2874 char** raw_data;
2875};
2876
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002877
2878/**
2879 * EntropySource is used as a callback function when v8 needs a source
2880 * of entropy.
2881 */
2882typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2883
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002884
2885/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002886 * ReturnAddressLocationResolver is used as a callback function when v8 is
2887 * resolving the location of a return address on the stack. Profilers that
2888 * change the return address on the stack can use this to resolve the stack
2889 * location to whereever the profiler stashed the original return address.
2890 * When invoked, return_addr_location will point to a location on stack where
2891 * a machine return address resides, this function should return either the
2892 * same pointer, or a pointer to the profiler's copy of the original return
2893 * address.
2894 */
2895typedef uintptr_t (*ReturnAddressLocationResolver)(
2896 uintptr_t return_addr_location);
2897
2898
2899/**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002900 * Interface for iterating though all external resources in the heap.
2901 */
2902class V8EXPORT ExternalResourceVisitor { // NOLINT
2903 public:
2904 virtual ~ExternalResourceVisitor() {}
2905 virtual void VisitExternalString(Handle<String> string) {}
2906};
2907
2908
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002909/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 * Container class for static utility functions.
2911 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002912class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002913 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002914 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915 static void SetFatalErrorHandler(FatalErrorCallback that);
2916
v8.team.kasperl727e9952008-09-02 14:56:44 +00002917 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002918 * Set the callback to invoke to check if code generation from
2919 * strings should be allowed.
2920 */
2921 static void SetAllowCodeGenerationFromStringsCallback(
2922 AllowCodeGenerationFromStringsCallback that);
2923
2924 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002925 * Ignore out-of-memory exceptions.
2926 *
2927 * V8 running out of memory is treated as a fatal error by default.
2928 * This means that the fatal error handler is called and that V8 is
2929 * terminated.
2930 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002931 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002932 * out-of-memory situation as a fatal error. This way, the contexts
2933 * that did not cause the out of memory problem might be able to
2934 * continue execution.
2935 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936 static void IgnoreOutOfMemoryException();
2937
v8.team.kasperl727e9952008-09-02 14:56:44 +00002938 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002939 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002940 * fatal errors such as out-of-memory situations.
2941 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002942 static bool IsDead();
2943
2944 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002945 * The following 4 functions are to be used when V8 is built with
2946 * the 'compress_startup_data' flag enabled. In this case, the
2947 * embedder must decompress startup data prior to initializing V8.
2948 *
2949 * This is how interaction with V8 should look like:
2950 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2951 * v8::StartupData* compressed_data =
2952 * new v8::StartupData[compressed_data_count];
2953 * v8::V8::GetCompressedStartupData(compressed_data);
2954 * ... decompress data (compressed_data can be updated in-place) ...
2955 * v8::V8::SetDecompressedStartupData(compressed_data);
2956 * ... now V8 can be initialized
2957 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002958 *
2959 * A helper class StartupDataDecompressor is provided. It implements
2960 * the protocol of the interaction described above, and can be used in
2961 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002962 */
2963 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2964 static int GetCompressedStartupDataCount();
2965 static void GetCompressedStartupData(StartupData* compressed_data);
2966 static void SetDecompressedStartupData(StartupData* decompressed_data);
2967
2968 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002969 * Adds a message listener.
2970 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002971 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00002972 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 */
2974 static bool AddMessageListener(MessageCallback that,
2975 Handle<Value> data = Handle<Value>());
2976
2977 /**
2978 * Remove all message listeners from the specified callback function.
2979 */
2980 static void RemoveMessageListeners(MessageCallback that);
2981
2982 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002983 * Tells V8 to capture current stack trace when uncaught exception occurs
2984 * and report it to the message listeners. The option is off by default.
2985 */
2986 static void SetCaptureStackTraceForUncaughtExceptions(
2987 bool capture,
2988 int frame_limit = 10,
2989 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2990
2991 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002992 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993 */
2994 static void SetFlagsFromString(const char* str, int length);
2995
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002996 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002997 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002998 */
2999 static void SetFlagsFromCommandLine(int* argc,
3000 char** argv,
3001 bool remove_flags);
3002
kasper.lund7276f142008-07-30 08:49:36 +00003003 /** Get the version string. */
3004 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003005
3006 /**
3007 * Enables the host application to provide a mechanism for recording
3008 * statistics counters.
3009 */
3010 static void SetCounterFunction(CounterLookupCallback);
3011
3012 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003013 * Enables the host application to provide a mechanism for recording
3014 * histograms. The CreateHistogram function returns a
3015 * histogram which will later be passed to the AddHistogramSample
3016 * function.
3017 */
3018 static void SetCreateHistogramFunction(CreateHistogramCallback);
3019 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3020
3021 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003022 * Enables the computation of a sliding window of states. The sliding
3023 * window information is recorded in statistics counters.
3024 */
3025 static void EnableSlidingStateWindow();
3026
3027 /** Callback function for reporting failed access checks.*/
3028 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3029
3030 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003031 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003032 * garbage collection. Allocations are not allowed in the
3033 * callback function, you therefore cannot manipulate objects (set
3034 * or delete properties for example) since it is possible such
3035 * operations will result in the allocation of objects. It is possible
3036 * to specify the GCType filter for your callback. But it is not possible to
3037 * register the same callback function two times with different
3038 * GCType filters.
3039 */
3040 static void AddGCPrologueCallback(
3041 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3042
3043 /**
3044 * This function removes callback which was installed by
3045 * AddGCPrologueCallback function.
3046 */
3047 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3048
3049 /**
3050 * The function is deprecated. Please use AddGCPrologueCallback instead.
3051 * Enables the host application to receive a notification before a
3052 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003053 * callback function, you therefore cannot manipulate objects (set
3054 * or delete properties for example) since it is possible such
3055 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056 */
3057 static void SetGlobalGCPrologueCallback(GCCallback);
3058
3059 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003060 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003061 * garbage collection. Allocations are not allowed in the
3062 * callback function, you therefore cannot manipulate objects (set
3063 * or delete properties for example) since it is possible such
3064 * operations will result in the allocation of objects. It is possible
3065 * to specify the GCType filter for your callback. But it is not possible to
3066 * register the same callback function two times with different
3067 * GCType filters.
3068 */
3069 static void AddGCEpilogueCallback(
3070 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3071
3072 /**
3073 * This function removes callback which was installed by
3074 * AddGCEpilogueCallback function.
3075 */
3076 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3077
3078 /**
3079 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3080 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003081 * major garbage collection. Allocations are not allowed in the
3082 * callback function, you therefore cannot manipulate objects (set
3083 * or delete properties for example) since it is possible such
3084 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085 */
3086 static void SetGlobalGCEpilogueCallback(GCCallback);
3087
3088 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003089 * Enables the host application to provide a mechanism to be notified
3090 * and perform custom logging when V8 Allocates Executable Memory.
3091 */
3092 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3093 ObjectSpace space,
3094 AllocationAction action);
3095
3096 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003097 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003098 */
3099 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3100
3101 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003102 * Adds a callback to notify the host application when a script finished
3103 * running. If a script re-enters the runtime during executing, the
3104 * CallCompletedCallback is only invoked when the outer-most script
3105 * execution ends. Executing scripts inside the callback do not trigger
3106 * further callbacks.
3107 */
3108 static void AddCallCompletedCallback(CallCompletedCallback callback);
3109
3110 /**
3111 * Removes callback that was installed by AddCallCompletedCallback.
3112 */
3113 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3114
3115 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003116 * Allows the host application to group objects together. If one
3117 * object in the group is alive, all objects in the group are alive.
3118 * After each garbage collection, object groups are removed. It is
3119 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003120 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00003121 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003122 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003124 static void AddObjectGroup(Persistent<Value>* objects,
3125 size_t length,
3126 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003127
3128 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003129 * Allows the host application to declare implicit references between
3130 * the objects: if |parent| is alive, all |children| are alive too.
3131 * After each garbage collection, all implicit references
3132 * are removed. It is intended to be used in the before-garbage-collection
3133 * callback function.
3134 */
3135 static void AddImplicitReferences(Persistent<Object> parent,
3136 Persistent<Value>* children,
3137 size_t length);
3138
3139 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003140 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003141 * initialize from scratch. This function is called implicitly if
3142 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003143 */
3144 static bool Initialize();
3145
kasper.lund7276f142008-07-30 08:49:36 +00003146 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003147 * Allows the host application to provide a callback which can be used
3148 * as a source of entropy for random number generators.
3149 */
3150 static void SetEntropySource(EntropySource source);
3151
3152 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003153 * Allows the host application to provide a callback that allows v8 to
3154 * cooperate with a profiler that rewrites return addresses on stack.
3155 */
3156 static void SetReturnAddressLocationResolver(
3157 ReturnAddressLocationResolver return_address_resolver);
3158
3159 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003160 * Adjusts the amount of registered external memory. Used to give
3161 * V8 an indication of the amount of externally allocated memory
3162 * that is kept alive by JavaScript objects. V8 uses this to decide
3163 * when to perform global garbage collections. Registering
3164 * externally allocated memory will trigger global garbage
3165 * collections more often than otherwise in an attempt to garbage
3166 * collect the JavaScript objects keeping the externally allocated
3167 * memory alive.
3168 *
3169 * \param change_in_bytes the change in externally allocated memory
3170 * that is kept alive by JavaScript objects.
3171 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003172 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003173 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3174 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003175
iposva@chromium.org245aa852009-02-10 00:49:54 +00003176 /**
3177 * Suspends recording of tick samples in the profiler.
3178 * When the V8 profiling mode is enabled (usually via command line
3179 * switches) this function suspends recording of tick samples.
3180 * Profiling ticks are discarded until ResumeProfiler() is called.
3181 *
3182 * See also the --prof and --prof_auto command line switches to
3183 * enable V8 profiling.
3184 */
3185 static void PauseProfiler();
3186
3187 /**
3188 * Resumes recording of tick samples in the profiler.
3189 * See also PauseProfiler().
3190 */
3191 static void ResumeProfiler();
3192
ager@chromium.org41826e72009-03-30 13:30:57 +00003193 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003194 * Return whether profiler is currently paused.
3195 */
3196 static bool IsProfilerPaused();
3197
3198 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003199 * Retrieve the V8 thread id of the calling thread.
3200 *
3201 * The thread id for a thread should only be retrieved after the V8
3202 * lock has been acquired with a Locker object with that thread.
3203 */
3204 static int GetCurrentThreadId();
3205
3206 /**
3207 * Forcefully terminate execution of a JavaScript thread. This can
3208 * be used to terminate long-running scripts.
3209 *
3210 * TerminateExecution should only be called when then V8 lock has
3211 * been acquired with a Locker object. Therefore, in order to be
3212 * able to terminate long-running threads, preemption must be
3213 * enabled to allow the user of TerminateExecution to acquire the
3214 * lock.
3215 *
3216 * The termination is achieved by throwing an exception that is
3217 * uncatchable by JavaScript exception handlers. Termination
3218 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003219 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003220 * exception handler that catches an exception should check if that
3221 * exception is a termination exception and immediately return if
3222 * that is the case. Returning immediately in that case will
3223 * continue the propagation of the termination exception if needed.
3224 *
3225 * The thread id passed to TerminateExecution must have been
3226 * obtained by calling GetCurrentThreadId on the thread in question.
3227 *
3228 * \param thread_id The thread id of the thread to terminate.
3229 */
3230 static void TerminateExecution(int thread_id);
3231
3232 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003233 * Forcefully terminate the current thread of JavaScript execution
3234 * in the given isolate. If no isolate is provided, the default
3235 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003236 *
3237 * This method can be used by any thread even if that thread has not
3238 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003239 *
3240 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003241 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003242 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003243
3244 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003245 * Is V8 terminating JavaScript execution.
3246 *
3247 * Returns true if JavaScript execution is currently terminating
3248 * because of a call to TerminateExecution. In that case there are
3249 * still JavaScript frames on the stack and the termination
3250 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003251 *
3252 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003253 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003254 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003255
3256 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003257 * Releases any resources used by v8 and stops any utility threads
3258 * that may be running. Note that disposing v8 is permanent, it
3259 * cannot be reinitialized.
3260 *
3261 * It should generally not be necessary to dispose v8 before exiting
3262 * a process, this should happen automatically. It is only necessary
3263 * to use if the process needs the resources taken up by v8.
3264 */
3265 static bool Dispose();
3266
ager@chromium.org3811b432009-10-28 14:53:37 +00003267 /**
3268 * Get statistics about the heap memory usage.
3269 */
3270 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003271
3272 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003273 * Iterates through all external resources referenced from current isolate
3274 * heap. This method is not expected to be used except for debugging purposes
3275 * and may be quite slow.
3276 */
3277 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3278
3279 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003280 * Optional notification that the embedder is idle.
3281 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003282 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003283 * Returns true if the embedder should stop calling IdleNotification
3284 * until real work has been done. This indicates that V8 has done
3285 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003286 *
3287 * The hint argument specifies the amount of work to be done in the function
3288 * on scale from 1 to 1000. There is no guarantee that the actual work will
3289 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003290 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003291 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003292
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003293 /**
3294 * Optional notification that the system is running low on memory.
3295 * V8 uses these notifications to attempt to free memory.
3296 */
3297 static void LowMemoryNotification();
3298
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003299 /**
3300 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003301 * these notifications to guide the GC heuristic. Returns the number
3302 * of context disposals - including this one - since the last time
3303 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003304 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003305 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003306
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307 private:
3308 V8();
3309
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003310 static internal::Object** GlobalizeReference(internal::Object** handle);
3311 static void DisposeGlobal(internal::Object** global_handle);
3312 static void MakeWeak(internal::Object** global_handle,
3313 void* data,
3314 WeakReferenceCallback);
3315 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003316 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003317 static bool IsGlobalNearDeath(internal::Object** global_handle);
3318 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003319 static void SetWrapperClassId(internal::Object** global_handle,
3320 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003321
3322 template <class T> friend class Handle;
3323 template <class T> friend class Local;
3324 template <class T> friend class Persistent;
3325 friend class Context;
3326};
3327
3328
3329/**
3330 * An external exception handler.
3331 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003332class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003333 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003334 /**
3335 * Creates a new try/catch block and registers it with v8.
3336 */
3337 TryCatch();
3338
3339 /**
3340 * Unregisters and deletes this try/catch block.
3341 */
3342 ~TryCatch();
3343
3344 /**
3345 * Returns true if an exception has been caught by this try/catch block.
3346 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003347 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003348
3349 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003350 * For certain types of exceptions, it makes no sense to continue
3351 * execution.
3352 *
3353 * Currently, the only type of exception that can be caught by a
3354 * TryCatch handler and for which it does not make sense to continue
3355 * is termination exception. Such exceptions are thrown when the
3356 * TerminateExecution methods are called to terminate a long-running
3357 * script.
3358 *
3359 * If CanContinue returns false, the correct action is to perform
3360 * any C++ cleanup needed and then return.
3361 */
3362 bool CanContinue() const;
3363
3364 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003365 * Throws the exception caught by this TryCatch in a way that avoids
3366 * it being caught again by this same TryCatch. As with ThrowException
3367 * it is illegal to execute any JavaScript operations after calling
3368 * ReThrow; the caller must return immediately to where the exception
3369 * is caught.
3370 */
3371 Handle<Value> ReThrow();
3372
3373 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003374 * Returns the exception caught by this try/catch block. If no exception has
3375 * been caught an empty handle is returned.
3376 *
3377 * The returned handle is valid until this TryCatch block has been destroyed.
3378 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003379 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003380
3381 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003382 * Returns the .stack property of the thrown object. If no .stack
3383 * property is present an empty handle is returned.
3384 */
3385 Local<Value> StackTrace() const;
3386
3387 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003388 * Returns the message associated with this exception. If there is
3389 * no message associated an empty handle is returned.
3390 *
3391 * The returned handle is valid until this TryCatch block has been
3392 * destroyed.
3393 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003394 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003395
3396 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003397 * Clears any exceptions that may have been caught by this try/catch block.
3398 * After this method has been called, HasCaught() will return false.
3399 *
3400 * It is not necessary to clear a try/catch block before using it again; if
3401 * another exception is thrown the previously caught exception will just be
3402 * overwritten. However, it is often a good idea since it makes it easier
3403 * to determine which operation threw a given exception.
3404 */
3405 void Reset();
3406
v8.team.kasperl727e9952008-09-02 14:56:44 +00003407 /**
3408 * Set verbosity of the external exception handler.
3409 *
3410 * By default, exceptions that are caught by an external exception
3411 * handler are not reported. Call SetVerbose with true on an
3412 * external exception handler to have exceptions caught by the
3413 * handler reported as if they were not caught.
3414 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003415 void SetVerbose(bool value);
3416
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003417 /**
3418 * Set whether or not this TryCatch should capture a Message object
3419 * which holds source information about where the exception
3420 * occurred. True by default.
3421 */
3422 void SetCaptureMessage(bool value);
3423
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003424 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003425 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003426 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003427 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003428 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003429 bool is_verbose_ : 1;
3430 bool can_continue_ : 1;
3431 bool capture_message_ : 1;
3432 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003433
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003434 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003435};
3436
3437
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003438// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003439
3440
3441/**
3442 * Ignore
3443 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003444class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003445 public:
3446 ExtensionConfiguration(int name_count, const char* names[])
3447 : name_count_(name_count), names_(names) { }
3448 private:
3449 friend class ImplementationUtilities;
3450 int name_count_;
3451 const char** names_;
3452};
3453
3454
3455/**
3456 * A sandboxed execution context with its own set of built-in objects
3457 * and functions.
3458 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003459class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003460 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003461 /**
3462 * Returns the global proxy object or global object itself for
3463 * detached contexts.
3464 *
3465 * Global proxy object is a thin wrapper whose prototype points to
3466 * actual context's global object with the properties like Object, etc.
3467 * This is done that way for security reasons (for more details see
3468 * https://wiki.mozilla.org/Gecko:SplitWindow).
3469 *
3470 * Please note that changes to global proxy object prototype most probably
3471 * would break VM---v8 expects only global object as a prototype of
3472 * global proxy object.
3473 *
3474 * If DetachGlobal() has been invoked, Global() would return actual global
3475 * object until global is reattached with ReattachGlobal().
3476 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003477 Local<Object> Global();
3478
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003479 /**
3480 * Detaches the global object from its context before
3481 * the global object can be reused to create a new context.
3482 */
3483 void DetachGlobal();
3484
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003485 /**
3486 * Reattaches a global object to a context. This can be used to
3487 * restore the connection between a global object and a context
3488 * after DetachGlobal has been called.
3489 *
3490 * \param global_object The global object to reattach to the
3491 * context. For this to work, the global object must be the global
3492 * object that was associated with this context before a call to
3493 * DetachGlobal.
3494 */
3495 void ReattachGlobal(Handle<Object> global_object);
3496
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003497 /** Creates a new context.
3498 *
3499 * Returns a persistent handle to the newly allocated context. This
3500 * persistent handle has to be disposed when the context is no
3501 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003502 *
3503 * \param extensions An optional extension configuration containing
3504 * the extensions to be installed in the newly created context.
3505 *
3506 * \param global_template An optional object template from which the
3507 * global object for the newly created context will be created.
3508 *
3509 * \param global_object An optional global object to be reused for
3510 * the newly created context. This global object must have been
3511 * created by a previous call to Context::New with the same global
3512 * template. The state of the global object will be completely reset
3513 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003514 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003515 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003516 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003517 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3518 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003519
kasper.lund44510672008-07-25 07:37:58 +00003520 /** Returns the last entered context. */
3521 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522
kasper.lund44510672008-07-25 07:37:58 +00003523 /** Returns the context that is on the top of the stack. */
3524 static Local<Context> GetCurrent();
3525
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003526 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003527 * Returns the context of the calling JavaScript code. That is the
3528 * context of the top-most JavaScript frame. If there are no
3529 * JavaScript frames an empty handle is returned.
3530 */
3531 static Local<Context> GetCalling();
3532
3533 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003534 * Sets the security token for the context. To access an object in
3535 * another context, the security tokens must match.
3536 */
3537 void SetSecurityToken(Handle<Value> token);
3538
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003539 /** Restores the security token to the default value. */
3540 void UseDefaultSecurityToken();
3541
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003542 /** Returns the security token of this context.*/
3543 Handle<Value> GetSecurityToken();
3544
v8.team.kasperl727e9952008-09-02 14:56:44 +00003545 /**
3546 * Enter this context. After entering a context, all code compiled
3547 * and run is compiled and run in this context. If another context
3548 * is already entered, this old context is saved so it can be
3549 * restored when the new context is exited.
3550 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003552
3553 /**
3554 * Exit this context. Exiting the current context restores the
3555 * context that was in place when entering the current context.
3556 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003557 void Exit();
3558
v8.team.kasperl727e9952008-09-02 14:56:44 +00003559 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003560 bool HasOutOfMemoryException();
3561
v8.team.kasperl727e9952008-09-02 14:56:44 +00003562 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003563 static bool InContext();
3564
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003565 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003566 * Associate an additional data object with the context. This is mainly used
3567 * with the debugger to provide additional information on the context through
3568 * the debugger API.
3569 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003570 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003571 Local<Value> GetData();
3572
3573 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003574 * Control whether code generation from strings is allowed. Calling
3575 * this method with false will disable 'eval' and the 'Function'
3576 * constructor for code running in this context. If 'eval' or the
3577 * 'Function' constructor are used an exception will be thrown.
3578 *
3579 * If code generation from strings is not allowed the
3580 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3581 * set before blocking the call to 'eval' or the 'Function'
3582 * constructor. If that callback returns true, the call will be
3583 * allowed, otherwise an exception will be thrown. If no callback is
3584 * set an exception will be thrown.
3585 */
3586 void AllowCodeGenerationFromStrings(bool allow);
3587
3588 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003589 * Returns true if code generation from strings is allowed for the context.
3590 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3591 */
3592 bool IsCodeGenerationFromStringsAllowed();
3593
3594 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003595 * Stack-allocated class which sets the execution context for all
3596 * operations executed within a local scope.
3597 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003598 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003599 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003600 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003601 context_->Enter();
3602 }
3603 inline ~Scope() { context_->Exit(); }
3604 private:
3605 Handle<Context> context_;
3606 };
3607
3608 private:
3609 friend class Value;
3610 friend class Script;
3611 friend class Object;
3612 friend class Function;
3613};
3614
3615
3616/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003617 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003618 * is allowed to use any given V8 isolate. See Isolate class
3619 * comments. The definition of 'using V8 isolate' includes
3620 * accessing handles or holding onto object pointers obtained
3621 * from V8 handles while in the particular V8 isolate. It is up
3622 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003623 * constraint is not violated. In addition to any other synchronization
3624 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3625 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003626 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003627 * v8::Locker is a scoped lock object. While it's
3628 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003629 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3630 * locked by at most one thread at any time. In other words, the scope of a
3631 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003632 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003633 * Sample usage:
3634* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003635 * ...
3636 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003637 * v8::Locker locker(isolate);
3638 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003639 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003640 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003641 * ...
3642 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003643 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003644 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003645 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003646 * by destroying the v8::Locker object as above or by constructing a
3647 * v8::Unlocker object:
3648 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003649 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003650 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003651 * isolate->Exit();
3652 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003653 * ...
3654 * // Code not using V8 goes here while V8 can run in another thread.
3655 * ...
3656 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003657 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003658 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003659 *
3660 * The Unlocker object is intended for use in a long-running callback
3661 * from V8, where you want to release the V8 lock for other threads to
3662 * use.
3663 *
3664 * The v8::Locker is a recursive lock. That is, you can lock more than
3665 * once in a given thread. This can be useful if you have code that can
3666 * be called either from code that holds the lock or from code that does
3667 * not. The Unlocker is not recursive so you can not have several
3668 * Unlockers on the stack at once, and you can not use an Unlocker in a
3669 * thread that is not inside a Locker's scope.
3670 *
3671 * An unlocker will unlock several lockers if it has to and reinstate
3672 * the correct depth of locking on its destruction. eg.:
3673 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003674 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003675 * // V8 not locked.
3676 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003677 * v8::Locker locker(isolate);
3678 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003679 * // V8 locked.
3680 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003681 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003682 * // V8 still locked (2 levels).
3683 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003684 * isolate->Exit();
3685 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003686 * // V8 not locked.
3687 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003688 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003689 * // V8 locked again (2 levels).
3690 * }
3691 * // V8 still locked (1 level).
3692 * }
3693 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003694 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003695 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003696 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003697 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003698class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003699 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003700 /**
3701 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3702 */
3703 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003704 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003705 private:
3706 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707};
3708
3709
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003710class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003712 /**
3713 * Initialize Locker for a given Isolate. NULL means default isolate.
3714 */
3715 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003717
3718 /**
3719 * Start preemption.
3720 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003721 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003722 * that will switch between multiple threads that are in contention
3723 * for the V8 lock.
3724 */
3725 static void StartPreemption(int every_n_ms);
3726
3727 /**
3728 * Stop preemption.
3729 */
3730 static void StopPreemption();
3731
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003732 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003733 * Returns whether or not the locker for a given isolate, or default isolate
3734 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003735 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003736 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003737
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003738 /**
3739 * Returns whether v8::Locker is being used by this V8 instance.
3740 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003741 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003742
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003743 private:
3744 bool has_lock_;
3745 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003746 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003747
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003748 static bool active_;
3749
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003750 // Disallow copying and assigning.
3751 Locker(const Locker&);
3752 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003753};
3754
3755
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003756/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003757 * A struct for exporting HeapStats data from V8, using "push" model.
3758 */
3759struct HeapStatsUpdate;
3760
3761
3762/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003763 * An interface for exporting data from V8, using "push" model.
3764 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003765class V8EXPORT OutputStream { // NOLINT
3766 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003767 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003768 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003769 };
3770 enum WriteResult {
3771 kContinue = 0,
3772 kAbort = 1
3773 };
3774 virtual ~OutputStream() {}
3775 /** Notify about the end of stream. */
3776 virtual void EndOfStream() = 0;
3777 /** Get preferred output chunk size. Called only once. */
3778 virtual int GetChunkSize() { return 1024; }
3779 /** Get preferred output encoding. Called only once. */
3780 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3781 /**
3782 * Writes the next chunk of snapshot data into the stream. Writing
3783 * can be stopped by returning kAbort as function result. EndOfStream
3784 * will not be called in case writing was aborted.
3785 */
3786 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003787 /**
3788 * Writes the next chunk of heap stats data into the stream. Writing
3789 * can be stopped by returning kAbort as function result. EndOfStream
3790 * will not be called in case writing was aborted.
3791 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003792 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00003793 return kAbort;
3794 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003795};
3796
3797
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003798/**
3799 * An interface for reporting progress and controlling long-running
3800 * activities.
3801 */
3802class V8EXPORT ActivityControl { // NOLINT
3803 public:
3804 enum ControlOption {
3805 kContinue = 0,
3806 kAbort = 1
3807 };
3808 virtual ~ActivityControl() {}
3809 /**
3810 * Notify about current progress. The activity can be stopped by
3811 * returning kAbort as the callback result.
3812 */
3813 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3814};
3815
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003817// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003818
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003819
3820namespace internal {
3821
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003822const int kApiPointerSize = sizeof(void*); // NOLINT
3823const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003824
3825// Tag information for HeapObject.
3826const int kHeapObjectTag = 1;
3827const int kHeapObjectTagSize = 2;
3828const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3829
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003830// Tag information for Smi.
3831const int kSmiTag = 0;
3832const int kSmiTagSize = 1;
3833const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3834
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003835template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003836
3837// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003838template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003839 static const int kSmiShiftSize = 0;
3840 static const int kSmiValueSize = 31;
3841 static inline int SmiToInt(internal::Object* value) {
3842 int shift_bits = kSmiTagSize + kSmiShiftSize;
3843 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3844 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3845 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003846
3847 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3848 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003849 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003850 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003851};
3852
3853// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003854template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003855 static const int kSmiShiftSize = 31;
3856 static const int kSmiValueSize = 32;
3857 static inline int SmiToInt(internal::Object* value) {
3858 int shift_bits = kSmiTagSize + kSmiShiftSize;
3859 // Shift down and throw away top 32 bits.
3860 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3861 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003862
3863 // To maximize the range of pointers that can be encoded
3864 // in the available 32 bits, we require them to be 8 bytes aligned.
3865 // This gives 2 ^ (32 + 3) = 32G address space covered.
3866 // It might be not enough to cover stack allocated objects on some platforms.
3867 static const int kPointerAlignment = 3;
3868
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003869 static const uintptr_t kEncodablePointerMask =
3870 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003871
3872 static const int kPointerToSmiShift =
3873 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003874};
3875
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003876typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3877const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3878const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003879const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003880 PlatformSmiTagging::kEncodablePointerMask;
3881const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003882
3883/**
3884 * This class exports constants and functionality from within v8 that
3885 * is necessary to implement inline functions in the v8 api. Don't
3886 * depend on functions and constants defined here.
3887 */
3888class Internals {
3889 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003890 // These values match non-compiler-dependent values defined within
3891 // the implementation of v8.
3892 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003893 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003894 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003895
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003896 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003897 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003898 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003899 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003900 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003901
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003902 static const int kIsolateStateOffset = 0;
3903 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
3904 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
3905 static const int kUndefinedValueRootIndex = 5;
3906 static const int kNullValueRootIndex = 7;
3907 static const int kTrueValueRootIndex = 8;
3908 static const int kFalseValueRootIndex = 9;
3909 static const int kEmptySymbolRootIndex = 128;
3910
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003911 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003912 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003913 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003914 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003915
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003916 static const int kUndefinedOddballKind = 5;
3917 static const int kNullOddballKind = 3;
3918
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003919 static inline bool HasHeapObjectTag(internal::Object* value) {
3920 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3921 kHeapObjectTag);
3922 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003923
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003924 static inline bool HasSmiTag(internal::Object* value) {
3925 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3926 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003927
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003928 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003929 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003930 }
3931
3932 static inline int GetInstanceType(internal::Object* obj) {
3933 typedef internal::Object O;
3934 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3935 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3936 }
3937
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003938 static inline int GetOddballKind(internal::Object* obj) {
3939 typedef internal::Object O;
3940 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
3941 }
3942
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003943 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003944 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003945 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3946 }
3947
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003948 static inline void* GetExternalPointer(internal::Object* obj) {
3949 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003950 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003951 } else if (GetInstanceType(obj) == kForeignType) {
3952 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003953 } else {
3954 return NULL;
3955 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003956 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003957
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003958 static inline bool IsExternalTwoByteString(int instance_type) {
3959 int representation = (instance_type & kFullStringRepresentationMask);
3960 return representation == kExternalTwoByteRepresentationTag;
3961 }
3962
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003963 static inline bool IsInitialized(v8::Isolate* isolate) {
3964 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
3965 return *reinterpret_cast<int*>(addr) == 1;
3966 }
3967
3968 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
3969 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
3970 kIsolateEmbedderDataOffset;
3971 *reinterpret_cast<void**>(addr) = data;
3972 }
3973
3974 static inline void* GetEmbedderData(v8::Isolate* isolate) {
3975 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
3976 kIsolateEmbedderDataOffset;
3977 return *reinterpret_cast<void**>(addr);
3978 }
3979
3980 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
3981 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
3982 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
3983 }
3984
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003985 template <typename T>
3986 static inline T ReadField(Object* ptr, int offset) {
3987 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3988 return *reinterpret_cast<T*>(addr);
3989 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003990
3991 static inline bool CanCastToHeapObject(void* o) { return false; }
3992 static inline bool CanCastToHeapObject(Context* o) { return true; }
3993 static inline bool CanCastToHeapObject(String* o) { return true; }
3994 static inline bool CanCastToHeapObject(Object* o) { return true; }
3995 static inline bool CanCastToHeapObject(Message* o) { return true; }
3996 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
3997 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003998};
3999
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004000} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004001
4002
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004004Local<T>::Local() : Handle<T>() { }
4005
4006
4007template <class T>
4008Local<T> Local<T>::New(Handle<T> that) {
4009 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004010 T* that_ptr = *that;
4011 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4012 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4013 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4014 reinterpret_cast<internal::HeapObject*>(*p))));
4015 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004016 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4017}
4018
4019
4020template <class T>
4021Persistent<T> Persistent<T>::New(Handle<T> that) {
4022 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004023 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4025}
4026
4027
4028template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004029bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004030 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004031 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032}
4033
4034
4035template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004036bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004037 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004038 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004039}
4040
4041
4042template <class T>
4043void Persistent<T>::Dispose() {
4044 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004045 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004046}
4047
4048
4049template <class T>
4050Persistent<T>::Persistent() : Handle<T>() { }
4051
4052template <class T>
4053void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004054 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4055 parameters,
4056 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004057}
4058
4059template <class T>
4060void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004061 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004062}
4063
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004064template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004065void Persistent<T>::MarkIndependent() {
4066 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4067}
4068
4069template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004070void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4071 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4072}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004073
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004074Arguments::Arguments(internal::Object** implicit_args,
4075 internal::Object** values, int length,
4076 bool is_construct_call)
4077 : implicit_args_(implicit_args),
4078 values_(values),
4079 length_(length),
4080 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004081
4082
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004083Local<Value> Arguments::operator[](int i) const {
4084 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4085 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4086}
4087
4088
4089Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004090 return Local<Function>(reinterpret_cast<Function*>(
4091 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004092}
4093
4094
4095Local<Object> Arguments::This() const {
4096 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4097}
4098
4099
4100Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004101 return Local<Object>(reinterpret_cast<Object*>(
4102 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004103}
4104
4105
4106Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004107 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108}
4109
4110
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004111Isolate* Arguments::GetIsolate() const {
4112 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4113}
4114
4115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004116bool Arguments::IsConstructCall() const {
4117 return is_construct_call_;
4118}
4119
4120
4121int Arguments::Length() const {
4122 return length_;
4123}
4124
4125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004126template <class T>
4127Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004128 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4129 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004130 return Local<T>(reinterpret_cast<T*>(after));
4131}
4132
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004133Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004134 return resource_name_;
4135}
4136
4137
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004138Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004139 return resource_line_offset_;
4140}
4141
4142
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004143Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004144 return resource_column_offset_;
4145}
4146
4147
4148Handle<Boolean> Boolean::New(bool value) {
4149 return value ? True() : False();
4150}
4151
4152
4153void Template::Set(const char* name, v8::Handle<Data> value) {
4154 Set(v8::String::New(name), value);
4155}
4156
4157
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004158Local<Value> Object::GetInternalField(int index) {
4159#ifndef V8_ENABLE_CHECKS
4160 Local<Value> quick_result = UncheckedGetInternalField(index);
4161 if (!quick_result.IsEmpty()) return quick_result;
4162#endif
4163 return CheckedGetInternalField(index);
4164}
4165
4166
4167Local<Value> Object::UncheckedGetInternalField(int index) {
4168 typedef internal::Object O;
4169 typedef internal::Internals I;
4170 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004171 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004172 // If the object is a plain JSObject, which is the common case,
4173 // we know where to find the internal fields and can return the
4174 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004175 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004176 O* value = I::ReadField<O*>(obj, offset);
4177 O** result = HandleScope::CreateHandle(value);
4178 return Local<Value>(reinterpret_cast<Value*>(result));
4179 } else {
4180 return Local<Value>();
4181 }
4182}
4183
4184
4185void* External::Unwrap(Handle<v8::Value> obj) {
4186#ifdef V8_ENABLE_CHECKS
4187 return FullUnwrap(obj);
4188#else
4189 return QuickUnwrap(obj);
4190#endif
4191}
4192
4193
4194void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4195 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004196 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004197 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004198}
4199
4200
4201void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004202 typedef internal::Object O;
4203 typedef internal::Internals I;
4204
4205 O* obj = *reinterpret_cast<O**>(this);
4206
4207 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4208 // If the object is a plain JSObject, which is the common case,
4209 // we know where to find the internal fields and can return the
4210 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004211 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004212 O* value = I::ReadField<O*>(obj, offset);
4213 return I::GetExternalPointer(value);
4214 }
4215
4216 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004217}
4218
4219
4220String* String::Cast(v8::Value* value) {
4221#ifdef V8_ENABLE_CHECKS
4222 CheckCast(value);
4223#endif
4224 return static_cast<String*>(value);
4225}
4226
4227
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004228Local<String> String::Empty(Isolate* isolate) {
4229 typedef internal::Object* S;
4230 typedef internal::Internals I;
4231 if (!I::IsInitialized(isolate)) return Empty();
4232 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4233 return Local<String>(reinterpret_cast<String*>(slot));
4234}
4235
4236
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004237String::ExternalStringResource* String::GetExternalStringResource() const {
4238 typedef internal::Object O;
4239 typedef internal::Internals I;
4240 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004241 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004242 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004243 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4244 result = reinterpret_cast<String::ExternalStringResource*>(value);
4245 } else {
4246 result = NULL;
4247 }
4248#ifdef V8_ENABLE_CHECKS
4249 VerifyExternalStringResource(result);
4250#endif
4251 return result;
4252}
4253
4254
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004255bool Value::IsUndefined() const {
4256#ifdef V8_ENABLE_CHECKS
4257 return FullIsUndefined();
4258#else
4259 return QuickIsUndefined();
4260#endif
4261}
4262
4263bool Value::QuickIsUndefined() const {
4264 typedef internal::Object O;
4265 typedef internal::Internals I;
4266 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4267 if (!I::HasHeapObjectTag(obj)) return false;
4268 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4269 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4270}
4271
4272
4273bool Value::IsNull() const {
4274#ifdef V8_ENABLE_CHECKS
4275 return FullIsNull();
4276#else
4277 return QuickIsNull();
4278#endif
4279}
4280
4281bool Value::QuickIsNull() const {
4282 typedef internal::Object O;
4283 typedef internal::Internals I;
4284 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4285 if (!I::HasHeapObjectTag(obj)) return false;
4286 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4287 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4288}
4289
4290
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004291bool Value::IsString() const {
4292#ifdef V8_ENABLE_CHECKS
4293 return FullIsString();
4294#else
4295 return QuickIsString();
4296#endif
4297}
4298
4299bool Value::QuickIsString() const {
4300 typedef internal::Object O;
4301 typedef internal::Internals I;
4302 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4303 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004304 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004305}
4306
4307
4308Number* Number::Cast(v8::Value* value) {
4309#ifdef V8_ENABLE_CHECKS
4310 CheckCast(value);
4311#endif
4312 return static_cast<Number*>(value);
4313}
4314
4315
4316Integer* Integer::Cast(v8::Value* value) {
4317#ifdef V8_ENABLE_CHECKS
4318 CheckCast(value);
4319#endif
4320 return static_cast<Integer*>(value);
4321}
4322
4323
4324Date* Date::Cast(v8::Value* value) {
4325#ifdef V8_ENABLE_CHECKS
4326 CheckCast(value);
4327#endif
4328 return static_cast<Date*>(value);
4329}
4330
4331
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004332StringObject* StringObject::Cast(v8::Value* value) {
4333#ifdef V8_ENABLE_CHECKS
4334 CheckCast(value);
4335#endif
4336 return static_cast<StringObject*>(value);
4337}
4338
4339
4340NumberObject* NumberObject::Cast(v8::Value* value) {
4341#ifdef V8_ENABLE_CHECKS
4342 CheckCast(value);
4343#endif
4344 return static_cast<NumberObject*>(value);
4345}
4346
4347
4348BooleanObject* BooleanObject::Cast(v8::Value* value) {
4349#ifdef V8_ENABLE_CHECKS
4350 CheckCast(value);
4351#endif
4352 return static_cast<BooleanObject*>(value);
4353}
4354
4355
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004356RegExp* RegExp::Cast(v8::Value* value) {
4357#ifdef V8_ENABLE_CHECKS
4358 CheckCast(value);
4359#endif
4360 return static_cast<RegExp*>(value);
4361}
4362
4363
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004364Object* Object::Cast(v8::Value* value) {
4365#ifdef V8_ENABLE_CHECKS
4366 CheckCast(value);
4367#endif
4368 return static_cast<Object*>(value);
4369}
4370
4371
4372Array* Array::Cast(v8::Value* value) {
4373#ifdef V8_ENABLE_CHECKS
4374 CheckCast(value);
4375#endif
4376 return static_cast<Array*>(value);
4377}
4378
4379
4380Function* Function::Cast(v8::Value* value) {
4381#ifdef V8_ENABLE_CHECKS
4382 CheckCast(value);
4383#endif
4384 return static_cast<Function*>(value);
4385}
4386
4387
4388External* External::Cast(v8::Value* value) {
4389#ifdef V8_ENABLE_CHECKS
4390 CheckCast(value);
4391#endif
4392 return static_cast<External*>(value);
4393}
4394
4395
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004396Isolate* AccessorInfo::GetIsolate() const {
4397 return *reinterpret_cast<Isolate**>(&args_[-3]);
4398}
4399
4400
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004401Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004402 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004403}
4404
4405
4406Local<Object> AccessorInfo::This() const {
4407 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4408}
4409
4410
4411Local<Object> AccessorInfo::Holder() const {
4412 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4413}
4414
4415
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004416Handle<Primitive> Undefined(Isolate* isolate) {
4417 typedef internal::Object* S;
4418 typedef internal::Internals I;
4419 if (!I::IsInitialized(isolate)) return Undefined();
4420 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4421 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4422}
4423
4424
4425Handle<Primitive> Null(Isolate* isolate) {
4426 typedef internal::Object* S;
4427 typedef internal::Internals I;
4428 if (!I::IsInitialized(isolate)) return Null();
4429 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4430 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4431}
4432
4433
4434Handle<Boolean> True(Isolate* isolate) {
4435 typedef internal::Object* S;
4436 typedef internal::Internals I;
4437 if (!I::IsInitialized(isolate)) return True();
4438 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4439 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4440}
4441
4442
4443Handle<Boolean> False(Isolate* isolate) {
4444 typedef internal::Object* S;
4445 typedef internal::Internals I;
4446 if (!I::IsInitialized(isolate)) return False();
4447 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4448 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4449}
4450
4451
4452void Isolate::SetData(void* data) {
4453 typedef internal::Internals I;
4454 I::SetEmbedderData(this, data);
4455}
4456
4457
4458void* Isolate::GetData() {
4459 typedef internal::Internals I;
4460 return I::GetEmbedderData(this);
4461}
4462
4463
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004464/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004465 * \example shell.cc
4466 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004467 * command-line and executes them.
4468 */
4469
4470
4471/**
4472 * \example process.cc
4473 */
4474
4475
4476} // namespace v8
4477
4478
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004479#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004480#undef TYPE_CHECK
4481
4482
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004483#endif // V8_H_