blob: 77ffb385ab97f34381177e4a39e2f01aec62fa94 [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 /**
1554 * Returns the name of the function invoked as a constructor for this object.
1555 */
1556 V8EXPORT Local<String> GetConstructorName();
1557
kasper.lund212ac232008-07-16 07:07:30 +00001558 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001559 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001560 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001561 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001562 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001563 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001565 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001566 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001567
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001568 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001569 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001570
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001572 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001573 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1574 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1575 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576
1577 /**
1578 * If result.IsEmpty() no real property was located in the prototype chain.
1579 * This means interceptors in the prototype chain are not called.
1580 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001581 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1582 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001583
1584 /**
1585 * If result.IsEmpty() no real property was located on the object or
1586 * in the prototype chain.
1587 * This means interceptors in the prototype chain are not called.
1588 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001589 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590
1591 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001592 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593
kasper.lund212ac232008-07-16 07:07:30 +00001594 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001595 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001597 /**
1598 * Turns on access check on the object if the object is an instance of
1599 * a template that has access check callbacks. If an object has no
1600 * access check info, the object cannot be accessed by anyone.
1601 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001602 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001603
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001604 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001605 * Returns the identity hash for this object. The current implementation
1606 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001607 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001608 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001609 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001610 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001611 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001612
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001613 /**
1614 * Access hidden properties on JavaScript objects. These properties are
1615 * hidden from the executing JavaScript and only accessible through the V8
1616 * C++ API. Hidden properties introduced by V8 internally (for example the
1617 * identity hash) are prefixed with "v8::".
1618 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001619 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1620 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1621 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001622
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001623 /**
1624 * Returns true if this is an instance of an api function (one
1625 * created from a function created from a function template) and has
1626 * been modified since it was created. Note that this method is
1627 * conservative and may return true for objects that haven't actually
1628 * been modified.
1629 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001630 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001631
1632 /**
1633 * Clone this object with a fast but shallow copy. Values will point
1634 * to the same values as the original object.
1635 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001636 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001638 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001639 * Returns the context in which the object was created.
1640 */
1641 V8EXPORT Local<Context> CreationContext();
1642
1643 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001644 * Set the backing store of the indexed properties to be managed by the
1645 * embedding layer. Access to the indexed properties will follow the rules
1646 * spelled out in CanvasPixelArray.
1647 * Note: The embedding program still owns the data and needs to ensure that
1648 * the backing store is preserved while V8 has a reference.
1649 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001650 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001651 V8EXPORT bool HasIndexedPropertiesInPixelData();
1652 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1653 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001654
ager@chromium.org3811b432009-10-28 14:53:37 +00001655 /**
1656 * Set the backing store of the indexed properties to be managed by the
1657 * embedding layer. Access to the indexed properties will follow the rules
1658 * spelled out for the CanvasArray subtypes in the WebGL specification.
1659 * Note: The embedding program still owns the data and needs to ensure that
1660 * the backing store is preserved while V8 has a reference.
1661 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001662 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1663 void* data,
1664 ExternalArrayType array_type,
1665 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001666 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1667 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1668 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1669 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001670
lrn@chromium.org1c092762011-05-09 09:42:16 +00001671 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001672 * Checks whether a callback is set by the
1673 * ObjectTemplate::SetCallAsFunctionHandler method.
1674 * When an Object is callable this method returns true.
1675 */
1676 V8EXPORT bool IsCallable();
1677
1678 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001679 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001680 * ObjectTemplate::SetCallAsFunctionHandler method.
1681 */
1682 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1683 int argc,
1684 Handle<Value> argv[]);
1685
1686 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001687 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001688 * ObjectTemplate::SetCallAsFunctionHandler method.
1689 * Note: This method behaves like the Function::NewInstance method.
1690 */
1691 V8EXPORT Local<Value> CallAsConstructor(int argc,
1692 Handle<Value> argv[]);
1693
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001694 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001695 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001696
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001698 V8EXPORT Object();
1699 V8EXPORT static void CheckCast(Value* obj);
1700 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1701 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001702
1703 /**
1704 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001705 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001706 */
1707 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708};
1709
1710
1711/**
1712 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1713 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001714class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001716 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717
ager@chromium.org3e875802009-06-29 08:26:34 +00001718 /**
1719 * Clones an element at index |index|. Returns an empty
1720 * handle if cloning fails (for any reason).
1721 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001722 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001723
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001724 /**
1725 * Creates a JavaScript array with the given length. If the length
1726 * is negative the returned array will have length 0.
1727 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001728 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001729
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001730 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001732 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001733 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734};
1735
1736
1737/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001738 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001740class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001742 V8EXPORT Local<Object> NewInstance() const;
1743 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1744 V8EXPORT Local<Value> Call(Handle<Object> recv,
1745 int argc,
1746 Handle<Value> argv[]);
1747 V8EXPORT void SetName(Handle<String> name);
1748 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001749
1750 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001751 * Name inferred from variable or property assignment of this function.
1752 * Used to facilitate debugging and profiling of JavaScript code written
1753 * in an OO style, where many functions are anonymous but are assigned
1754 * to object properties.
1755 */
1756 V8EXPORT Handle<Value> GetInferredName() const;
1757
1758 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001759 * Returns zero based line number of function body and
1760 * kLineOffsetNotFound if no information available.
1761 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001762 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001763 /**
1764 * Returns zero based column number of function body and
1765 * kLineOffsetNotFound if no information available.
1766 */
1767 V8EXPORT int GetScriptColumnNumber() const;
1768 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001769 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001770 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001771 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001772
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001774 V8EXPORT Function();
1775 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776};
1777
1778
1779/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001780 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1781 */
1782class Date : public Object {
1783 public:
1784 V8EXPORT static Local<Value> New(double time);
1785
1786 /**
1787 * A specialization of Value::NumberValue that is more efficient
1788 * because we know the structure of this object.
1789 */
1790 V8EXPORT double NumberValue() const;
1791
1792 static inline Date* Cast(v8::Value* obj);
1793
1794 /**
1795 * Notification that the embedder has changed the time zone,
1796 * daylight savings time, or other date / time configuration
1797 * parameters. V8 keeps a cache of various values used for
1798 * date / time computation. This notification will reset
1799 * those cached values for the current context so that date /
1800 * time configuration changes would be reflected in the Date
1801 * object.
1802 *
1803 * This API should not be called more than needed as it will
1804 * negatively impact the performance of date operations.
1805 */
1806 V8EXPORT static void DateTimeConfigurationChangeNotification();
1807
1808 private:
1809 V8EXPORT static void CheckCast(v8::Value* obj);
1810};
1811
1812
1813/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001814 * A Number object (ECMA-262, 4.3.21).
1815 */
1816class NumberObject : public Object {
1817 public:
1818 V8EXPORT static Local<Value> New(double value);
1819
1820 /**
1821 * Returns the Number held by the object.
1822 */
1823 V8EXPORT double NumberValue() const;
1824
1825 static inline NumberObject* Cast(v8::Value* obj);
1826
1827 private:
1828 V8EXPORT static void CheckCast(v8::Value* obj);
1829};
1830
1831
1832/**
1833 * A Boolean object (ECMA-262, 4.3.15).
1834 */
1835class BooleanObject : public Object {
1836 public:
1837 V8EXPORT static Local<Value> New(bool value);
1838
1839 /**
1840 * Returns the Boolean held by the object.
1841 */
1842 V8EXPORT bool BooleanValue() const;
1843
1844 static inline BooleanObject* Cast(v8::Value* obj);
1845
1846 private:
1847 V8EXPORT static void CheckCast(v8::Value* obj);
1848};
1849
1850
1851/**
1852 * A String object (ECMA-262, 4.3.18).
1853 */
1854class StringObject : public Object {
1855 public:
1856 V8EXPORT static Local<Value> New(Handle<String> value);
1857
1858 /**
1859 * Returns the String held by the object.
1860 */
1861 V8EXPORT Local<String> StringValue() const;
1862
1863 static inline StringObject* Cast(v8::Value* obj);
1864
1865 private:
1866 V8EXPORT static void CheckCast(v8::Value* obj);
1867};
1868
1869
1870/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001871 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1872 */
1873class RegExp : public Object {
1874 public:
1875 /**
1876 * Regular expression flag bits. They can be or'ed to enable a set
1877 * of flags.
1878 */
1879 enum Flags {
1880 kNone = 0,
1881 kGlobal = 1,
1882 kIgnoreCase = 2,
1883 kMultiline = 4
1884 };
1885
1886 /**
1887 * Creates a regular expression from the given pattern string and
1888 * the flags bit field. May throw a JavaScript exception as
1889 * described in ECMA-262, 15.10.4.1.
1890 *
1891 * For example,
1892 * RegExp::New(v8::String::New("foo"),
1893 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1894 * is equivalent to evaluating "/foo/gm".
1895 */
1896 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1897 Flags flags);
1898
1899 /**
1900 * Returns the value of the source property: a string representing
1901 * the regular expression.
1902 */
1903 V8EXPORT Local<String> GetSource() const;
1904
1905 /**
1906 * Returns the flags bit field.
1907 */
1908 V8EXPORT Flags GetFlags() const;
1909
1910 static inline RegExp* Cast(v8::Value* obj);
1911
1912 private:
1913 V8EXPORT static void CheckCast(v8::Value* obj);
1914};
1915
1916
1917/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001918 * A JavaScript value that wraps a C++ void*. This type of value is
1919 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001921 *
1922 * The Wrap function V8 will return the most optimal Value object wrapping the
1923 * C++ void*. The type of the value is not guaranteed to be an External object
1924 * and no assumptions about its type should be made. To access the wrapped
1925 * value Unwrap should be used, all other operations on that object will lead
1926 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001928class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001930 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001931 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001932
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001933 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001934 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001935 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001937 V8EXPORT External();
1938 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001939 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001940 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941};
1942
1943
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001944// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945
1946
1947/**
1948 * The superclass of object and function templates.
1949 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001950class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951 public:
1952 /** Adds a property to each instance created by this template.*/
1953 void Set(Handle<String> name, Handle<Data> value,
1954 PropertyAttribute attributes = None);
1955 inline void Set(const char* name, Handle<Data> value);
1956 private:
1957 Template();
1958
1959 friend class ObjectTemplate;
1960 friend class FunctionTemplate;
1961};
1962
1963
1964/**
1965 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001966 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 * including the receiver, the number and values of arguments, and
1968 * the holder of the function.
1969 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001970class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 public:
1972 inline int Length() const;
1973 inline Local<Value> operator[](int i) const;
1974 inline Local<Function> Callee() const;
1975 inline Local<Object> This() const;
1976 inline Local<Object> Holder() const;
1977 inline bool IsConstructCall() const;
1978 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001979 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00001980
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001981 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001982 static const int kIsolateIndex = 0;
1983 static const int kDataIndex = -1;
1984 static const int kCalleeIndex = -2;
1985 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001986
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001988 inline Arguments(internal::Object** implicit_args,
1989 internal::Object** values,
1990 int length,
1991 bool is_construct_call);
1992 internal::Object** implicit_args_;
1993 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001995 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996};
1997
1998
1999/**
2000 * The information passed to an accessor callback about the context
2001 * of the property access.
2002 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002003class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002005 inline AccessorInfo(internal::Object** args)
2006 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002007 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 inline Local<Value> Data() const;
2009 inline Local<Object> This() const;
2010 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002011
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002013 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014};
2015
2016
2017typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2018
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 * NamedProperty[Getter|Setter] are used as interceptors on object.
2021 * See ObjectTemplate::SetNamedPropertyHandler.
2022 */
2023typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2024 const AccessorInfo& info);
2025
2026
2027/**
2028 * Returns the value if the setter intercepts the request.
2029 * Otherwise, returns an empty handle.
2030 */
2031typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2032 Local<Value> value,
2033 const AccessorInfo& info);
2034
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035/**
2036 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002037 * The result is an integer encoding property attributes (like v8::None,
2038 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002040typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2041 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042
2043
2044/**
2045 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002046 * The return value is true if the property could be deleted and false
2047 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 */
2049typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2050 const AccessorInfo& info);
2051
2052/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002053 * Returns an array containing the names of the properties the named
2054 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 */
2056typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2057
v8.team.kasperl727e9952008-09-02 14:56:44 +00002058
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002060 * Returns the value of the property if the getter intercepts the
2061 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 */
2063typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2064 const AccessorInfo& info);
2065
2066
2067/**
2068 * Returns the value if the setter intercepts the request.
2069 * Otherwise, returns an empty handle.
2070 */
2071typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2072 Local<Value> value,
2073 const AccessorInfo& info);
2074
2075
2076/**
2077 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002078 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002080typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2081 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082
2083/**
2084 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002085 * The return value is true if the property could be deleted and false
2086 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 */
2088typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2089 const AccessorInfo& info);
2090
v8.team.kasperl727e9952008-09-02 14:56:44 +00002091/**
2092 * Returns an array containing the indices of the properties the
2093 * indexed property getter intercepts.
2094 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2096
2097
2098/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002099 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002100 */
2101enum AccessType {
2102 ACCESS_GET,
2103 ACCESS_SET,
2104 ACCESS_HAS,
2105 ACCESS_DELETE,
2106 ACCESS_KEYS
2107};
2108
v8.team.kasperl727e9952008-09-02 14:56:44 +00002109
2110/**
2111 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002112 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002113 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002114typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 Local<Value> key,
2116 AccessType type,
2117 Local<Value> data);
2118
v8.team.kasperl727e9952008-09-02 14:56:44 +00002119
2120/**
2121 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002122 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002123 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002124typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125 uint32_t index,
2126 AccessType type,
2127 Local<Value> data);
2128
2129
2130/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002131 * A FunctionTemplate is used to create functions at runtime. There
2132 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002133 * context. The lifetime of the created function is equal to the
2134 * lifetime of the context. So in case the embedder needs to create
2135 * temporary functions that can be collected using Scripts is
2136 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 *
2138 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002139 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002141 * A FunctionTemplate has a corresponding instance template which is
2142 * used to create object instances when the function is used as a
2143 * constructor. Properties added to the instance template are added to
2144 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145 *
2146 * A FunctionTemplate can have a prototype template. The prototype template
2147 * is used to create the prototype object of the function.
2148 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002149 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002151 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2153 * t->Set("func_property", v8::Number::New(1));
2154 *
2155 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2156 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2157 * proto_t->Set("proto_const", v8::Number::New(2));
2158 *
2159 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2160 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2161 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2162 * instance_t->Set("instance_property", Number::New(3));
2163 *
2164 * v8::Local<v8::Function> function = t->GetFunction();
2165 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002166 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 *
2168 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002169 * and "instance" for the instance object created above. The function
2170 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 * \code
2173 * func_property in function == true;
2174 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002176 * function.prototype.proto_method() invokes 'InvokeCallback'
2177 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002179 * instance instanceof function == true;
2180 * instance.instance_accessor calls 'InstanceAccessorCallback'
2181 * instance.instance_property == 3;
2182 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002184 * A FunctionTemplate can inherit from another one by calling the
2185 * FunctionTemplate::Inherit method. The following graph illustrates
2186 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002188 * \code
2189 * FunctionTemplate Parent -> Parent() . prototype -> { }
2190 * ^ ^
2191 * | Inherit(Parent) | .__proto__
2192 * | |
2193 * FunctionTemplate Child -> Child() . prototype -> { }
2194 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002196 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2197 * object of the Child() function has __proto__ pointing to the
2198 * Parent() function's prototype object. An instance of the Child
2199 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002201 * Let Parent be the FunctionTemplate initialized in the previous
2202 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002204 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 * Local<FunctionTemplate> parent = t;
2206 * Local<FunctionTemplate> child = FunctionTemplate::New();
2207 * child->Inherit(parent);
2208 *
2209 * Local<Function> child_function = child->GetFunction();
2210 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002211 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002213 * The Child function and Child instance will have the following
2214 * properties:
2215 *
2216 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002218 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002220 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002221 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002222class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 public:
2224 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002225 static Local<FunctionTemplate> New(
2226 InvocationCallback callback = 0,
2227 Handle<Value> data = Handle<Value>(),
2228 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 /** Returns the unique function instance in the current execution context.*/
2230 Local<Function> GetFunction();
2231
v8.team.kasperl727e9952008-09-02 14:56:44 +00002232 /**
2233 * Set the call-handler callback for a FunctionTemplate. This
2234 * callback is called whenever the function created from this
2235 * FunctionTemplate is called.
2236 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 void SetCallHandler(InvocationCallback callback,
2238 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239
v8.team.kasperl727e9952008-09-02 14:56:44 +00002240 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 Local<ObjectTemplate> InstanceTemplate();
2242
2243 /** Causes the function template to inherit from a parent function template.*/
2244 void Inherit(Handle<FunctionTemplate> parent);
2245
2246 /**
2247 * A PrototypeTemplate is the template used to create the prototype object
2248 * of the function created by this template.
2249 */
2250 Local<ObjectTemplate> PrototypeTemplate();
2251
v8.team.kasperl727e9952008-09-02 14:56:44 +00002252
2253 /**
2254 * Set the class name of the FunctionTemplate. This is used for
2255 * printing objects created with the function created from the
2256 * FunctionTemplate as its constructor.
2257 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258 void SetClassName(Handle<String> name);
2259
2260 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002261 * Determines whether the __proto__ accessor ignores instances of
2262 * the function template. If instances of the function template are
2263 * ignored, __proto__ skips all instances and instead returns the
2264 * next object in the prototype chain.
2265 *
2266 * Call with a value of true to make the __proto__ accessor ignore
2267 * instances of the function template. Call with a value of false
2268 * to make the __proto__ accessor not ignore instances of the
2269 * function template. By default, instances of a function template
2270 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 */
2272 void SetHiddenPrototype(bool value);
2273
2274 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002275 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2276 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002277 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002278 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002279
2280 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002281 * Returns true if the given object is an instance of this function
2282 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 */
2284 bool HasInstance(Handle<Value> object);
2285
2286 private:
2287 FunctionTemplate();
2288 void AddInstancePropertyAccessor(Handle<String> name,
2289 AccessorGetter getter,
2290 AccessorSetter setter,
2291 Handle<Value> data,
2292 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002293 PropertyAttribute attributes,
2294 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2296 NamedPropertySetter setter,
2297 NamedPropertyQuery query,
2298 NamedPropertyDeleter remover,
2299 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002300 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2302 IndexedPropertySetter setter,
2303 IndexedPropertyQuery query,
2304 IndexedPropertyDeleter remover,
2305 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002306 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002307 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2308 Handle<Value> data);
2309
2310 friend class Context;
2311 friend class ObjectTemplate;
2312};
2313
2314
2315/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002316 * An ObjectTemplate is used to create objects at runtime.
2317 *
2318 * Properties added to an ObjectTemplate are added to each object
2319 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002321class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002323 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 /** Creates a new instance of this template.*/
2327 Local<Object> NewInstance();
2328
2329 /**
2330 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002331 *
2332 * Whenever the property with the given name is accessed on objects
2333 * created from this ObjectTemplate the getter and setter callbacks
2334 * are called instead of getting and setting the property directly
2335 * on the JavaScript object.
2336 *
2337 * \param name The name of the property for which an accessor is added.
2338 * \param getter The callback to invoke when getting the property.
2339 * \param setter The callback to invoke when setting the property.
2340 * \param data A piece of data that will be passed to the getter and setter
2341 * callbacks whenever they are invoked.
2342 * \param settings Access control settings for the accessor. This is a bit
2343 * field consisting of one of more of
2344 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2345 * The default is to not allow cross-context access.
2346 * ALL_CAN_READ means that all cross-context reads are allowed.
2347 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2348 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2349 * cross-context access.
2350 * \param attribute The attributes of the property for which an accessor
2351 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002352 * \param signature The signature describes valid receivers for the accessor
2353 * and is used to perform implicit instance checks against them. If the
2354 * receiver is incompatible (i.e. is not an instance of the constructor as
2355 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2356 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357 */
2358 void SetAccessor(Handle<String> name,
2359 AccessorGetter getter,
2360 AccessorSetter setter = 0,
2361 Handle<Value> data = Handle<Value>(),
2362 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002363 PropertyAttribute attribute = None,
2364 Handle<AccessorSignature> signature =
2365 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366
2367 /**
2368 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002369 *
2370 * Whenever a named property is accessed on objects created from
2371 * this object template, the provided callback is invoked instead of
2372 * accessing the property directly on the JavaScript object.
2373 *
2374 * \param getter The callback to invoke when getting a property.
2375 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002376 * \param query The callback to invoke to check if a property is present,
2377 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002378 * \param deleter The callback to invoke when deleting a property.
2379 * \param enumerator The callback to invoke to enumerate all the named
2380 * properties of an object.
2381 * \param data A piece of data that will be passed to the callbacks
2382 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383 */
2384 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2385 NamedPropertySetter setter = 0,
2386 NamedPropertyQuery query = 0,
2387 NamedPropertyDeleter deleter = 0,
2388 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002389 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390
2391 /**
2392 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002393 *
2394 * Whenever an indexed property is accessed on objects created from
2395 * this object template, the provided callback is invoked instead of
2396 * accessing the property directly on the JavaScript object.
2397 *
2398 * \param getter The callback to invoke when getting a property.
2399 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002400 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002401 * \param deleter The callback to invoke when deleting a property.
2402 * \param enumerator The callback to invoke to enumerate all the indexed
2403 * properties of an object.
2404 * \param data A piece of data that will be passed to the callbacks
2405 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406 */
2407 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2408 IndexedPropertySetter setter = 0,
2409 IndexedPropertyQuery query = 0,
2410 IndexedPropertyDeleter deleter = 0,
2411 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002412 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002413
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 /**
2415 * Sets the callback to be used when calling instances created from
2416 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002417 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 * function.
2419 */
2420 void SetCallAsFunctionHandler(InvocationCallback callback,
2421 Handle<Value> data = Handle<Value>());
2422
v8.team.kasperl727e9952008-09-02 14:56:44 +00002423 /**
2424 * Mark object instances of the template as undetectable.
2425 *
2426 * In many ways, undetectable objects behave as though they are not
2427 * there. They behave like 'undefined' in conditionals and when
2428 * printed. However, properties can be accessed and called as on
2429 * normal objects.
2430 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431 void MarkAsUndetectable();
2432
v8.team.kasperl727e9952008-09-02 14:56:44 +00002433 /**
2434 * Sets access check callbacks on the object template.
2435 *
2436 * When accessing properties on instances of this object template,
2437 * the access check callback will be called to determine whether or
2438 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002439 * The last parameter specifies whether access checks are turned
2440 * on by default on instances. If access checks are off by default,
2441 * they can be turned on on individual instances by calling
2442 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002443 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2445 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002446 Handle<Value> data = Handle<Value>(),
2447 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448
kasper.lund212ac232008-07-16 07:07:30 +00002449 /**
2450 * Gets the number of internal fields for objects generated from
2451 * this template.
2452 */
2453 int InternalFieldCount();
2454
2455 /**
2456 * Sets the number of internal fields for objects generated from
2457 * this template.
2458 */
2459 void SetInternalFieldCount(int value);
2460
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 private:
2462 ObjectTemplate();
2463 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2464 friend class FunctionTemplate;
2465};
2466
2467
2468/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002469 * A Signature specifies which receivers and arguments are valid
2470 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002472class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 public:
2474 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2475 Handle<FunctionTemplate>(),
2476 int argc = 0,
2477 Handle<FunctionTemplate> argv[] = 0);
2478 private:
2479 Signature();
2480};
2481
2482
2483/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002484 * An AccessorSignature specifies which receivers are valid parameters
2485 * to an accessor callback.
2486 */
2487class V8EXPORT AccessorSignature : public Data {
2488 public:
2489 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2490 Handle<FunctionTemplate>());
2491 private:
2492 AccessorSignature();
2493};
2494
2495
2496/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002497 * A utility for determining the type of objects based on the template
2498 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002500class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002501 public:
2502 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2503 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2504 int match(Handle<Value> value);
2505 private:
2506 TypeSwitch();
2507};
2508
2509
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002510// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002512class V8EXPORT ExternalAsciiStringResourceImpl
2513 : public String::ExternalAsciiStringResource {
2514 public:
2515 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2516 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2517 : data_(data), length_(length) {}
2518 const char* data() const { return data_; }
2519 size_t length() const { return length_; }
2520
2521 private:
2522 const char* data_;
2523 size_t length_;
2524};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525
2526/**
2527 * Ignore
2528 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002529class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002530 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002531 // Note that the strings passed into this constructor must live as long
2532 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533 Extension(const char* name,
2534 const char* source = 0,
2535 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002536 const char** deps = 0,
2537 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 virtual ~Extension() { }
2539 virtual v8::Handle<v8::FunctionTemplate>
2540 GetNativeFunction(v8::Handle<v8::String> name) {
2541 return v8::Handle<v8::FunctionTemplate>();
2542 }
2543
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002544 const char* name() const { return name_; }
2545 size_t source_length() const { return source_length_; }
2546 const String::ExternalAsciiStringResource* source() const {
2547 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548 int dependency_count() { return dep_count_; }
2549 const char** dependencies() { return deps_; }
2550 void set_auto_enable(bool value) { auto_enable_ = value; }
2551 bool auto_enable() { return auto_enable_; }
2552
2553 private:
2554 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002555 size_t source_length_; // expected to initialize before source_
2556 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557 int dep_count_;
2558 const char** deps_;
2559 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002560
2561 // Disallow copying and assigning.
2562 Extension(const Extension&);
2563 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564};
2565
2566
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002567void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568
2569
2570/**
2571 * Ignore
2572 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002573class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574 public:
2575 inline DeclareExtension(Extension* extension) {
2576 RegisterExtension(extension);
2577 }
2578};
2579
2580
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002581// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582
2583
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002584Handle<Primitive> V8EXPORT Undefined();
2585Handle<Primitive> V8EXPORT Null();
2586Handle<Boolean> V8EXPORT True();
2587Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002589inline Handle<Primitive> Undefined(Isolate* isolate);
2590inline Handle<Primitive> Null(Isolate* isolate);
2591inline Handle<Boolean> True(Isolate* isolate);
2592inline Handle<Boolean> False(Isolate* isolate);
2593
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594
2595/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002596 * A set of constraints that specifies the limits of the runtime's memory use.
2597 * You must set the heap size before initializing the VM - the size cannot be
2598 * adjusted after the VM is initialized.
2599 *
2600 * If you are using threads then you should hold the V8::Locker lock while
2601 * setting the stack limit and you must set a non-default stack limit separately
2602 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002604class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002605 public:
2606 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002607 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002609 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002611 int max_executable_size() { return max_executable_size_; }
2612 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002613 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002614 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2616 private:
2617 int max_young_space_size_;
2618 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002619 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620 uint32_t* stack_limit_;
2621};
2622
2623
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002624bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625
2626
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002627// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628
2629
2630typedef void (*FatalErrorCallback)(const char* location, const char* message);
2631
2632
2633typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2634
2635
2636/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002637 * Schedules an exception to be thrown when returning to JavaScript. When an
2638 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002640 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002642Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643
2644/**
2645 * Create new error objects by calling the corresponding error object
2646 * constructor with the message.
2647 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002648class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649 public:
2650 static Local<Value> RangeError(Handle<String> message);
2651 static Local<Value> ReferenceError(Handle<String> message);
2652 static Local<Value> SyntaxError(Handle<String> message);
2653 static Local<Value> TypeError(Handle<String> message);
2654 static Local<Value> Error(Handle<String> message);
2655};
2656
2657
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002658// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002660typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002662typedef void* (*CreateHistogramCallback)(const char* name,
2663 int min,
2664 int max,
2665 size_t buckets);
2666
2667typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2668
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002669// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002670 enum ObjectSpace {
2671 kObjectSpaceNewSpace = 1 << 0,
2672 kObjectSpaceOldPointerSpace = 1 << 1,
2673 kObjectSpaceOldDataSpace = 1 << 2,
2674 kObjectSpaceCodeSpace = 1 << 3,
2675 kObjectSpaceMapSpace = 1 << 4,
2676 kObjectSpaceLoSpace = 1 << 5,
2677
2678 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2679 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2680 kObjectSpaceLoSpace
2681 };
2682
2683 enum AllocationAction {
2684 kAllocationActionAllocate = 1 << 0,
2685 kAllocationActionFree = 1 << 1,
2686 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2687 };
2688
2689typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2690 AllocationAction action,
2691 int size);
2692
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002693// --- Leave Script Callback ---
2694typedef void (*CallCompletedCallback)();
2695
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002696// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2698 AccessType type,
2699 Local<Value> data);
2700
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002701// --- AllowCodeGenerationFromStrings callbacks ---
2702
2703/**
2704 * Callback to check if code generation from strings is allowed. See
2705 * Context::AllowCodeGenerationFromStrings.
2706 */
2707typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2708
2709// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710
2711/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002712 * Applications can register callback functions which will be called
2713 * before and after a garbage collection. Allocations are not
2714 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002715 * objects (set or delete properties for example) since it is possible
2716 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002718enum GCType {
2719 kGCTypeScavenge = 1 << 0,
2720 kGCTypeMarkSweepCompact = 1 << 1,
2721 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2722};
2723
2724enum GCCallbackFlags {
2725 kNoGCCallbackFlags = 0,
2726 kGCCallbackFlagCompacted = 1 << 0
2727};
2728
2729typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2730typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2731
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002732typedef void (*GCCallback)();
2733
2734
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002736 * Collection of V8 heap information.
2737 *
2738 * Instances of this class can be passed to v8::V8::HeapStatistics to
2739 * get heap statistics from V8.
2740 */
2741class V8EXPORT HeapStatistics {
2742 public:
2743 HeapStatistics();
2744 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002745 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002746 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002747 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002748
2749 private:
2750 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002751 void set_total_heap_size_executable(size_t size) {
2752 total_heap_size_executable_ = size;
2753 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002754 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002755 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002756
2757 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002758 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002759 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002760 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002761
2762 friend class V8;
2763};
2764
2765
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002766class RetainedObjectInfo;
2767
ager@chromium.org3811b432009-10-28 14:53:37 +00002768/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002769 * Isolate represents an isolated instance of the V8 engine. V8
2770 * isolates have completely separate states. Objects from one isolate
2771 * must not be used in other isolates. When V8 is initialized a
2772 * default isolate is implicitly created and entered. The embedder
2773 * can create additional isolates and use them in parallel in multiple
2774 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002775 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002776 */
2777class V8EXPORT Isolate {
2778 public:
2779 /**
2780 * Stack-allocated class which sets the isolate for all operations
2781 * executed within a local scope.
2782 */
2783 class V8EXPORT Scope {
2784 public:
2785 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2786 isolate->Enter();
2787 }
2788
2789 ~Scope() { isolate_->Exit(); }
2790
2791 private:
2792 Isolate* const isolate_;
2793
2794 // Prevent copying of Scope objects.
2795 Scope(const Scope&);
2796 Scope& operator=(const Scope&);
2797 };
2798
2799 /**
2800 * Creates a new isolate. Does not change the currently entered
2801 * isolate.
2802 *
2803 * When an isolate is no longer used its resources should be freed
2804 * by calling Dispose(). Using the delete operator is not allowed.
2805 */
2806 static Isolate* New();
2807
2808 /**
2809 * Returns the entered isolate for the current thread or NULL in
2810 * case there is no current isolate.
2811 */
2812 static Isolate* GetCurrent();
2813
2814 /**
2815 * Methods below this point require holding a lock (using Locker) in
2816 * a multi-threaded environment.
2817 */
2818
2819 /**
2820 * Sets this isolate as the entered one for the current thread.
2821 * Saves the previously entered one (if any), so that it can be
2822 * restored when exiting. Re-entering an isolate is allowed.
2823 */
2824 void Enter();
2825
2826 /**
2827 * Exits this isolate by restoring the previously entered one in the
2828 * current thread. The isolate may still stay the same, if it was
2829 * entered more than once.
2830 *
2831 * Requires: this == Isolate::GetCurrent().
2832 */
2833 void Exit();
2834
2835 /**
2836 * Disposes the isolate. The isolate must not be entered by any
2837 * thread to be disposable.
2838 */
2839 void Dispose();
2840
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002841 /**
2842 * Associate embedder-specific data with the isolate
2843 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002844 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002845
2846 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002847 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002848 * Returns NULL if SetData has never been called.
2849 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002850 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002851
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002852 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002853 Isolate();
2854 Isolate(const Isolate&);
2855 ~Isolate();
2856 Isolate& operator=(const Isolate&);
2857 void* operator new(size_t size);
2858 void operator delete(void*, size_t);
2859};
2860
2861
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002862class StartupData {
2863 public:
2864 enum CompressionAlgorithm {
2865 kUncompressed,
2866 kBZip2
2867 };
2868
2869 const char* data;
2870 int compressed_size;
2871 int raw_size;
2872};
2873
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002874
2875/**
2876 * A helper class for driving V8 startup data decompression. It is based on
2877 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2878 * for an embedder to use this class, instead, API functions can be used
2879 * directly.
2880 *
2881 * For an example of the class usage, see the "shell.cc" sample application.
2882 */
2883class V8EXPORT StartupDataDecompressor { // NOLINT
2884 public:
2885 StartupDataDecompressor();
2886 virtual ~StartupDataDecompressor();
2887 int Decompress();
2888
2889 protected:
2890 virtual int DecompressData(char* raw_data,
2891 int* raw_data_size,
2892 const char* compressed_data,
2893 int compressed_data_size) = 0;
2894
2895 private:
2896 char** raw_data;
2897};
2898
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002899
2900/**
2901 * EntropySource is used as a callback function when v8 needs a source
2902 * of entropy.
2903 */
2904typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2905
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002906
2907/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002908 * ReturnAddressLocationResolver is used as a callback function when v8 is
2909 * resolving the location of a return address on the stack. Profilers that
2910 * change the return address on the stack can use this to resolve the stack
2911 * location to whereever the profiler stashed the original return address.
2912 * When invoked, return_addr_location will point to a location on stack where
2913 * a machine return address resides, this function should return either the
2914 * same pointer, or a pointer to the profiler's copy of the original return
2915 * address.
2916 */
2917typedef uintptr_t (*ReturnAddressLocationResolver)(
2918 uintptr_t return_addr_location);
2919
2920
2921/**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002922 * Interface for iterating though all external resources in the heap.
2923 */
2924class V8EXPORT ExternalResourceVisitor { // NOLINT
2925 public:
2926 virtual ~ExternalResourceVisitor() {}
2927 virtual void VisitExternalString(Handle<String> string) {}
2928};
2929
2930
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002931/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002932 * Container class for static utility functions.
2933 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002934class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002936 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937 static void SetFatalErrorHandler(FatalErrorCallback that);
2938
v8.team.kasperl727e9952008-09-02 14:56:44 +00002939 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002940 * Set the callback to invoke to check if code generation from
2941 * strings should be allowed.
2942 */
2943 static void SetAllowCodeGenerationFromStringsCallback(
2944 AllowCodeGenerationFromStringsCallback that);
2945
2946 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002947 * Ignore out-of-memory exceptions.
2948 *
2949 * V8 running out of memory is treated as a fatal error by default.
2950 * This means that the fatal error handler is called and that V8 is
2951 * terminated.
2952 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002953 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002954 * out-of-memory situation as a fatal error. This way, the contexts
2955 * that did not cause the out of memory problem might be able to
2956 * continue execution.
2957 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 static void IgnoreOutOfMemoryException();
2959
v8.team.kasperl727e9952008-09-02 14:56:44 +00002960 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002961 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002962 * fatal errors such as out-of-memory situations.
2963 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964 static bool IsDead();
2965
2966 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002967 * The following 4 functions are to be used when V8 is built with
2968 * the 'compress_startup_data' flag enabled. In this case, the
2969 * embedder must decompress startup data prior to initializing V8.
2970 *
2971 * This is how interaction with V8 should look like:
2972 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2973 * v8::StartupData* compressed_data =
2974 * new v8::StartupData[compressed_data_count];
2975 * v8::V8::GetCompressedStartupData(compressed_data);
2976 * ... decompress data (compressed_data can be updated in-place) ...
2977 * v8::V8::SetDecompressedStartupData(compressed_data);
2978 * ... now V8 can be initialized
2979 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002980 *
2981 * A helper class StartupDataDecompressor is provided. It implements
2982 * the protocol of the interaction described above, and can be used in
2983 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002984 */
2985 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2986 static int GetCompressedStartupDataCount();
2987 static void GetCompressedStartupData(StartupData* compressed_data);
2988 static void SetDecompressedStartupData(StartupData* decompressed_data);
2989
2990 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002991 * Adds a message listener.
2992 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002993 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00002994 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995 */
2996 static bool AddMessageListener(MessageCallback that,
2997 Handle<Value> data = Handle<Value>());
2998
2999 /**
3000 * Remove all message listeners from the specified callback function.
3001 */
3002 static void RemoveMessageListeners(MessageCallback that);
3003
3004 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003005 * Tells V8 to capture current stack trace when uncaught exception occurs
3006 * and report it to the message listeners. The option is off by default.
3007 */
3008 static void SetCaptureStackTraceForUncaughtExceptions(
3009 bool capture,
3010 int frame_limit = 10,
3011 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3012
3013 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003014 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015 */
3016 static void SetFlagsFromString(const char* str, int length);
3017
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003018 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003019 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003020 */
3021 static void SetFlagsFromCommandLine(int* argc,
3022 char** argv,
3023 bool remove_flags);
3024
kasper.lund7276f142008-07-30 08:49:36 +00003025 /** Get the version string. */
3026 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003027
3028 /**
3029 * Enables the host application to provide a mechanism for recording
3030 * statistics counters.
3031 */
3032 static void SetCounterFunction(CounterLookupCallback);
3033
3034 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003035 * Enables the host application to provide a mechanism for recording
3036 * histograms. The CreateHistogram function returns a
3037 * histogram which will later be passed to the AddHistogramSample
3038 * function.
3039 */
3040 static void SetCreateHistogramFunction(CreateHistogramCallback);
3041 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3042
3043 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003044 * Enables the computation of a sliding window of states. The sliding
3045 * window information is recorded in statistics counters.
3046 */
3047 static void EnableSlidingStateWindow();
3048
3049 /** Callback function for reporting failed access checks.*/
3050 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3051
3052 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003053 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003054 * garbage collection. Allocations are not allowed in the
3055 * callback function, you therefore cannot manipulate objects (set
3056 * or delete properties for example) since it is possible such
3057 * operations will result in the allocation of objects. It is possible
3058 * to specify the GCType filter for your callback. But it is not possible to
3059 * register the same callback function two times with different
3060 * GCType filters.
3061 */
3062 static void AddGCPrologueCallback(
3063 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3064
3065 /**
3066 * This function removes callback which was installed by
3067 * AddGCPrologueCallback function.
3068 */
3069 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3070
3071 /**
3072 * The function is deprecated. Please use AddGCPrologueCallback instead.
3073 * Enables the host application to receive a notification before a
3074 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003075 * callback function, you therefore cannot manipulate objects (set
3076 * or delete properties for example) since it is possible such
3077 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003078 */
3079 static void SetGlobalGCPrologueCallback(GCCallback);
3080
3081 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003082 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003083 * garbage collection. Allocations are not allowed in the
3084 * callback function, you therefore cannot manipulate objects (set
3085 * or delete properties for example) since it is possible such
3086 * operations will result in the allocation of objects. It is possible
3087 * to specify the GCType filter for your callback. But it is not possible to
3088 * register the same callback function two times with different
3089 * GCType filters.
3090 */
3091 static void AddGCEpilogueCallback(
3092 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3093
3094 /**
3095 * This function removes callback which was installed by
3096 * AddGCEpilogueCallback function.
3097 */
3098 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3099
3100 /**
3101 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3102 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003103 * major garbage collection. Allocations are not allowed in the
3104 * callback function, you therefore cannot manipulate objects (set
3105 * or delete properties for example) since it is possible such
3106 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003107 */
3108 static void SetGlobalGCEpilogueCallback(GCCallback);
3109
3110 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003111 * Enables the host application to provide a mechanism to be notified
3112 * and perform custom logging when V8 Allocates Executable Memory.
3113 */
3114 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3115 ObjectSpace space,
3116 AllocationAction action);
3117
3118 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003119 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003120 */
3121 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3122
3123 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003124 * Adds a callback to notify the host application when a script finished
3125 * running. If a script re-enters the runtime during executing, the
3126 * CallCompletedCallback is only invoked when the outer-most script
3127 * execution ends. Executing scripts inside the callback do not trigger
3128 * further callbacks.
3129 */
3130 static void AddCallCompletedCallback(CallCompletedCallback callback);
3131
3132 /**
3133 * Removes callback that was installed by AddCallCompletedCallback.
3134 */
3135 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3136
3137 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003138 * Allows the host application to group objects together. If one
3139 * object in the group is alive, all objects in the group are alive.
3140 * After each garbage collection, object groups are removed. It is
3141 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003142 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00003143 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003144 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003145 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003146 static void AddObjectGroup(Persistent<Value>* objects,
3147 size_t length,
3148 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003149
3150 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003151 * Allows the host application to declare implicit references between
3152 * the objects: if |parent| is alive, all |children| are alive too.
3153 * After each garbage collection, all implicit references
3154 * are removed. It is intended to be used in the before-garbage-collection
3155 * callback function.
3156 */
3157 static void AddImplicitReferences(Persistent<Object> parent,
3158 Persistent<Value>* children,
3159 size_t length);
3160
3161 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003162 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003163 * initialize from scratch. This function is called implicitly if
3164 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003165 */
3166 static bool Initialize();
3167
kasper.lund7276f142008-07-30 08:49:36 +00003168 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003169 * Allows the host application to provide a callback which can be used
3170 * as a source of entropy for random number generators.
3171 */
3172 static void SetEntropySource(EntropySource source);
3173
3174 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003175 * Allows the host application to provide a callback that allows v8 to
3176 * cooperate with a profiler that rewrites return addresses on stack.
3177 */
3178 static void SetReturnAddressLocationResolver(
3179 ReturnAddressLocationResolver return_address_resolver);
3180
3181 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003182 * Adjusts the amount of registered external memory. Used to give
3183 * V8 an indication of the amount of externally allocated memory
3184 * that is kept alive by JavaScript objects. V8 uses this to decide
3185 * when to perform global garbage collections. Registering
3186 * externally allocated memory will trigger global garbage
3187 * collections more often than otherwise in an attempt to garbage
3188 * collect the JavaScript objects keeping the externally allocated
3189 * memory alive.
3190 *
3191 * \param change_in_bytes the change in externally allocated memory
3192 * that is kept alive by JavaScript objects.
3193 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003194 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003195 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3196 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003197
iposva@chromium.org245aa852009-02-10 00:49:54 +00003198 /**
3199 * Suspends recording of tick samples in the profiler.
3200 * When the V8 profiling mode is enabled (usually via command line
3201 * switches) this function suspends recording of tick samples.
3202 * Profiling ticks are discarded until ResumeProfiler() is called.
3203 *
3204 * See also the --prof and --prof_auto command line switches to
3205 * enable V8 profiling.
3206 */
3207 static void PauseProfiler();
3208
3209 /**
3210 * Resumes recording of tick samples in the profiler.
3211 * See also PauseProfiler().
3212 */
3213 static void ResumeProfiler();
3214
ager@chromium.org41826e72009-03-30 13:30:57 +00003215 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003216 * Return whether profiler is currently paused.
3217 */
3218 static bool IsProfilerPaused();
3219
3220 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003221 * Retrieve the V8 thread id of the calling thread.
3222 *
3223 * The thread id for a thread should only be retrieved after the V8
3224 * lock has been acquired with a Locker object with that thread.
3225 */
3226 static int GetCurrentThreadId();
3227
3228 /**
3229 * Forcefully terminate execution of a JavaScript thread. This can
3230 * be used to terminate long-running scripts.
3231 *
3232 * TerminateExecution should only be called when then V8 lock has
3233 * been acquired with a Locker object. Therefore, in order to be
3234 * able to terminate long-running threads, preemption must be
3235 * enabled to allow the user of TerminateExecution to acquire the
3236 * lock.
3237 *
3238 * The termination is achieved by throwing an exception that is
3239 * uncatchable by JavaScript exception handlers. Termination
3240 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003241 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003242 * exception handler that catches an exception should check if that
3243 * exception is a termination exception and immediately return if
3244 * that is the case. Returning immediately in that case will
3245 * continue the propagation of the termination exception if needed.
3246 *
3247 * The thread id passed to TerminateExecution must have been
3248 * obtained by calling GetCurrentThreadId on the thread in question.
3249 *
3250 * \param thread_id The thread id of the thread to terminate.
3251 */
3252 static void TerminateExecution(int thread_id);
3253
3254 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003255 * Forcefully terminate the current thread of JavaScript execution
3256 * in the given isolate. If no isolate is provided, the default
3257 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003258 *
3259 * This method can be used by any thread even if that thread has not
3260 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003261 *
3262 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003263 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003264 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003265
3266 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003267 * Is V8 terminating JavaScript execution.
3268 *
3269 * Returns true if JavaScript execution is currently terminating
3270 * because of a call to TerminateExecution. In that case there are
3271 * still JavaScript frames on the stack and the termination
3272 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003273 *
3274 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003275 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003276 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003277
3278 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003279 * Releases any resources used by v8 and stops any utility threads
3280 * that may be running. Note that disposing v8 is permanent, it
3281 * cannot be reinitialized.
3282 *
3283 * It should generally not be necessary to dispose v8 before exiting
3284 * a process, this should happen automatically. It is only necessary
3285 * to use if the process needs the resources taken up by v8.
3286 */
3287 static bool Dispose();
3288
ager@chromium.org3811b432009-10-28 14:53:37 +00003289 /**
3290 * Get statistics about the heap memory usage.
3291 */
3292 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003293
3294 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003295 * Iterates through all external resources referenced from current isolate
3296 * heap. This method is not expected to be used except for debugging purposes
3297 * and may be quite slow.
3298 */
3299 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3300
3301 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003302 * Optional notification that the embedder is idle.
3303 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003304 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003305 * Returns true if the embedder should stop calling IdleNotification
3306 * until real work has been done. This indicates that V8 has done
3307 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003308 *
3309 * The hint argument specifies the amount of work to be done in the function
3310 * on scale from 1 to 1000. There is no guarantee that the actual work will
3311 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003312 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003313 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003314
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003315 /**
3316 * Optional notification that the system is running low on memory.
3317 * V8 uses these notifications to attempt to free memory.
3318 */
3319 static void LowMemoryNotification();
3320
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003321 /**
3322 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003323 * these notifications to guide the GC heuristic. Returns the number
3324 * of context disposals - including this one - since the last time
3325 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003326 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003327 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003328
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329 private:
3330 V8();
3331
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003332 static internal::Object** GlobalizeReference(internal::Object** handle);
3333 static void DisposeGlobal(internal::Object** global_handle);
3334 static void MakeWeak(internal::Object** global_handle,
3335 void* data,
3336 WeakReferenceCallback);
3337 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003338 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003339 static bool IsGlobalNearDeath(internal::Object** global_handle);
3340 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003341 static void SetWrapperClassId(internal::Object** global_handle,
3342 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003343
3344 template <class T> friend class Handle;
3345 template <class T> friend class Local;
3346 template <class T> friend class Persistent;
3347 friend class Context;
3348};
3349
3350
3351/**
3352 * An external exception handler.
3353 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003354class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003355 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003356 /**
3357 * Creates a new try/catch block and registers it with v8.
3358 */
3359 TryCatch();
3360
3361 /**
3362 * Unregisters and deletes this try/catch block.
3363 */
3364 ~TryCatch();
3365
3366 /**
3367 * Returns true if an exception has been caught by this try/catch block.
3368 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003369 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003370
3371 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003372 * For certain types of exceptions, it makes no sense to continue
3373 * execution.
3374 *
3375 * Currently, the only type of exception that can be caught by a
3376 * TryCatch handler and for which it does not make sense to continue
3377 * is termination exception. Such exceptions are thrown when the
3378 * TerminateExecution methods are called to terminate a long-running
3379 * script.
3380 *
3381 * If CanContinue returns false, the correct action is to perform
3382 * any C++ cleanup needed and then return.
3383 */
3384 bool CanContinue() const;
3385
3386 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003387 * Throws the exception caught by this TryCatch in a way that avoids
3388 * it being caught again by this same TryCatch. As with ThrowException
3389 * it is illegal to execute any JavaScript operations after calling
3390 * ReThrow; the caller must return immediately to where the exception
3391 * is caught.
3392 */
3393 Handle<Value> ReThrow();
3394
3395 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003396 * Returns the exception caught by this try/catch block. If no exception has
3397 * been caught an empty handle is returned.
3398 *
3399 * The returned handle is valid until this TryCatch block has been destroyed.
3400 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003401 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003402
3403 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003404 * Returns the .stack property of the thrown object. If no .stack
3405 * property is present an empty handle is returned.
3406 */
3407 Local<Value> StackTrace() const;
3408
3409 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003410 * Returns the message associated with this exception. If there is
3411 * no message associated an empty handle is returned.
3412 *
3413 * The returned handle is valid until this TryCatch block has been
3414 * destroyed.
3415 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003416 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003417
3418 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419 * Clears any exceptions that may have been caught by this try/catch block.
3420 * After this method has been called, HasCaught() will return false.
3421 *
3422 * It is not necessary to clear a try/catch block before using it again; if
3423 * another exception is thrown the previously caught exception will just be
3424 * overwritten. However, it is often a good idea since it makes it easier
3425 * to determine which operation threw a given exception.
3426 */
3427 void Reset();
3428
v8.team.kasperl727e9952008-09-02 14:56:44 +00003429 /**
3430 * Set verbosity of the external exception handler.
3431 *
3432 * By default, exceptions that are caught by an external exception
3433 * handler are not reported. Call SetVerbose with true on an
3434 * external exception handler to have exceptions caught by the
3435 * handler reported as if they were not caught.
3436 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003437 void SetVerbose(bool value);
3438
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003439 /**
3440 * Set whether or not this TryCatch should capture a Message object
3441 * which holds source information about where the exception
3442 * occurred. True by default.
3443 */
3444 void SetCaptureMessage(bool value);
3445
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003446 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003447 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003448 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003449 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003450 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003451 bool is_verbose_ : 1;
3452 bool can_continue_ : 1;
3453 bool capture_message_ : 1;
3454 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003455
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003456 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003457};
3458
3459
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003460// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003461
3462
3463/**
3464 * Ignore
3465 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003466class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003467 public:
3468 ExtensionConfiguration(int name_count, const char* names[])
3469 : name_count_(name_count), names_(names) { }
3470 private:
3471 friend class ImplementationUtilities;
3472 int name_count_;
3473 const char** names_;
3474};
3475
3476
3477/**
3478 * A sandboxed execution context with its own set of built-in objects
3479 * and functions.
3480 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003481class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003482 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003483 /**
3484 * Returns the global proxy object or global object itself for
3485 * detached contexts.
3486 *
3487 * Global proxy object is a thin wrapper whose prototype points to
3488 * actual context's global object with the properties like Object, etc.
3489 * This is done that way for security reasons (for more details see
3490 * https://wiki.mozilla.org/Gecko:SplitWindow).
3491 *
3492 * Please note that changes to global proxy object prototype most probably
3493 * would break VM---v8 expects only global object as a prototype of
3494 * global proxy object.
3495 *
3496 * If DetachGlobal() has been invoked, Global() would return actual global
3497 * object until global is reattached with ReattachGlobal().
3498 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499 Local<Object> Global();
3500
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003501 /**
3502 * Detaches the global object from its context before
3503 * the global object can be reused to create a new context.
3504 */
3505 void DetachGlobal();
3506
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003507 /**
3508 * Reattaches a global object to a context. This can be used to
3509 * restore the connection between a global object and a context
3510 * after DetachGlobal has been called.
3511 *
3512 * \param global_object The global object to reattach to the
3513 * context. For this to work, the global object must be the global
3514 * object that was associated with this context before a call to
3515 * DetachGlobal.
3516 */
3517 void ReattachGlobal(Handle<Object> global_object);
3518
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003519 /** Creates a new context.
3520 *
3521 * Returns a persistent handle to the newly allocated context. This
3522 * persistent handle has to be disposed when the context is no
3523 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003524 *
3525 * \param extensions An optional extension configuration containing
3526 * the extensions to be installed in the newly created context.
3527 *
3528 * \param global_template An optional object template from which the
3529 * global object for the newly created context will be created.
3530 *
3531 * \param global_object An optional global object to be reused for
3532 * the newly created context. This global object must have been
3533 * created by a previous call to Context::New with the same global
3534 * template. The state of the global object will be completely reset
3535 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003536 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003537 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003538 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003539 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3540 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003541
kasper.lund44510672008-07-25 07:37:58 +00003542 /** Returns the last entered context. */
3543 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003544
kasper.lund44510672008-07-25 07:37:58 +00003545 /** Returns the context that is on the top of the stack. */
3546 static Local<Context> GetCurrent();
3547
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003548 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003549 * Returns the context of the calling JavaScript code. That is the
3550 * context of the top-most JavaScript frame. If there are no
3551 * JavaScript frames an empty handle is returned.
3552 */
3553 static Local<Context> GetCalling();
3554
3555 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003556 * Sets the security token for the context. To access an object in
3557 * another context, the security tokens must match.
3558 */
3559 void SetSecurityToken(Handle<Value> token);
3560
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003561 /** Restores the security token to the default value. */
3562 void UseDefaultSecurityToken();
3563
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003564 /** Returns the security token of this context.*/
3565 Handle<Value> GetSecurityToken();
3566
v8.team.kasperl727e9952008-09-02 14:56:44 +00003567 /**
3568 * Enter this context. After entering a context, all code compiled
3569 * and run is compiled and run in this context. If another context
3570 * is already entered, this old context is saved so it can be
3571 * restored when the new context is exited.
3572 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003573 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003574
3575 /**
3576 * Exit this context. Exiting the current context restores the
3577 * context that was in place when entering the current context.
3578 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003579 void Exit();
3580
v8.team.kasperl727e9952008-09-02 14:56:44 +00003581 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003582 bool HasOutOfMemoryException();
3583
v8.team.kasperl727e9952008-09-02 14:56:44 +00003584 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003585 static bool InContext();
3586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003588 * Associate an additional data object with the context. This is mainly used
3589 * with the debugger to provide additional information on the context through
3590 * the debugger API.
3591 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003592 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003593 Local<Value> GetData();
3594
3595 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003596 * Control whether code generation from strings is allowed. Calling
3597 * this method with false will disable 'eval' and the 'Function'
3598 * constructor for code running in this context. If 'eval' or the
3599 * 'Function' constructor are used an exception will be thrown.
3600 *
3601 * If code generation from strings is not allowed the
3602 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3603 * set before blocking the call to 'eval' or the 'Function'
3604 * constructor. If that callback returns true, the call will be
3605 * allowed, otherwise an exception will be thrown. If no callback is
3606 * set an exception will be thrown.
3607 */
3608 void AllowCodeGenerationFromStrings(bool allow);
3609
3610 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003611 * Returns true if code generation from strings is allowed for the context.
3612 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3613 */
3614 bool IsCodeGenerationFromStringsAllowed();
3615
3616 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003617 * Stack-allocated class which sets the execution context for all
3618 * operations executed within a local scope.
3619 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003620 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003621 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003622 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003623 context_->Enter();
3624 }
3625 inline ~Scope() { context_->Exit(); }
3626 private:
3627 Handle<Context> context_;
3628 };
3629
3630 private:
3631 friend class Value;
3632 friend class Script;
3633 friend class Object;
3634 friend class Function;
3635};
3636
3637
3638/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003639 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003640 * is allowed to use any given V8 isolate. See Isolate class
3641 * comments. The definition of 'using V8 isolate' includes
3642 * accessing handles or holding onto object pointers obtained
3643 * from V8 handles while in the particular V8 isolate. It is up
3644 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003645 * constraint is not violated. In addition to any other synchronization
3646 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3647 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003648 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003649 * v8::Locker is a scoped lock object. While it's
3650 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003651 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3652 * locked by at most one thread at any time. In other words, the scope of a
3653 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003654 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003655 * Sample usage:
3656* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003657 * ...
3658 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003659 * v8::Locker locker(isolate);
3660 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003661 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003662 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003663 * ...
3664 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003665 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003666 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003667 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003668 * by destroying the v8::Locker object as above or by constructing a
3669 * v8::Unlocker object:
3670 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003671 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003672 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003673 * isolate->Exit();
3674 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003675 * ...
3676 * // Code not using V8 goes here while V8 can run in another thread.
3677 * ...
3678 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003679 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003680 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003681 *
3682 * The Unlocker object is intended for use in a long-running callback
3683 * from V8, where you want to release the V8 lock for other threads to
3684 * use.
3685 *
3686 * The v8::Locker is a recursive lock. That is, you can lock more than
3687 * once in a given thread. This can be useful if you have code that can
3688 * be called either from code that holds the lock or from code that does
3689 * not. The Unlocker is not recursive so you can not have several
3690 * Unlockers on the stack at once, and you can not use an Unlocker in a
3691 * thread that is not inside a Locker's scope.
3692 *
3693 * An unlocker will unlock several lockers if it has to and reinstate
3694 * the correct depth of locking on its destruction. eg.:
3695 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003696 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003697 * // V8 not locked.
3698 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003699 * v8::Locker locker(isolate);
3700 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003701 * // V8 locked.
3702 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003703 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003704 * // V8 still locked (2 levels).
3705 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003706 * isolate->Exit();
3707 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003708 * // V8 not locked.
3709 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003710 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711 * // V8 locked again (2 levels).
3712 * }
3713 * // V8 still locked (1 level).
3714 * }
3715 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003716 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003717 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003718 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003719 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003720class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003721 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003722 /**
3723 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3724 */
3725 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003726 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003727 private:
3728 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003729};
3730
3731
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003732class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003733 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003734 /**
3735 * Initialize Locker for a given Isolate. NULL means default isolate.
3736 */
3737 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003738 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003739
3740 /**
3741 * Start preemption.
3742 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003743 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003744 * that will switch between multiple threads that are in contention
3745 * for the V8 lock.
3746 */
3747 static void StartPreemption(int every_n_ms);
3748
3749 /**
3750 * Stop preemption.
3751 */
3752 static void StopPreemption();
3753
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003754 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003755 * Returns whether or not the locker for a given isolate, or default isolate
3756 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003757 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003758 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003759
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003760 /**
3761 * Returns whether v8::Locker is being used by this V8 instance.
3762 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003763 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003764
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003765 private:
3766 bool has_lock_;
3767 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003768 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003769
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003770 static bool active_;
3771
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003772 // Disallow copying and assigning.
3773 Locker(const Locker&);
3774 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003775};
3776
3777
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003778/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003779 * A struct for exporting HeapStats data from V8, using "push" model.
3780 */
3781struct HeapStatsUpdate;
3782
3783
3784/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003785 * An interface for exporting data from V8, using "push" model.
3786 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003787class V8EXPORT OutputStream { // NOLINT
3788 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003789 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003790 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003791 };
3792 enum WriteResult {
3793 kContinue = 0,
3794 kAbort = 1
3795 };
3796 virtual ~OutputStream() {}
3797 /** Notify about the end of stream. */
3798 virtual void EndOfStream() = 0;
3799 /** Get preferred output chunk size. Called only once. */
3800 virtual int GetChunkSize() { return 1024; }
3801 /** Get preferred output encoding. Called only once. */
3802 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3803 /**
3804 * Writes the next chunk of snapshot data into the stream. Writing
3805 * can be stopped by returning kAbort as function result. EndOfStream
3806 * will not be called in case writing was aborted.
3807 */
3808 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003809 /**
3810 * Writes the next chunk of heap stats 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 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003814 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00003815 return kAbort;
3816 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003817};
3818
3819
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003820/**
3821 * An interface for reporting progress and controlling long-running
3822 * activities.
3823 */
3824class V8EXPORT ActivityControl { // NOLINT
3825 public:
3826 enum ControlOption {
3827 kContinue = 0,
3828 kAbort = 1
3829 };
3830 virtual ~ActivityControl() {}
3831 /**
3832 * Notify about current progress. The activity can be stopped by
3833 * returning kAbort as the callback result.
3834 */
3835 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3836};
3837
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003838
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003839// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003841
3842namespace internal {
3843
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003844const int kApiPointerSize = sizeof(void*); // NOLINT
3845const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003846
3847// Tag information for HeapObject.
3848const int kHeapObjectTag = 1;
3849const int kHeapObjectTagSize = 2;
3850const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3851
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003852// Tag information for Smi.
3853const int kSmiTag = 0;
3854const int kSmiTagSize = 1;
3855const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3856
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003857template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003858
3859// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003860template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003861 static const int kSmiShiftSize = 0;
3862 static const int kSmiValueSize = 31;
3863 static inline int SmiToInt(internal::Object* value) {
3864 int shift_bits = kSmiTagSize + kSmiShiftSize;
3865 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3866 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3867 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003868
3869 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3870 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003871 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003872 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003873};
3874
3875// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003876template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003877 static const int kSmiShiftSize = 31;
3878 static const int kSmiValueSize = 32;
3879 static inline int SmiToInt(internal::Object* value) {
3880 int shift_bits = kSmiTagSize + kSmiShiftSize;
3881 // Shift down and throw away top 32 bits.
3882 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3883 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003884
3885 // To maximize the range of pointers that can be encoded
3886 // in the available 32 bits, we require them to be 8 bytes aligned.
3887 // This gives 2 ^ (32 + 3) = 32G address space covered.
3888 // It might be not enough to cover stack allocated objects on some platforms.
3889 static const int kPointerAlignment = 3;
3890
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003891 static const uintptr_t kEncodablePointerMask =
3892 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003893
3894 static const int kPointerToSmiShift =
3895 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003896};
3897
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003898typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3899const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3900const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003901const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003902 PlatformSmiTagging::kEncodablePointerMask;
3903const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003904
3905/**
3906 * This class exports constants and functionality from within v8 that
3907 * is necessary to implement inline functions in the v8 api. Don't
3908 * depend on functions and constants defined here.
3909 */
3910class Internals {
3911 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003912 // These values match non-compiler-dependent values defined within
3913 // the implementation of v8.
3914 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003915 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003916 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003917
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003918 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003919 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003920 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003921 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003922 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003923
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003924 static const int kIsolateStateOffset = 0;
3925 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
3926 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
3927 static const int kUndefinedValueRootIndex = 5;
3928 static const int kNullValueRootIndex = 7;
3929 static const int kTrueValueRootIndex = 8;
3930 static const int kFalseValueRootIndex = 9;
3931 static const int kEmptySymbolRootIndex = 128;
3932
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003933 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003934 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003935 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003936 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003937
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003938 static const int kUndefinedOddballKind = 5;
3939 static const int kNullOddballKind = 3;
3940
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003941 static inline bool HasHeapObjectTag(internal::Object* value) {
3942 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3943 kHeapObjectTag);
3944 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003945
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003946 static inline bool HasSmiTag(internal::Object* value) {
3947 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3948 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003949
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003950 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003951 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003952 }
3953
3954 static inline int GetInstanceType(internal::Object* obj) {
3955 typedef internal::Object O;
3956 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3957 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3958 }
3959
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003960 static inline int GetOddballKind(internal::Object* obj) {
3961 typedef internal::Object O;
3962 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
3963 }
3964
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003965 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003966 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003967 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3968 }
3969
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003970 static inline void* GetExternalPointer(internal::Object* obj) {
3971 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003972 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003973 } else if (GetInstanceType(obj) == kForeignType) {
3974 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003975 } else {
3976 return NULL;
3977 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003978 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003979
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003980 static inline bool IsExternalTwoByteString(int instance_type) {
3981 int representation = (instance_type & kFullStringRepresentationMask);
3982 return representation == kExternalTwoByteRepresentationTag;
3983 }
3984
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003985 static inline bool IsInitialized(v8::Isolate* isolate) {
3986 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
3987 return *reinterpret_cast<int*>(addr) == 1;
3988 }
3989
3990 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
3991 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
3992 kIsolateEmbedderDataOffset;
3993 *reinterpret_cast<void**>(addr) = data;
3994 }
3995
3996 static inline void* GetEmbedderData(v8::Isolate* isolate) {
3997 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
3998 kIsolateEmbedderDataOffset;
3999 return *reinterpret_cast<void**>(addr);
4000 }
4001
4002 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
4003 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4004 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4005 }
4006
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004007 template <typename T>
4008 static inline T ReadField(Object* ptr, int offset) {
4009 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4010 return *reinterpret_cast<T*>(addr);
4011 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004012
4013 static inline bool CanCastToHeapObject(void* o) { return false; }
4014 static inline bool CanCastToHeapObject(Context* o) { return true; }
4015 static inline bool CanCastToHeapObject(String* o) { return true; }
4016 static inline bool CanCastToHeapObject(Object* o) { return true; }
4017 static inline bool CanCastToHeapObject(Message* o) { return true; }
4018 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
4019 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004020};
4021
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004022} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004023
4024
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004025template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004026Local<T>::Local() : Handle<T>() { }
4027
4028
4029template <class T>
4030Local<T> Local<T>::New(Handle<T> that) {
4031 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004032 T* that_ptr = *that;
4033 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4034 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4035 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4036 reinterpret_cast<internal::HeapObject*>(*p))));
4037 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004038 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4039}
4040
4041
4042template <class T>
4043Persistent<T> Persistent<T>::New(Handle<T> that) {
4044 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004045 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004046 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4047}
4048
4049
4050template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004051bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004052 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004053 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004054}
4055
4056
4057template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004058bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004059 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004060 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004061}
4062
4063
4064template <class T>
4065void Persistent<T>::Dispose() {
4066 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004067 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004068}
4069
4070
4071template <class T>
4072Persistent<T>::Persistent() : Handle<T>() { }
4073
4074template <class T>
4075void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004076 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4077 parameters,
4078 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004079}
4080
4081template <class T>
4082void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004083 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084}
4085
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004086template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004087void Persistent<T>::MarkIndependent() {
4088 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4089}
4090
4091template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004092void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4093 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4094}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004095
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004096Arguments::Arguments(internal::Object** implicit_args,
4097 internal::Object** values, int length,
4098 bool is_construct_call)
4099 : implicit_args_(implicit_args),
4100 values_(values),
4101 length_(length),
4102 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004103
4104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004105Local<Value> Arguments::operator[](int i) const {
4106 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4107 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4108}
4109
4110
4111Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004112 return Local<Function>(reinterpret_cast<Function*>(
4113 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004114}
4115
4116
4117Local<Object> Arguments::This() const {
4118 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4119}
4120
4121
4122Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004123 return Local<Object>(reinterpret_cast<Object*>(
4124 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004125}
4126
4127
4128Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004129 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004130}
4131
4132
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004133Isolate* Arguments::GetIsolate() const {
4134 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4135}
4136
4137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004138bool Arguments::IsConstructCall() const {
4139 return is_construct_call_;
4140}
4141
4142
4143int Arguments::Length() const {
4144 return length_;
4145}
4146
4147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004148template <class T>
4149Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004150 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4151 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004152 return Local<T>(reinterpret_cast<T*>(after));
4153}
4154
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004155Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004156 return resource_name_;
4157}
4158
4159
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004160Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004161 return resource_line_offset_;
4162}
4163
4164
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004165Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004166 return resource_column_offset_;
4167}
4168
4169
4170Handle<Boolean> Boolean::New(bool value) {
4171 return value ? True() : False();
4172}
4173
4174
4175void Template::Set(const char* name, v8::Handle<Data> value) {
4176 Set(v8::String::New(name), value);
4177}
4178
4179
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004180Local<Value> Object::GetInternalField(int index) {
4181#ifndef V8_ENABLE_CHECKS
4182 Local<Value> quick_result = UncheckedGetInternalField(index);
4183 if (!quick_result.IsEmpty()) return quick_result;
4184#endif
4185 return CheckedGetInternalField(index);
4186}
4187
4188
4189Local<Value> Object::UncheckedGetInternalField(int index) {
4190 typedef internal::Object O;
4191 typedef internal::Internals I;
4192 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004193 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004194 // If the object is a plain JSObject, which is the common case,
4195 // we know where to find the internal fields and can return the
4196 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004197 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004198 O* value = I::ReadField<O*>(obj, offset);
4199 O** result = HandleScope::CreateHandle(value);
4200 return Local<Value>(reinterpret_cast<Value*>(result));
4201 } else {
4202 return Local<Value>();
4203 }
4204}
4205
4206
4207void* External::Unwrap(Handle<v8::Value> obj) {
4208#ifdef V8_ENABLE_CHECKS
4209 return FullUnwrap(obj);
4210#else
4211 return QuickUnwrap(obj);
4212#endif
4213}
4214
4215
4216void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4217 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004218 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004219 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004220}
4221
4222
4223void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004224 typedef internal::Object O;
4225 typedef internal::Internals I;
4226
4227 O* obj = *reinterpret_cast<O**>(this);
4228
4229 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4230 // If the object is a plain JSObject, which is the common case,
4231 // we know where to find the internal fields and can return the
4232 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004233 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004234 O* value = I::ReadField<O*>(obj, offset);
4235 return I::GetExternalPointer(value);
4236 }
4237
4238 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004239}
4240
4241
4242String* String::Cast(v8::Value* value) {
4243#ifdef V8_ENABLE_CHECKS
4244 CheckCast(value);
4245#endif
4246 return static_cast<String*>(value);
4247}
4248
4249
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004250Local<String> String::Empty(Isolate* isolate) {
4251 typedef internal::Object* S;
4252 typedef internal::Internals I;
4253 if (!I::IsInitialized(isolate)) return Empty();
4254 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4255 return Local<String>(reinterpret_cast<String*>(slot));
4256}
4257
4258
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004259String::ExternalStringResource* String::GetExternalStringResource() const {
4260 typedef internal::Object O;
4261 typedef internal::Internals I;
4262 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004263 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004264 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004265 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4266 result = reinterpret_cast<String::ExternalStringResource*>(value);
4267 } else {
4268 result = NULL;
4269 }
4270#ifdef V8_ENABLE_CHECKS
4271 VerifyExternalStringResource(result);
4272#endif
4273 return result;
4274}
4275
4276
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004277bool Value::IsUndefined() const {
4278#ifdef V8_ENABLE_CHECKS
4279 return FullIsUndefined();
4280#else
4281 return QuickIsUndefined();
4282#endif
4283}
4284
4285bool Value::QuickIsUndefined() const {
4286 typedef internal::Object O;
4287 typedef internal::Internals I;
4288 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4289 if (!I::HasHeapObjectTag(obj)) return false;
4290 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4291 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4292}
4293
4294
4295bool Value::IsNull() const {
4296#ifdef V8_ENABLE_CHECKS
4297 return FullIsNull();
4298#else
4299 return QuickIsNull();
4300#endif
4301}
4302
4303bool Value::QuickIsNull() const {
4304 typedef internal::Object O;
4305 typedef internal::Internals I;
4306 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4307 if (!I::HasHeapObjectTag(obj)) return false;
4308 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4309 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4310}
4311
4312
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004313bool Value::IsString() const {
4314#ifdef V8_ENABLE_CHECKS
4315 return FullIsString();
4316#else
4317 return QuickIsString();
4318#endif
4319}
4320
4321bool Value::QuickIsString() const {
4322 typedef internal::Object O;
4323 typedef internal::Internals I;
4324 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4325 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004326 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004327}
4328
4329
4330Number* Number::Cast(v8::Value* value) {
4331#ifdef V8_ENABLE_CHECKS
4332 CheckCast(value);
4333#endif
4334 return static_cast<Number*>(value);
4335}
4336
4337
4338Integer* Integer::Cast(v8::Value* value) {
4339#ifdef V8_ENABLE_CHECKS
4340 CheckCast(value);
4341#endif
4342 return static_cast<Integer*>(value);
4343}
4344
4345
4346Date* Date::Cast(v8::Value* value) {
4347#ifdef V8_ENABLE_CHECKS
4348 CheckCast(value);
4349#endif
4350 return static_cast<Date*>(value);
4351}
4352
4353
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004354StringObject* StringObject::Cast(v8::Value* value) {
4355#ifdef V8_ENABLE_CHECKS
4356 CheckCast(value);
4357#endif
4358 return static_cast<StringObject*>(value);
4359}
4360
4361
4362NumberObject* NumberObject::Cast(v8::Value* value) {
4363#ifdef V8_ENABLE_CHECKS
4364 CheckCast(value);
4365#endif
4366 return static_cast<NumberObject*>(value);
4367}
4368
4369
4370BooleanObject* BooleanObject::Cast(v8::Value* value) {
4371#ifdef V8_ENABLE_CHECKS
4372 CheckCast(value);
4373#endif
4374 return static_cast<BooleanObject*>(value);
4375}
4376
4377
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004378RegExp* RegExp::Cast(v8::Value* value) {
4379#ifdef V8_ENABLE_CHECKS
4380 CheckCast(value);
4381#endif
4382 return static_cast<RegExp*>(value);
4383}
4384
4385
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004386Object* Object::Cast(v8::Value* value) {
4387#ifdef V8_ENABLE_CHECKS
4388 CheckCast(value);
4389#endif
4390 return static_cast<Object*>(value);
4391}
4392
4393
4394Array* Array::Cast(v8::Value* value) {
4395#ifdef V8_ENABLE_CHECKS
4396 CheckCast(value);
4397#endif
4398 return static_cast<Array*>(value);
4399}
4400
4401
4402Function* Function::Cast(v8::Value* value) {
4403#ifdef V8_ENABLE_CHECKS
4404 CheckCast(value);
4405#endif
4406 return static_cast<Function*>(value);
4407}
4408
4409
4410External* External::Cast(v8::Value* value) {
4411#ifdef V8_ENABLE_CHECKS
4412 CheckCast(value);
4413#endif
4414 return static_cast<External*>(value);
4415}
4416
4417
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004418Isolate* AccessorInfo::GetIsolate() const {
4419 return *reinterpret_cast<Isolate**>(&args_[-3]);
4420}
4421
4422
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004423Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004424 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004425}
4426
4427
4428Local<Object> AccessorInfo::This() const {
4429 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4430}
4431
4432
4433Local<Object> AccessorInfo::Holder() const {
4434 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4435}
4436
4437
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004438Handle<Primitive> Undefined(Isolate* isolate) {
4439 typedef internal::Object* S;
4440 typedef internal::Internals I;
4441 if (!I::IsInitialized(isolate)) return Undefined();
4442 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4443 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4444}
4445
4446
4447Handle<Primitive> Null(Isolate* isolate) {
4448 typedef internal::Object* S;
4449 typedef internal::Internals I;
4450 if (!I::IsInitialized(isolate)) return Null();
4451 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4452 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4453}
4454
4455
4456Handle<Boolean> True(Isolate* isolate) {
4457 typedef internal::Object* S;
4458 typedef internal::Internals I;
4459 if (!I::IsInitialized(isolate)) return True();
4460 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4461 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4462}
4463
4464
4465Handle<Boolean> False(Isolate* isolate) {
4466 typedef internal::Object* S;
4467 typedef internal::Internals I;
4468 if (!I::IsInitialized(isolate)) return False();
4469 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4470 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4471}
4472
4473
4474void Isolate::SetData(void* data) {
4475 typedef internal::Internals I;
4476 I::SetEmbedderData(this, data);
4477}
4478
4479
4480void* Isolate::GetData() {
4481 typedef internal::Internals I;
4482 return I::GetEmbedderData(this);
4483}
4484
4485
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004486/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004487 * \example shell.cc
4488 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004489 * command-line and executes them.
4490 */
4491
4492
4493/**
4494 * \example process.cc
4495 */
4496
4497
4498} // namespace v8
4499
4500
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004501#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004502#undef TYPE_CHECK
4503
4504
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004505#endif // V8_H_