blob: f8883fc1de96e9839d3ed353597e417a2b69d6c3 [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;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000103class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104template <class T> class Handle;
105template <class T> class Local;
106template <class T> class Persistent;
107class FunctionTemplate;
108class ObjectTemplate;
109class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000110class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000111class StackTrace;
112class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000113class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000115namespace internal {
116
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000117class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000118class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000119class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000120class HeapObject;
121class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000122}
123
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000125// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126
127
128/**
129 * A weak reference callback function.
130 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000131 * This callback should either explicitly invoke Dispose on |object| if
132 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
133 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 * \param object the weak global object to be reclaimed by the garbage collector
135 * \param parameter the value passed in when making the weak global object
136 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000137typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 void* parameter);
139
140
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000141// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000143#define TYPE_CHECK(T, S) \
144 while (false) { \
145 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 }
147
148/**
149 * An object reference managed by the v8 garbage collector.
150 *
151 * All objects returned from v8 have to be tracked by the garbage
152 * collector so that it knows that the objects are still alive. Also,
153 * because the garbage collector may move objects, it is unsafe to
154 * point directly to an object. Instead, all objects are stored in
155 * handles which are known by the garbage collector and updated
156 * whenever an object moves. Handles should always be passed by value
157 * (except in cases like out-parameters) and they should never be
158 * allocated on the heap.
159 *
160 * There are two types of handles: local and persistent handles.
161 * Local handles are light-weight and transient and typically used in
162 * local operations. They are managed by HandleScopes. Persistent
163 * handles can be used when storing objects across several independent
164 * operations and have to be explicitly deallocated when they're no
165 * longer used.
166 *
167 * It is safe to extract the object stored in the handle by
168 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000169 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 * behind the scenes and the same rules apply to these values as to
171 * their handles.
172 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000173template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 /**
176 * Creates an empty handle.
177 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000178 inline Handle() : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179
180 /**
181 * Creates a new handle for the specified value.
182 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000183 inline explicit Handle(T* val) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184
185 /**
186 * Creates a handle for the contents of the specified handle. This
187 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000188 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000190 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 * between compatible handles, for instance assigning a
192 * Handle<String> to a variable declared as Handle<Value>, is legal
193 * because String is a subclass of Value.
194 */
195 template <class S> inline Handle(Handle<S> that)
196 : val_(reinterpret_cast<T*>(*that)) {
197 /**
198 * This check fails when trying to convert between incompatible
199 * handles. For example, converting from a Handle<String> to a
200 * Handle<Number>.
201 */
202 TYPE_CHECK(T, S);
203 }
204
205 /**
206 * Returns true if the handle is empty.
207 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000208 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 /**
211 * Sets the handle to be empty. IsEmpty() will then return true.
212 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000213 inline void Clear() { val_ = 0; }
214
215 inline T* operator->() const { return val_; }
216
217 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218
219 /**
220 * Checks whether two handles are the same.
221 * Returns true if both are empty, or if the objects
222 * to which they refer are identical.
223 * The handles' references are not checked.
224 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000225 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000226 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
227 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228 if (a == 0) return b == 0;
229 if (b == 0) return false;
230 return *a == *b;
231 }
232
233 /**
234 * Checks whether two handles are different.
235 * Returns true if only one of the handles is empty, or if
236 * the objects to which they refer are different.
237 * The handles' references are not checked.
238 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000239 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 return !operator==(that);
241 }
242
243 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000244#ifdef V8_ENABLE_CHECKS
245 // If we're going to perform the type check then we have to check
246 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000248#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 return Handle<T>(T::Cast(*that));
250 }
251
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000252 template <class S> inline Handle<S> As() {
253 return Handle<S>::Cast(*this);
254 }
255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 private:
257 T* val_;
258};
259
260
261/**
262 * A light-weight stack-allocated object handle. All operations
263 * that return objects from within v8 return them in local handles. They
264 * are created within HandleScopes, and all local handles allocated within a
265 * handle scope are destroyed when the handle scope is destroyed. Hence it
266 * is not necessary to explicitly deallocate local handles.
267 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000268template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000270 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271 template <class S> inline Local(Local<S> that)
272 : Handle<T>(reinterpret_cast<T*>(*that)) {
273 /**
274 * This check fails when trying to convert between incompatible
275 * handles. For example, converting from a Handle<String> to a
276 * Handle<Number>.
277 */
278 TYPE_CHECK(T, S);
279 }
280 template <class S> inline Local(S* that) : Handle<T>(that) { }
281 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000282#ifdef V8_ENABLE_CHECKS
283 // If we're going to perform the type check then we have to check
284 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000286#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 return Local<T>(T::Cast(*that));
288 }
289
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000290 template <class S> inline Local<S> As() {
291 return Local<S>::Cast(*this);
292 }
293
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 /** Create a local handle for the content of another handle.
295 * The referee is kept alive by the local handle even when
296 * the original handle is destroyed/disposed.
297 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000298 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299};
300
301
302/**
303 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000304 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 * allocated, a Persistent handle remains valid until it is explicitly
306 * disposed.
307 *
308 * A persistent handle contains a reference to a storage cell within
309 * the v8 engine which holds an object value and which is updated by
310 * the garbage collector whenever the object is moved. A new storage
311 * cell can be created using Persistent::New and existing handles can
312 * be disposed using Persistent::Dispose. Since persistent handles
313 * are passed by value you may have many persistent handle objects
314 * that point to the same storage cell. For instance, if you pass a
315 * persistent handle as an argument to a function you will not get two
316 * different storage cells but rather two references to the same
317 * storage cell.
318 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000319template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 /**
322 * Creates an empty persistent handle that doesn't point to any
323 * storage cell.
324 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000325 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326
327 /**
328 * Creates a persistent handle for the same storage cell as the
329 * specified handle. This constructor allows you to pass persistent
330 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000331 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000333 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 * between compatible persistent handles, for instance assigning a
335 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000336 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 */
338 template <class S> inline Persistent(Persistent<S> that)
339 : Handle<T>(reinterpret_cast<T*>(*that)) {
340 /**
341 * This check fails when trying to convert between incompatible
342 * handles. For example, converting from a Handle<String> to a
343 * Handle<Number>.
344 */
345 TYPE_CHECK(T, S);
346 }
347
348 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
349
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000350 /**
351 * "Casts" a plain handle which is known to be a persistent handle
352 * to a persistent handle.
353 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 template <class S> explicit inline Persistent(Handle<S> that)
355 : Handle<T>(*that) { }
356
357 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000358#ifdef V8_ENABLE_CHECKS
359 // If we're going to perform the type check then we have to check
360 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000362#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 return Persistent<T>(T::Cast(*that));
364 }
365
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000366 template <class S> inline Persistent<S> As() {
367 return Persistent<S>::Cast(*this);
368 }
369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000371 * Creates a new persistent handle for an existing local or
372 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000374 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375
376 /**
377 * Releases the storage cell referenced by this persistent handle.
378 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000379 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 * cell remain and IsEmpty will still return false.
381 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000382 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383
384 /**
385 * Make the reference to this object weak. When only weak handles
386 * refer to the object, the garbage collector will perform a
387 * callback to the given V8::WeakReferenceCallback function, passing
388 * it the object reference and the given parameters.
389 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000390 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391
392 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000393 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394
395 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000396 * Marks the reference to this object independent. Garbage collector
397 * is free to ignore any object groups containing this object.
398 * Weak callback for an independent handle should not
399 * assume that it will be preceded by a global GC prologue callback
400 * or followed by a global GC epilogue callback.
401 */
402 inline void MarkIndependent();
403
404 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405 *Checks if the handle holds the only reference to an object.
406 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000407 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408
409 /**
410 * Returns true if the handle's reference is weak.
411 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000412 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000414 /**
415 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
416 * interface description in v8-profiler.h for details.
417 */
418 inline void SetWrapperClassId(uint16_t class_id);
419
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420 private:
421 friend class ImplementationUtilities;
422 friend class ObjectTemplate;
423};
424
425
v8.team.kasperl727e9952008-09-02 14:56:44 +0000426 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 * A stack-allocated class that governs a number of local handles.
428 * After a handle scope has been created, all local handles will be
429 * allocated within that handle scope until either the handle scope is
430 * deleted or another handle scope is created. If there is already a
431 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000432 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 * new handles will again be allocated in the original handle scope.
434 *
435 * After the handle scope of a local handle has been deleted the
436 * garbage collector will no longer track the object stored in the
437 * handle and may deallocate it. The behavior of accessing a handle
438 * for which the handle scope has been deleted is undefined.
439 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000440class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000442 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000444 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445
446 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447 * Closes the handle scope and returns the value as a handle in the
448 * previous scope, which is the new current scope after the call.
449 */
450 template <class T> Local<T> Close(Handle<T> value);
451
452 /**
453 * Counts the number of allocated handles.
454 */
455 static int NumberOfHandles();
456
457 /**
458 * Creates a new handle with the given value.
459 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000460 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000461 // Faster version, uses HeapObject to obtain the current Isolate.
462 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463
464 private:
465 // Make it impossible to create heap-allocated or illegal handle
466 // scopes by disallowing certain operations.
467 HandleScope(const HandleScope&);
468 void operator=(const HandleScope&);
469 void* operator new(size_t size);
470 void operator delete(void*, size_t);
471
ager@chromium.org3811b432009-10-28 14:53:37 +0000472 // This Data class is accessible internally as HandleScopeData through a
473 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000474 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000476 internal::Object** next;
477 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000478 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000481 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482 }
483 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000484
lrn@chromium.org303ada72010-10-27 09:33:13 +0000485 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000487 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000488 internal::Object** prev_next_;
489 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000491 // Allow for the active closing of HandleScopes which allows to pass a handle
492 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000494 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 friend class ImplementationUtilities;
497};
498
499
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000500// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501
502
503/**
504 * The superclass of values and API object templates.
505 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000506class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507 private:
508 Data();
509};
510
511
512/**
513 * Pre-compilation data that can be associated with a script. This
514 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000515 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000516 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000518class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519 public:
520 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000521
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000522 /**
523 * Pre-compiles the specified script (context-independent).
524 *
525 * \param input Pointer to UTF-8 script source code.
526 * \param length Length of UTF-8 script source code.
527 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000530 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000531 * Pre-compiles the specified script (context-independent).
532 *
533 * NOTE: Pre-compilation using this method cannot happen on another thread
534 * without using Lockers.
535 *
536 * \param source Script source code.
537 */
538 static ScriptData* PreCompile(Handle<String> source);
539
540 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000541 * Load previous pre-compilation data.
542 *
543 * \param data Pointer to data returned by a call to Data() of a previous
544 * ScriptData. Ownership is not transferred.
545 * \param length Length of data.
546 */
547 static ScriptData* New(const char* data, int length);
548
549 /**
550 * Returns the length of Data().
551 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000553
554 /**
555 * Returns a serialized representation of this ScriptData that can later be
556 * passed to New(). NOTE: Serialized data is platform-dependent.
557 */
558 virtual const char* Data() = 0;
559
560 /**
561 * Returns true if the source code could not be parsed.
562 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000563 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564};
565
566
567/**
568 * The origin, within a file, of a script.
569 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000570class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000572 inline ScriptOrigin(
573 Handle<Value> resource_name,
574 Handle<Integer> resource_line_offset = Handle<Integer>(),
575 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 : resource_name_(resource_name),
577 resource_line_offset_(resource_line_offset),
578 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000579 inline Handle<Value> ResourceName() const;
580 inline Handle<Integer> ResourceLineOffset() const;
581 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000583 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 Handle<Integer> resource_line_offset_;
585 Handle<Integer> resource_column_offset_;
586};
587
588
589/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000590 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000592class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000595 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000596 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000597 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000598 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000599 * when New() returns
600 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
601 * using pre_data speeds compilation if it's done multiple times.
602 * Owned by caller, no references are kept when New() returns.
603 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000604 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000605 * available to compile event handlers.
606 * \return Compiled script object (context independent; when run it
607 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000609 static Local<Script> New(Handle<String> source,
610 ScriptOrigin* origin = NULL,
611 ScriptData* pre_data = NULL,
612 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613
mads.s.agercbaa0602008-08-14 13:41:48 +0000614 /**
615 * Compiles the specified script using the specified file name
616 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000617 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000618 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000619 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000620 * as the script's origin.
621 * \return Compiled script object (context independent; when run it
622 * will use the currently entered context).
623 */
624 static Local<Script> New(Handle<String> source,
625 Handle<Value> file_name);
626
627 /**
628 * Compiles the specified script (bound to current context).
629 *
630 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000631 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000632 * when Compile() returns
633 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
634 * using pre_data speeds compilation if it's done multiple times.
635 * Owned by caller, no references are kept when Compile() returns.
636 * \param script_data Arbitrary data associated with script. Using
637 * this has same effect as calling SetData(), but makes data available
638 * earlier (i.e. to compile event handlers).
639 * \return Compiled script object, bound to the context that was active
640 * when this function was called. When run it will always use this
641 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000642 */
643 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000644 ScriptOrigin* origin = NULL,
645 ScriptData* pre_data = NULL,
646 Handle<String> script_data = Handle<String>());
647
648 /**
649 * Compiles the specified script using the specified file name
650 * object (typically a string) as the script's origin.
651 *
652 * \param source Script source code.
653 * \param file_name File name to use as script's origin
654 * \param script_data Arbitrary data associated with script. Using
655 * this has same effect as calling SetData(), but makes data available
656 * earlier (i.e. to compile event handlers).
657 * \return Compiled script object, bound to the context that was active
658 * when this function was called. When run it will always use this
659 * context.
660 */
661 static Local<Script> Compile(Handle<String> source,
662 Handle<Value> file_name,
663 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000664
v8.team.kasperl727e9952008-09-02 14:56:44 +0000665 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000666 * Runs the script returning the resulting value. If the script is
667 * context independent (created using ::New) it will be run in the
668 * currently entered context. If it is context specific (created
669 * using ::Compile) it will be run in the context in which it was
670 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000671 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000673
674 /**
675 * Returns the script id value.
676 */
677 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000678
679 /**
680 * Associate an additional data object with the script. This is mainly used
681 * with the debugger as this data object is only available through the
682 * debugger API.
683 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000684 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685};
686
687
688/**
689 * An error message.
690 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000691class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000693 Local<String> Get() const;
694 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000696 /**
697 * Returns the resource name for the script from where the function causing
698 * the error originates.
699 */
ager@chromium.org32912102009-01-16 10:38:43 +0000700 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000702 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000703 * Returns the resource data for the script from where the function causing
704 * the error originates.
705 */
706 Handle<Value> GetScriptData() const;
707
708 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000709 * Exception stack trace. By default stack traces are not captured for
710 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
711 * to change this option.
712 */
713 Handle<StackTrace> GetStackTrace() const;
714
715 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000716 * Returns the number, 1-based, of the line where the error occurred.
717 */
ager@chromium.org32912102009-01-16 10:38:43 +0000718 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000720 /**
721 * Returns the index within the script of the first character where
722 * the error occurred.
723 */
ager@chromium.org32912102009-01-16 10:38:43 +0000724 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000725
726 /**
727 * Returns the index within the script of the last character where
728 * the error occurred.
729 */
ager@chromium.org32912102009-01-16 10:38:43 +0000730 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000731
732 /**
733 * Returns the index within the line of the first character where
734 * the error occurred.
735 */
ager@chromium.org32912102009-01-16 10:38:43 +0000736 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000737
738 /**
739 * Returns the index within the line of the last character where
740 * the error occurred.
741 */
ager@chromium.org32912102009-01-16 10:38:43 +0000742 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 // TODO(1245381): Print to a string instead of on a FILE.
745 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000746
747 static const int kNoLineNumberInfo = 0;
748 static const int kNoColumnInfo = 0;
749};
750
751
752/**
753 * Representation of a JavaScript stack trace. The information collected is a
754 * snapshot of the execution stack and the information remains valid after
755 * execution continues.
756 */
757class V8EXPORT StackTrace {
758 public:
759 /**
760 * Flags that determine what information is placed captured for each
761 * StackFrame when grabbing the current stack trace.
762 */
763 enum StackTraceOptions {
764 kLineNumber = 1,
765 kColumnOffset = 1 << 1 | kLineNumber,
766 kScriptName = 1 << 2,
767 kFunctionName = 1 << 3,
768 kIsEval = 1 << 4,
769 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000770 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000771 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000772 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000773 };
774
775 /**
776 * Returns a StackFrame at a particular index.
777 */
778 Local<StackFrame> GetFrame(uint32_t index) const;
779
780 /**
781 * Returns the number of StackFrames.
782 */
783 int GetFrameCount() const;
784
785 /**
786 * Returns StackTrace as a v8::Array that contains StackFrame objects.
787 */
788 Local<Array> AsArray();
789
790 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000791 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000792 *
793 * \param frame_limit The maximum number of stack frames we want to capture.
794 * \param options Enumerates the set of things we will capture for each
795 * StackFrame.
796 */
797 static Local<StackTrace> CurrentStackTrace(
798 int frame_limit,
799 StackTraceOptions options = kOverview);
800};
801
802
803/**
804 * A single JavaScript stack frame.
805 */
806class V8EXPORT StackFrame {
807 public:
808 /**
809 * Returns the number, 1-based, of the line for the associate function call.
810 * This method will return Message::kNoLineNumberInfo if it is unable to
811 * retrieve the line number, or if kLineNumber was not passed as an option
812 * when capturing the StackTrace.
813 */
814 int GetLineNumber() const;
815
816 /**
817 * Returns the 1-based column offset on the line for the associated function
818 * call.
819 * This method will return Message::kNoColumnInfo if it is unable to retrieve
820 * the column number, or if kColumnOffset was not passed as an option when
821 * capturing the StackTrace.
822 */
823 int GetColumn() const;
824
825 /**
826 * Returns the name of the resource that contains the script for the
827 * function for this StackFrame.
828 */
829 Local<String> GetScriptName() const;
830
831 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000832 * Returns the name of the resource that contains the script for the
833 * function for this StackFrame or sourceURL value if the script name
834 * is undefined and its source ends with //@ sourceURL=... string.
835 */
836 Local<String> GetScriptNameOrSourceURL() const;
837
838 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000839 * Returns the name of the function associated with this stack frame.
840 */
841 Local<String> GetFunctionName() const;
842
843 /**
844 * Returns whether or not the associated function is compiled via a call to
845 * eval().
846 */
847 bool IsEval() const;
848
849 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000850 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000851 * constructor via "new".
852 */
853 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854};
855
856
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000857// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858
859
860/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000861 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000863class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 /**
866 * Returns true if this value is the undefined value. See ECMA-262
867 * 4.3.10.
868 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000869 inline bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870
871 /**
872 * Returns true if this value is the null value. See ECMA-262
873 * 4.3.11.
874 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000875 inline bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
877 /**
878 * Returns true if this value is true.
879 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000880 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881
882 /**
883 * Returns true if this value is false.
884 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000885 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886
887 /**
888 * Returns true if this value is an instance of the String type.
889 * See ECMA-262 8.4.
890 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000891 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892
893 /**
894 * Returns true if this value is a function.
895 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000896 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897
898 /**
899 * Returns true if this value is an array.
900 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000901 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
v8.team.kasperl727e9952008-09-02 14:56:44 +0000903 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 * Returns true if this value is an object.
905 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000906 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907
v8.team.kasperl727e9952008-09-02 14:56:44 +0000908 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 * Returns true if this value is boolean.
910 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000911 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000912
v8.team.kasperl727e9952008-09-02 14:56:44 +0000913 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 * Returns true if this value is a number.
915 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000916 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
v8.team.kasperl727e9952008-09-02 14:56:44 +0000918 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 * Returns true if this value is external.
920 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000921 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922
v8.team.kasperl727e9952008-09-02 14:56:44 +0000923 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924 * Returns true if this value is a 32-bit signed integer.
925 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000926 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000928 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000929 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000930 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000931 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000932
933 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000934 * Returns true if this value is a Date.
935 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000936 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000937
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000938 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000939 * Returns true if this value is a Boolean object.
940 */
941 V8EXPORT bool IsBooleanObject() const;
942
943 /**
944 * Returns true if this value is a Number object.
945 */
946 V8EXPORT bool IsNumberObject() const;
947
948 /**
949 * Returns true if this value is a String object.
950 */
951 V8EXPORT bool IsStringObject() const;
952
953 /**
954 * Returns true if this value is a NativeError.
955 */
956 V8EXPORT bool IsNativeError() const;
957
958 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000959 * Returns true if this value is a RegExp.
960 */
961 V8EXPORT bool IsRegExp() const;
962
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000963 V8EXPORT Local<Boolean> ToBoolean() const;
964 V8EXPORT Local<Number> ToNumber() const;
965 V8EXPORT Local<String> ToString() const;
966 V8EXPORT Local<String> ToDetailString() const;
967 V8EXPORT Local<Object> ToObject() const;
968 V8EXPORT Local<Integer> ToInteger() const;
969 V8EXPORT Local<Uint32> ToUint32() const;
970 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971
972 /**
973 * Attempts to convert a string to an array index.
974 * Returns an empty handle if the conversion fails.
975 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000976 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000978 V8EXPORT bool BooleanValue() const;
979 V8EXPORT double NumberValue() const;
980 V8EXPORT int64_t IntegerValue() const;
981 V8EXPORT uint32_t Uint32Value() const;
982 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983
984 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000985 V8EXPORT bool Equals(Handle<Value> that) const;
986 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000987
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000988 private:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000989 inline bool QuickIsUndefined() const;
990 inline bool QuickIsNull() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000991 inline bool QuickIsString() const;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000992 V8EXPORT bool FullIsUndefined() const;
993 V8EXPORT bool FullIsNull() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000994 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995};
996
997
998/**
999 * The superclass of primitive values. See ECMA-262 4.3.2.
1000 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001001class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002
1003
1004/**
1005 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1006 * or false value.
1007 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001008class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001010 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 static inline Handle<Boolean> New(bool value);
1012};
1013
1014
1015/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001016 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001018class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001020 /**
1021 * Returns the number of characters in this string.
1022 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001023 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024
v8.team.kasperl727e9952008-09-02 14:56:44 +00001025 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001026 * Returns the number of bytes in the UTF-8 encoded
1027 * representation of this string.
1028 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001029 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001030
1031 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001032 * A fast conservative check for non-ASCII characters. May
1033 * return true even for ASCII strings, but if it returns
1034 * false you can be sure that all characters are in the range
1035 * 0-127.
1036 */
1037 V8EXPORT bool MayContainNonAscii() const;
1038
1039 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001040 * Write the contents of the string to an external buffer.
1041 * If no arguments are given, expects the buffer to be large
1042 * enough to hold the entire string and NULL terminator. Copies
1043 * the contents of the string and the NULL terminator into the
1044 * buffer.
1045 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001046 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1047 * before the end of the buffer.
1048 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001049 * Copies up to length characters into the output buffer.
1050 * Only null-terminates if there is enough space in the buffer.
1051 *
1052 * \param buffer The buffer into which the string will be copied.
1053 * \param start The starting position within the string at which
1054 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001055 * \param length The number of characters to copy from the string. For
1056 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001057 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001058 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001059 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001060 * \return The number of characters copied to the buffer excluding the null
1061 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001062 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001063 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001064 enum WriteOptions {
1065 NO_OPTIONS = 0,
1066 HINT_MANY_WRITES_EXPECTED = 1,
1067 NO_NULL_TERMINATION = 2
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001068 };
1069
lrn@chromium.org34e60782011-09-15 07:25:40 +00001070 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001071 V8EXPORT int Write(uint16_t* buffer,
1072 int start = 0,
1073 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001074 int options = NO_OPTIONS) const;
1075 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001076 V8EXPORT int WriteAscii(char* buffer,
1077 int start = 0,
1078 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001079 int options = NO_OPTIONS) const;
1080 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001081 V8EXPORT int WriteUtf8(char* buffer,
1082 int length = -1,
1083 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001084 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085
v8.team.kasperl727e9952008-09-02 14:56:44 +00001086 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001087 * A zero length string.
1088 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001089 V8EXPORT static v8::Local<v8::String> Empty();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001090 inline static v8::Local<v8::String> Empty(Isolate* isolate);
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001091
1092 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001093 * Returns true if the string is external
1094 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001095 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096
v8.team.kasperl727e9952008-09-02 14:56:44 +00001097 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001098 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001099 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001100 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001101
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001102 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001103 public:
1104 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001105
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001106 protected:
1107 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001108
1109 /**
1110 * Internally V8 will call this Dispose method when the external string
1111 * resource is no longer needed. The default implementation will use the
1112 * delete operator. This method can be overridden in subclasses to
1113 * control how allocated external string resources are disposed.
1114 */
1115 virtual void Dispose() { delete this; }
1116
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001117 private:
1118 // Disallow copying and assigning.
1119 ExternalStringResourceBase(const ExternalStringResourceBase&);
1120 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001121
1122 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001123 };
1124
v8.team.kasperl727e9952008-09-02 14:56:44 +00001125 /**
1126 * An ExternalStringResource is a wrapper around a two-byte string
1127 * buffer that resides outside V8's heap. Implement an
1128 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001129 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001130 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001131 class V8EXPORT ExternalStringResource
1132 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 public:
1134 /**
1135 * Override the destructor to manage the life cycle of the underlying
1136 * buffer.
1137 */
1138 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001139
1140 /**
1141 * The string data from the underlying buffer.
1142 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001144
1145 /**
1146 * The length of the string. That is, the number of two-byte characters.
1147 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001149
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 protected:
1151 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152 };
1153
1154 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001155 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001156 * string buffer that resides outside V8's heap. Implement an
1157 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001158 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001159 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001160 * UTF-8, which would require special treatment internally in the
1161 * engine and, in the case of UTF-8, do not allow efficient indexing.
1162 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001163 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001165 class V8EXPORT ExternalAsciiStringResource
1166 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 public:
1168 /**
1169 * Override the destructor to manage the life cycle of the underlying
1170 * buffer.
1171 */
1172 virtual ~ExternalAsciiStringResource() {}
1173 /** The string data from the underlying buffer.*/
1174 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001175 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176 virtual size_t length() const = 0;
1177 protected:
1178 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 };
1180
1181 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001182 * Get the ExternalStringResource for an external string. Returns
1183 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001185 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186
1187 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001188 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001189 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001191 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1192 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001194 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195
1196 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001197 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001199 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 * be careful to supply the length parameter.
1201 * If it is not given, the function calls
1202 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001203 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001205 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206
lrn@chromium.org34e60782011-09-15 07:25:40 +00001207 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001208 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209
1210 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001211 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001212
v8.team.kasperl727e9952008-09-02 14:56:44 +00001213 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001214 * Creates a new string by concatenating the left and the right strings
1215 * passed in as parameters.
1216 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001217 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001218 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001219
1220 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001221 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001222 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001223 * resource will be disposed by calling its Dispose method. The caller of
1224 * this function should not otherwise delete or modify the resource. Neither
1225 * should the underlying buffer be deallocated or modified except through the
1226 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001227 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001228 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001229
ager@chromium.org6f10e412009-02-13 10:11:16 +00001230 /**
1231 * Associate an external string resource with this string by transforming it
1232 * in place so that existing references to this string in the JavaScript heap
1233 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001234 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001235 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001236 * The string is not modified if the operation fails. See NewExternal for
1237 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001238 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001239 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240
v8.team.kasperl727e9952008-09-02 14:56:44 +00001241 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001242 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001243 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001244 * resource will be disposed by calling its Dispose method. The caller of
1245 * this function should not otherwise delete or modify the resource. Neither
1246 * should the underlying buffer be deallocated or modified except through the
1247 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001248 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001249 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001250
ager@chromium.org6f10e412009-02-13 10:11:16 +00001251 /**
1252 * Associate an external string resource with this string by transforming it
1253 * in place so that existing references to this string in the JavaScript heap
1254 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001255 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001256 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001257 * The string is not modified if the operation fails. See NewExternal for
1258 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001259 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001260 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001262 /**
1263 * Returns true if this string can be made external.
1264 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001265 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001266
lrn@chromium.org34e60782011-09-15 07:25:40 +00001267 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001268 V8EXPORT static Local<String> NewUndetectable(const char* data,
1269 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270
lrn@chromium.org34e60782011-09-15 07:25:40 +00001271 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001272 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1273 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274
1275 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001276 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001277 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001278 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001279 * then the length() method returns 0 and the * operator returns
1280 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001281 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001282 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001283 public:
1284 explicit Utf8Value(Handle<v8::Value> obj);
1285 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001286 char* operator*() { return str_; }
1287 const char* operator*() const { return str_; }
1288 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001289 private:
1290 char* str_;
1291 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001292
1293 // Disallow copying and assigning.
1294 Utf8Value(const Utf8Value&);
1295 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001296 };
1297
1298 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001299 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001301 * If conversion to a string fails (eg. due to an exception in the toString()
1302 * method of the object) then the length() method returns 0 and the * operator
1303 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001305 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 public:
1307 explicit AsciiValue(Handle<v8::Value> obj);
1308 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001309 char* operator*() { return str_; }
1310 const char* operator*() const { return str_; }
1311 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 private:
1313 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001314 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001315
1316 // Disallow copying and assigning.
1317 AsciiValue(const AsciiValue&);
1318 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 };
1320
1321 /**
1322 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001323 * If conversion to a string fails (eg. due to an exception in the toString()
1324 * method of the object) then the length() method returns 0 and the * operator
1325 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001327 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 public:
1329 explicit Value(Handle<v8::Value> obj);
1330 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001331 uint16_t* operator*() { return str_; }
1332 const uint16_t* operator*() const { return str_; }
1333 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 private:
1335 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001336 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001337
1338 // Disallow copying and assigning.
1339 Value(const Value&);
1340 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001342
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001343 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001344 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1345 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346};
1347
1348
1349/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001350 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001352class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001354 V8EXPORT double Value() const;
1355 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001356 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001358 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001359 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360};
1361
1362
1363/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001364 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001366class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001368 V8EXPORT static Local<Integer> New(int32_t value);
1369 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1370 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001371 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001373 V8EXPORT Integer();
1374 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375};
1376
1377
1378/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001379 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001381class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001383 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001385 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386};
1387
1388
1389/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001390 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001392class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001394 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001396 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397};
1398
1399
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400enum PropertyAttribute {
1401 None = 0,
1402 ReadOnly = 1 << 0,
1403 DontEnum = 1 << 1,
1404 DontDelete = 1 << 2
1405};
1406
ager@chromium.org3811b432009-10-28 14:53:37 +00001407enum ExternalArrayType {
1408 kExternalByteArray = 1,
1409 kExternalUnsignedByteArray,
1410 kExternalShortArray,
1411 kExternalUnsignedShortArray,
1412 kExternalIntArray,
1413 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001414 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001415 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001416 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001417};
1418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001420 * Accessor[Getter|Setter] are used as callback functions when
1421 * setting|getting a particular property. See Object and ObjectTemplate's
1422 * method SetAccessor.
1423 */
1424typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1425 const AccessorInfo& info);
1426
1427
1428typedef void (*AccessorSetter)(Local<String> property,
1429 Local<Value> value,
1430 const AccessorInfo& info);
1431
1432
1433/**
1434 * Access control specifications.
1435 *
1436 * Some accessors should be accessible across contexts. These
1437 * accessors have an explicit access control parameter which specifies
1438 * the kind of cross-context access that should be allowed.
1439 *
1440 * Additionally, for security, accessors can prohibit overwriting by
1441 * accessors defined in JavaScript. For objects that have such
1442 * accessors either locally or in their prototype chain it is not
1443 * possible to overwrite the accessor by using __defineGetter__ or
1444 * __defineSetter__ from JavaScript code.
1445 */
1446enum AccessControl {
1447 DEFAULT = 0,
1448 ALL_CAN_READ = 1,
1449 ALL_CAN_WRITE = 1 << 1,
1450 PROHIBITS_OVERWRITING = 1 << 2
1451};
1452
1453
1454/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001455 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001457class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001459 V8EXPORT bool Set(Handle<Value> key,
1460 Handle<Value> value,
1461 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001462
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001463 V8EXPORT bool Set(uint32_t index,
1464 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001465
ager@chromium.orge2902be2009-06-08 12:21:35 +00001466 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001467 // overriding accessors or read-only properties.
1468 //
1469 // Note that if the object has an interceptor the property will be set
1470 // locally, but since the interceptor takes precedence the local property
1471 // will only be returned if the interceptor doesn't return a value.
1472 //
1473 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001474 V8EXPORT bool ForceSet(Handle<Value> key,
1475 Handle<Value> value,
1476 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001477
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001478 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001480 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001481
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001482 /**
1483 * Gets the property attributes of a property which can be None or
1484 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1485 * None when the property doesn't exist.
1486 */
1487 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1488
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001489 // TODO(1245389): Replace the type-specific versions of these
1490 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001491 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001492
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001493 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001494
1495 // Delete a property on this object bypassing interceptors and
1496 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001497 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001498
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001499 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001500
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001501 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001503 V8EXPORT bool SetAccessor(Handle<String> name,
1504 AccessorGetter getter,
1505 AccessorSetter setter = 0,
1506 Handle<Value> data = Handle<Value>(),
1507 AccessControl settings = DEFAULT,
1508 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001509
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001510 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001511 * Returns an array containing the names of the enumerable properties
1512 * of this object, including properties from prototype objects. The
1513 * array returned by this method contains the same values as would
1514 * be enumerated by a for-in statement over this object.
1515 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001516 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001517
1518 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001519 * This function has the same functionality as GetPropertyNames but
1520 * the returned array doesn't contain the names of properties from
1521 * prototype objects.
1522 */
1523 V8EXPORT Local<Array> GetOwnPropertyNames();
1524
1525 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 * Get the prototype object. This does not skip objects marked to
1527 * be skipped by __proto__ and it does not consult the security
1528 * handler.
1529 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001530 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531
1532 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001533 * Set the prototype object. This does not skip objects marked to
1534 * be skipped by __proto__ and it does not consult the security
1535 * handler.
1536 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001537 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001538
1539 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001540 * Finds an instance of the given function template in the prototype
1541 * chain.
1542 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001543 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1544 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001545
1546 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547 * Call builtin Object.prototype.toString on this object.
1548 * This is different from Value::ToString() that may call
1549 * user-defined toString function. This one does not.
1550 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001551 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001552
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001553 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001554 * Returns the function invoked as a constructor for this object.
1555 * May be the null value.
1556 */
1557 V8EXPORT Local<Value> GetConstructor();
1558
1559 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001560 * Returns the name of the function invoked as a constructor for this object.
1561 */
1562 V8EXPORT Local<String> GetConstructorName();
1563
kasper.lund212ac232008-07-16 07:07:30 +00001564 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001565 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001566 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001567 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001568 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001569 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001571 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001572 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001573
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001574 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001575 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001576
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001578 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001579 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1580 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1581 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582
1583 /**
1584 * If result.IsEmpty() no real property was located in the prototype chain.
1585 * This means interceptors in the prototype chain are not called.
1586 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001587 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1588 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001589
1590 /**
1591 * If result.IsEmpty() no real property was located on the object or
1592 * in the prototype chain.
1593 * This means interceptors in the prototype chain are not called.
1594 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001595 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596
1597 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001598 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599
kasper.lund212ac232008-07-16 07:07:30 +00001600 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001601 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001603 /**
1604 * Turns on access check on the object if the object is an instance of
1605 * a template that has access check callbacks. If an object has no
1606 * access check info, the object cannot be accessed by anyone.
1607 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001608 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001609
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001610 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001611 * Returns the identity hash for this object. The current implementation
1612 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001613 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001614 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001615 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001616 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001617 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001618
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001619 /**
1620 * Access hidden properties on JavaScript objects. These properties are
1621 * hidden from the executing JavaScript and only accessible through the V8
1622 * C++ API. Hidden properties introduced by V8 internally (for example the
1623 * identity hash) are prefixed with "v8::".
1624 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001625 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1626 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1627 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001628
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001629 /**
1630 * Returns true if this is an instance of an api function (one
1631 * created from a function created from a function template) and has
1632 * been modified since it was created. Note that this method is
1633 * conservative and may return true for objects that haven't actually
1634 * been modified.
1635 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001636 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001637
1638 /**
1639 * Clone this object with a fast but shallow copy. Values will point
1640 * to the same values as the original object.
1641 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001642 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001644 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001645 * Returns the context in which the object was created.
1646 */
1647 V8EXPORT Local<Context> CreationContext();
1648
1649 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001650 * Set the backing store of the indexed properties to be managed by the
1651 * embedding layer. Access to the indexed properties will follow the rules
1652 * spelled out in CanvasPixelArray.
1653 * Note: The embedding program still owns the data and needs to ensure that
1654 * the backing store is preserved while V8 has a reference.
1655 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001656 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001657 V8EXPORT bool HasIndexedPropertiesInPixelData();
1658 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1659 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001660
ager@chromium.org3811b432009-10-28 14:53:37 +00001661 /**
1662 * Set the backing store of the indexed properties to be managed by the
1663 * embedding layer. Access to the indexed properties will follow the rules
1664 * spelled out for the CanvasArray subtypes in the WebGL specification.
1665 * Note: The embedding program still owns the data and needs to ensure that
1666 * the backing store is preserved while V8 has a reference.
1667 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001668 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1669 void* data,
1670 ExternalArrayType array_type,
1671 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001672 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1673 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1674 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1675 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001676
lrn@chromium.org1c092762011-05-09 09:42:16 +00001677 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001678 * Checks whether a callback is set by the
1679 * ObjectTemplate::SetCallAsFunctionHandler method.
1680 * When an Object is callable this method returns true.
1681 */
1682 V8EXPORT bool IsCallable();
1683
1684 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001685 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001686 * ObjectTemplate::SetCallAsFunctionHandler method.
1687 */
1688 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1689 int argc,
1690 Handle<Value> argv[]);
1691
1692 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001693 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001694 * ObjectTemplate::SetCallAsFunctionHandler method.
1695 * Note: This method behaves like the Function::NewInstance method.
1696 */
1697 V8EXPORT Local<Value> CallAsConstructor(int argc,
1698 Handle<Value> argv[]);
1699
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001700 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001701 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001702
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001704 V8EXPORT Object();
1705 V8EXPORT static void CheckCast(Value* obj);
1706 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1707 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001708
1709 /**
1710 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001711 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001712 */
1713 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714};
1715
1716
1717/**
1718 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1719 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001720class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001722 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723
ager@chromium.org3e875802009-06-29 08:26:34 +00001724 /**
1725 * Clones an element at index |index|. Returns an empty
1726 * handle if cloning fails (for any reason).
1727 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001728 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001729
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001730 /**
1731 * Creates a JavaScript array with the given length. If the length
1732 * is negative the returned array will have length 0.
1733 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001734 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001735
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001736 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001738 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001739 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740};
1741
1742
1743/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001744 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001746class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001748 V8EXPORT Local<Object> NewInstance() const;
1749 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1750 V8EXPORT Local<Value> Call(Handle<Object> recv,
1751 int argc,
1752 Handle<Value> argv[]);
1753 V8EXPORT void SetName(Handle<String> name);
1754 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001755
1756 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001757 * Name inferred from variable or property assignment of this function.
1758 * Used to facilitate debugging and profiling of JavaScript code written
1759 * in an OO style, where many functions are anonymous but are assigned
1760 * to object properties.
1761 */
1762 V8EXPORT Handle<Value> GetInferredName() const;
1763
1764 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001765 * Returns zero based line number of function body and
1766 * kLineOffsetNotFound if no information available.
1767 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001768 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001769 /**
1770 * Returns zero based column number of function body and
1771 * kLineOffsetNotFound if no information available.
1772 */
1773 V8EXPORT int GetScriptColumnNumber() const;
1774 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001775 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001776 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001777 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001778
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001780 V8EXPORT Function();
1781 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782};
1783
1784
1785/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001786 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1787 */
1788class Date : public Object {
1789 public:
1790 V8EXPORT static Local<Value> New(double time);
1791
1792 /**
1793 * A specialization of Value::NumberValue that is more efficient
1794 * because we know the structure of this object.
1795 */
1796 V8EXPORT double NumberValue() const;
1797
1798 static inline Date* Cast(v8::Value* obj);
1799
1800 /**
1801 * Notification that the embedder has changed the time zone,
1802 * daylight savings time, or other date / time configuration
1803 * parameters. V8 keeps a cache of various values used for
1804 * date / time computation. This notification will reset
1805 * those cached values for the current context so that date /
1806 * time configuration changes would be reflected in the Date
1807 * object.
1808 *
1809 * This API should not be called more than needed as it will
1810 * negatively impact the performance of date operations.
1811 */
1812 V8EXPORT static void DateTimeConfigurationChangeNotification();
1813
1814 private:
1815 V8EXPORT static void CheckCast(v8::Value* obj);
1816};
1817
1818
1819/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001820 * A Number object (ECMA-262, 4.3.21).
1821 */
1822class NumberObject : public Object {
1823 public:
1824 V8EXPORT static Local<Value> New(double value);
1825
1826 /**
1827 * Returns the Number held by the object.
1828 */
1829 V8EXPORT double NumberValue() const;
1830
1831 static inline NumberObject* Cast(v8::Value* obj);
1832
1833 private:
1834 V8EXPORT static void CheckCast(v8::Value* obj);
1835};
1836
1837
1838/**
1839 * A Boolean object (ECMA-262, 4.3.15).
1840 */
1841class BooleanObject : public Object {
1842 public:
1843 V8EXPORT static Local<Value> New(bool value);
1844
1845 /**
1846 * Returns the Boolean held by the object.
1847 */
1848 V8EXPORT bool BooleanValue() const;
1849
1850 static inline BooleanObject* Cast(v8::Value* obj);
1851
1852 private:
1853 V8EXPORT static void CheckCast(v8::Value* obj);
1854};
1855
1856
1857/**
1858 * A String object (ECMA-262, 4.3.18).
1859 */
1860class StringObject : public Object {
1861 public:
1862 V8EXPORT static Local<Value> New(Handle<String> value);
1863
1864 /**
1865 * Returns the String held by the object.
1866 */
1867 V8EXPORT Local<String> StringValue() const;
1868
1869 static inline StringObject* Cast(v8::Value* obj);
1870
1871 private:
1872 V8EXPORT static void CheckCast(v8::Value* obj);
1873};
1874
1875
1876/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001877 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1878 */
1879class RegExp : public Object {
1880 public:
1881 /**
1882 * Regular expression flag bits. They can be or'ed to enable a set
1883 * of flags.
1884 */
1885 enum Flags {
1886 kNone = 0,
1887 kGlobal = 1,
1888 kIgnoreCase = 2,
1889 kMultiline = 4
1890 };
1891
1892 /**
1893 * Creates a regular expression from the given pattern string and
1894 * the flags bit field. May throw a JavaScript exception as
1895 * described in ECMA-262, 15.10.4.1.
1896 *
1897 * For example,
1898 * RegExp::New(v8::String::New("foo"),
1899 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1900 * is equivalent to evaluating "/foo/gm".
1901 */
1902 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1903 Flags flags);
1904
1905 /**
1906 * Returns the value of the source property: a string representing
1907 * the regular expression.
1908 */
1909 V8EXPORT Local<String> GetSource() const;
1910
1911 /**
1912 * Returns the flags bit field.
1913 */
1914 V8EXPORT Flags GetFlags() const;
1915
1916 static inline RegExp* Cast(v8::Value* obj);
1917
1918 private:
1919 V8EXPORT static void CheckCast(v8::Value* obj);
1920};
1921
1922
1923/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001924 * A JavaScript value that wraps a C++ void*. This type of value is
1925 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001927 *
1928 * The Wrap function V8 will return the most optimal Value object wrapping the
1929 * C++ void*. The type of the value is not guaranteed to be an External object
1930 * and no assumptions about its type should be made. To access the wrapped
1931 * value Unwrap should be used, all other operations on that object will lead
1932 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001934class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001936 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001937 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001938
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001939 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001940 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001941 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001943 V8EXPORT External();
1944 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001945 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001946 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947};
1948
1949
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001950// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951
1952
1953/**
1954 * The superclass of object and function templates.
1955 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001956class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 public:
1958 /** Adds a property to each instance created by this template.*/
1959 void Set(Handle<String> name, Handle<Data> value,
1960 PropertyAttribute attributes = None);
1961 inline void Set(const char* name, Handle<Data> value);
1962 private:
1963 Template();
1964
1965 friend class ObjectTemplate;
1966 friend class FunctionTemplate;
1967};
1968
1969
1970/**
1971 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001972 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 * including the receiver, the number and values of arguments, and
1974 * the holder of the function.
1975 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001976class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977 public:
1978 inline int Length() const;
1979 inline Local<Value> operator[](int i) const;
1980 inline Local<Function> Callee() const;
1981 inline Local<Object> This() const;
1982 inline Local<Object> Holder() const;
1983 inline bool IsConstructCall() const;
1984 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001985 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00001986
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001988 static const int kIsolateIndex = 0;
1989 static const int kDataIndex = -1;
1990 static const int kCalleeIndex = -2;
1991 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001992
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001994 inline Arguments(internal::Object** implicit_args,
1995 internal::Object** values,
1996 int length,
1997 bool is_construct_call);
1998 internal::Object** implicit_args_;
1999 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002001 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002};
2003
2004
2005/**
2006 * The information passed to an accessor callback about the context
2007 * of the property access.
2008 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002009class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002011 inline AccessorInfo(internal::Object** args)
2012 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002013 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 inline Local<Value> Data() const;
2015 inline Local<Object> This() const;
2016 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002017
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002019 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020};
2021
2022
2023typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2024
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 * NamedProperty[Getter|Setter] are used as interceptors on object.
2027 * See ObjectTemplate::SetNamedPropertyHandler.
2028 */
2029typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2030 const AccessorInfo& info);
2031
2032
2033/**
2034 * Returns the value if the setter intercepts the request.
2035 * Otherwise, returns an empty handle.
2036 */
2037typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2038 Local<Value> value,
2039 const AccessorInfo& info);
2040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041/**
2042 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002043 * The result is an integer encoding property attributes (like v8::None,
2044 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002046typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2047 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048
2049
2050/**
2051 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002052 * The return value is true if the property could be deleted and false
2053 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 */
2055typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2056 const AccessorInfo& info);
2057
2058/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002059 * Returns an array containing the names of the properties the named
2060 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 */
2062typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2063
v8.team.kasperl727e9952008-09-02 14:56:44 +00002064
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002066 * Returns the value of the property if the getter intercepts the
2067 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 */
2069typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2070 const AccessorInfo& info);
2071
2072
2073/**
2074 * Returns the value if the setter intercepts the request.
2075 * Otherwise, returns an empty handle.
2076 */
2077typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2078 Local<Value> value,
2079 const AccessorInfo& info);
2080
2081
2082/**
2083 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002084 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002086typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2087 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088
2089/**
2090 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002091 * The return value is true if the property could be deleted and false
2092 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 */
2094typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2095 const AccessorInfo& info);
2096
v8.team.kasperl727e9952008-09-02 14:56:44 +00002097/**
2098 * Returns an array containing the indices of the properties the
2099 * indexed property getter intercepts.
2100 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2102
2103
2104/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002105 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 */
2107enum AccessType {
2108 ACCESS_GET,
2109 ACCESS_SET,
2110 ACCESS_HAS,
2111 ACCESS_DELETE,
2112 ACCESS_KEYS
2113};
2114
v8.team.kasperl727e9952008-09-02 14:56:44 +00002115
2116/**
2117 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002118 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002119 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002120typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 Local<Value> key,
2122 AccessType type,
2123 Local<Value> data);
2124
v8.team.kasperl727e9952008-09-02 14:56:44 +00002125
2126/**
2127 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002128 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002129 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002130typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 uint32_t index,
2132 AccessType type,
2133 Local<Value> data);
2134
2135
2136/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002137 * A FunctionTemplate is used to create functions at runtime. There
2138 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002139 * context. The lifetime of the created function is equal to the
2140 * lifetime of the context. So in case the embedder needs to create
2141 * temporary functions that can be collected using Scripts is
2142 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 *
2144 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002145 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002147 * A FunctionTemplate has a corresponding instance template which is
2148 * used to create object instances when the function is used as a
2149 * constructor. Properties added to the instance template are added to
2150 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 *
2152 * A FunctionTemplate can have a prototype template. The prototype template
2153 * is used to create the prototype object of the function.
2154 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002155 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002157 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2159 * t->Set("func_property", v8::Number::New(1));
2160 *
2161 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2162 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2163 * proto_t->Set("proto_const", v8::Number::New(2));
2164 *
2165 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2166 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2167 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2168 * instance_t->Set("instance_property", Number::New(3));
2169 *
2170 * v8::Local<v8::Function> function = t->GetFunction();
2171 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 *
2174 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002175 * and "instance" for the instance object created above. The function
2176 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002178 * \code
2179 * func_property in function == true;
2180 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002182 * function.prototype.proto_method() invokes 'InvokeCallback'
2183 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002185 * instance instanceof function == true;
2186 * instance.instance_accessor calls 'InstanceAccessorCallback'
2187 * instance.instance_property == 3;
2188 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002190 * A FunctionTemplate can inherit from another one by calling the
2191 * FunctionTemplate::Inherit method. The following graph illustrates
2192 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002194 * \code
2195 * FunctionTemplate Parent -> Parent() . prototype -> { }
2196 * ^ ^
2197 * | Inherit(Parent) | .__proto__
2198 * | |
2199 * FunctionTemplate Child -> Child() . prototype -> { }
2200 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002202 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2203 * object of the Child() function has __proto__ pointing to the
2204 * Parent() function's prototype object. An instance of the Child
2205 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002207 * Let Parent be the FunctionTemplate initialized in the previous
2208 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002210 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 * Local<FunctionTemplate> parent = t;
2212 * Local<FunctionTemplate> child = FunctionTemplate::New();
2213 * child->Inherit(parent);
2214 *
2215 * Local<Function> child_function = child->GetFunction();
2216 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002217 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002219 * The Child function and Child instance will have the following
2220 * properties:
2221 *
2222 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002224 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002226 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002228class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 public:
2230 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002231 static Local<FunctionTemplate> New(
2232 InvocationCallback callback = 0,
2233 Handle<Value> data = Handle<Value>(),
2234 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 /** Returns the unique function instance in the current execution context.*/
2236 Local<Function> GetFunction();
2237
v8.team.kasperl727e9952008-09-02 14:56:44 +00002238 /**
2239 * Set the call-handler callback for a FunctionTemplate. This
2240 * callback is called whenever the function created from this
2241 * FunctionTemplate is called.
2242 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 void SetCallHandler(InvocationCallback callback,
2244 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245
v8.team.kasperl727e9952008-09-02 14:56:44 +00002246 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 Local<ObjectTemplate> InstanceTemplate();
2248
2249 /** Causes the function template to inherit from a parent function template.*/
2250 void Inherit(Handle<FunctionTemplate> parent);
2251
2252 /**
2253 * A PrototypeTemplate is the template used to create the prototype object
2254 * of the function created by this template.
2255 */
2256 Local<ObjectTemplate> PrototypeTemplate();
2257
v8.team.kasperl727e9952008-09-02 14:56:44 +00002258
2259 /**
2260 * Set the class name of the FunctionTemplate. This is used for
2261 * printing objects created with the function created from the
2262 * FunctionTemplate as its constructor.
2263 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 void SetClassName(Handle<String> name);
2265
2266 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002267 * Determines whether the __proto__ accessor ignores instances of
2268 * the function template. If instances of the function template are
2269 * ignored, __proto__ skips all instances and instead returns the
2270 * next object in the prototype chain.
2271 *
2272 * Call with a value of true to make the __proto__ accessor ignore
2273 * instances of the function template. Call with a value of false
2274 * to make the __proto__ accessor not ignore instances of the
2275 * function template. By default, instances of a function template
2276 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 */
2278 void SetHiddenPrototype(bool value);
2279
2280 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002281 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2282 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002283 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002284 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002285
2286 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002287 * Returns true if the given object is an instance of this function
2288 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 */
2290 bool HasInstance(Handle<Value> object);
2291
2292 private:
2293 FunctionTemplate();
2294 void AddInstancePropertyAccessor(Handle<String> name,
2295 AccessorGetter getter,
2296 AccessorSetter setter,
2297 Handle<Value> data,
2298 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002299 PropertyAttribute attributes,
2300 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2302 NamedPropertySetter setter,
2303 NamedPropertyQuery query,
2304 NamedPropertyDeleter remover,
2305 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002306 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002307 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2308 IndexedPropertySetter setter,
2309 IndexedPropertyQuery query,
2310 IndexedPropertyDeleter remover,
2311 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002312 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2314 Handle<Value> data);
2315
2316 friend class Context;
2317 friend class ObjectTemplate;
2318};
2319
2320
2321/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002322 * An ObjectTemplate is used to create objects at runtime.
2323 *
2324 * Properties added to an ObjectTemplate are added to each object
2325 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002327class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002329 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332 /** Creates a new instance of this template.*/
2333 Local<Object> NewInstance();
2334
2335 /**
2336 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002337 *
2338 * Whenever the property with the given name is accessed on objects
2339 * created from this ObjectTemplate the getter and setter callbacks
2340 * are called instead of getting and setting the property directly
2341 * on the JavaScript object.
2342 *
2343 * \param name The name of the property for which an accessor is added.
2344 * \param getter The callback to invoke when getting the property.
2345 * \param setter The callback to invoke when setting the property.
2346 * \param data A piece of data that will be passed to the getter and setter
2347 * callbacks whenever they are invoked.
2348 * \param settings Access control settings for the accessor. This is a bit
2349 * field consisting of one of more of
2350 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2351 * The default is to not allow cross-context access.
2352 * ALL_CAN_READ means that all cross-context reads are allowed.
2353 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2354 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2355 * cross-context access.
2356 * \param attribute The attributes of the property for which an accessor
2357 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002358 * \param signature The signature describes valid receivers for the accessor
2359 * and is used to perform implicit instance checks against them. If the
2360 * receiver is incompatible (i.e. is not an instance of the constructor as
2361 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2362 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002363 */
2364 void SetAccessor(Handle<String> name,
2365 AccessorGetter getter,
2366 AccessorSetter setter = 0,
2367 Handle<Value> data = Handle<Value>(),
2368 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002369 PropertyAttribute attribute = None,
2370 Handle<AccessorSignature> signature =
2371 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372
2373 /**
2374 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002375 *
2376 * Whenever a named property is accessed on objects created from
2377 * this object template, the provided callback is invoked instead of
2378 * accessing the property directly on the JavaScript object.
2379 *
2380 * \param getter The callback to invoke when getting a property.
2381 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002382 * \param query The callback to invoke to check if a property is present,
2383 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002384 * \param deleter The callback to invoke when deleting a property.
2385 * \param enumerator The callback to invoke to enumerate all the named
2386 * properties of an object.
2387 * \param data A piece of data that will be passed to the callbacks
2388 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389 */
2390 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2391 NamedPropertySetter setter = 0,
2392 NamedPropertyQuery query = 0,
2393 NamedPropertyDeleter deleter = 0,
2394 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002395 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396
2397 /**
2398 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002399 *
2400 * Whenever an indexed property is accessed on objects created from
2401 * this object template, the provided callback is invoked instead of
2402 * accessing the property directly on the JavaScript object.
2403 *
2404 * \param getter The callback to invoke when getting a property.
2405 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002406 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002407 * \param deleter The callback to invoke when deleting a property.
2408 * \param enumerator The callback to invoke to enumerate all the indexed
2409 * properties of an object.
2410 * \param data A piece of data that will be passed to the callbacks
2411 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002412 */
2413 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2414 IndexedPropertySetter setter = 0,
2415 IndexedPropertyQuery query = 0,
2416 IndexedPropertyDeleter deleter = 0,
2417 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002418 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002419
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420 /**
2421 * Sets the callback to be used when calling instances created from
2422 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002423 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424 * function.
2425 */
2426 void SetCallAsFunctionHandler(InvocationCallback callback,
2427 Handle<Value> data = Handle<Value>());
2428
v8.team.kasperl727e9952008-09-02 14:56:44 +00002429 /**
2430 * Mark object instances of the template as undetectable.
2431 *
2432 * In many ways, undetectable objects behave as though they are not
2433 * there. They behave like 'undefined' in conditionals and when
2434 * printed. However, properties can be accessed and called as on
2435 * normal objects.
2436 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437 void MarkAsUndetectable();
2438
v8.team.kasperl727e9952008-09-02 14:56:44 +00002439 /**
2440 * Sets access check callbacks on the object template.
2441 *
2442 * When accessing properties on instances of this object template,
2443 * the access check callback will be called to determine whether or
2444 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002445 * The last parameter specifies whether access checks are turned
2446 * on by default on instances. If access checks are off by default,
2447 * they can be turned on on individual instances by calling
2448 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002449 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2451 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002452 Handle<Value> data = Handle<Value>(),
2453 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002454
kasper.lund212ac232008-07-16 07:07:30 +00002455 /**
2456 * Gets the number of internal fields for objects generated from
2457 * this template.
2458 */
2459 int InternalFieldCount();
2460
2461 /**
2462 * Sets the number of internal fields for objects generated from
2463 * this template.
2464 */
2465 void SetInternalFieldCount(int value);
2466
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 private:
2468 ObjectTemplate();
2469 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2470 friend class FunctionTemplate;
2471};
2472
2473
2474/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002475 * A Signature specifies which receivers and arguments are valid
2476 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002478class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479 public:
2480 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2481 Handle<FunctionTemplate>(),
2482 int argc = 0,
2483 Handle<FunctionTemplate> argv[] = 0);
2484 private:
2485 Signature();
2486};
2487
2488
2489/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002490 * An AccessorSignature specifies which receivers are valid parameters
2491 * to an accessor callback.
2492 */
2493class V8EXPORT AccessorSignature : public Data {
2494 public:
2495 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2496 Handle<FunctionTemplate>());
2497 private:
2498 AccessorSignature();
2499};
2500
2501
2502/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002503 * A utility for determining the type of objects based on the template
2504 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002506class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507 public:
2508 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2509 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2510 int match(Handle<Value> value);
2511 private:
2512 TypeSwitch();
2513};
2514
2515
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002516// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002518class V8EXPORT ExternalAsciiStringResourceImpl
2519 : public String::ExternalAsciiStringResource {
2520 public:
2521 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2522 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2523 : data_(data), length_(length) {}
2524 const char* data() const { return data_; }
2525 size_t length() const { return length_; }
2526
2527 private:
2528 const char* data_;
2529 size_t length_;
2530};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531
2532/**
2533 * Ignore
2534 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002535class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002536 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002537 // Note that the strings passed into this constructor must live as long
2538 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002539 Extension(const char* name,
2540 const char* source = 0,
2541 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002542 const char** deps = 0,
2543 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002544 virtual ~Extension() { }
2545 virtual v8::Handle<v8::FunctionTemplate>
2546 GetNativeFunction(v8::Handle<v8::String> name) {
2547 return v8::Handle<v8::FunctionTemplate>();
2548 }
2549
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002550 const char* name() const { return name_; }
2551 size_t source_length() const { return source_length_; }
2552 const String::ExternalAsciiStringResource* source() const {
2553 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002554 int dependency_count() { return dep_count_; }
2555 const char** dependencies() { return deps_; }
2556 void set_auto_enable(bool value) { auto_enable_ = value; }
2557 bool auto_enable() { return auto_enable_; }
2558
2559 private:
2560 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002561 size_t source_length_; // expected to initialize before source_
2562 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 int dep_count_;
2564 const char** deps_;
2565 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002566
2567 // Disallow copying and assigning.
2568 Extension(const Extension&);
2569 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570};
2571
2572
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002573void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574
2575
2576/**
2577 * Ignore
2578 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002579class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580 public:
2581 inline DeclareExtension(Extension* extension) {
2582 RegisterExtension(extension);
2583 }
2584};
2585
2586
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002587// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588
2589
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002590Handle<Primitive> V8EXPORT Undefined();
2591Handle<Primitive> V8EXPORT Null();
2592Handle<Boolean> V8EXPORT True();
2593Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002595inline Handle<Primitive> Undefined(Isolate* isolate);
2596inline Handle<Primitive> Null(Isolate* isolate);
2597inline Handle<Boolean> True(Isolate* isolate);
2598inline Handle<Boolean> False(Isolate* isolate);
2599
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600
2601/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002602 * A set of constraints that specifies the limits of the runtime's memory use.
2603 * You must set the heap size before initializing the VM - the size cannot be
2604 * adjusted after the VM is initialized.
2605 *
2606 * If you are using threads then you should hold the V8::Locker lock while
2607 * setting the stack limit and you must set a non-default stack limit separately
2608 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002609 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002610class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611 public:
2612 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002613 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002615 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002617 int max_executable_size() { return max_executable_size_; }
2618 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002619 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002620 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2622 private:
2623 int max_young_space_size_;
2624 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002625 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626 uint32_t* stack_limit_;
2627};
2628
2629
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002630bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631
2632
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002633// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634
2635
2636typedef void (*FatalErrorCallback)(const char* location, const char* message);
2637
2638
2639typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2640
2641
2642/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002643 * Schedules an exception to be thrown when returning to JavaScript. When an
2644 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002646 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002648Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649
2650/**
2651 * Create new error objects by calling the corresponding error object
2652 * constructor with the message.
2653 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002654class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655 public:
2656 static Local<Value> RangeError(Handle<String> message);
2657 static Local<Value> ReferenceError(Handle<String> message);
2658 static Local<Value> SyntaxError(Handle<String> message);
2659 static Local<Value> TypeError(Handle<String> message);
2660 static Local<Value> Error(Handle<String> message);
2661};
2662
2663
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002664// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002666typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002668typedef void* (*CreateHistogramCallback)(const char* name,
2669 int min,
2670 int max,
2671 size_t buckets);
2672
2673typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2674
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002675// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002676 enum ObjectSpace {
2677 kObjectSpaceNewSpace = 1 << 0,
2678 kObjectSpaceOldPointerSpace = 1 << 1,
2679 kObjectSpaceOldDataSpace = 1 << 2,
2680 kObjectSpaceCodeSpace = 1 << 3,
2681 kObjectSpaceMapSpace = 1 << 4,
2682 kObjectSpaceLoSpace = 1 << 5,
2683
2684 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2685 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2686 kObjectSpaceLoSpace
2687 };
2688
2689 enum AllocationAction {
2690 kAllocationActionAllocate = 1 << 0,
2691 kAllocationActionFree = 1 << 1,
2692 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2693 };
2694
2695typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2696 AllocationAction action,
2697 int size);
2698
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002699// --- Leave Script Callback ---
2700typedef void (*CallCompletedCallback)();
2701
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002702// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2704 AccessType type,
2705 Local<Value> data);
2706
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002707// --- AllowCodeGenerationFromStrings callbacks ---
2708
2709/**
2710 * Callback to check if code generation from strings is allowed. See
2711 * Context::AllowCodeGenerationFromStrings.
2712 */
2713typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2714
2715// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716
2717/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002718 * Applications can register callback functions which will be called
2719 * before and after a garbage collection. Allocations are not
2720 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002721 * objects (set or delete properties for example) since it is possible
2722 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002724enum GCType {
2725 kGCTypeScavenge = 1 << 0,
2726 kGCTypeMarkSweepCompact = 1 << 1,
2727 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2728};
2729
2730enum GCCallbackFlags {
2731 kNoGCCallbackFlags = 0,
2732 kGCCallbackFlagCompacted = 1 << 0
2733};
2734
2735typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2736typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2737
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738typedef void (*GCCallback)();
2739
2740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002742 * Collection of V8 heap information.
2743 *
2744 * Instances of this class can be passed to v8::V8::HeapStatistics to
2745 * get heap statistics from V8.
2746 */
2747class V8EXPORT HeapStatistics {
2748 public:
2749 HeapStatistics();
2750 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002751 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002752 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002753 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002754
2755 private:
2756 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002757 void set_total_heap_size_executable(size_t size) {
2758 total_heap_size_executable_ = size;
2759 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002760 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002761 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002762
2763 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002764 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002765 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002766 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002767
2768 friend class V8;
2769};
2770
2771
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002772class RetainedObjectInfo;
2773
ager@chromium.org3811b432009-10-28 14:53:37 +00002774/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002775 * Isolate represents an isolated instance of the V8 engine. V8
2776 * isolates have completely separate states. Objects from one isolate
2777 * must not be used in other isolates. When V8 is initialized a
2778 * default isolate is implicitly created and entered. The embedder
2779 * can create additional isolates and use them in parallel in multiple
2780 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002781 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002782 */
2783class V8EXPORT Isolate {
2784 public:
2785 /**
2786 * Stack-allocated class which sets the isolate for all operations
2787 * executed within a local scope.
2788 */
2789 class V8EXPORT Scope {
2790 public:
2791 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2792 isolate->Enter();
2793 }
2794
2795 ~Scope() { isolate_->Exit(); }
2796
2797 private:
2798 Isolate* const isolate_;
2799
2800 // Prevent copying of Scope objects.
2801 Scope(const Scope&);
2802 Scope& operator=(const Scope&);
2803 };
2804
2805 /**
2806 * Creates a new isolate. Does not change the currently entered
2807 * isolate.
2808 *
2809 * When an isolate is no longer used its resources should be freed
2810 * by calling Dispose(). Using the delete operator is not allowed.
2811 */
2812 static Isolate* New();
2813
2814 /**
2815 * Returns the entered isolate for the current thread or NULL in
2816 * case there is no current isolate.
2817 */
2818 static Isolate* GetCurrent();
2819
2820 /**
2821 * Methods below this point require holding a lock (using Locker) in
2822 * a multi-threaded environment.
2823 */
2824
2825 /**
2826 * Sets this isolate as the entered one for the current thread.
2827 * Saves the previously entered one (if any), so that it can be
2828 * restored when exiting. Re-entering an isolate is allowed.
2829 */
2830 void Enter();
2831
2832 /**
2833 * Exits this isolate by restoring the previously entered one in the
2834 * current thread. The isolate may still stay the same, if it was
2835 * entered more than once.
2836 *
2837 * Requires: this == Isolate::GetCurrent().
2838 */
2839 void Exit();
2840
2841 /**
2842 * Disposes the isolate. The isolate must not be entered by any
2843 * thread to be disposable.
2844 */
2845 void Dispose();
2846
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002847 /**
2848 * Associate embedder-specific data with the isolate
2849 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002850 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002851
2852 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002853 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002854 * Returns NULL if SetData has never been called.
2855 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002856 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002857
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002858 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002859 Isolate();
2860 Isolate(const Isolate&);
2861 ~Isolate();
2862 Isolate& operator=(const Isolate&);
2863 void* operator new(size_t size);
2864 void operator delete(void*, size_t);
2865};
2866
2867
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002868class StartupData {
2869 public:
2870 enum CompressionAlgorithm {
2871 kUncompressed,
2872 kBZip2
2873 };
2874
2875 const char* data;
2876 int compressed_size;
2877 int raw_size;
2878};
2879
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002880
2881/**
2882 * A helper class for driving V8 startup data decompression. It is based on
2883 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2884 * for an embedder to use this class, instead, API functions can be used
2885 * directly.
2886 *
2887 * For an example of the class usage, see the "shell.cc" sample application.
2888 */
2889class V8EXPORT StartupDataDecompressor { // NOLINT
2890 public:
2891 StartupDataDecompressor();
2892 virtual ~StartupDataDecompressor();
2893 int Decompress();
2894
2895 protected:
2896 virtual int DecompressData(char* raw_data,
2897 int* raw_data_size,
2898 const char* compressed_data,
2899 int compressed_data_size) = 0;
2900
2901 private:
2902 char** raw_data;
2903};
2904
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002905
2906/**
2907 * EntropySource is used as a callback function when v8 needs a source
2908 * of entropy.
2909 */
2910typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2911
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002912
2913/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002914 * ReturnAddressLocationResolver is used as a callback function when v8 is
2915 * resolving the location of a return address on the stack. Profilers that
2916 * change the return address on the stack can use this to resolve the stack
2917 * location to whereever the profiler stashed the original return address.
2918 * When invoked, return_addr_location will point to a location on stack where
2919 * a machine return address resides, this function should return either the
2920 * same pointer, or a pointer to the profiler's copy of the original return
2921 * address.
2922 */
2923typedef uintptr_t (*ReturnAddressLocationResolver)(
2924 uintptr_t return_addr_location);
2925
2926
2927/**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002928 * Interface for iterating though all external resources in the heap.
2929 */
2930class V8EXPORT ExternalResourceVisitor { // NOLINT
2931 public:
2932 virtual ~ExternalResourceVisitor() {}
2933 virtual void VisitExternalString(Handle<String> string) {}
2934};
2935
2936
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002937/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002938 * Container class for static utility functions.
2939 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002940class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002942 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002943 static void SetFatalErrorHandler(FatalErrorCallback that);
2944
v8.team.kasperl727e9952008-09-02 14:56:44 +00002945 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002946 * Set the callback to invoke to check if code generation from
2947 * strings should be allowed.
2948 */
2949 static void SetAllowCodeGenerationFromStringsCallback(
2950 AllowCodeGenerationFromStringsCallback that);
2951
2952 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002953 * Ignore out-of-memory exceptions.
2954 *
2955 * V8 running out of memory is treated as a fatal error by default.
2956 * This means that the fatal error handler is called and that V8 is
2957 * terminated.
2958 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002959 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002960 * out-of-memory situation as a fatal error. This way, the contexts
2961 * that did not cause the out of memory problem might be able to
2962 * continue execution.
2963 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964 static void IgnoreOutOfMemoryException();
2965
v8.team.kasperl727e9952008-09-02 14:56:44 +00002966 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002967 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002968 * fatal errors such as out-of-memory situations.
2969 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970 static bool IsDead();
2971
2972 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002973 * The following 4 functions are to be used when V8 is built with
2974 * the 'compress_startup_data' flag enabled. In this case, the
2975 * embedder must decompress startup data prior to initializing V8.
2976 *
2977 * This is how interaction with V8 should look like:
2978 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2979 * v8::StartupData* compressed_data =
2980 * new v8::StartupData[compressed_data_count];
2981 * v8::V8::GetCompressedStartupData(compressed_data);
2982 * ... decompress data (compressed_data can be updated in-place) ...
2983 * v8::V8::SetDecompressedStartupData(compressed_data);
2984 * ... now V8 can be initialized
2985 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002986 *
2987 * A helper class StartupDataDecompressor is provided. It implements
2988 * the protocol of the interaction described above, and can be used in
2989 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002990 */
2991 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2992 static int GetCompressedStartupDataCount();
2993 static void GetCompressedStartupData(StartupData* compressed_data);
2994 static void SetDecompressedStartupData(StartupData* decompressed_data);
2995
2996 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002997 * Adds a message listener.
2998 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002999 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003000 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003001 */
3002 static bool AddMessageListener(MessageCallback that,
3003 Handle<Value> data = Handle<Value>());
3004
3005 /**
3006 * Remove all message listeners from the specified callback function.
3007 */
3008 static void RemoveMessageListeners(MessageCallback that);
3009
3010 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003011 * Tells V8 to capture current stack trace when uncaught exception occurs
3012 * and report it to the message listeners. The option is off by default.
3013 */
3014 static void SetCaptureStackTraceForUncaughtExceptions(
3015 bool capture,
3016 int frame_limit = 10,
3017 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3018
3019 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003020 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021 */
3022 static void SetFlagsFromString(const char* str, int length);
3023
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003024 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003025 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003026 */
3027 static void SetFlagsFromCommandLine(int* argc,
3028 char** argv,
3029 bool remove_flags);
3030
kasper.lund7276f142008-07-30 08:49:36 +00003031 /** Get the version string. */
3032 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003033
3034 /**
3035 * Enables the host application to provide a mechanism for recording
3036 * statistics counters.
3037 */
3038 static void SetCounterFunction(CounterLookupCallback);
3039
3040 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003041 * Enables the host application to provide a mechanism for recording
3042 * histograms. The CreateHistogram function returns a
3043 * histogram which will later be passed to the AddHistogramSample
3044 * function.
3045 */
3046 static void SetCreateHistogramFunction(CreateHistogramCallback);
3047 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3048
3049 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050 * Enables the computation of a sliding window of states. The sliding
3051 * window information is recorded in statistics counters.
3052 */
3053 static void EnableSlidingStateWindow();
3054
3055 /** Callback function for reporting failed access checks.*/
3056 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3057
3058 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003059 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003060 * garbage collection. Allocations are not allowed in the
3061 * callback function, you therefore cannot manipulate objects (set
3062 * or delete properties for example) since it is possible such
3063 * operations will result in the allocation of objects. It is possible
3064 * to specify the GCType filter for your callback. But it is not possible to
3065 * register the same callback function two times with different
3066 * GCType filters.
3067 */
3068 static void AddGCPrologueCallback(
3069 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3070
3071 /**
3072 * This function removes callback which was installed by
3073 * AddGCPrologueCallback function.
3074 */
3075 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3076
3077 /**
3078 * The function is deprecated. Please use AddGCPrologueCallback instead.
3079 * Enables the host application to receive a notification before a
3080 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003081 * callback function, you therefore cannot manipulate objects (set
3082 * or delete properties for example) since it is possible such
3083 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084 */
3085 static void SetGlobalGCPrologueCallback(GCCallback);
3086
3087 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003088 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003089 * garbage collection. Allocations are not allowed in the
3090 * callback function, you therefore cannot manipulate objects (set
3091 * or delete properties for example) since it is possible such
3092 * operations will result in the allocation of objects. It is possible
3093 * to specify the GCType filter for your callback. But it is not possible to
3094 * register the same callback function two times with different
3095 * GCType filters.
3096 */
3097 static void AddGCEpilogueCallback(
3098 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3099
3100 /**
3101 * This function removes callback which was installed by
3102 * AddGCEpilogueCallback function.
3103 */
3104 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3105
3106 /**
3107 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3108 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003109 * major garbage collection. Allocations are not allowed in the
3110 * callback function, you therefore cannot manipulate objects (set
3111 * or delete properties for example) since it is possible such
3112 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003113 */
3114 static void SetGlobalGCEpilogueCallback(GCCallback);
3115
3116 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003117 * Enables the host application to provide a mechanism to be notified
3118 * and perform custom logging when V8 Allocates Executable Memory.
3119 */
3120 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3121 ObjectSpace space,
3122 AllocationAction action);
3123
3124 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003125 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003126 */
3127 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3128
3129 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003130 * Adds a callback to notify the host application when a script finished
3131 * running. If a script re-enters the runtime during executing, the
3132 * CallCompletedCallback is only invoked when the outer-most script
3133 * execution ends. Executing scripts inside the callback do not trigger
3134 * further callbacks.
3135 */
3136 static void AddCallCompletedCallback(CallCompletedCallback callback);
3137
3138 /**
3139 * Removes callback that was installed by AddCallCompletedCallback.
3140 */
3141 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3142
3143 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003144 * Allows the host application to group objects together. If one
3145 * object in the group is alive, all objects in the group are alive.
3146 * After each garbage collection, object groups are removed. It is
3147 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003148 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00003149 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003150 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003151 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003152 static void AddObjectGroup(Persistent<Value>* objects,
3153 size_t length,
3154 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003155
3156 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003157 * Allows the host application to declare implicit references between
3158 * the objects: if |parent| is alive, all |children| are alive too.
3159 * After each garbage collection, all implicit references
3160 * are removed. It is intended to be used in the before-garbage-collection
3161 * callback function.
3162 */
3163 static void AddImplicitReferences(Persistent<Object> parent,
3164 Persistent<Value>* children,
3165 size_t length);
3166
3167 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003168 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003169 * initialize from scratch. This function is called implicitly if
3170 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003171 */
3172 static bool Initialize();
3173
kasper.lund7276f142008-07-30 08:49:36 +00003174 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003175 * Allows the host application to provide a callback which can be used
3176 * as a source of entropy for random number generators.
3177 */
3178 static void SetEntropySource(EntropySource source);
3179
3180 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003181 * Allows the host application to provide a callback that allows v8 to
3182 * cooperate with a profiler that rewrites return addresses on stack.
3183 */
3184 static void SetReturnAddressLocationResolver(
3185 ReturnAddressLocationResolver return_address_resolver);
3186
3187 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003188 * Adjusts the amount of registered external memory. Used to give
3189 * V8 an indication of the amount of externally allocated memory
3190 * that is kept alive by JavaScript objects. V8 uses this to decide
3191 * when to perform global garbage collections. Registering
3192 * externally allocated memory will trigger global garbage
3193 * collections more often than otherwise in an attempt to garbage
3194 * collect the JavaScript objects keeping the externally allocated
3195 * memory alive.
3196 *
3197 * \param change_in_bytes the change in externally allocated memory
3198 * that is kept alive by JavaScript objects.
3199 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003200 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003201 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3202 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003203
iposva@chromium.org245aa852009-02-10 00:49:54 +00003204 /**
3205 * Suspends recording of tick samples in the profiler.
3206 * When the V8 profiling mode is enabled (usually via command line
3207 * switches) this function suspends recording of tick samples.
3208 * Profiling ticks are discarded until ResumeProfiler() is called.
3209 *
3210 * See also the --prof and --prof_auto command line switches to
3211 * enable V8 profiling.
3212 */
3213 static void PauseProfiler();
3214
3215 /**
3216 * Resumes recording of tick samples in the profiler.
3217 * See also PauseProfiler().
3218 */
3219 static void ResumeProfiler();
3220
ager@chromium.org41826e72009-03-30 13:30:57 +00003221 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003222 * Return whether profiler is currently paused.
3223 */
3224 static bool IsProfilerPaused();
3225
3226 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003227 * Retrieve the V8 thread id of the calling thread.
3228 *
3229 * The thread id for a thread should only be retrieved after the V8
3230 * lock has been acquired with a Locker object with that thread.
3231 */
3232 static int GetCurrentThreadId();
3233
3234 /**
3235 * Forcefully terminate execution of a JavaScript thread. This can
3236 * be used to terminate long-running scripts.
3237 *
3238 * TerminateExecution should only be called when then V8 lock has
3239 * been acquired with a Locker object. Therefore, in order to be
3240 * able to terminate long-running threads, preemption must be
3241 * enabled to allow the user of TerminateExecution to acquire the
3242 * lock.
3243 *
3244 * The termination is achieved by throwing an exception that is
3245 * uncatchable by JavaScript exception handlers. Termination
3246 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003247 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003248 * exception handler that catches an exception should check if that
3249 * exception is a termination exception and immediately return if
3250 * that is the case. Returning immediately in that case will
3251 * continue the propagation of the termination exception if needed.
3252 *
3253 * The thread id passed to TerminateExecution must have been
3254 * obtained by calling GetCurrentThreadId on the thread in question.
3255 *
3256 * \param thread_id The thread id of the thread to terminate.
3257 */
3258 static void TerminateExecution(int thread_id);
3259
3260 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003261 * Forcefully terminate the current thread of JavaScript execution
3262 * in the given isolate. If no isolate is provided, the default
3263 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003264 *
3265 * This method can be used by any thread even if that thread has not
3266 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003267 *
3268 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003269 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003270 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003271
3272 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003273 * Is V8 terminating JavaScript execution.
3274 *
3275 * Returns true if JavaScript execution is currently terminating
3276 * because of a call to TerminateExecution. In that case there are
3277 * still JavaScript frames on the stack and the termination
3278 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003279 *
3280 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003281 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003282 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003283
3284 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003285 * Releases any resources used by v8 and stops any utility threads
3286 * that may be running. Note that disposing v8 is permanent, it
3287 * cannot be reinitialized.
3288 *
3289 * It should generally not be necessary to dispose v8 before exiting
3290 * a process, this should happen automatically. It is only necessary
3291 * to use if the process needs the resources taken up by v8.
3292 */
3293 static bool Dispose();
3294
ager@chromium.org3811b432009-10-28 14:53:37 +00003295 /**
3296 * Get statistics about the heap memory usage.
3297 */
3298 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003299
3300 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003301 * Iterates through all external resources referenced from current isolate
3302 * heap. This method is not expected to be used except for debugging purposes
3303 * and may be quite slow.
3304 */
3305 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3306
3307 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003308 * Optional notification that the embedder is idle.
3309 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003310 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003311 * Returns true if the embedder should stop calling IdleNotification
3312 * until real work has been done. This indicates that V8 has done
3313 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003314 *
3315 * The hint argument specifies the amount of work to be done in the function
3316 * on scale from 1 to 1000. There is no guarantee that the actual work will
3317 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003318 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003319 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003320
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003321 /**
3322 * Optional notification that the system is running low on memory.
3323 * V8 uses these notifications to attempt to free memory.
3324 */
3325 static void LowMemoryNotification();
3326
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003327 /**
3328 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003329 * these notifications to guide the GC heuristic. Returns the number
3330 * of context disposals - including this one - since the last time
3331 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003332 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003333 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003334
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003335 private:
3336 V8();
3337
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003338 static internal::Object** GlobalizeReference(internal::Object** handle);
3339 static void DisposeGlobal(internal::Object** global_handle);
3340 static void MakeWeak(internal::Object** global_handle,
3341 void* data,
3342 WeakReferenceCallback);
3343 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003344 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003345 static bool IsGlobalNearDeath(internal::Object** global_handle);
3346 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003347 static void SetWrapperClassId(internal::Object** global_handle,
3348 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003349
3350 template <class T> friend class Handle;
3351 template <class T> friend class Local;
3352 template <class T> friend class Persistent;
3353 friend class Context;
3354};
3355
3356
3357/**
3358 * An external exception handler.
3359 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003360class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003361 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003362 /**
3363 * Creates a new try/catch block and registers it with v8.
3364 */
3365 TryCatch();
3366
3367 /**
3368 * Unregisters and deletes this try/catch block.
3369 */
3370 ~TryCatch();
3371
3372 /**
3373 * Returns true if an exception has been caught by this try/catch block.
3374 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003375 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003376
3377 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003378 * For certain types of exceptions, it makes no sense to continue
3379 * execution.
3380 *
3381 * Currently, the only type of exception that can be caught by a
3382 * TryCatch handler and for which it does not make sense to continue
3383 * is termination exception. Such exceptions are thrown when the
3384 * TerminateExecution methods are called to terminate a long-running
3385 * script.
3386 *
3387 * If CanContinue returns false, the correct action is to perform
3388 * any C++ cleanup needed and then return.
3389 */
3390 bool CanContinue() const;
3391
3392 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003393 * Throws the exception caught by this TryCatch in a way that avoids
3394 * it being caught again by this same TryCatch. As with ThrowException
3395 * it is illegal to execute any JavaScript operations after calling
3396 * ReThrow; the caller must return immediately to where the exception
3397 * is caught.
3398 */
3399 Handle<Value> ReThrow();
3400
3401 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003402 * Returns the exception caught by this try/catch block. If no exception has
3403 * been caught an empty handle is returned.
3404 *
3405 * The returned handle is valid until this TryCatch block has been destroyed.
3406 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003407 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003408
3409 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003410 * Returns the .stack property of the thrown object. If no .stack
3411 * property is present an empty handle is returned.
3412 */
3413 Local<Value> StackTrace() const;
3414
3415 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003416 * Returns the message associated with this exception. If there is
3417 * no message associated an empty handle is returned.
3418 *
3419 * The returned handle is valid until this TryCatch block has been
3420 * destroyed.
3421 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003422 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003423
3424 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003425 * Clears any exceptions that may have been caught by this try/catch block.
3426 * After this method has been called, HasCaught() will return false.
3427 *
3428 * It is not necessary to clear a try/catch block before using it again; if
3429 * another exception is thrown the previously caught exception will just be
3430 * overwritten. However, it is often a good idea since it makes it easier
3431 * to determine which operation threw a given exception.
3432 */
3433 void Reset();
3434
v8.team.kasperl727e9952008-09-02 14:56:44 +00003435 /**
3436 * Set verbosity of the external exception handler.
3437 *
3438 * By default, exceptions that are caught by an external exception
3439 * handler are not reported. Call SetVerbose with true on an
3440 * external exception handler to have exceptions caught by the
3441 * handler reported as if they were not caught.
3442 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443 void SetVerbose(bool value);
3444
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003445 /**
3446 * Set whether or not this TryCatch should capture a Message object
3447 * which holds source information about where the exception
3448 * occurred. True by default.
3449 */
3450 void SetCaptureMessage(bool value);
3451
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003452 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003453 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003454 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003455 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003456 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003457 bool is_verbose_ : 1;
3458 bool can_continue_ : 1;
3459 bool capture_message_ : 1;
3460 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003461
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003462 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003463};
3464
3465
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003466// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003467
3468
3469/**
3470 * Ignore
3471 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003472class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473 public:
3474 ExtensionConfiguration(int name_count, const char* names[])
3475 : name_count_(name_count), names_(names) { }
3476 private:
3477 friend class ImplementationUtilities;
3478 int name_count_;
3479 const char** names_;
3480};
3481
3482
3483/**
3484 * A sandboxed execution context with its own set of built-in objects
3485 * and functions.
3486 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003487class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003489 /**
3490 * Returns the global proxy object or global object itself for
3491 * detached contexts.
3492 *
3493 * Global proxy object is a thin wrapper whose prototype points to
3494 * actual context's global object with the properties like Object, etc.
3495 * This is done that way for security reasons (for more details see
3496 * https://wiki.mozilla.org/Gecko:SplitWindow).
3497 *
3498 * Please note that changes to global proxy object prototype most probably
3499 * would break VM---v8 expects only global object as a prototype of
3500 * global proxy object.
3501 *
3502 * If DetachGlobal() has been invoked, Global() would return actual global
3503 * object until global is reattached with ReattachGlobal().
3504 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003505 Local<Object> Global();
3506
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003507 /**
3508 * Detaches the global object from its context before
3509 * the global object can be reused to create a new context.
3510 */
3511 void DetachGlobal();
3512
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003513 /**
3514 * Reattaches a global object to a context. This can be used to
3515 * restore the connection between a global object and a context
3516 * after DetachGlobal has been called.
3517 *
3518 * \param global_object The global object to reattach to the
3519 * context. For this to work, the global object must be the global
3520 * object that was associated with this context before a call to
3521 * DetachGlobal.
3522 */
3523 void ReattachGlobal(Handle<Object> global_object);
3524
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003525 /** Creates a new context.
3526 *
3527 * Returns a persistent handle to the newly allocated context. This
3528 * persistent handle has to be disposed when the context is no
3529 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003530 *
3531 * \param extensions An optional extension configuration containing
3532 * the extensions to be installed in the newly created context.
3533 *
3534 * \param global_template An optional object template from which the
3535 * global object for the newly created context will be created.
3536 *
3537 * \param global_object An optional global object to be reused for
3538 * the newly created context. This global object must have been
3539 * created by a previous call to Context::New with the same global
3540 * template. The state of the global object will be completely reset
3541 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003542 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003543 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003544 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003545 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3546 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003547
kasper.lund44510672008-07-25 07:37:58 +00003548 /** Returns the last entered context. */
3549 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003550
kasper.lund44510672008-07-25 07:37:58 +00003551 /** Returns the context that is on the top of the stack. */
3552 static Local<Context> GetCurrent();
3553
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003554 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003555 * Returns the context of the calling JavaScript code. That is the
3556 * context of the top-most JavaScript frame. If there are no
3557 * JavaScript frames an empty handle is returned.
3558 */
3559 static Local<Context> GetCalling();
3560
3561 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003562 * Sets the security token for the context. To access an object in
3563 * another context, the security tokens must match.
3564 */
3565 void SetSecurityToken(Handle<Value> token);
3566
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003567 /** Restores the security token to the default value. */
3568 void UseDefaultSecurityToken();
3569
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570 /** Returns the security token of this context.*/
3571 Handle<Value> GetSecurityToken();
3572
v8.team.kasperl727e9952008-09-02 14:56:44 +00003573 /**
3574 * Enter this context. After entering a context, all code compiled
3575 * and run is compiled and run in this context. If another context
3576 * is already entered, this old context is saved so it can be
3577 * restored when the new context is exited.
3578 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003579 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003580
3581 /**
3582 * Exit this context. Exiting the current context restores the
3583 * context that was in place when entering the current context.
3584 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003585 void Exit();
3586
v8.team.kasperl727e9952008-09-02 14:56:44 +00003587 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003588 bool HasOutOfMemoryException();
3589
v8.team.kasperl727e9952008-09-02 14:56:44 +00003590 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591 static bool InContext();
3592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003594 * Associate an additional data object with the context. This is mainly used
3595 * with the debugger to provide additional information on the context through
3596 * the debugger API.
3597 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003598 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003599 Local<Value> GetData();
3600
3601 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003602 * Control whether code generation from strings is allowed. Calling
3603 * this method with false will disable 'eval' and the 'Function'
3604 * constructor for code running in this context. If 'eval' or the
3605 * 'Function' constructor are used an exception will be thrown.
3606 *
3607 * If code generation from strings is not allowed the
3608 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3609 * set before blocking the call to 'eval' or the 'Function'
3610 * constructor. If that callback returns true, the call will be
3611 * allowed, otherwise an exception will be thrown. If no callback is
3612 * set an exception will be thrown.
3613 */
3614 void AllowCodeGenerationFromStrings(bool allow);
3615
3616 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003617 * Returns true if code generation from strings is allowed for the context.
3618 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3619 */
3620 bool IsCodeGenerationFromStringsAllowed();
3621
3622 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003623 * Stack-allocated class which sets the execution context for all
3624 * operations executed within a local scope.
3625 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003626 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003627 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003628 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003629 context_->Enter();
3630 }
3631 inline ~Scope() { context_->Exit(); }
3632 private:
3633 Handle<Context> context_;
3634 };
3635
3636 private:
3637 friend class Value;
3638 friend class Script;
3639 friend class Object;
3640 friend class Function;
3641};
3642
3643
3644/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003645 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003646 * is allowed to use any given V8 isolate. See Isolate class
3647 * comments. The definition of 'using V8 isolate' includes
3648 * accessing handles or holding onto object pointers obtained
3649 * from V8 handles while in the particular V8 isolate. It is up
3650 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003651 * constraint is not violated. In addition to any other synchronization
3652 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3653 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003655 * v8::Locker is a scoped lock object. While it's
3656 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003657 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3658 * locked by at most one thread at any time. In other words, the scope of a
3659 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003660 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003661 * Sample usage:
3662* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003663 * ...
3664 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003665 * v8::Locker locker(isolate);
3666 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003667 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003668 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003669 * ...
3670 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003671 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003672 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003673 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003674 * by destroying the v8::Locker object as above or by constructing a
3675 * v8::Unlocker object:
3676 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003677 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003678 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003679 * isolate->Exit();
3680 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003681 * ...
3682 * // Code not using V8 goes here while V8 can run in another thread.
3683 * ...
3684 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003685 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003686 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003687 *
3688 * The Unlocker object is intended for use in a long-running callback
3689 * from V8, where you want to release the V8 lock for other threads to
3690 * use.
3691 *
3692 * The v8::Locker is a recursive lock. That is, you can lock more than
3693 * once in a given thread. This can be useful if you have code that can
3694 * be called either from code that holds the lock or from code that does
3695 * not. The Unlocker is not recursive so you can not have several
3696 * Unlockers on the stack at once, and you can not use an Unlocker in a
3697 * thread that is not inside a Locker's scope.
3698 *
3699 * An unlocker will unlock several lockers if it has to and reinstate
3700 * the correct depth of locking on its destruction. eg.:
3701 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003702 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003703 * // V8 not locked.
3704 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003705 * v8::Locker locker(isolate);
3706 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707 * // V8 locked.
3708 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003709 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003710 * // V8 still locked (2 levels).
3711 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003712 * isolate->Exit();
3713 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003714 * // V8 not locked.
3715 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003716 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003717 * // V8 locked again (2 levels).
3718 * }
3719 * // V8 still locked (1 level).
3720 * }
3721 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003722 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003723 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003724 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003725 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003726class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003728 /**
3729 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3730 */
3731 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003732 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003733 private:
3734 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003735};
3736
3737
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003738class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003739 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003740 /**
3741 * Initialize Locker for a given Isolate. NULL means default isolate.
3742 */
3743 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003744 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003745
3746 /**
3747 * Start preemption.
3748 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003749 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003750 * that will switch between multiple threads that are in contention
3751 * for the V8 lock.
3752 */
3753 static void StartPreemption(int every_n_ms);
3754
3755 /**
3756 * Stop preemption.
3757 */
3758 static void StopPreemption();
3759
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003760 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003761 * Returns whether or not the locker for a given isolate, or default isolate
3762 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003763 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003764 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003765
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003766 /**
3767 * Returns whether v8::Locker is being used by this V8 instance.
3768 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003769 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003770
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003771 private:
3772 bool has_lock_;
3773 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003774 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003775
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003776 static bool active_;
3777
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003778 // Disallow copying and assigning.
3779 Locker(const Locker&);
3780 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003781};
3782
3783
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003784/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003785 * A struct for exporting HeapStats data from V8, using "push" model.
3786 */
3787struct HeapStatsUpdate;
3788
3789
3790/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003791 * An interface for exporting data from V8, using "push" model.
3792 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003793class V8EXPORT OutputStream { // NOLINT
3794 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003795 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003796 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003797 };
3798 enum WriteResult {
3799 kContinue = 0,
3800 kAbort = 1
3801 };
3802 virtual ~OutputStream() {}
3803 /** Notify about the end of stream. */
3804 virtual void EndOfStream() = 0;
3805 /** Get preferred output chunk size. Called only once. */
3806 virtual int GetChunkSize() { return 1024; }
3807 /** Get preferred output encoding. Called only once. */
3808 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3809 /**
3810 * Writes the next chunk of snapshot data into the stream. Writing
3811 * can be stopped by returning kAbort as function result. EndOfStream
3812 * will not be called in case writing was aborted.
3813 */
3814 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003815 /**
3816 * Writes the next chunk of heap stats data into the stream. Writing
3817 * can be stopped by returning kAbort as function result. EndOfStream
3818 * will not be called in case writing was aborted.
3819 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003820 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00003821 return kAbort;
3822 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003823};
3824
3825
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003826/**
3827 * An interface for reporting progress and controlling long-running
3828 * activities.
3829 */
3830class V8EXPORT ActivityControl { // NOLINT
3831 public:
3832 enum ControlOption {
3833 kContinue = 0,
3834 kAbort = 1
3835 };
3836 virtual ~ActivityControl() {}
3837 /**
3838 * Notify about current progress. The activity can be stopped by
3839 * returning kAbort as the callback result.
3840 */
3841 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3842};
3843
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003844
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003845// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003846
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003847
3848namespace internal {
3849
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003850const int kApiPointerSize = sizeof(void*); // NOLINT
3851const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003852
3853// Tag information for HeapObject.
3854const int kHeapObjectTag = 1;
3855const int kHeapObjectTagSize = 2;
3856const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3857
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003858// Tag information for Smi.
3859const int kSmiTag = 0;
3860const int kSmiTagSize = 1;
3861const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3862
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003863template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003864
3865// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003866template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003867 static const int kSmiShiftSize = 0;
3868 static const int kSmiValueSize = 31;
3869 static inline int SmiToInt(internal::Object* value) {
3870 int shift_bits = kSmiTagSize + kSmiShiftSize;
3871 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3872 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3873 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003874
3875 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3876 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003877 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003878 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003879};
3880
3881// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003882template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003883 static const int kSmiShiftSize = 31;
3884 static const int kSmiValueSize = 32;
3885 static inline int SmiToInt(internal::Object* value) {
3886 int shift_bits = kSmiTagSize + kSmiShiftSize;
3887 // Shift down and throw away top 32 bits.
3888 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3889 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003890
3891 // To maximize the range of pointers that can be encoded
3892 // in the available 32 bits, we require them to be 8 bytes aligned.
3893 // This gives 2 ^ (32 + 3) = 32G address space covered.
3894 // It might be not enough to cover stack allocated objects on some platforms.
3895 static const int kPointerAlignment = 3;
3896
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003897 static const uintptr_t kEncodablePointerMask =
3898 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003899
3900 static const int kPointerToSmiShift =
3901 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003902};
3903
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003904typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3905const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3906const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003907const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003908 PlatformSmiTagging::kEncodablePointerMask;
3909const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003910
3911/**
3912 * This class exports constants and functionality from within v8 that
3913 * is necessary to implement inline functions in the v8 api. Don't
3914 * depend on functions and constants defined here.
3915 */
3916class Internals {
3917 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003918 // These values match non-compiler-dependent values defined within
3919 // the implementation of v8.
3920 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003921 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003922 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003923
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003924 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003925 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003926 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003927 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003928 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003929
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003930 static const int kIsolateStateOffset = 0;
3931 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
3932 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
3933 static const int kUndefinedValueRootIndex = 5;
3934 static const int kNullValueRootIndex = 7;
3935 static const int kTrueValueRootIndex = 8;
3936 static const int kFalseValueRootIndex = 9;
3937 static const int kEmptySymbolRootIndex = 128;
3938
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003939 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003940 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003941 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003942 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003943
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003944 static const int kUndefinedOddballKind = 5;
3945 static const int kNullOddballKind = 3;
3946
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003947 static inline bool HasHeapObjectTag(internal::Object* value) {
3948 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3949 kHeapObjectTag);
3950 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003951
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003952 static inline bool HasSmiTag(internal::Object* value) {
3953 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3954 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003955
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003956 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003957 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003958 }
3959
3960 static inline int GetInstanceType(internal::Object* obj) {
3961 typedef internal::Object O;
3962 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3963 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3964 }
3965
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003966 static inline int GetOddballKind(internal::Object* obj) {
3967 typedef internal::Object O;
3968 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
3969 }
3970
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003971 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003972 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003973 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3974 }
3975
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003976 static inline void* GetExternalPointer(internal::Object* obj) {
3977 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003978 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003979 } else if (GetInstanceType(obj) == kForeignType) {
3980 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003981 } else {
3982 return NULL;
3983 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003984 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003985
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003986 static inline bool IsExternalTwoByteString(int instance_type) {
3987 int representation = (instance_type & kFullStringRepresentationMask);
3988 return representation == kExternalTwoByteRepresentationTag;
3989 }
3990
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003991 static inline bool IsInitialized(v8::Isolate* isolate) {
3992 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
3993 return *reinterpret_cast<int*>(addr) == 1;
3994 }
3995
3996 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
3997 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
3998 kIsolateEmbedderDataOffset;
3999 *reinterpret_cast<void**>(addr) = data;
4000 }
4001
4002 static inline void* GetEmbedderData(v8::Isolate* isolate) {
4003 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4004 kIsolateEmbedderDataOffset;
4005 return *reinterpret_cast<void**>(addr);
4006 }
4007
4008 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
4009 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4010 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4011 }
4012
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004013 template <typename T>
4014 static inline T ReadField(Object* ptr, int offset) {
4015 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4016 return *reinterpret_cast<T*>(addr);
4017 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004018
4019 static inline bool CanCastToHeapObject(void* o) { return false; }
4020 static inline bool CanCastToHeapObject(Context* o) { return true; }
4021 static inline bool CanCastToHeapObject(String* o) { return true; }
4022 static inline bool CanCastToHeapObject(Object* o) { return true; }
4023 static inline bool CanCastToHeapObject(Message* o) { return true; }
4024 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
4025 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004026};
4027
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004028} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004029
4030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004031template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032Local<T>::Local() : Handle<T>() { }
4033
4034
4035template <class T>
4036Local<T> Local<T>::New(Handle<T> that) {
4037 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004038 T* that_ptr = *that;
4039 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4040 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4041 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4042 reinterpret_cast<internal::HeapObject*>(*p))));
4043 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004044 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4045}
4046
4047
4048template <class T>
4049Persistent<T> Persistent<T>::New(Handle<T> that) {
4050 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004051 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004052 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4053}
4054
4055
4056template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004057bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004058 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004059 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004060}
4061
4062
4063template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004064bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004065 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004066 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004067}
4068
4069
4070template <class T>
4071void Persistent<T>::Dispose() {
4072 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004073 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004074}
4075
4076
4077template <class T>
4078Persistent<T>::Persistent() : Handle<T>() { }
4079
4080template <class T>
4081void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004082 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4083 parameters,
4084 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004085}
4086
4087template <class T>
4088void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004089 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090}
4091
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004092template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004093void Persistent<T>::MarkIndependent() {
4094 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4095}
4096
4097template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004098void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4099 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4100}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004101
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004102Arguments::Arguments(internal::Object** implicit_args,
4103 internal::Object** values, int length,
4104 bool is_construct_call)
4105 : implicit_args_(implicit_args),
4106 values_(values),
4107 length_(length),
4108 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004109
4110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004111Local<Value> Arguments::operator[](int i) const {
4112 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4113 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4114}
4115
4116
4117Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004118 return Local<Function>(reinterpret_cast<Function*>(
4119 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004120}
4121
4122
4123Local<Object> Arguments::This() const {
4124 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4125}
4126
4127
4128Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004129 return Local<Object>(reinterpret_cast<Object*>(
4130 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004131}
4132
4133
4134Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004135 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004136}
4137
4138
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004139Isolate* Arguments::GetIsolate() const {
4140 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4141}
4142
4143
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004144bool Arguments::IsConstructCall() const {
4145 return is_construct_call_;
4146}
4147
4148
4149int Arguments::Length() const {
4150 return length_;
4151}
4152
4153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004154template <class T>
4155Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004156 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4157 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004158 return Local<T>(reinterpret_cast<T*>(after));
4159}
4160
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004161Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004162 return resource_name_;
4163}
4164
4165
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004166Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004167 return resource_line_offset_;
4168}
4169
4170
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004171Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004172 return resource_column_offset_;
4173}
4174
4175
4176Handle<Boolean> Boolean::New(bool value) {
4177 return value ? True() : False();
4178}
4179
4180
4181void Template::Set(const char* name, v8::Handle<Data> value) {
4182 Set(v8::String::New(name), value);
4183}
4184
4185
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004186Local<Value> Object::GetInternalField(int index) {
4187#ifndef V8_ENABLE_CHECKS
4188 Local<Value> quick_result = UncheckedGetInternalField(index);
4189 if (!quick_result.IsEmpty()) return quick_result;
4190#endif
4191 return CheckedGetInternalField(index);
4192}
4193
4194
4195Local<Value> Object::UncheckedGetInternalField(int index) {
4196 typedef internal::Object O;
4197 typedef internal::Internals I;
4198 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004199 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004200 // If the object is a plain JSObject, which is the common case,
4201 // we know where to find the internal fields and can return the
4202 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004203 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004204 O* value = I::ReadField<O*>(obj, offset);
4205 O** result = HandleScope::CreateHandle(value);
4206 return Local<Value>(reinterpret_cast<Value*>(result));
4207 } else {
4208 return Local<Value>();
4209 }
4210}
4211
4212
4213void* External::Unwrap(Handle<v8::Value> obj) {
4214#ifdef V8_ENABLE_CHECKS
4215 return FullUnwrap(obj);
4216#else
4217 return QuickUnwrap(obj);
4218#endif
4219}
4220
4221
4222void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4223 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004224 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004225 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004226}
4227
4228
4229void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004230 typedef internal::Object O;
4231 typedef internal::Internals I;
4232
4233 O* obj = *reinterpret_cast<O**>(this);
4234
4235 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4236 // If the object is a plain JSObject, which is the common case,
4237 // we know where to find the internal fields and can return the
4238 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004239 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004240 O* value = I::ReadField<O*>(obj, offset);
4241 return I::GetExternalPointer(value);
4242 }
4243
4244 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004245}
4246
4247
4248String* String::Cast(v8::Value* value) {
4249#ifdef V8_ENABLE_CHECKS
4250 CheckCast(value);
4251#endif
4252 return static_cast<String*>(value);
4253}
4254
4255
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004256Local<String> String::Empty(Isolate* isolate) {
4257 typedef internal::Object* S;
4258 typedef internal::Internals I;
4259 if (!I::IsInitialized(isolate)) return Empty();
4260 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4261 return Local<String>(reinterpret_cast<String*>(slot));
4262}
4263
4264
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004265String::ExternalStringResource* String::GetExternalStringResource() const {
4266 typedef internal::Object O;
4267 typedef internal::Internals I;
4268 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004269 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004270 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004271 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4272 result = reinterpret_cast<String::ExternalStringResource*>(value);
4273 } else {
4274 result = NULL;
4275 }
4276#ifdef V8_ENABLE_CHECKS
4277 VerifyExternalStringResource(result);
4278#endif
4279 return result;
4280}
4281
4282
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004283bool Value::IsUndefined() const {
4284#ifdef V8_ENABLE_CHECKS
4285 return FullIsUndefined();
4286#else
4287 return QuickIsUndefined();
4288#endif
4289}
4290
4291bool Value::QuickIsUndefined() const {
4292 typedef internal::Object O;
4293 typedef internal::Internals I;
4294 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4295 if (!I::HasHeapObjectTag(obj)) return false;
4296 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4297 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4298}
4299
4300
4301bool Value::IsNull() const {
4302#ifdef V8_ENABLE_CHECKS
4303 return FullIsNull();
4304#else
4305 return QuickIsNull();
4306#endif
4307}
4308
4309bool Value::QuickIsNull() const {
4310 typedef internal::Object O;
4311 typedef internal::Internals I;
4312 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4313 if (!I::HasHeapObjectTag(obj)) return false;
4314 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4315 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4316}
4317
4318
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004319bool Value::IsString() const {
4320#ifdef V8_ENABLE_CHECKS
4321 return FullIsString();
4322#else
4323 return QuickIsString();
4324#endif
4325}
4326
4327bool Value::QuickIsString() const {
4328 typedef internal::Object O;
4329 typedef internal::Internals I;
4330 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4331 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004332 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004333}
4334
4335
4336Number* Number::Cast(v8::Value* value) {
4337#ifdef V8_ENABLE_CHECKS
4338 CheckCast(value);
4339#endif
4340 return static_cast<Number*>(value);
4341}
4342
4343
4344Integer* Integer::Cast(v8::Value* value) {
4345#ifdef V8_ENABLE_CHECKS
4346 CheckCast(value);
4347#endif
4348 return static_cast<Integer*>(value);
4349}
4350
4351
4352Date* Date::Cast(v8::Value* value) {
4353#ifdef V8_ENABLE_CHECKS
4354 CheckCast(value);
4355#endif
4356 return static_cast<Date*>(value);
4357}
4358
4359
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004360StringObject* StringObject::Cast(v8::Value* value) {
4361#ifdef V8_ENABLE_CHECKS
4362 CheckCast(value);
4363#endif
4364 return static_cast<StringObject*>(value);
4365}
4366
4367
4368NumberObject* NumberObject::Cast(v8::Value* value) {
4369#ifdef V8_ENABLE_CHECKS
4370 CheckCast(value);
4371#endif
4372 return static_cast<NumberObject*>(value);
4373}
4374
4375
4376BooleanObject* BooleanObject::Cast(v8::Value* value) {
4377#ifdef V8_ENABLE_CHECKS
4378 CheckCast(value);
4379#endif
4380 return static_cast<BooleanObject*>(value);
4381}
4382
4383
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004384RegExp* RegExp::Cast(v8::Value* value) {
4385#ifdef V8_ENABLE_CHECKS
4386 CheckCast(value);
4387#endif
4388 return static_cast<RegExp*>(value);
4389}
4390
4391
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004392Object* Object::Cast(v8::Value* value) {
4393#ifdef V8_ENABLE_CHECKS
4394 CheckCast(value);
4395#endif
4396 return static_cast<Object*>(value);
4397}
4398
4399
4400Array* Array::Cast(v8::Value* value) {
4401#ifdef V8_ENABLE_CHECKS
4402 CheckCast(value);
4403#endif
4404 return static_cast<Array*>(value);
4405}
4406
4407
4408Function* Function::Cast(v8::Value* value) {
4409#ifdef V8_ENABLE_CHECKS
4410 CheckCast(value);
4411#endif
4412 return static_cast<Function*>(value);
4413}
4414
4415
4416External* External::Cast(v8::Value* value) {
4417#ifdef V8_ENABLE_CHECKS
4418 CheckCast(value);
4419#endif
4420 return static_cast<External*>(value);
4421}
4422
4423
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004424Isolate* AccessorInfo::GetIsolate() const {
4425 return *reinterpret_cast<Isolate**>(&args_[-3]);
4426}
4427
4428
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004429Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004430 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004431}
4432
4433
4434Local<Object> AccessorInfo::This() const {
4435 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4436}
4437
4438
4439Local<Object> AccessorInfo::Holder() const {
4440 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4441}
4442
4443
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004444Handle<Primitive> Undefined(Isolate* isolate) {
4445 typedef internal::Object* S;
4446 typedef internal::Internals I;
4447 if (!I::IsInitialized(isolate)) return Undefined();
4448 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4449 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4450}
4451
4452
4453Handle<Primitive> Null(Isolate* isolate) {
4454 typedef internal::Object* S;
4455 typedef internal::Internals I;
4456 if (!I::IsInitialized(isolate)) return Null();
4457 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4458 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4459}
4460
4461
4462Handle<Boolean> True(Isolate* isolate) {
4463 typedef internal::Object* S;
4464 typedef internal::Internals I;
4465 if (!I::IsInitialized(isolate)) return True();
4466 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4467 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4468}
4469
4470
4471Handle<Boolean> False(Isolate* isolate) {
4472 typedef internal::Object* S;
4473 typedef internal::Internals I;
4474 if (!I::IsInitialized(isolate)) return False();
4475 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4476 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4477}
4478
4479
4480void Isolate::SetData(void* data) {
4481 typedef internal::Internals I;
4482 I::SetEmbedderData(this, data);
4483}
4484
4485
4486void* Isolate::GetData() {
4487 typedef internal::Internals I;
4488 return I::GetEmbedderData(this);
4489}
4490
4491
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004492/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004493 * \example shell.cc
4494 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004495 * command-line and executes them.
4496 */
4497
4498
4499/**
4500 * \example process.cc
4501 */
4502
4503
4504} // namespace v8
4505
4506
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004507#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004508#undef TYPE_CHECK
4509
4510
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004511#endif // V8_H_