blob: 5c2c8b6671827f56ddddb6ef4a71182b6d54c164 [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,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001067 NO_NULL_TERMINATION = 2,
1068 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001069 };
1070
lrn@chromium.org34e60782011-09-15 07:25:40 +00001071 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001072 V8EXPORT int Write(uint16_t* buffer,
1073 int start = 0,
1074 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001075 int options = NO_OPTIONS) const;
1076 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001077 V8EXPORT int WriteAscii(char* buffer,
1078 int start = 0,
1079 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001080 int options = NO_OPTIONS) const;
1081 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001082 V8EXPORT int WriteUtf8(char* buffer,
1083 int length = -1,
1084 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001085 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086
v8.team.kasperl727e9952008-09-02 14:56:44 +00001087 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001088 * A zero length string.
1089 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001090 V8EXPORT static v8::Local<v8::String> Empty();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001091 inline static v8::Local<v8::String> Empty(Isolate* isolate);
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001092
1093 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001094 * Returns true if the string is external
1095 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001096 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097
v8.team.kasperl727e9952008-09-02 14:56:44 +00001098 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001099 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001100 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001101 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001102
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001103 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001104 public:
1105 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001106
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001107 protected:
1108 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001109
1110 /**
1111 * Internally V8 will call this Dispose method when the external string
1112 * resource is no longer needed. The default implementation will use the
1113 * delete operator. This method can be overridden in subclasses to
1114 * control how allocated external string resources are disposed.
1115 */
1116 virtual void Dispose() { delete this; }
1117
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001118 private:
1119 // Disallow copying and assigning.
1120 ExternalStringResourceBase(const ExternalStringResourceBase&);
1121 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001122
1123 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001124 };
1125
v8.team.kasperl727e9952008-09-02 14:56:44 +00001126 /**
1127 * An ExternalStringResource is a wrapper around a two-byte string
1128 * buffer that resides outside V8's heap. Implement an
1129 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001130 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001131 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001132 class V8EXPORT ExternalStringResource
1133 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001134 public:
1135 /**
1136 * Override the destructor to manage the life cycle of the underlying
1137 * buffer.
1138 */
1139 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001140
1141 /**
1142 * The string data from the underlying buffer.
1143 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001145
1146 /**
1147 * The length of the string. That is, the number of two-byte characters.
1148 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001150
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151 protected:
1152 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153 };
1154
1155 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001156 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001157 * string buffer that resides outside V8's heap. Implement an
1158 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001159 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001160 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001161 * UTF-8, which would require special treatment internally in the
1162 * engine and, in the case of UTF-8, do not allow efficient indexing.
1163 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001164 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001166 class V8EXPORT ExternalAsciiStringResource
1167 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168 public:
1169 /**
1170 * Override the destructor to manage the life cycle of the underlying
1171 * buffer.
1172 */
1173 virtual ~ExternalAsciiStringResource() {}
1174 /** The string data from the underlying buffer.*/
1175 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001176 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 virtual size_t length() const = 0;
1178 protected:
1179 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 };
1181
1182 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001183 * Get the ExternalStringResource for an external string. Returns
1184 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001186 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187
1188 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001189 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001190 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001192 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1193 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001195 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001196
1197 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001198 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001200 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 * be careful to supply the length parameter.
1202 * If it is not given, the function calls
1203 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001204 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001206 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207
lrn@chromium.org34e60782011-09-15 07:25:40 +00001208 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001209 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210
1211 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001212 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213
v8.team.kasperl727e9952008-09-02 14:56:44 +00001214 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001215 * Creates a new string by concatenating the left and the right strings
1216 * passed in as parameters.
1217 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001218 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001219 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001220
1221 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001222 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001223 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001224 * resource will be disposed by calling its Dispose method. The caller of
1225 * this function should not otherwise delete or modify the resource. Neither
1226 * should the underlying buffer be deallocated or modified except through the
1227 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001228 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001229 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001230
ager@chromium.org6f10e412009-02-13 10:11:16 +00001231 /**
1232 * Associate an external string resource with this string by transforming it
1233 * in place so that existing references to this string in the JavaScript heap
1234 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001235 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001236 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001237 * The string is not modified if the operation fails. See NewExternal for
1238 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001239 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001240 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241
v8.team.kasperl727e9952008-09-02 14:56:44 +00001242 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001243 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001244 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001245 * resource will be disposed by calling its Dispose method. The caller of
1246 * this function should not otherwise delete or modify the resource. Neither
1247 * should the underlying buffer be deallocated or modified except through the
1248 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001249 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001250 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001251
ager@chromium.org6f10e412009-02-13 10:11:16 +00001252 /**
1253 * Associate an external string resource with this string by transforming it
1254 * in place so that existing references to this string in the JavaScript heap
1255 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001256 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001257 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001258 * The string is not modified if the operation fails. See NewExternal for
1259 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001260 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001261 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001263 /**
1264 * Returns true if this string can be made external.
1265 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001266 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001267
lrn@chromium.org34e60782011-09-15 07:25:40 +00001268 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001269 V8EXPORT static Local<String> NewUndetectable(const char* data,
1270 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271
lrn@chromium.org34e60782011-09-15 07:25:40 +00001272 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001273 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1274 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275
1276 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001277 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001278 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001279 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001280 * then the length() method returns 0 and the * operator returns
1281 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001282 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001283 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001284 public:
1285 explicit Utf8Value(Handle<v8::Value> obj);
1286 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001287 char* operator*() { return str_; }
1288 const char* operator*() const { return str_; }
1289 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001290 private:
1291 char* str_;
1292 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001293
1294 // Disallow copying and assigning.
1295 Utf8Value(const Utf8Value&);
1296 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001297 };
1298
1299 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001300 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001302 * If conversion to a string fails (eg. due to an exception in the toString()
1303 * method of the object) then the length() method returns 0 and the * operator
1304 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001306 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 public:
1308 explicit AsciiValue(Handle<v8::Value> obj);
1309 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001310 char* operator*() { return str_; }
1311 const char* operator*() const { return str_; }
1312 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 private:
1314 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001315 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001316
1317 // Disallow copying and assigning.
1318 AsciiValue(const AsciiValue&);
1319 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 };
1321
1322 /**
1323 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001324 * If conversion to a string fails (eg. due to an exception in the toString()
1325 * method of the object) then the length() method returns 0 and the * operator
1326 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001328 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 public:
1330 explicit Value(Handle<v8::Value> obj);
1331 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001332 uint16_t* operator*() { return str_; }
1333 const uint16_t* operator*() const { return str_; }
1334 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001335 private:
1336 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001337 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001338
1339 // Disallow copying and assigning.
1340 Value(const Value&);
1341 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001343
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001344 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001345 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1346 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347};
1348
1349
1350/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001351 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001353class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001355 V8EXPORT double Value() const;
1356 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001357 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001359 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001360 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361};
1362
1363
1364/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001365 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001367class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001369 V8EXPORT static Local<Integer> New(int32_t value);
1370 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1371 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001372 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001374 V8EXPORT Integer();
1375 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376};
1377
1378
1379/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001380 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001382class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001384 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001386 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387};
1388
1389
1390/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001391 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001393class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001395 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001397 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398};
1399
1400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401enum PropertyAttribute {
1402 None = 0,
1403 ReadOnly = 1 << 0,
1404 DontEnum = 1 << 1,
1405 DontDelete = 1 << 2
1406};
1407
ager@chromium.org3811b432009-10-28 14:53:37 +00001408enum ExternalArrayType {
1409 kExternalByteArray = 1,
1410 kExternalUnsignedByteArray,
1411 kExternalShortArray,
1412 kExternalUnsignedShortArray,
1413 kExternalIntArray,
1414 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001415 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001416 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001417 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001418};
1419
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001421 * Accessor[Getter|Setter] are used as callback functions when
1422 * setting|getting a particular property. See Object and ObjectTemplate's
1423 * method SetAccessor.
1424 */
1425typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1426 const AccessorInfo& info);
1427
1428
1429typedef void (*AccessorSetter)(Local<String> property,
1430 Local<Value> value,
1431 const AccessorInfo& info);
1432
1433
1434/**
1435 * Access control specifications.
1436 *
1437 * Some accessors should be accessible across contexts. These
1438 * accessors have an explicit access control parameter which specifies
1439 * the kind of cross-context access that should be allowed.
1440 *
1441 * Additionally, for security, accessors can prohibit overwriting by
1442 * accessors defined in JavaScript. For objects that have such
1443 * accessors either locally or in their prototype chain it is not
1444 * possible to overwrite the accessor by using __defineGetter__ or
1445 * __defineSetter__ from JavaScript code.
1446 */
1447enum AccessControl {
1448 DEFAULT = 0,
1449 ALL_CAN_READ = 1,
1450 ALL_CAN_WRITE = 1 << 1,
1451 PROHIBITS_OVERWRITING = 1 << 2
1452};
1453
1454
1455/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001456 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001458class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001460 V8EXPORT bool Set(Handle<Value> key,
1461 Handle<Value> value,
1462 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001463
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001464 V8EXPORT bool Set(uint32_t index,
1465 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001466
ager@chromium.orge2902be2009-06-08 12:21:35 +00001467 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001468 // overriding accessors or read-only properties.
1469 //
1470 // Note that if the object has an interceptor the property will be set
1471 // locally, but since the interceptor takes precedence the local property
1472 // will only be returned if the interceptor doesn't return a value.
1473 //
1474 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001475 V8EXPORT bool ForceSet(Handle<Value> key,
1476 Handle<Value> value,
1477 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001478
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001479 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001481 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001482
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001483 /**
1484 * Gets the property attributes of a property which can be None or
1485 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1486 * None when the property doesn't exist.
1487 */
1488 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1489
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490 // TODO(1245389): Replace the type-specific versions of these
1491 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001492 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001493
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001494 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001495
1496 // Delete a property on this object bypassing interceptors and
1497 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001498 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001499
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001500 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001501
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001502 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001504 V8EXPORT bool SetAccessor(Handle<String> name,
1505 AccessorGetter getter,
1506 AccessorSetter setter = 0,
1507 Handle<Value> data = Handle<Value>(),
1508 AccessControl settings = DEFAULT,
1509 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001510
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001512 * Returns an array containing the names of the enumerable properties
1513 * of this object, including properties from prototype objects. The
1514 * array returned by this method contains the same values as would
1515 * be enumerated by a for-in statement over this object.
1516 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001517 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001518
1519 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001520 * This function has the same functionality as GetPropertyNames but
1521 * the returned array doesn't contain the names of properties from
1522 * prototype objects.
1523 */
1524 V8EXPORT Local<Array> GetOwnPropertyNames();
1525
1526 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 * Get the prototype object. This does not skip objects marked to
1528 * be skipped by __proto__ and it does not consult the security
1529 * handler.
1530 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001531 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001532
1533 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001534 * Set the prototype object. This does not skip objects marked to
1535 * be skipped by __proto__ and it does not consult the security
1536 * handler.
1537 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001538 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001539
1540 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001541 * Finds an instance of the given function template in the prototype
1542 * chain.
1543 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001544 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1545 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001546
1547 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548 * Call builtin Object.prototype.toString on this object.
1549 * This is different from Value::ToString() that may call
1550 * user-defined toString function. This one does not.
1551 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001552 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001554 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001555 * Returns the function invoked as a constructor for this object.
1556 * May be the null value.
1557 */
1558 V8EXPORT Local<Value> GetConstructor();
1559
1560 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001561 * Returns the name of the function invoked as a constructor for this object.
1562 */
1563 V8EXPORT Local<String> GetConstructorName();
1564
kasper.lund212ac232008-07-16 07:07:30 +00001565 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001566 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001567 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001568 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001569 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001570 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001572 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001573 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001574
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001575 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001576 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001577
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001579 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001580 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1581 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1582 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583
1584 /**
1585 * If result.IsEmpty() no real property was located in the prototype chain.
1586 * This means interceptors in the prototype chain are not called.
1587 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001588 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1589 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001590
1591 /**
1592 * If result.IsEmpty() no real property was located on the object or
1593 * in the prototype chain.
1594 * This means interceptors in the prototype chain are not called.
1595 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001596 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597
1598 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001599 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600
kasper.lund212ac232008-07-16 07:07:30 +00001601 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001602 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001604 /**
1605 * Turns on access check on the object if the object is an instance of
1606 * a template that has access check callbacks. If an object has no
1607 * access check info, the object cannot be accessed by anyone.
1608 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001609 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001610
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001611 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001612 * Returns the identity hash for this object. The current implementation
1613 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001614 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001615 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001616 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001617 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001618 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001619
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001620 /**
1621 * Access hidden properties on JavaScript objects. These properties are
1622 * hidden from the executing JavaScript and only accessible through the V8
1623 * C++ API. Hidden properties introduced by V8 internally (for example the
1624 * identity hash) are prefixed with "v8::".
1625 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001626 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1627 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1628 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001629
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001630 /**
1631 * Returns true if this is an instance of an api function (one
1632 * created from a function created from a function template) and has
1633 * been modified since it was created. Note that this method is
1634 * conservative and may return true for objects that haven't actually
1635 * been modified.
1636 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001637 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001638
1639 /**
1640 * Clone this object with a fast but shallow copy. Values will point
1641 * to the same values as the original object.
1642 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001643 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001645 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001646 * Returns the context in which the object was created.
1647 */
1648 V8EXPORT Local<Context> CreationContext();
1649
1650 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001651 * Set the backing store of the indexed properties to be managed by the
1652 * embedding layer. Access to the indexed properties will follow the rules
1653 * spelled out in CanvasPixelArray.
1654 * Note: The embedding program still owns the data and needs to ensure that
1655 * the backing store is preserved while V8 has a reference.
1656 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001657 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001658 V8EXPORT bool HasIndexedPropertiesInPixelData();
1659 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1660 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001661
ager@chromium.org3811b432009-10-28 14:53:37 +00001662 /**
1663 * Set the backing store of the indexed properties to be managed by the
1664 * embedding layer. Access to the indexed properties will follow the rules
1665 * spelled out for the CanvasArray subtypes in the WebGL specification.
1666 * Note: The embedding program still owns the data and needs to ensure that
1667 * the backing store is preserved while V8 has a reference.
1668 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001669 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1670 void* data,
1671 ExternalArrayType array_type,
1672 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001673 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1674 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1675 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1676 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001677
lrn@chromium.org1c092762011-05-09 09:42:16 +00001678 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001679 * Checks whether a callback is set by the
1680 * ObjectTemplate::SetCallAsFunctionHandler method.
1681 * When an Object is callable this method returns true.
1682 */
1683 V8EXPORT bool IsCallable();
1684
1685 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001686 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001687 * ObjectTemplate::SetCallAsFunctionHandler method.
1688 */
1689 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1690 int argc,
1691 Handle<Value> argv[]);
1692
1693 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001694 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001695 * ObjectTemplate::SetCallAsFunctionHandler method.
1696 * Note: This method behaves like the Function::NewInstance method.
1697 */
1698 V8EXPORT Local<Value> CallAsConstructor(int argc,
1699 Handle<Value> argv[]);
1700
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001701 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001702 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001703
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001705 V8EXPORT Object();
1706 V8EXPORT static void CheckCast(Value* obj);
1707 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1708 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001709
1710 /**
1711 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001712 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001713 */
1714 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715};
1716
1717
1718/**
1719 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1720 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001721class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001722 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001723 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724
ager@chromium.org3e875802009-06-29 08:26:34 +00001725 /**
1726 * Clones an element at index |index|. Returns an empty
1727 * handle if cloning fails (for any reason).
1728 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001729 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001730
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001731 /**
1732 * Creates a JavaScript array with the given length. If the length
1733 * is negative the returned array will have length 0.
1734 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001735 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001736
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001737 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001739 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001740 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741};
1742
1743
1744/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001745 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001746 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001747class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001749 V8EXPORT Local<Object> NewInstance() const;
1750 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1751 V8EXPORT Local<Value> Call(Handle<Object> recv,
1752 int argc,
1753 Handle<Value> argv[]);
1754 V8EXPORT void SetName(Handle<String> name);
1755 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001756
1757 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001758 * Name inferred from variable or property assignment of this function.
1759 * Used to facilitate debugging and profiling of JavaScript code written
1760 * in an OO style, where many functions are anonymous but are assigned
1761 * to object properties.
1762 */
1763 V8EXPORT Handle<Value> GetInferredName() const;
1764
1765 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001766 * Returns zero based line number of function body and
1767 * kLineOffsetNotFound if no information available.
1768 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001769 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001770 /**
1771 * Returns zero based column number of function body and
1772 * kLineOffsetNotFound if no information available.
1773 */
1774 V8EXPORT int GetScriptColumnNumber() const;
1775 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001776 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001777 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001778 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001779
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001781 V8EXPORT Function();
1782 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783};
1784
1785
1786/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001787 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1788 */
1789class Date : public Object {
1790 public:
1791 V8EXPORT static Local<Value> New(double time);
1792
1793 /**
1794 * A specialization of Value::NumberValue that is more efficient
1795 * because we know the structure of this object.
1796 */
1797 V8EXPORT double NumberValue() const;
1798
1799 static inline Date* Cast(v8::Value* obj);
1800
1801 /**
1802 * Notification that the embedder has changed the time zone,
1803 * daylight savings time, or other date / time configuration
1804 * parameters. V8 keeps a cache of various values used for
1805 * date / time computation. This notification will reset
1806 * those cached values for the current context so that date /
1807 * time configuration changes would be reflected in the Date
1808 * object.
1809 *
1810 * This API should not be called more than needed as it will
1811 * negatively impact the performance of date operations.
1812 */
1813 V8EXPORT static void DateTimeConfigurationChangeNotification();
1814
1815 private:
1816 V8EXPORT static void CheckCast(v8::Value* obj);
1817};
1818
1819
1820/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001821 * A Number object (ECMA-262, 4.3.21).
1822 */
1823class NumberObject : public Object {
1824 public:
1825 V8EXPORT static Local<Value> New(double value);
1826
1827 /**
1828 * Returns the Number held by the object.
1829 */
1830 V8EXPORT double NumberValue() const;
1831
1832 static inline NumberObject* Cast(v8::Value* obj);
1833
1834 private:
1835 V8EXPORT static void CheckCast(v8::Value* obj);
1836};
1837
1838
1839/**
1840 * A Boolean object (ECMA-262, 4.3.15).
1841 */
1842class BooleanObject : public Object {
1843 public:
1844 V8EXPORT static Local<Value> New(bool value);
1845
1846 /**
1847 * Returns the Boolean held by the object.
1848 */
1849 V8EXPORT bool BooleanValue() const;
1850
1851 static inline BooleanObject* Cast(v8::Value* obj);
1852
1853 private:
1854 V8EXPORT static void CheckCast(v8::Value* obj);
1855};
1856
1857
1858/**
1859 * A String object (ECMA-262, 4.3.18).
1860 */
1861class StringObject : public Object {
1862 public:
1863 V8EXPORT static Local<Value> New(Handle<String> value);
1864
1865 /**
1866 * Returns the String held by the object.
1867 */
1868 V8EXPORT Local<String> StringValue() const;
1869
1870 static inline StringObject* Cast(v8::Value* obj);
1871
1872 private:
1873 V8EXPORT static void CheckCast(v8::Value* obj);
1874};
1875
1876
1877/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001878 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1879 */
1880class RegExp : public Object {
1881 public:
1882 /**
1883 * Regular expression flag bits. They can be or'ed to enable a set
1884 * of flags.
1885 */
1886 enum Flags {
1887 kNone = 0,
1888 kGlobal = 1,
1889 kIgnoreCase = 2,
1890 kMultiline = 4
1891 };
1892
1893 /**
1894 * Creates a regular expression from the given pattern string and
1895 * the flags bit field. May throw a JavaScript exception as
1896 * described in ECMA-262, 15.10.4.1.
1897 *
1898 * For example,
1899 * RegExp::New(v8::String::New("foo"),
1900 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1901 * is equivalent to evaluating "/foo/gm".
1902 */
1903 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1904 Flags flags);
1905
1906 /**
1907 * Returns the value of the source property: a string representing
1908 * the regular expression.
1909 */
1910 V8EXPORT Local<String> GetSource() const;
1911
1912 /**
1913 * Returns the flags bit field.
1914 */
1915 V8EXPORT Flags GetFlags() const;
1916
1917 static inline RegExp* Cast(v8::Value* obj);
1918
1919 private:
1920 V8EXPORT static void CheckCast(v8::Value* obj);
1921};
1922
1923
1924/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001925 * A JavaScript value that wraps a C++ void*. This type of value is
1926 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001928 *
1929 * The Wrap function V8 will return the most optimal Value object wrapping the
1930 * C++ void*. The type of the value is not guaranteed to be an External object
1931 * and no assumptions about its type should be made. To access the wrapped
1932 * value Unwrap should be used, all other operations on that object will lead
1933 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001935class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001937 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001938 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001939
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001940 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001941 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001942 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001944 V8EXPORT External();
1945 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001946 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001947 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948};
1949
1950
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001951// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952
1953
1954/**
1955 * The superclass of object and function templates.
1956 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001957class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 public:
1959 /** Adds a property to each instance created by this template.*/
1960 void Set(Handle<String> name, Handle<Data> value,
1961 PropertyAttribute attributes = None);
1962 inline void Set(const char* name, Handle<Data> value);
1963 private:
1964 Template();
1965
1966 friend class ObjectTemplate;
1967 friend class FunctionTemplate;
1968};
1969
1970
1971/**
1972 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001973 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974 * including the receiver, the number and values of arguments, and
1975 * the holder of the function.
1976 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001977class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 public:
1979 inline int Length() const;
1980 inline Local<Value> operator[](int i) const;
1981 inline Local<Function> Callee() const;
1982 inline Local<Object> This() const;
1983 inline Local<Object> Holder() const;
1984 inline bool IsConstructCall() const;
1985 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001986 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00001987
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001989 static const int kIsolateIndex = 0;
1990 static const int kDataIndex = -1;
1991 static const int kCalleeIndex = -2;
1992 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001993
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001995 inline Arguments(internal::Object** implicit_args,
1996 internal::Object** values,
1997 int length,
1998 bool is_construct_call);
1999 internal::Object** implicit_args_;
2000 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002002 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003};
2004
2005
2006/**
2007 * The information passed to an accessor callback about the context
2008 * of the property access.
2009 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002010class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002012 inline AccessorInfo(internal::Object** args)
2013 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002014 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015 inline Local<Value> Data() const;
2016 inline Local<Object> This() const;
2017 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002018
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002020 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021};
2022
2023
2024typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2025
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 * NamedProperty[Getter|Setter] are used as interceptors on object.
2028 * See ObjectTemplate::SetNamedPropertyHandler.
2029 */
2030typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2031 const AccessorInfo& info);
2032
2033
2034/**
2035 * Returns the value if the setter intercepts the request.
2036 * Otherwise, returns an empty handle.
2037 */
2038typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2039 Local<Value> value,
2040 const AccessorInfo& info);
2041
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042/**
2043 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002044 * The result is an integer encoding property attributes (like v8::None,
2045 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002047typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2048 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049
2050
2051/**
2052 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002053 * The return value is true if the property could be deleted and false
2054 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 */
2056typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2057 const AccessorInfo& info);
2058
2059/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002060 * Returns an array containing the names of the properties the named
2061 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 */
2063typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2064
v8.team.kasperl727e9952008-09-02 14:56:44 +00002065
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002067 * Returns the value of the property if the getter intercepts the
2068 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 */
2070typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2071 const AccessorInfo& info);
2072
2073
2074/**
2075 * Returns the value if the setter intercepts the request.
2076 * Otherwise, returns an empty handle.
2077 */
2078typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2079 Local<Value> value,
2080 const AccessorInfo& info);
2081
2082
2083/**
2084 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002085 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002087typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2088 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089
2090/**
2091 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002092 * The return value is true if the property could be deleted and false
2093 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 */
2095typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2096 const AccessorInfo& info);
2097
v8.team.kasperl727e9952008-09-02 14:56:44 +00002098/**
2099 * Returns an array containing the indices of the properties the
2100 * indexed property getter intercepts.
2101 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2103
2104
2105/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002106 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 */
2108enum AccessType {
2109 ACCESS_GET,
2110 ACCESS_SET,
2111 ACCESS_HAS,
2112 ACCESS_DELETE,
2113 ACCESS_KEYS
2114};
2115
v8.team.kasperl727e9952008-09-02 14:56:44 +00002116
2117/**
2118 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002119 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002120 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002121typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122 Local<Value> key,
2123 AccessType type,
2124 Local<Value> data);
2125
v8.team.kasperl727e9952008-09-02 14:56:44 +00002126
2127/**
2128 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002129 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002130 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002131typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132 uint32_t index,
2133 AccessType type,
2134 Local<Value> data);
2135
2136
2137/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002138 * A FunctionTemplate is used to create functions at runtime. There
2139 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002140 * context. The lifetime of the created function is equal to the
2141 * lifetime of the context. So in case the embedder needs to create
2142 * temporary functions that can be collected using Scripts is
2143 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144 *
2145 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002146 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002148 * A FunctionTemplate has a corresponding instance template which is
2149 * used to create object instances when the function is used as a
2150 * constructor. Properties added to the instance template are added to
2151 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 *
2153 * A FunctionTemplate can have a prototype template. The prototype template
2154 * is used to create the prototype object of the function.
2155 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002156 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002158 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2160 * t->Set("func_property", v8::Number::New(1));
2161 *
2162 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2163 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2164 * proto_t->Set("proto_const", v8::Number::New(2));
2165 *
2166 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2167 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2168 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2169 * instance_t->Set("instance_property", Number::New(3));
2170 *
2171 * v8::Local<v8::Function> function = t->GetFunction();
2172 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002173 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 *
2175 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002176 * and "instance" for the instance object created above. The function
2177 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002179 * \code
2180 * func_property in function == true;
2181 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002183 * function.prototype.proto_method() invokes 'InvokeCallback'
2184 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002186 * instance instanceof function == true;
2187 * instance.instance_accessor calls 'InstanceAccessorCallback'
2188 * instance.instance_property == 3;
2189 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002190 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002191 * A FunctionTemplate can inherit from another one by calling the
2192 * FunctionTemplate::Inherit method. The following graph illustrates
2193 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002195 * \code
2196 * FunctionTemplate Parent -> Parent() . prototype -> { }
2197 * ^ ^
2198 * | Inherit(Parent) | .__proto__
2199 * | |
2200 * FunctionTemplate Child -> Child() . prototype -> { }
2201 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002203 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2204 * object of the Child() function has __proto__ pointing to the
2205 * Parent() function's prototype object. An instance of the Child
2206 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002208 * Let Parent be the FunctionTemplate initialized in the previous
2209 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002211 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 * Local<FunctionTemplate> parent = t;
2213 * Local<FunctionTemplate> child = FunctionTemplate::New();
2214 * child->Inherit(parent);
2215 *
2216 * Local<Function> child_function = child->GetFunction();
2217 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002218 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002220 * The Child function and Child instance will have the following
2221 * properties:
2222 *
2223 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002225 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002227 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002229class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230 public:
2231 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002232 static Local<FunctionTemplate> New(
2233 InvocationCallback callback = 0,
2234 Handle<Value> data = Handle<Value>(),
2235 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 /** Returns the unique function instance in the current execution context.*/
2237 Local<Function> GetFunction();
2238
v8.team.kasperl727e9952008-09-02 14:56:44 +00002239 /**
2240 * Set the call-handler callback for a FunctionTemplate. This
2241 * callback is called whenever the function created from this
2242 * FunctionTemplate is called.
2243 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 void SetCallHandler(InvocationCallback callback,
2245 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246
v8.team.kasperl727e9952008-09-02 14:56:44 +00002247 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 Local<ObjectTemplate> InstanceTemplate();
2249
2250 /** Causes the function template to inherit from a parent function template.*/
2251 void Inherit(Handle<FunctionTemplate> parent);
2252
2253 /**
2254 * A PrototypeTemplate is the template used to create the prototype object
2255 * of the function created by this template.
2256 */
2257 Local<ObjectTemplate> PrototypeTemplate();
2258
v8.team.kasperl727e9952008-09-02 14:56:44 +00002259
2260 /**
2261 * Set the class name of the FunctionTemplate. This is used for
2262 * printing objects created with the function created from the
2263 * FunctionTemplate as its constructor.
2264 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 void SetClassName(Handle<String> name);
2266
2267 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002268 * Determines whether the __proto__ accessor ignores instances of
2269 * the function template. If instances of the function template are
2270 * ignored, __proto__ skips all instances and instead returns the
2271 * next object in the prototype chain.
2272 *
2273 * Call with a value of true to make the __proto__ accessor ignore
2274 * instances of the function template. Call with a value of false
2275 * to make the __proto__ accessor not ignore instances of the
2276 * function template. By default, instances of a function template
2277 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002278 */
2279 void SetHiddenPrototype(bool value);
2280
2281 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002282 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2283 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002284 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002285 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002286
2287 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002288 * Returns true if the given object is an instance of this function
2289 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 */
2291 bool HasInstance(Handle<Value> object);
2292
2293 private:
2294 FunctionTemplate();
2295 void AddInstancePropertyAccessor(Handle<String> name,
2296 AccessorGetter getter,
2297 AccessorSetter setter,
2298 Handle<Value> data,
2299 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002300 PropertyAttribute attributes,
2301 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2303 NamedPropertySetter setter,
2304 NamedPropertyQuery query,
2305 NamedPropertyDeleter remover,
2306 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002307 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2309 IndexedPropertySetter setter,
2310 IndexedPropertyQuery query,
2311 IndexedPropertyDeleter remover,
2312 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002313 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2315 Handle<Value> data);
2316
2317 friend class Context;
2318 friend class ObjectTemplate;
2319};
2320
2321
2322/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002323 * An ObjectTemplate is used to create objects at runtime.
2324 *
2325 * Properties added to an ObjectTemplate are added to each object
2326 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002328class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002330 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002333 /** Creates a new instance of this template.*/
2334 Local<Object> NewInstance();
2335
2336 /**
2337 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002338 *
2339 * Whenever the property with the given name is accessed on objects
2340 * created from this ObjectTemplate the getter and setter callbacks
2341 * are called instead of getting and setting the property directly
2342 * on the JavaScript object.
2343 *
2344 * \param name The name of the property for which an accessor is added.
2345 * \param getter The callback to invoke when getting the property.
2346 * \param setter The callback to invoke when setting the property.
2347 * \param data A piece of data that will be passed to the getter and setter
2348 * callbacks whenever they are invoked.
2349 * \param settings Access control settings for the accessor. This is a bit
2350 * field consisting of one of more of
2351 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2352 * The default is to not allow cross-context access.
2353 * ALL_CAN_READ means that all cross-context reads are allowed.
2354 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2355 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2356 * cross-context access.
2357 * \param attribute The attributes of the property for which an accessor
2358 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002359 * \param signature The signature describes valid receivers for the accessor
2360 * and is used to perform implicit instance checks against them. If the
2361 * receiver is incompatible (i.e. is not an instance of the constructor as
2362 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2363 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364 */
2365 void SetAccessor(Handle<String> name,
2366 AccessorGetter getter,
2367 AccessorSetter setter = 0,
2368 Handle<Value> data = Handle<Value>(),
2369 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002370 PropertyAttribute attribute = None,
2371 Handle<AccessorSignature> signature =
2372 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373
2374 /**
2375 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002376 *
2377 * Whenever a named property is accessed on objects created from
2378 * this object template, the provided callback is invoked instead of
2379 * accessing the property directly on the JavaScript object.
2380 *
2381 * \param getter The callback to invoke when getting a property.
2382 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002383 * \param query The callback to invoke to check if a property is present,
2384 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002385 * \param deleter The callback to invoke when deleting a property.
2386 * \param enumerator The callback to invoke to enumerate all the named
2387 * properties of an object.
2388 * \param data A piece of data that will be passed to the callbacks
2389 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390 */
2391 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2392 NamedPropertySetter setter = 0,
2393 NamedPropertyQuery query = 0,
2394 NamedPropertyDeleter deleter = 0,
2395 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002396 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397
2398 /**
2399 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002400 *
2401 * Whenever an indexed property is accessed on objects created from
2402 * this object template, the provided callback is invoked instead of
2403 * accessing the property directly on the JavaScript object.
2404 *
2405 * \param getter The callback to invoke when getting a property.
2406 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002407 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002408 * \param deleter The callback to invoke when deleting a property.
2409 * \param enumerator The callback to invoke to enumerate all the indexed
2410 * properties of an object.
2411 * \param data A piece of data that will be passed to the callbacks
2412 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 */
2414 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2415 IndexedPropertySetter setter = 0,
2416 IndexedPropertyQuery query = 0,
2417 IndexedPropertyDeleter deleter = 0,
2418 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002419 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 /**
2422 * Sets the callback to be used when calling instances created from
2423 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002424 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002425 * function.
2426 */
2427 void SetCallAsFunctionHandler(InvocationCallback callback,
2428 Handle<Value> data = Handle<Value>());
2429
v8.team.kasperl727e9952008-09-02 14:56:44 +00002430 /**
2431 * Mark object instances of the template as undetectable.
2432 *
2433 * In many ways, undetectable objects behave as though they are not
2434 * there. They behave like 'undefined' in conditionals and when
2435 * printed. However, properties can be accessed and called as on
2436 * normal objects.
2437 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 void MarkAsUndetectable();
2439
v8.team.kasperl727e9952008-09-02 14:56:44 +00002440 /**
2441 * Sets access check callbacks on the object template.
2442 *
2443 * When accessing properties on instances of this object template,
2444 * the access check callback will be called to determine whether or
2445 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002446 * The last parameter specifies whether access checks are turned
2447 * on by default on instances. If access checks are off by default,
2448 * they can be turned on on individual instances by calling
2449 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002450 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002451 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2452 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002453 Handle<Value> data = Handle<Value>(),
2454 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002455
kasper.lund212ac232008-07-16 07:07:30 +00002456 /**
2457 * Gets the number of internal fields for objects generated from
2458 * this template.
2459 */
2460 int InternalFieldCount();
2461
2462 /**
2463 * Sets the number of internal fields for objects generated from
2464 * this template.
2465 */
2466 void SetInternalFieldCount(int value);
2467
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 private:
2469 ObjectTemplate();
2470 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2471 friend class FunctionTemplate;
2472};
2473
2474
2475/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002476 * A Signature specifies which receivers and arguments are valid
2477 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002479class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 public:
2481 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2482 Handle<FunctionTemplate>(),
2483 int argc = 0,
2484 Handle<FunctionTemplate> argv[] = 0);
2485 private:
2486 Signature();
2487};
2488
2489
2490/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002491 * An AccessorSignature specifies which receivers are valid parameters
2492 * to an accessor callback.
2493 */
2494class V8EXPORT AccessorSignature : public Data {
2495 public:
2496 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2497 Handle<FunctionTemplate>());
2498 private:
2499 AccessorSignature();
2500};
2501
2502
2503/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002504 * A utility for determining the type of objects based on the template
2505 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002507class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 public:
2509 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2510 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2511 int match(Handle<Value> value);
2512 private:
2513 TypeSwitch();
2514};
2515
2516
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002517// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002518
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002519class V8EXPORT ExternalAsciiStringResourceImpl
2520 : public String::ExternalAsciiStringResource {
2521 public:
2522 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2523 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2524 : data_(data), length_(length) {}
2525 const char* data() const { return data_; }
2526 size_t length() const { return length_; }
2527
2528 private:
2529 const char* data_;
2530 size_t length_;
2531};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002532
2533/**
2534 * Ignore
2535 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002536class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002537 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002538 // Note that the strings passed into this constructor must live as long
2539 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540 Extension(const char* name,
2541 const char* source = 0,
2542 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002543 const char** deps = 0,
2544 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002545 virtual ~Extension() { }
2546 virtual v8::Handle<v8::FunctionTemplate>
2547 GetNativeFunction(v8::Handle<v8::String> name) {
2548 return v8::Handle<v8::FunctionTemplate>();
2549 }
2550
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002551 const char* name() const { return name_; }
2552 size_t source_length() const { return source_length_; }
2553 const String::ExternalAsciiStringResource* source() const {
2554 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002555 int dependency_count() { return dep_count_; }
2556 const char** dependencies() { return deps_; }
2557 void set_auto_enable(bool value) { auto_enable_ = value; }
2558 bool auto_enable() { return auto_enable_; }
2559
2560 private:
2561 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002562 size_t source_length_; // expected to initialize before source_
2563 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564 int dep_count_;
2565 const char** deps_;
2566 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002567
2568 // Disallow copying and assigning.
2569 Extension(const Extension&);
2570 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571};
2572
2573
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002574void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575
2576
2577/**
2578 * Ignore
2579 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002580class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581 public:
2582 inline DeclareExtension(Extension* extension) {
2583 RegisterExtension(extension);
2584 }
2585};
2586
2587
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002588// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589
2590
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002591Handle<Primitive> V8EXPORT Undefined();
2592Handle<Primitive> V8EXPORT Null();
2593Handle<Boolean> V8EXPORT True();
2594Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002596inline Handle<Primitive> Undefined(Isolate* isolate);
2597inline Handle<Primitive> Null(Isolate* isolate);
2598inline Handle<Boolean> True(Isolate* isolate);
2599inline Handle<Boolean> False(Isolate* isolate);
2600
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601
2602/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002603 * A set of constraints that specifies the limits of the runtime's memory use.
2604 * You must set the heap size before initializing the VM - the size cannot be
2605 * adjusted after the VM is initialized.
2606 *
2607 * If you are using threads then you should hold the V8::Locker lock while
2608 * setting the stack limit and you must set a non-default stack limit separately
2609 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002611class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612 public:
2613 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002614 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002616 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002618 int max_executable_size() { return max_executable_size_; }
2619 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002620 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002621 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2623 private:
2624 int max_young_space_size_;
2625 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002626 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 uint32_t* stack_limit_;
2628};
2629
2630
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002631bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632
2633
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002634// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635
2636
2637typedef void (*FatalErrorCallback)(const char* location, const char* message);
2638
2639
2640typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2641
2642
2643/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002644 * Schedules an exception to be thrown when returning to JavaScript. When an
2645 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002647 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002649Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650
2651/**
2652 * Create new error objects by calling the corresponding error object
2653 * constructor with the message.
2654 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002655class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656 public:
2657 static Local<Value> RangeError(Handle<String> message);
2658 static Local<Value> ReferenceError(Handle<String> message);
2659 static Local<Value> SyntaxError(Handle<String> message);
2660 static Local<Value> TypeError(Handle<String> message);
2661 static Local<Value> Error(Handle<String> message);
2662};
2663
2664
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002665// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002667typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002669typedef void* (*CreateHistogramCallback)(const char* name,
2670 int min,
2671 int max,
2672 size_t buckets);
2673
2674typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2675
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002676// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002677 enum ObjectSpace {
2678 kObjectSpaceNewSpace = 1 << 0,
2679 kObjectSpaceOldPointerSpace = 1 << 1,
2680 kObjectSpaceOldDataSpace = 1 << 2,
2681 kObjectSpaceCodeSpace = 1 << 3,
2682 kObjectSpaceMapSpace = 1 << 4,
2683 kObjectSpaceLoSpace = 1 << 5,
2684
2685 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2686 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2687 kObjectSpaceLoSpace
2688 };
2689
2690 enum AllocationAction {
2691 kAllocationActionAllocate = 1 << 0,
2692 kAllocationActionFree = 1 << 1,
2693 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2694 };
2695
2696typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2697 AllocationAction action,
2698 int size);
2699
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002700// --- Leave Script Callback ---
2701typedef void (*CallCompletedCallback)();
2702
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002703// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2705 AccessType type,
2706 Local<Value> data);
2707
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002708// --- AllowCodeGenerationFromStrings callbacks ---
2709
2710/**
2711 * Callback to check if code generation from strings is allowed. See
2712 * Context::AllowCodeGenerationFromStrings.
2713 */
2714typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2715
2716// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717
2718/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002719 * Applications can register callback functions which will be called
2720 * before and after a garbage collection. Allocations are not
2721 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002722 * objects (set or delete properties for example) since it is possible
2723 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002725enum GCType {
2726 kGCTypeScavenge = 1 << 0,
2727 kGCTypeMarkSweepCompact = 1 << 1,
2728 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2729};
2730
2731enum GCCallbackFlags {
2732 kNoGCCallbackFlags = 0,
2733 kGCCallbackFlagCompacted = 1 << 0
2734};
2735
2736typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2737typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2738
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002739typedef void (*GCCallback)();
2740
2741
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002742/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002743 * Collection of V8 heap information.
2744 *
2745 * Instances of this class can be passed to v8::V8::HeapStatistics to
2746 * get heap statistics from V8.
2747 */
2748class V8EXPORT HeapStatistics {
2749 public:
2750 HeapStatistics();
2751 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002752 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002753 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002754 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002755
2756 private:
2757 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002758 void set_total_heap_size_executable(size_t size) {
2759 total_heap_size_executable_ = size;
2760 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002761 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002762 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002763
2764 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002765 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002766 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002767 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002768
2769 friend class V8;
2770};
2771
2772
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002773class RetainedObjectInfo;
2774
ager@chromium.org3811b432009-10-28 14:53:37 +00002775/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002776 * Isolate represents an isolated instance of the V8 engine. V8
2777 * isolates have completely separate states. Objects from one isolate
2778 * must not be used in other isolates. When V8 is initialized a
2779 * default isolate is implicitly created and entered. The embedder
2780 * can create additional isolates and use them in parallel in multiple
2781 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002782 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002783 */
2784class V8EXPORT Isolate {
2785 public:
2786 /**
2787 * Stack-allocated class which sets the isolate for all operations
2788 * executed within a local scope.
2789 */
2790 class V8EXPORT Scope {
2791 public:
2792 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2793 isolate->Enter();
2794 }
2795
2796 ~Scope() { isolate_->Exit(); }
2797
2798 private:
2799 Isolate* const isolate_;
2800
2801 // Prevent copying of Scope objects.
2802 Scope(const Scope&);
2803 Scope& operator=(const Scope&);
2804 };
2805
2806 /**
2807 * Creates a new isolate. Does not change the currently entered
2808 * isolate.
2809 *
2810 * When an isolate is no longer used its resources should be freed
2811 * by calling Dispose(). Using the delete operator is not allowed.
2812 */
2813 static Isolate* New();
2814
2815 /**
2816 * Returns the entered isolate for the current thread or NULL in
2817 * case there is no current isolate.
2818 */
2819 static Isolate* GetCurrent();
2820
2821 /**
2822 * Methods below this point require holding a lock (using Locker) in
2823 * a multi-threaded environment.
2824 */
2825
2826 /**
2827 * Sets this isolate as the entered one for the current thread.
2828 * Saves the previously entered one (if any), so that it can be
2829 * restored when exiting. Re-entering an isolate is allowed.
2830 */
2831 void Enter();
2832
2833 /**
2834 * Exits this isolate by restoring the previously entered one in the
2835 * current thread. The isolate may still stay the same, if it was
2836 * entered more than once.
2837 *
2838 * Requires: this == Isolate::GetCurrent().
2839 */
2840 void Exit();
2841
2842 /**
2843 * Disposes the isolate. The isolate must not be entered by any
2844 * thread to be disposable.
2845 */
2846 void Dispose();
2847
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002848 /**
2849 * Associate embedder-specific data with the isolate
2850 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002851 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002852
2853 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002854 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002855 * Returns NULL if SetData has never been called.
2856 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002857 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002858
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002859 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002860 Isolate();
2861 Isolate(const Isolate&);
2862 ~Isolate();
2863 Isolate& operator=(const Isolate&);
2864 void* operator new(size_t size);
2865 void operator delete(void*, size_t);
2866};
2867
2868
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002869class StartupData {
2870 public:
2871 enum CompressionAlgorithm {
2872 kUncompressed,
2873 kBZip2
2874 };
2875
2876 const char* data;
2877 int compressed_size;
2878 int raw_size;
2879};
2880
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002881
2882/**
2883 * A helper class for driving V8 startup data decompression. It is based on
2884 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2885 * for an embedder to use this class, instead, API functions can be used
2886 * directly.
2887 *
2888 * For an example of the class usage, see the "shell.cc" sample application.
2889 */
2890class V8EXPORT StartupDataDecompressor { // NOLINT
2891 public:
2892 StartupDataDecompressor();
2893 virtual ~StartupDataDecompressor();
2894 int Decompress();
2895
2896 protected:
2897 virtual int DecompressData(char* raw_data,
2898 int* raw_data_size,
2899 const char* compressed_data,
2900 int compressed_data_size) = 0;
2901
2902 private:
2903 char** raw_data;
2904};
2905
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002906
2907/**
2908 * EntropySource is used as a callback function when v8 needs a source
2909 * of entropy.
2910 */
2911typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2912
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002913
2914/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002915 * ReturnAddressLocationResolver is used as a callback function when v8 is
2916 * resolving the location of a return address on the stack. Profilers that
2917 * change the return address on the stack can use this to resolve the stack
2918 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002919 *
2920 * \param return_addr_location points to a location on stack where a machine
2921 * return address resides.
2922 * \returns either return_addr_location, or else a pointer to the profiler's
2923 * copy of the original return address.
2924 *
2925 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00002926 */
2927typedef uintptr_t (*ReturnAddressLocationResolver)(
2928 uintptr_t return_addr_location);
2929
2930
2931/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002932 * FunctionEntryHook is the type of the profile entry hook called at entry to
2933 * any generated function when function-level profiling is enabled.
2934 *
2935 * \param function the address of the function that's being entered.
2936 * \param return_addr_location points to a location on stack where the machine
2937 * return address resides. This can be used to identify the caller of
2938 * \p function, and/or modified to divert execution when \p function exits.
2939 *
2940 * \note the entry hook must not cause garbage collection.
2941 */
2942typedef void (*FunctionEntryHook)(uintptr_t function,
2943 uintptr_t return_addr_location);
2944
2945
2946/**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002947 * Interface for iterating though all external resources in the heap.
2948 */
2949class V8EXPORT ExternalResourceVisitor { // NOLINT
2950 public:
2951 virtual ~ExternalResourceVisitor() {}
2952 virtual void VisitExternalString(Handle<String> string) {}
2953};
2954
2955
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002956/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 * Container class for static utility functions.
2958 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002959class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002960 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002961 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002962 static void SetFatalErrorHandler(FatalErrorCallback that);
2963
v8.team.kasperl727e9952008-09-02 14:56:44 +00002964 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002965 * Set the callback to invoke to check if code generation from
2966 * strings should be allowed.
2967 */
2968 static void SetAllowCodeGenerationFromStringsCallback(
2969 AllowCodeGenerationFromStringsCallback that);
2970
2971 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002972 * Ignore out-of-memory exceptions.
2973 *
2974 * V8 running out of memory is treated as a fatal error by default.
2975 * This means that the fatal error handler is called and that V8 is
2976 * terminated.
2977 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002978 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002979 * out-of-memory situation as a fatal error. This way, the contexts
2980 * that did not cause the out of memory problem might be able to
2981 * continue execution.
2982 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002983 static void IgnoreOutOfMemoryException();
2984
v8.team.kasperl727e9952008-09-02 14:56:44 +00002985 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002986 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002987 * fatal errors such as out-of-memory situations.
2988 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002989 static bool IsDead();
2990
2991 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002992 * The following 4 functions are to be used when V8 is built with
2993 * the 'compress_startup_data' flag enabled. In this case, the
2994 * embedder must decompress startup data prior to initializing V8.
2995 *
2996 * This is how interaction with V8 should look like:
2997 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2998 * v8::StartupData* compressed_data =
2999 * new v8::StartupData[compressed_data_count];
3000 * v8::V8::GetCompressedStartupData(compressed_data);
3001 * ... decompress data (compressed_data can be updated in-place) ...
3002 * v8::V8::SetDecompressedStartupData(compressed_data);
3003 * ... now V8 can be initialized
3004 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003005 *
3006 * A helper class StartupDataDecompressor is provided. It implements
3007 * the protocol of the interaction described above, and can be used in
3008 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003009 */
3010 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3011 static int GetCompressedStartupDataCount();
3012 static void GetCompressedStartupData(StartupData* compressed_data);
3013 static void SetDecompressedStartupData(StartupData* decompressed_data);
3014
3015 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003016 * Adds a message listener.
3017 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003018 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003019 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003020 */
3021 static bool AddMessageListener(MessageCallback that,
3022 Handle<Value> data = Handle<Value>());
3023
3024 /**
3025 * Remove all message listeners from the specified callback function.
3026 */
3027 static void RemoveMessageListeners(MessageCallback that);
3028
3029 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003030 * Tells V8 to capture current stack trace when uncaught exception occurs
3031 * and report it to the message listeners. The option is off by default.
3032 */
3033 static void SetCaptureStackTraceForUncaughtExceptions(
3034 bool capture,
3035 int frame_limit = 10,
3036 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3037
3038 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003039 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003040 */
3041 static void SetFlagsFromString(const char* str, int length);
3042
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003043 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003044 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003045 */
3046 static void SetFlagsFromCommandLine(int* argc,
3047 char** argv,
3048 bool remove_flags);
3049
kasper.lund7276f142008-07-30 08:49:36 +00003050 /** Get the version string. */
3051 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052
3053 /**
3054 * Enables the host application to provide a mechanism for recording
3055 * statistics counters.
3056 */
3057 static void SetCounterFunction(CounterLookupCallback);
3058
3059 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003060 * Enables the host application to provide a mechanism for recording
3061 * histograms. The CreateHistogram function returns a
3062 * histogram which will later be passed to the AddHistogramSample
3063 * function.
3064 */
3065 static void SetCreateHistogramFunction(CreateHistogramCallback);
3066 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3067
3068 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069 * Enables the computation of a sliding window of states. The sliding
3070 * window information is recorded in statistics counters.
3071 */
3072 static void EnableSlidingStateWindow();
3073
3074 /** Callback function for reporting failed access checks.*/
3075 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3076
3077 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003078 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003079 * garbage collection. Allocations are not allowed in the
3080 * callback function, you therefore cannot manipulate objects (set
3081 * or delete properties for example) since it is possible such
3082 * operations will result in the allocation of objects. It is possible
3083 * to specify the GCType filter for your callback. But it is not possible to
3084 * register the same callback function two times with different
3085 * GCType filters.
3086 */
3087 static void AddGCPrologueCallback(
3088 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3089
3090 /**
3091 * This function removes callback which was installed by
3092 * AddGCPrologueCallback function.
3093 */
3094 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3095
3096 /**
3097 * The function is deprecated. Please use AddGCPrologueCallback instead.
3098 * Enables the host application to receive a notification before a
3099 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003100 * callback function, you therefore cannot manipulate objects (set
3101 * or delete properties for example) since it is possible such
3102 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003103 */
3104 static void SetGlobalGCPrologueCallback(GCCallback);
3105
3106 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003107 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003108 * garbage collection. Allocations are not allowed in the
3109 * callback function, you therefore cannot manipulate objects (set
3110 * or delete properties for example) since it is possible such
3111 * operations will result in the allocation of objects. It is possible
3112 * to specify the GCType filter for your callback. But it is not possible to
3113 * register the same callback function two times with different
3114 * GCType filters.
3115 */
3116 static void AddGCEpilogueCallback(
3117 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3118
3119 /**
3120 * This function removes callback which was installed by
3121 * AddGCEpilogueCallback function.
3122 */
3123 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3124
3125 /**
3126 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3127 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003128 * major garbage collection. Allocations are not allowed in the
3129 * callback function, you therefore cannot manipulate objects (set
3130 * or delete properties for example) since it is possible such
3131 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003132 */
3133 static void SetGlobalGCEpilogueCallback(GCCallback);
3134
3135 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003136 * Enables the host application to provide a mechanism to be notified
3137 * and perform custom logging when V8 Allocates Executable Memory.
3138 */
3139 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3140 ObjectSpace space,
3141 AllocationAction action);
3142
3143 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003144 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003145 */
3146 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3147
3148 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003149 * Adds a callback to notify the host application when a script finished
3150 * running. If a script re-enters the runtime during executing, the
3151 * CallCompletedCallback is only invoked when the outer-most script
3152 * execution ends. Executing scripts inside the callback do not trigger
3153 * further callbacks.
3154 */
3155 static void AddCallCompletedCallback(CallCompletedCallback callback);
3156
3157 /**
3158 * Removes callback that was installed by AddCallCompletedCallback.
3159 */
3160 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3161
3162 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003163 * Allows the host application to group objects together. If one
3164 * object in the group is alive, all objects in the group are alive.
3165 * After each garbage collection, object groups are removed. It is
3166 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003167 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00003168 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003169 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003170 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003171 static void AddObjectGroup(Persistent<Value>* objects,
3172 size_t length,
3173 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003174
3175 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003176 * Allows the host application to declare implicit references between
3177 * the objects: if |parent| is alive, all |children| are alive too.
3178 * After each garbage collection, all implicit references
3179 * are removed. It is intended to be used in the before-garbage-collection
3180 * callback function.
3181 */
3182 static void AddImplicitReferences(Persistent<Object> parent,
3183 Persistent<Value>* children,
3184 size_t length);
3185
3186 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003187 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003188 * initialize from scratch. This function is called implicitly if
3189 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003190 */
3191 static bool Initialize();
3192
kasper.lund7276f142008-07-30 08:49:36 +00003193 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003194 * Allows the host application to provide a callback which can be used
3195 * as a source of entropy for random number generators.
3196 */
3197 static void SetEntropySource(EntropySource source);
3198
3199 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003200 * Allows the host application to provide a callback that allows v8 to
3201 * cooperate with a profiler that rewrites return addresses on stack.
3202 */
3203 static void SetReturnAddressLocationResolver(
3204 ReturnAddressLocationResolver return_address_resolver);
3205
3206 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003207 * Allows the host application to provide the address of a function that's
3208 * invoked on entry to every V8-generated function.
3209 * Note that \p entry_hook is invoked at the very start of each
3210 * generated function.
3211 *
3212 * \param entry_hook a function that will be invoked on entry to every
3213 * V8-generated function.
3214 * \returns true on success on supported platforms, false on failure.
3215 * \note Setting a new entry hook function when one is already active will
3216 * fail.
3217 */
3218 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3219
3220 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003221 * Adjusts the amount of registered external memory. Used to give
3222 * V8 an indication of the amount of externally allocated memory
3223 * that is kept alive by JavaScript objects. V8 uses this to decide
3224 * when to perform global garbage collections. Registering
3225 * externally allocated memory will trigger global garbage
3226 * collections more often than otherwise in an attempt to garbage
3227 * collect the JavaScript objects keeping the externally allocated
3228 * memory alive.
3229 *
3230 * \param change_in_bytes the change in externally allocated memory
3231 * that is kept alive by JavaScript objects.
3232 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003233 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003234 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3235 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003236
iposva@chromium.org245aa852009-02-10 00:49:54 +00003237 /**
3238 * Suspends recording of tick samples in the profiler.
3239 * When the V8 profiling mode is enabled (usually via command line
3240 * switches) this function suspends recording of tick samples.
3241 * Profiling ticks are discarded until ResumeProfiler() is called.
3242 *
3243 * See also the --prof and --prof_auto command line switches to
3244 * enable V8 profiling.
3245 */
3246 static void PauseProfiler();
3247
3248 /**
3249 * Resumes recording of tick samples in the profiler.
3250 * See also PauseProfiler().
3251 */
3252 static void ResumeProfiler();
3253
ager@chromium.org41826e72009-03-30 13:30:57 +00003254 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003255 * Return whether profiler is currently paused.
3256 */
3257 static bool IsProfilerPaused();
3258
3259 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003260 * Retrieve the V8 thread id of the calling thread.
3261 *
3262 * The thread id for a thread should only be retrieved after the V8
3263 * lock has been acquired with a Locker object with that thread.
3264 */
3265 static int GetCurrentThreadId();
3266
3267 /**
3268 * Forcefully terminate execution of a JavaScript thread. This can
3269 * be used to terminate long-running scripts.
3270 *
3271 * TerminateExecution should only be called when then V8 lock has
3272 * been acquired with a Locker object. Therefore, in order to be
3273 * able to terminate long-running threads, preemption must be
3274 * enabled to allow the user of TerminateExecution to acquire the
3275 * lock.
3276 *
3277 * The termination is achieved by throwing an exception that is
3278 * uncatchable by JavaScript exception handlers. Termination
3279 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003280 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003281 * exception handler that catches an exception should check if that
3282 * exception is a termination exception and immediately return if
3283 * that is the case. Returning immediately in that case will
3284 * continue the propagation of the termination exception if needed.
3285 *
3286 * The thread id passed to TerminateExecution must have been
3287 * obtained by calling GetCurrentThreadId on the thread in question.
3288 *
3289 * \param thread_id The thread id of the thread to terminate.
3290 */
3291 static void TerminateExecution(int thread_id);
3292
3293 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003294 * Forcefully terminate the current thread of JavaScript execution
3295 * in the given isolate. If no isolate is provided, the default
3296 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003297 *
3298 * This method can be used by any thread even if that thread has not
3299 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003300 *
3301 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003302 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003303 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003304
3305 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003306 * Is V8 terminating JavaScript execution.
3307 *
3308 * Returns true if JavaScript execution is currently terminating
3309 * because of a call to TerminateExecution. In that case there are
3310 * still JavaScript frames on the stack and the termination
3311 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003312 *
3313 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003314 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003315 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003316
3317 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003318 * Releases any resources used by v8 and stops any utility threads
3319 * that may be running. Note that disposing v8 is permanent, it
3320 * cannot be reinitialized.
3321 *
3322 * It should generally not be necessary to dispose v8 before exiting
3323 * a process, this should happen automatically. It is only necessary
3324 * to use if the process needs the resources taken up by v8.
3325 */
3326 static bool Dispose();
3327
ager@chromium.org3811b432009-10-28 14:53:37 +00003328 /**
3329 * Get statistics about the heap memory usage.
3330 */
3331 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003332
3333 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003334 * Iterates through all external resources referenced from current isolate
3335 * heap. This method is not expected to be used except for debugging purposes
3336 * and may be quite slow.
3337 */
3338 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3339
3340 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003341 * Optional notification that the embedder is idle.
3342 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003343 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003344 * Returns true if the embedder should stop calling IdleNotification
3345 * until real work has been done. This indicates that V8 has done
3346 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003347 *
3348 * The hint argument specifies the amount of work to be done in the function
3349 * on scale from 1 to 1000. There is no guarantee that the actual work will
3350 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003351 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003352 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003353
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003354 /**
3355 * Optional notification that the system is running low on memory.
3356 * V8 uses these notifications to attempt to free memory.
3357 */
3358 static void LowMemoryNotification();
3359
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003360 /**
3361 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003362 * these notifications to guide the GC heuristic. Returns the number
3363 * of context disposals - including this one - since the last time
3364 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003365 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003366 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003367
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003368 private:
3369 V8();
3370
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003371 static internal::Object** GlobalizeReference(internal::Object** handle);
3372 static void DisposeGlobal(internal::Object** global_handle);
3373 static void MakeWeak(internal::Object** global_handle,
3374 void* data,
3375 WeakReferenceCallback);
3376 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003377 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003378 static bool IsGlobalNearDeath(internal::Object** global_handle);
3379 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003380 static void SetWrapperClassId(internal::Object** global_handle,
3381 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003382
3383 template <class T> friend class Handle;
3384 template <class T> friend class Local;
3385 template <class T> friend class Persistent;
3386 friend class Context;
3387};
3388
3389
3390/**
3391 * An external exception handler.
3392 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003393class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003395 /**
3396 * Creates a new try/catch block and registers it with v8.
3397 */
3398 TryCatch();
3399
3400 /**
3401 * Unregisters and deletes this try/catch block.
3402 */
3403 ~TryCatch();
3404
3405 /**
3406 * Returns true if an exception has been caught by this try/catch block.
3407 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003408 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003409
3410 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003411 * For certain types of exceptions, it makes no sense to continue
3412 * execution.
3413 *
3414 * Currently, the only type of exception that can be caught by a
3415 * TryCatch handler and for which it does not make sense to continue
3416 * is termination exception. Such exceptions are thrown when the
3417 * TerminateExecution methods are called to terminate a long-running
3418 * script.
3419 *
3420 * If CanContinue returns false, the correct action is to perform
3421 * any C++ cleanup needed and then return.
3422 */
3423 bool CanContinue() const;
3424
3425 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003426 * Throws the exception caught by this TryCatch in a way that avoids
3427 * it being caught again by this same TryCatch. As with ThrowException
3428 * it is illegal to execute any JavaScript operations after calling
3429 * ReThrow; the caller must return immediately to where the exception
3430 * is caught.
3431 */
3432 Handle<Value> ReThrow();
3433
3434 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003435 * Returns the exception caught by this try/catch block. If no exception has
3436 * been caught an empty handle is returned.
3437 *
3438 * The returned handle is valid until this TryCatch block has been destroyed.
3439 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003440 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441
3442 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003443 * Returns the .stack property of the thrown object. If no .stack
3444 * property is present an empty handle is returned.
3445 */
3446 Local<Value> StackTrace() const;
3447
3448 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003449 * Returns the message associated with this exception. If there is
3450 * no message associated an empty handle is returned.
3451 *
3452 * The returned handle is valid until this TryCatch block has been
3453 * destroyed.
3454 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003455 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003456
3457 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003458 * Clears any exceptions that may have been caught by this try/catch block.
3459 * After this method has been called, HasCaught() will return false.
3460 *
3461 * It is not necessary to clear a try/catch block before using it again; if
3462 * another exception is thrown the previously caught exception will just be
3463 * overwritten. However, it is often a good idea since it makes it easier
3464 * to determine which operation threw a given exception.
3465 */
3466 void Reset();
3467
v8.team.kasperl727e9952008-09-02 14:56:44 +00003468 /**
3469 * Set verbosity of the external exception handler.
3470 *
3471 * By default, exceptions that are caught by an external exception
3472 * handler are not reported. Call SetVerbose with true on an
3473 * external exception handler to have exceptions caught by the
3474 * handler reported as if they were not caught.
3475 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003476 void SetVerbose(bool value);
3477
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003478 /**
3479 * Set whether or not this TryCatch should capture a Message object
3480 * which holds source information about where the exception
3481 * occurred. True by default.
3482 */
3483 void SetCaptureMessage(bool value);
3484
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003485 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003486 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003487 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003489 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003490 bool is_verbose_ : 1;
3491 bool can_continue_ : 1;
3492 bool capture_message_ : 1;
3493 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003494
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003495 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003496};
3497
3498
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003499// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003500
3501
3502/**
3503 * Ignore
3504 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003505class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003506 public:
3507 ExtensionConfiguration(int name_count, const char* names[])
3508 : name_count_(name_count), names_(names) { }
3509 private:
3510 friend class ImplementationUtilities;
3511 int name_count_;
3512 const char** names_;
3513};
3514
3515
3516/**
3517 * A sandboxed execution context with its own set of built-in objects
3518 * and functions.
3519 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003520class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003522 /**
3523 * Returns the global proxy object or global object itself for
3524 * detached contexts.
3525 *
3526 * Global proxy object is a thin wrapper whose prototype points to
3527 * actual context's global object with the properties like Object, etc.
3528 * This is done that way for security reasons (for more details see
3529 * https://wiki.mozilla.org/Gecko:SplitWindow).
3530 *
3531 * Please note that changes to global proxy object prototype most probably
3532 * would break VM---v8 expects only global object as a prototype of
3533 * global proxy object.
3534 *
3535 * If DetachGlobal() has been invoked, Global() would return actual global
3536 * object until global is reattached with ReattachGlobal().
3537 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003538 Local<Object> Global();
3539
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003540 /**
3541 * Detaches the global object from its context before
3542 * the global object can be reused to create a new context.
3543 */
3544 void DetachGlobal();
3545
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003546 /**
3547 * Reattaches a global object to a context. This can be used to
3548 * restore the connection between a global object and a context
3549 * after DetachGlobal has been called.
3550 *
3551 * \param global_object The global object to reattach to the
3552 * context. For this to work, the global object must be the global
3553 * object that was associated with this context before a call to
3554 * DetachGlobal.
3555 */
3556 void ReattachGlobal(Handle<Object> global_object);
3557
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003558 /** Creates a new context.
3559 *
3560 * Returns a persistent handle to the newly allocated context. This
3561 * persistent handle has to be disposed when the context is no
3562 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003563 *
3564 * \param extensions An optional extension configuration containing
3565 * the extensions to be installed in the newly created context.
3566 *
3567 * \param global_template An optional object template from which the
3568 * global object for the newly created context will be created.
3569 *
3570 * \param global_object An optional global object to be reused for
3571 * the newly created context. This global object must have been
3572 * created by a previous call to Context::New with the same global
3573 * template. The state of the global object will be completely reset
3574 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003575 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003576 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003577 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003578 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3579 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003580
kasper.lund44510672008-07-25 07:37:58 +00003581 /** Returns the last entered context. */
3582 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003583
kasper.lund44510672008-07-25 07:37:58 +00003584 /** Returns the context that is on the top of the stack. */
3585 static Local<Context> GetCurrent();
3586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003588 * Returns the context of the calling JavaScript code. That is the
3589 * context of the top-most JavaScript frame. If there are no
3590 * JavaScript frames an empty handle is returned.
3591 */
3592 static Local<Context> GetCalling();
3593
3594 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003595 * Sets the security token for the context. To access an object in
3596 * another context, the security tokens must match.
3597 */
3598 void SetSecurityToken(Handle<Value> token);
3599
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003600 /** Restores the security token to the default value. */
3601 void UseDefaultSecurityToken();
3602
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 /** Returns the security token of this context.*/
3604 Handle<Value> GetSecurityToken();
3605
v8.team.kasperl727e9952008-09-02 14:56:44 +00003606 /**
3607 * Enter this context. After entering a context, all code compiled
3608 * and run is compiled and run in this context. If another context
3609 * is already entered, this old context is saved so it can be
3610 * restored when the new context is exited.
3611 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003612 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003613
3614 /**
3615 * Exit this context. Exiting the current context restores the
3616 * context that was in place when entering the current context.
3617 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003618 void Exit();
3619
v8.team.kasperl727e9952008-09-02 14:56:44 +00003620 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003621 bool HasOutOfMemoryException();
3622
v8.team.kasperl727e9952008-09-02 14:56:44 +00003623 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003624 static bool InContext();
3625
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003626 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003627 * Associate an additional data object with the context. This is mainly used
3628 * with the debugger to provide additional information on the context through
3629 * the debugger API.
3630 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003631 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003632 Local<Value> GetData();
3633
3634 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003635 * Control whether code generation from strings is allowed. Calling
3636 * this method with false will disable 'eval' and the 'Function'
3637 * constructor for code running in this context. If 'eval' or the
3638 * 'Function' constructor are used an exception will be thrown.
3639 *
3640 * If code generation from strings is not allowed the
3641 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3642 * set before blocking the call to 'eval' or the 'Function'
3643 * constructor. If that callback returns true, the call will be
3644 * allowed, otherwise an exception will be thrown. If no callback is
3645 * set an exception will be thrown.
3646 */
3647 void AllowCodeGenerationFromStrings(bool allow);
3648
3649 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003650 * Returns true if code generation from strings is allowed for the context.
3651 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3652 */
3653 bool IsCodeGenerationFromStringsAllowed();
3654
3655 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003656 * Stack-allocated class which sets the execution context for all
3657 * operations executed within a local scope.
3658 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003659 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003661 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003662 context_->Enter();
3663 }
3664 inline ~Scope() { context_->Exit(); }
3665 private:
3666 Handle<Context> context_;
3667 };
3668
3669 private:
3670 friend class Value;
3671 friend class Script;
3672 friend class Object;
3673 friend class Function;
3674};
3675
3676
3677/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003678 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003679 * is allowed to use any given V8 isolate. See Isolate class
3680 * comments. The definition of 'using V8 isolate' includes
3681 * accessing handles or holding onto object pointers obtained
3682 * from V8 handles while in the particular V8 isolate. It is up
3683 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003684 * constraint is not violated. In addition to any other synchronization
3685 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3686 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003687 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003688 * v8::Locker is a scoped lock object. While it's
3689 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003690 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3691 * locked by at most one thread at any time. In other words, the scope of a
3692 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003693 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003694 * Sample usage:
3695* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003696 * ...
3697 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003698 * v8::Locker locker(isolate);
3699 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003700 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003701 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003702 * ...
3703 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003704 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003705 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003706 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707 * by destroying the v8::Locker object as above or by constructing a
3708 * v8::Unlocker object:
3709 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003710 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003712 * isolate->Exit();
3713 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003714 * ...
3715 * // Code not using V8 goes here while V8 can run in another thread.
3716 * ...
3717 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003718 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003719 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003720 *
3721 * The Unlocker object is intended for use in a long-running callback
3722 * from V8, where you want to release the V8 lock for other threads to
3723 * use.
3724 *
3725 * The v8::Locker is a recursive lock. That is, you can lock more than
3726 * once in a given thread. This can be useful if you have code that can
3727 * be called either from code that holds the lock or from code that does
3728 * not. The Unlocker is not recursive so you can not have several
3729 * Unlockers on the stack at once, and you can not use an Unlocker in a
3730 * thread that is not inside a Locker's scope.
3731 *
3732 * An unlocker will unlock several lockers if it has to and reinstate
3733 * the correct depth of locking on its destruction. eg.:
3734 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003735 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003736 * // V8 not locked.
3737 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003738 * v8::Locker locker(isolate);
3739 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003740 * // V8 locked.
3741 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003742 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003743 * // V8 still locked (2 levels).
3744 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003745 * isolate->Exit();
3746 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003747 * // V8 not locked.
3748 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003749 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003750 * // V8 locked again (2 levels).
3751 * }
3752 * // V8 still locked (1 level).
3753 * }
3754 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003755 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003756 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003757 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003758 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003759class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003760 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003761 /**
3762 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3763 */
3764 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003765 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003766 private:
3767 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003768};
3769
3770
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003771class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003773 /**
3774 * Initialize Locker for a given Isolate. NULL means default isolate.
3775 */
3776 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003777 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003778
3779 /**
3780 * Start preemption.
3781 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003782 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003783 * that will switch between multiple threads that are in contention
3784 * for the V8 lock.
3785 */
3786 static void StartPreemption(int every_n_ms);
3787
3788 /**
3789 * Stop preemption.
3790 */
3791 static void StopPreemption();
3792
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003793 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003794 * Returns whether or not the locker for a given isolate, or default isolate
3795 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003796 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003797 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003798
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003799 /**
3800 * Returns whether v8::Locker is being used by this V8 instance.
3801 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003802 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003803
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804 private:
3805 bool has_lock_;
3806 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003807 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003808
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003809 static bool active_;
3810
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003811 // Disallow copying and assigning.
3812 Locker(const Locker&);
3813 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003814};
3815
3816
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003817/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003818 * A struct for exporting HeapStats data from V8, using "push" model.
3819 */
3820struct HeapStatsUpdate;
3821
3822
3823/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003824 * An interface for exporting data from V8, using "push" model.
3825 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003826class V8EXPORT OutputStream { // NOLINT
3827 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003828 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003829 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003830 };
3831 enum WriteResult {
3832 kContinue = 0,
3833 kAbort = 1
3834 };
3835 virtual ~OutputStream() {}
3836 /** Notify about the end of stream. */
3837 virtual void EndOfStream() = 0;
3838 /** Get preferred output chunk size. Called only once. */
3839 virtual int GetChunkSize() { return 1024; }
3840 /** Get preferred output encoding. Called only once. */
3841 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3842 /**
3843 * Writes the next chunk of snapshot data into the stream. Writing
3844 * can be stopped by returning kAbort as function result. EndOfStream
3845 * will not be called in case writing was aborted.
3846 */
3847 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003848 /**
3849 * Writes the next chunk of heap stats data into the stream. Writing
3850 * can be stopped by returning kAbort as function result. EndOfStream
3851 * will not be called in case writing was aborted.
3852 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003853 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00003854 return kAbort;
3855 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003856};
3857
3858
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003859/**
3860 * An interface for reporting progress and controlling long-running
3861 * activities.
3862 */
3863class V8EXPORT ActivityControl { // NOLINT
3864 public:
3865 enum ControlOption {
3866 kContinue = 0,
3867 kAbort = 1
3868 };
3869 virtual ~ActivityControl() {}
3870 /**
3871 * Notify about current progress. The activity can be stopped by
3872 * returning kAbort as the callback result.
3873 */
3874 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3875};
3876
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003877
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003878// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003879
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003880
3881namespace internal {
3882
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003883const int kApiPointerSize = sizeof(void*); // NOLINT
3884const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003885
3886// Tag information for HeapObject.
3887const int kHeapObjectTag = 1;
3888const int kHeapObjectTagSize = 2;
3889const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3890
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003891// Tag information for Smi.
3892const int kSmiTag = 0;
3893const int kSmiTagSize = 1;
3894const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3895
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003896template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003897
3898// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003899template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003900 static const int kSmiShiftSize = 0;
3901 static const int kSmiValueSize = 31;
3902 static inline int SmiToInt(internal::Object* value) {
3903 int shift_bits = kSmiTagSize + kSmiShiftSize;
3904 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3905 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3906 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003907
3908 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3909 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003910 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003911 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003912};
3913
3914// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003915template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003916 static const int kSmiShiftSize = 31;
3917 static const int kSmiValueSize = 32;
3918 static inline int SmiToInt(internal::Object* value) {
3919 int shift_bits = kSmiTagSize + kSmiShiftSize;
3920 // Shift down and throw away top 32 bits.
3921 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3922 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003923
3924 // To maximize the range of pointers that can be encoded
3925 // in the available 32 bits, we require them to be 8 bytes aligned.
3926 // This gives 2 ^ (32 + 3) = 32G address space covered.
3927 // It might be not enough to cover stack allocated objects on some platforms.
3928 static const int kPointerAlignment = 3;
3929
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003930 static const uintptr_t kEncodablePointerMask =
3931 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003932
3933 static const int kPointerToSmiShift =
3934 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003935};
3936
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003937typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3938const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3939const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003940const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003941 PlatformSmiTagging::kEncodablePointerMask;
3942const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003943
3944/**
3945 * This class exports constants and functionality from within v8 that
3946 * is necessary to implement inline functions in the v8 api. Don't
3947 * depend on functions and constants defined here.
3948 */
3949class Internals {
3950 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003951 // These values match non-compiler-dependent values defined within
3952 // the implementation of v8.
3953 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003954 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003955 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003956
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003957 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003958 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003959 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003960 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003961 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003962
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003963 static const int kIsolateStateOffset = 0;
3964 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
3965 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
3966 static const int kUndefinedValueRootIndex = 5;
3967 static const int kNullValueRootIndex = 7;
3968 static const int kTrueValueRootIndex = 8;
3969 static const int kFalseValueRootIndex = 9;
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00003970 static const int kEmptySymbolRootIndex = 112;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003971
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003972 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003973 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003974 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003975 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003976
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003977 static const int kUndefinedOddballKind = 5;
3978 static const int kNullOddballKind = 3;
3979
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003980 static inline bool HasHeapObjectTag(internal::Object* value) {
3981 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3982 kHeapObjectTag);
3983 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003984
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003985 static inline bool HasSmiTag(internal::Object* value) {
3986 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3987 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003988
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003989 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003990 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003991 }
3992
3993 static inline int GetInstanceType(internal::Object* obj) {
3994 typedef internal::Object O;
3995 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3996 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3997 }
3998
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003999 static inline int GetOddballKind(internal::Object* obj) {
4000 typedef internal::Object O;
4001 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4002 }
4003
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004004 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004005 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004006 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
4007 }
4008
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004009 static inline void* GetExternalPointer(internal::Object* obj) {
4010 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004011 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004012 } else if (GetInstanceType(obj) == kForeignType) {
4013 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004014 } else {
4015 return NULL;
4016 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004017 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004018
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004019 static inline bool IsExternalTwoByteString(int instance_type) {
4020 int representation = (instance_type & kFullStringRepresentationMask);
4021 return representation == kExternalTwoByteRepresentationTag;
4022 }
4023
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004024 static inline bool IsInitialized(v8::Isolate* isolate) {
4025 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4026 return *reinterpret_cast<int*>(addr) == 1;
4027 }
4028
4029 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
4030 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4031 kIsolateEmbedderDataOffset;
4032 *reinterpret_cast<void**>(addr) = data;
4033 }
4034
4035 static inline void* GetEmbedderData(v8::Isolate* isolate) {
4036 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4037 kIsolateEmbedderDataOffset;
4038 return *reinterpret_cast<void**>(addr);
4039 }
4040
4041 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
4042 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4043 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4044 }
4045
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004046 template <typename T>
4047 static inline T ReadField(Object* ptr, int offset) {
4048 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4049 return *reinterpret_cast<T*>(addr);
4050 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004051
4052 static inline bool CanCastToHeapObject(void* o) { return false; }
4053 static inline bool CanCastToHeapObject(Context* o) { return true; }
4054 static inline bool CanCastToHeapObject(String* o) { return true; }
4055 static inline bool CanCastToHeapObject(Object* o) { return true; }
4056 static inline bool CanCastToHeapObject(Message* o) { return true; }
4057 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
4058 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004059};
4060
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004061} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004062
4063
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004064template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004065Local<T>::Local() : Handle<T>() { }
4066
4067
4068template <class T>
4069Local<T> Local<T>::New(Handle<T> that) {
4070 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004071 T* that_ptr = *that;
4072 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4073 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4074 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4075 reinterpret_cast<internal::HeapObject*>(*p))));
4076 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004077 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4078}
4079
4080
4081template <class T>
4082Persistent<T> Persistent<T>::New(Handle<T> that) {
4083 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004084 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004085 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4086}
4087
4088
4089template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004090bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004091 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004092 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004093}
4094
4095
4096template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004097bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004098 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004099 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004100}
4101
4102
4103template <class T>
4104void Persistent<T>::Dispose() {
4105 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004106 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004107}
4108
4109
4110template <class T>
4111Persistent<T>::Persistent() : Handle<T>() { }
4112
4113template <class T>
4114void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004115 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4116 parameters,
4117 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004118}
4119
4120template <class T>
4121void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004122 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004123}
4124
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004125template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004126void Persistent<T>::MarkIndependent() {
4127 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4128}
4129
4130template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004131void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4132 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4133}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004134
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004135Arguments::Arguments(internal::Object** implicit_args,
4136 internal::Object** values, int length,
4137 bool is_construct_call)
4138 : implicit_args_(implicit_args),
4139 values_(values),
4140 length_(length),
4141 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004142
4143
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004144Local<Value> Arguments::operator[](int i) const {
4145 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4146 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4147}
4148
4149
4150Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004151 return Local<Function>(reinterpret_cast<Function*>(
4152 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004153}
4154
4155
4156Local<Object> Arguments::This() const {
4157 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4158}
4159
4160
4161Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004162 return Local<Object>(reinterpret_cast<Object*>(
4163 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004164}
4165
4166
4167Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004168 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004169}
4170
4171
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004172Isolate* Arguments::GetIsolate() const {
4173 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4174}
4175
4176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004177bool Arguments::IsConstructCall() const {
4178 return is_construct_call_;
4179}
4180
4181
4182int Arguments::Length() const {
4183 return length_;
4184}
4185
4186
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004187template <class T>
4188Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004189 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4190 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004191 return Local<T>(reinterpret_cast<T*>(after));
4192}
4193
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004194Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004195 return resource_name_;
4196}
4197
4198
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004199Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004200 return resource_line_offset_;
4201}
4202
4203
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004204Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004205 return resource_column_offset_;
4206}
4207
4208
4209Handle<Boolean> Boolean::New(bool value) {
4210 return value ? True() : False();
4211}
4212
4213
4214void Template::Set(const char* name, v8::Handle<Data> value) {
4215 Set(v8::String::New(name), value);
4216}
4217
4218
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004219Local<Value> Object::GetInternalField(int index) {
4220#ifndef V8_ENABLE_CHECKS
4221 Local<Value> quick_result = UncheckedGetInternalField(index);
4222 if (!quick_result.IsEmpty()) return quick_result;
4223#endif
4224 return CheckedGetInternalField(index);
4225}
4226
4227
4228Local<Value> Object::UncheckedGetInternalField(int index) {
4229 typedef internal::Object O;
4230 typedef internal::Internals I;
4231 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004232 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004233 // If the object is a plain JSObject, which is the common case,
4234 // we know where to find the internal fields and can return the
4235 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004236 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004237 O* value = I::ReadField<O*>(obj, offset);
4238 O** result = HandleScope::CreateHandle(value);
4239 return Local<Value>(reinterpret_cast<Value*>(result));
4240 } else {
4241 return Local<Value>();
4242 }
4243}
4244
4245
4246void* External::Unwrap(Handle<v8::Value> obj) {
4247#ifdef V8_ENABLE_CHECKS
4248 return FullUnwrap(obj);
4249#else
4250 return QuickUnwrap(obj);
4251#endif
4252}
4253
4254
4255void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4256 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004257 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004258 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004259}
4260
4261
4262void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004263 typedef internal::Object O;
4264 typedef internal::Internals I;
4265
4266 O* obj = *reinterpret_cast<O**>(this);
4267
4268 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4269 // If the object is a plain JSObject, which is the common case,
4270 // we know where to find the internal fields and can return the
4271 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004272 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004273 O* value = I::ReadField<O*>(obj, offset);
4274 return I::GetExternalPointer(value);
4275 }
4276
4277 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004278}
4279
4280
4281String* String::Cast(v8::Value* value) {
4282#ifdef V8_ENABLE_CHECKS
4283 CheckCast(value);
4284#endif
4285 return static_cast<String*>(value);
4286}
4287
4288
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004289Local<String> String::Empty(Isolate* isolate) {
4290 typedef internal::Object* S;
4291 typedef internal::Internals I;
4292 if (!I::IsInitialized(isolate)) return Empty();
4293 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4294 return Local<String>(reinterpret_cast<String*>(slot));
4295}
4296
4297
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004298String::ExternalStringResource* String::GetExternalStringResource() const {
4299 typedef internal::Object O;
4300 typedef internal::Internals I;
4301 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004302 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004303 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004304 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4305 result = reinterpret_cast<String::ExternalStringResource*>(value);
4306 } else {
4307 result = NULL;
4308 }
4309#ifdef V8_ENABLE_CHECKS
4310 VerifyExternalStringResource(result);
4311#endif
4312 return result;
4313}
4314
4315
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004316bool Value::IsUndefined() const {
4317#ifdef V8_ENABLE_CHECKS
4318 return FullIsUndefined();
4319#else
4320 return QuickIsUndefined();
4321#endif
4322}
4323
4324bool Value::QuickIsUndefined() const {
4325 typedef internal::Object O;
4326 typedef internal::Internals I;
4327 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4328 if (!I::HasHeapObjectTag(obj)) return false;
4329 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4330 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4331}
4332
4333
4334bool Value::IsNull() const {
4335#ifdef V8_ENABLE_CHECKS
4336 return FullIsNull();
4337#else
4338 return QuickIsNull();
4339#endif
4340}
4341
4342bool Value::QuickIsNull() const {
4343 typedef internal::Object O;
4344 typedef internal::Internals I;
4345 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4346 if (!I::HasHeapObjectTag(obj)) return false;
4347 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4348 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4349}
4350
4351
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004352bool Value::IsString() const {
4353#ifdef V8_ENABLE_CHECKS
4354 return FullIsString();
4355#else
4356 return QuickIsString();
4357#endif
4358}
4359
4360bool Value::QuickIsString() const {
4361 typedef internal::Object O;
4362 typedef internal::Internals I;
4363 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4364 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004365 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004366}
4367
4368
4369Number* Number::Cast(v8::Value* value) {
4370#ifdef V8_ENABLE_CHECKS
4371 CheckCast(value);
4372#endif
4373 return static_cast<Number*>(value);
4374}
4375
4376
4377Integer* Integer::Cast(v8::Value* value) {
4378#ifdef V8_ENABLE_CHECKS
4379 CheckCast(value);
4380#endif
4381 return static_cast<Integer*>(value);
4382}
4383
4384
4385Date* Date::Cast(v8::Value* value) {
4386#ifdef V8_ENABLE_CHECKS
4387 CheckCast(value);
4388#endif
4389 return static_cast<Date*>(value);
4390}
4391
4392
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004393StringObject* StringObject::Cast(v8::Value* value) {
4394#ifdef V8_ENABLE_CHECKS
4395 CheckCast(value);
4396#endif
4397 return static_cast<StringObject*>(value);
4398}
4399
4400
4401NumberObject* NumberObject::Cast(v8::Value* value) {
4402#ifdef V8_ENABLE_CHECKS
4403 CheckCast(value);
4404#endif
4405 return static_cast<NumberObject*>(value);
4406}
4407
4408
4409BooleanObject* BooleanObject::Cast(v8::Value* value) {
4410#ifdef V8_ENABLE_CHECKS
4411 CheckCast(value);
4412#endif
4413 return static_cast<BooleanObject*>(value);
4414}
4415
4416
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004417RegExp* RegExp::Cast(v8::Value* value) {
4418#ifdef V8_ENABLE_CHECKS
4419 CheckCast(value);
4420#endif
4421 return static_cast<RegExp*>(value);
4422}
4423
4424
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004425Object* Object::Cast(v8::Value* value) {
4426#ifdef V8_ENABLE_CHECKS
4427 CheckCast(value);
4428#endif
4429 return static_cast<Object*>(value);
4430}
4431
4432
4433Array* Array::Cast(v8::Value* value) {
4434#ifdef V8_ENABLE_CHECKS
4435 CheckCast(value);
4436#endif
4437 return static_cast<Array*>(value);
4438}
4439
4440
4441Function* Function::Cast(v8::Value* value) {
4442#ifdef V8_ENABLE_CHECKS
4443 CheckCast(value);
4444#endif
4445 return static_cast<Function*>(value);
4446}
4447
4448
4449External* External::Cast(v8::Value* value) {
4450#ifdef V8_ENABLE_CHECKS
4451 CheckCast(value);
4452#endif
4453 return static_cast<External*>(value);
4454}
4455
4456
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004457Isolate* AccessorInfo::GetIsolate() const {
4458 return *reinterpret_cast<Isolate**>(&args_[-3]);
4459}
4460
4461
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004462Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004463 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004464}
4465
4466
4467Local<Object> AccessorInfo::This() const {
4468 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4469}
4470
4471
4472Local<Object> AccessorInfo::Holder() const {
4473 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4474}
4475
4476
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004477Handle<Primitive> Undefined(Isolate* isolate) {
4478 typedef internal::Object* S;
4479 typedef internal::Internals I;
4480 if (!I::IsInitialized(isolate)) return Undefined();
4481 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4482 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4483}
4484
4485
4486Handle<Primitive> Null(Isolate* isolate) {
4487 typedef internal::Object* S;
4488 typedef internal::Internals I;
4489 if (!I::IsInitialized(isolate)) return Null();
4490 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4491 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4492}
4493
4494
4495Handle<Boolean> True(Isolate* isolate) {
4496 typedef internal::Object* S;
4497 typedef internal::Internals I;
4498 if (!I::IsInitialized(isolate)) return True();
4499 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4500 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4501}
4502
4503
4504Handle<Boolean> False(Isolate* isolate) {
4505 typedef internal::Object* S;
4506 typedef internal::Internals I;
4507 if (!I::IsInitialized(isolate)) return False();
4508 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4509 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4510}
4511
4512
4513void Isolate::SetData(void* data) {
4514 typedef internal::Internals I;
4515 I::SetEmbedderData(this, data);
4516}
4517
4518
4519void* Isolate::GetData() {
4520 typedef internal::Internals I;
4521 return I::GetEmbedderData(this);
4522}
4523
4524
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004525/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004526 * \example shell.cc
4527 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004528 * command-line and executes them.
4529 */
4530
4531
4532/**
4533 * \example process.cc
4534 */
4535
4536
4537} // namespace v8
4538
4539
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004540#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004541#undef TYPE_CHECK
4542
4543
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004544#endif // V8_H_