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