blob: 480cbaa9553f295f6ad844f789e54fa128059402 [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.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000066#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
67 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070#else
71#define V8EXPORT
72#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000075#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076
77#endif // _WIN32
78
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000080 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 */
82namespace v8 {
83
84class Context;
85class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000086class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087class Value;
88class Utils;
89class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000090class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091class Object;
92class Array;
93class Int32;
94class Uint32;
95class External;
96class Primitive;
97class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000098class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099class Integer;
100class Function;
101class Date;
102class ImplementationUtilities;
103class Signature;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000104class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105template <class T> class Handle;
106template <class T> class Local;
107template <class T> class Persistent;
108class FunctionTemplate;
109class ObjectTemplate;
110class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000111class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000112class StackTrace;
113class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000114class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000116namespace internal {
117
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000118class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000119class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000120class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121class HeapObject;
122class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000123}
124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000126// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127
128
129/**
130 * A weak reference callback function.
131 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000132 * This callback should either explicitly invoke Dispose on |object| if
133 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
134 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 * \param object the weak global object to be reclaimed by the garbage collector
136 * \param parameter the value passed in when making the weak global object
137 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000138typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 void* parameter);
140
141
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000142// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000144#define TYPE_CHECK(T, S) \
145 while (false) { \
146 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 }
148
149/**
150 * An object reference managed by the v8 garbage collector.
151 *
152 * All objects returned from v8 have to be tracked by the garbage
153 * collector so that it knows that the objects are still alive. Also,
154 * because the garbage collector may move objects, it is unsafe to
155 * point directly to an object. Instead, all objects are stored in
156 * handles which are known by the garbage collector and updated
157 * whenever an object moves. Handles should always be passed by value
158 * (except in cases like out-parameters) and they should never be
159 * allocated on the heap.
160 *
161 * There are two types of handles: local and persistent handles.
162 * Local handles are light-weight and transient and typically used in
163 * local operations. They are managed by HandleScopes. Persistent
164 * handles can be used when storing objects across several independent
165 * operations and have to be explicitly deallocated when they're no
166 * longer used.
167 *
168 * It is safe to extract the object stored in the handle by
169 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000170 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 * behind the scenes and the same rules apply to these values as to
172 * their handles.
173 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000174template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 /**
177 * Creates an empty handle.
178 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000179 inline Handle() : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
181 /**
182 * Creates a new handle for the specified value.
183 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000184 inline explicit Handle(T* val) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185
186 /**
187 * Creates a handle for the contents of the specified handle. This
188 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000189 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000191 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192 * between compatible handles, for instance assigning a
193 * Handle<String> to a variable declared as Handle<Value>, is legal
194 * because String is a subclass of Value.
195 */
196 template <class S> inline Handle(Handle<S> that)
197 : val_(reinterpret_cast<T*>(*that)) {
198 /**
199 * This check fails when trying to convert between incompatible
200 * handles. For example, converting from a Handle<String> to a
201 * Handle<Number>.
202 */
203 TYPE_CHECK(T, S);
204 }
205
206 /**
207 * Returns true if the handle is empty.
208 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000209 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 /**
212 * Sets the handle to be empty. IsEmpty() will then return true.
213 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000214 inline void Clear() { val_ = 0; }
215
216 inline T* operator->() const { return val_; }
217
218 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
220 /**
221 * Checks whether two handles are the same.
222 * Returns true if both are empty, or if the objects
223 * to which they refer are identical.
224 * The handles' references are not checked.
225 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000226 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000227 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
228 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 if (a == 0) return b == 0;
230 if (b == 0) return false;
231 return *a == *b;
232 }
233
234 /**
235 * Checks whether two handles are different.
236 * Returns true if only one of the handles is empty, or if
237 * the objects to which they refer are different.
238 * The handles' references are not checked.
239 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000240 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 return !operator==(that);
242 }
243
244 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000245#ifdef V8_ENABLE_CHECKS
246 // If we're going to perform the type check then we have to check
247 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000249#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 return Handle<T>(T::Cast(*that));
251 }
252
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000253 template <class S> inline Handle<S> As() {
254 return Handle<S>::Cast(*this);
255 }
256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 private:
258 T* val_;
259};
260
261
262/**
263 * A light-weight stack-allocated object handle. All operations
264 * that return objects from within v8 return them in local handles. They
265 * are created within HandleScopes, and all local handles allocated within a
266 * handle scope are destroyed when the handle scope is destroyed. Hence it
267 * is not necessary to explicitly deallocate local handles.
268 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000269template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000271 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 template <class S> inline Local(Local<S> that)
273 : Handle<T>(reinterpret_cast<T*>(*that)) {
274 /**
275 * This check fails when trying to convert between incompatible
276 * handles. For example, converting from a Handle<String> to a
277 * Handle<Number>.
278 */
279 TYPE_CHECK(T, S);
280 }
281 template <class S> inline Local(S* that) : Handle<T>(that) { }
282 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000283#ifdef V8_ENABLE_CHECKS
284 // If we're going to perform the type check then we have to check
285 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000287#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 return Local<T>(T::Cast(*that));
289 }
290
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000291 template <class S> inline Local<S> As() {
292 return Local<S>::Cast(*this);
293 }
294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 /** Create a local handle for the content of another handle.
296 * The referee is kept alive by the local handle even when
297 * the original handle is destroyed/disposed.
298 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000299 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300};
301
302
303/**
304 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000305 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306 * allocated, a Persistent handle remains valid until it is explicitly
307 * disposed.
308 *
309 * A persistent handle contains a reference to a storage cell within
310 * the v8 engine which holds an object value and which is updated by
311 * the garbage collector whenever the object is moved. A new storage
312 * cell can be created using Persistent::New and existing handles can
313 * be disposed using Persistent::Dispose. Since persistent handles
314 * are passed by value you may have many persistent handle objects
315 * that point to the same storage cell. For instance, if you pass a
316 * persistent handle as an argument to a function you will not get two
317 * different storage cells but rather two references to the same
318 * storage cell.
319 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000320template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 /**
323 * Creates an empty persistent handle that doesn't point to any
324 * storage cell.
325 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000326 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327
328 /**
329 * Creates a persistent handle for the same storage cell as the
330 * specified handle. This constructor allows you to pass persistent
331 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000332 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000334 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 * between compatible persistent handles, for instance assigning a
336 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000337 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 */
339 template <class S> inline Persistent(Persistent<S> that)
340 : Handle<T>(reinterpret_cast<T*>(*that)) {
341 /**
342 * This check fails when trying to convert between incompatible
343 * handles. For example, converting from a Handle<String> to a
344 * Handle<Number>.
345 */
346 TYPE_CHECK(T, S);
347 }
348
349 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
350
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000351 /**
352 * "Casts" a plain handle which is known to be a persistent handle
353 * to a persistent handle.
354 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 template <class S> explicit inline Persistent(Handle<S> that)
356 : Handle<T>(*that) { }
357
358 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000359#ifdef V8_ENABLE_CHECKS
360 // If we're going to perform the type check then we have to check
361 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000363#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 return Persistent<T>(T::Cast(*that));
365 }
366
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000367 template <class S> inline Persistent<S> As() {
368 return Persistent<S>::Cast(*this);
369 }
370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000372 * Creates a new persistent handle for an existing local or
373 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000375 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 /**
378 * Releases the storage cell referenced by this persistent handle.
379 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000380 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 * cell remain and IsEmpty will still return false.
382 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000383 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385 /**
386 * Make the reference to this object weak. When only weak handles
387 * refer to the object, the garbage collector will perform a
388 * callback to the given V8::WeakReferenceCallback function, passing
389 * it the object reference and the given parameters.
390 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000391 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392
393 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000394 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395
396 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000397 * Marks the reference to this object independent. Garbage collector
398 * is free to ignore any object groups containing this object.
399 * Weak callback for an independent handle should not
400 * assume that it will be preceded by a global GC prologue callback
401 * or followed by a global GC epilogue callback.
402 */
403 inline void MarkIndependent();
404
405 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 *Checks if the handle holds the only reference to an object.
407 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000408 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409
410 /**
411 * Returns true if the handle's reference is weak.
412 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000413 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000415 /**
416 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
417 * interface description in v8-profiler.h for details.
418 */
419 inline void SetWrapperClassId(uint16_t class_id);
420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421 private:
422 friend class ImplementationUtilities;
423 friend class ObjectTemplate;
424};
425
426
v8.team.kasperl727e9952008-09-02 14:56:44 +0000427 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 * A stack-allocated class that governs a number of local handles.
429 * After a handle scope has been created, all local handles will be
430 * allocated within that handle scope until either the handle scope is
431 * deleted or another handle scope is created. If there is already a
432 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000433 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 * new handles will again be allocated in the original handle scope.
435 *
436 * After the handle scope of a local handle has been deleted the
437 * garbage collector will no longer track the object stored in the
438 * handle and may deallocate it. The behavior of accessing a handle
439 * for which the handle scope has been deleted is undefined.
440 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000441class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000443 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000445 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446
447 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 * Closes the handle scope and returns the value as a handle in the
449 * previous scope, which is the new current scope after the call.
450 */
451 template <class T> Local<T> Close(Handle<T> value);
452
453 /**
454 * Counts the number of allocated handles.
455 */
456 static int NumberOfHandles();
457
458 /**
459 * Creates a new handle with the given value.
460 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000461 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462 // Faster version, uses HeapObject to obtain the current Isolate.
463 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464
465 private:
466 // Make it impossible to create heap-allocated or illegal handle
467 // scopes by disallowing certain operations.
468 HandleScope(const HandleScope&);
469 void operator=(const HandleScope&);
470 void* operator new(size_t size);
471 void operator delete(void*, size_t);
472
ager@chromium.org3811b432009-10-28 14:53:37 +0000473 // This Data class is accessible internally as HandleScopeData through a
474 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000475 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000477 internal::Object** next;
478 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000479 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000482 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 }
484 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000485
lrn@chromium.org303ada72010-10-27 09:33:13 +0000486 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000488 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000489 internal::Object** prev_next_;
490 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000492 // Allow for the active closing of HandleScopes which allows to pass a handle
493 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000495 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 friend class ImplementationUtilities;
498};
499
500
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000501// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502
503
504/**
505 * The superclass of values and API object templates.
506 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000507class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 private:
509 Data();
510};
511
512
513/**
514 * Pre-compilation data that can be associated with a script. This
515 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000516 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000517 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000519class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 public:
521 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000522
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000523 /**
524 * Pre-compiles the specified script (context-independent).
525 *
526 * \param input Pointer to UTF-8 script source code.
527 * \param length Length of UTF-8 script source code.
528 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000531 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000532 * Pre-compiles the specified script (context-independent).
533 *
534 * NOTE: Pre-compilation using this method cannot happen on another thread
535 * without using Lockers.
536 *
537 * \param source Script source code.
538 */
539 static ScriptData* PreCompile(Handle<String> source);
540
541 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000542 * Load previous pre-compilation data.
543 *
544 * \param data Pointer to data returned by a call to Data() of a previous
545 * ScriptData. Ownership is not transferred.
546 * \param length Length of data.
547 */
548 static ScriptData* New(const char* data, int length);
549
550 /**
551 * Returns the length of Data().
552 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000554
555 /**
556 * Returns a serialized representation of this ScriptData that can later be
557 * passed to New(). NOTE: Serialized data is platform-dependent.
558 */
559 virtual const char* Data() = 0;
560
561 /**
562 * Returns true if the source code could not be parsed.
563 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000564 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565};
566
567
568/**
569 * The origin, within a file, of a script.
570 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000571class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000573 inline ScriptOrigin(
574 Handle<Value> resource_name,
575 Handle<Integer> resource_line_offset = Handle<Integer>(),
576 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 : resource_name_(resource_name),
578 resource_line_offset_(resource_line_offset),
579 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000580 inline Handle<Value> ResourceName() const;
581 inline Handle<Integer> ResourceLineOffset() const;
582 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000584 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585 Handle<Integer> resource_line_offset_;
586 Handle<Integer> resource_column_offset_;
587};
588
589
590/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000591 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000593class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000596 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000597 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000598 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000599 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000600 * when New() returns
601 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
602 * using pre_data speeds compilation if it's done multiple times.
603 * Owned by caller, no references are kept when New() returns.
604 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000605 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000606 * available to compile event handlers.
607 * \return Compiled script object (context independent; when run it
608 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000610 static Local<Script> New(Handle<String> source,
611 ScriptOrigin* origin = NULL,
612 ScriptData* pre_data = NULL,
613 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614
mads.s.agercbaa0602008-08-14 13:41:48 +0000615 /**
616 * Compiles the specified script using the specified file name
617 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000618 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000619 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000620 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000621 * as the script's origin.
622 * \return Compiled script object (context independent; when run it
623 * will use the currently entered context).
624 */
625 static Local<Script> New(Handle<String> source,
626 Handle<Value> file_name);
627
628 /**
629 * Compiles the specified script (bound to current context).
630 *
631 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000632 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000633 * when Compile() returns
634 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
635 * using pre_data speeds compilation if it's done multiple times.
636 * Owned by caller, no references are kept when Compile() returns.
637 * \param script_data Arbitrary data associated with script. Using
638 * this has same effect as calling SetData(), but makes data available
639 * earlier (i.e. to compile event handlers).
640 * \return Compiled script object, bound to the context that was active
641 * when this function was called. When run it will always use this
642 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000643 */
644 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000645 ScriptOrigin* origin = NULL,
646 ScriptData* pre_data = NULL,
647 Handle<String> script_data = Handle<String>());
648
649 /**
650 * Compiles the specified script using the specified file name
651 * object (typically a string) as the script's origin.
652 *
653 * \param source Script source code.
654 * \param file_name File name to use as script's origin
655 * \param script_data Arbitrary data associated with script. Using
656 * this has same effect as calling SetData(), but makes data available
657 * earlier (i.e. to compile event handlers).
658 * \return Compiled script object, bound to the context that was active
659 * when this function was called. When run it will always use this
660 * context.
661 */
662 static Local<Script> Compile(Handle<String> source,
663 Handle<Value> file_name,
664 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000665
v8.team.kasperl727e9952008-09-02 14:56:44 +0000666 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000667 * Runs the script returning the resulting value. If the script is
668 * context independent (created using ::New) it will be run in the
669 * currently entered context. If it is context specific (created
670 * using ::Compile) it will be run in the context in which it was
671 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000672 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000674
675 /**
676 * Returns the script id value.
677 */
678 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000679
680 /**
681 * Associate an additional data object with the script. This is mainly used
682 * with the debugger as this data object is only available through the
683 * debugger API.
684 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000685 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686};
687
688
689/**
690 * An error message.
691 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000692class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000694 Local<String> Get() const;
695 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000697 /**
698 * Returns the resource name for the script from where the function causing
699 * the error originates.
700 */
ager@chromium.org32912102009-01-16 10:38:43 +0000701 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000703 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000704 * Returns the resource data for the script from where the function causing
705 * the error originates.
706 */
707 Handle<Value> GetScriptData() const;
708
709 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000710 * Exception stack trace. By default stack traces are not captured for
711 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
712 * to change this option.
713 */
714 Handle<StackTrace> GetStackTrace() const;
715
716 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000717 * Returns the number, 1-based, of the line where the error occurred.
718 */
ager@chromium.org32912102009-01-16 10:38:43 +0000719 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000721 /**
722 * Returns the index within the script of the first character where
723 * the error occurred.
724 */
ager@chromium.org32912102009-01-16 10:38:43 +0000725 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000726
727 /**
728 * Returns the index within the script of the last character where
729 * the error occurred.
730 */
ager@chromium.org32912102009-01-16 10:38:43 +0000731 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000732
733 /**
734 * Returns the index within the line of the first character where
735 * the error occurred.
736 */
ager@chromium.org32912102009-01-16 10:38:43 +0000737 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000738
739 /**
740 * Returns the index within the line of the last character where
741 * the error occurred.
742 */
ager@chromium.org32912102009-01-16 10:38:43 +0000743 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000744
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 // TODO(1245381): Print to a string instead of on a FILE.
746 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000747
748 static const int kNoLineNumberInfo = 0;
749 static const int kNoColumnInfo = 0;
750};
751
752
753/**
754 * Representation of a JavaScript stack trace. The information collected is a
755 * snapshot of the execution stack and the information remains valid after
756 * execution continues.
757 */
758class V8EXPORT StackTrace {
759 public:
760 /**
761 * Flags that determine what information is placed captured for each
762 * StackFrame when grabbing the current stack trace.
763 */
764 enum StackTraceOptions {
765 kLineNumber = 1,
766 kColumnOffset = 1 << 1 | kLineNumber,
767 kScriptName = 1 << 2,
768 kFunctionName = 1 << 3,
769 kIsEval = 1 << 4,
770 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000771 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000772 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000773 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000774 };
775
776 /**
777 * Returns a StackFrame at a particular index.
778 */
779 Local<StackFrame> GetFrame(uint32_t index) const;
780
781 /**
782 * Returns the number of StackFrames.
783 */
784 int GetFrameCount() const;
785
786 /**
787 * Returns StackTrace as a v8::Array that contains StackFrame objects.
788 */
789 Local<Array> AsArray();
790
791 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000792 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000793 *
794 * \param frame_limit The maximum number of stack frames we want to capture.
795 * \param options Enumerates the set of things we will capture for each
796 * StackFrame.
797 */
798 static Local<StackTrace> CurrentStackTrace(
799 int frame_limit,
800 StackTraceOptions options = kOverview);
801};
802
803
804/**
805 * A single JavaScript stack frame.
806 */
807class V8EXPORT StackFrame {
808 public:
809 /**
810 * Returns the number, 1-based, of the line for the associate function call.
811 * This method will return Message::kNoLineNumberInfo if it is unable to
812 * retrieve the line number, or if kLineNumber was not passed as an option
813 * when capturing the StackTrace.
814 */
815 int GetLineNumber() const;
816
817 /**
818 * Returns the 1-based column offset on the line for the associated function
819 * call.
820 * This method will return Message::kNoColumnInfo if it is unable to retrieve
821 * the column number, or if kColumnOffset was not passed as an option when
822 * capturing the StackTrace.
823 */
824 int GetColumn() const;
825
826 /**
827 * Returns the name of the resource that contains the script for the
828 * function for this StackFrame.
829 */
830 Local<String> GetScriptName() const;
831
832 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000833 * Returns the name of the resource that contains the script for the
834 * function for this StackFrame or sourceURL value if the script name
835 * is undefined and its source ends with //@ sourceURL=... string.
836 */
837 Local<String> GetScriptNameOrSourceURL() const;
838
839 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000840 * Returns the name of the function associated with this stack frame.
841 */
842 Local<String> GetFunctionName() const;
843
844 /**
845 * Returns whether or not the associated function is compiled via a call to
846 * eval().
847 */
848 bool IsEval() const;
849
850 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000851 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000852 * constructor via "new".
853 */
854 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855};
856
857
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000858// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859
860
861/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000862 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000864class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 /**
867 * Returns true if this value is the undefined value. See ECMA-262
868 * 4.3.10.
869 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000870 inline bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871
872 /**
873 * Returns true if this value is the null value. See ECMA-262
874 * 4.3.11.
875 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000876 inline bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877
878 /**
879 * Returns true if this value is true.
880 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000881 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882
883 /**
884 * Returns true if this value is false.
885 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000886 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887
888 /**
889 * Returns true if this value is an instance of the String type.
890 * See ECMA-262 8.4.
891 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000892 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893
894 /**
895 * Returns true if this value is a function.
896 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000897 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
899 /**
900 * Returns true if this value is an array.
901 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000902 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
v8.team.kasperl727e9952008-09-02 14:56:44 +0000904 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 * Returns true if this value is an object.
906 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000907 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908
v8.team.kasperl727e9952008-09-02 14:56:44 +0000909 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910 * Returns true if this value is boolean.
911 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000912 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000913
v8.team.kasperl727e9952008-09-02 14:56:44 +0000914 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 * Returns true if this value is a number.
916 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000917 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918
v8.team.kasperl727e9952008-09-02 14:56:44 +0000919 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 * Returns true if this value is external.
921 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000922 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923
v8.team.kasperl727e9952008-09-02 14:56:44 +0000924 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925 * Returns true if this value is a 32-bit signed integer.
926 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000927 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000929 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000930 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000931 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000932 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000933
934 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000935 * Returns true if this value is a Date.
936 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000937 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000938
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000939 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000940 * Returns true if this value is a Boolean object.
941 */
942 V8EXPORT bool IsBooleanObject() const;
943
944 /**
945 * Returns true if this value is a Number object.
946 */
947 V8EXPORT bool IsNumberObject() const;
948
949 /**
950 * Returns true if this value is a String object.
951 */
952 V8EXPORT bool IsStringObject() const;
953
954 /**
955 * Returns true if this value is a NativeError.
956 */
957 V8EXPORT bool IsNativeError() const;
958
959 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000960 * Returns true if this value is a RegExp.
961 */
962 V8EXPORT bool IsRegExp() const;
963
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000964 V8EXPORT Local<Boolean> ToBoolean() const;
965 V8EXPORT Local<Number> ToNumber() const;
966 V8EXPORT Local<String> ToString() const;
967 V8EXPORT Local<String> ToDetailString() const;
968 V8EXPORT Local<Object> ToObject() const;
969 V8EXPORT Local<Integer> ToInteger() const;
970 V8EXPORT Local<Uint32> ToUint32() const;
971 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972
973 /**
974 * Attempts to convert a string to an array index.
975 * Returns an empty handle if the conversion fails.
976 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000977 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000979 V8EXPORT bool BooleanValue() const;
980 V8EXPORT double NumberValue() const;
981 V8EXPORT int64_t IntegerValue() const;
982 V8EXPORT uint32_t Uint32Value() const;
983 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984
985 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000986 V8EXPORT bool Equals(Handle<Value> that) const;
987 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000988
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000989 private:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000990 inline bool QuickIsUndefined() const;
991 inline bool QuickIsNull() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000992 inline bool QuickIsString() const;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000993 V8EXPORT bool FullIsUndefined() const;
994 V8EXPORT bool FullIsNull() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000995 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996};
997
998
999/**
1000 * The superclass of primitive values. See ECMA-262 4.3.2.
1001 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001002class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003
1004
1005/**
1006 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1007 * or false value.
1008 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001009class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001011 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012 static inline Handle<Boolean> New(bool value);
1013};
1014
1015
1016/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001017 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001019class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001021 /**
1022 * Returns the number of characters in this string.
1023 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001024 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025
v8.team.kasperl727e9952008-09-02 14:56:44 +00001026 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001027 * Returns the number of bytes in the UTF-8 encoded
1028 * representation of this string.
1029 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001030 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001031
1032 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001033 * A fast conservative check for non-ASCII characters. May
1034 * return true even for ASCII strings, but if it returns
1035 * false you can be sure that all characters are in the range
1036 * 0-127.
1037 */
1038 V8EXPORT bool MayContainNonAscii() const;
1039
1040 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001041 * Write the contents of the string to an external buffer.
1042 * If no arguments are given, expects the buffer to be large
1043 * enough to hold the entire string and NULL terminator. Copies
1044 * the contents of the string and the NULL terminator into the
1045 * buffer.
1046 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001047 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1048 * before the end of the buffer.
1049 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001050 * Copies up to length characters into the output buffer.
1051 * Only null-terminates if there is enough space in the buffer.
1052 *
1053 * \param buffer The buffer into which the string will be copied.
1054 * \param start The starting position within the string at which
1055 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001056 * \param length The number of characters to copy from the string. For
1057 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001058 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001059 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001060 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001061 * \return The number of characters copied to the buffer excluding the null
1062 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001063 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001064 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001065 enum WriteOptions {
1066 NO_OPTIONS = 0,
1067 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001068 NO_NULL_TERMINATION = 2,
1069 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001070 };
1071
lrn@chromium.org34e60782011-09-15 07:25:40 +00001072 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001073 V8EXPORT int Write(uint16_t* buffer,
1074 int start = 0,
1075 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001076 int options = NO_OPTIONS) const;
1077 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001078 V8EXPORT int WriteAscii(char* buffer,
1079 int start = 0,
1080 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001081 int options = NO_OPTIONS) const;
1082 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001083 V8EXPORT int WriteUtf8(char* buffer,
1084 int length = -1,
1085 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001086 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087
v8.team.kasperl727e9952008-09-02 14:56:44 +00001088 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001089 * A zero length string.
1090 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001091 V8EXPORT static v8::Local<v8::String> Empty();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001092 inline static v8::Local<v8::String> Empty(Isolate* isolate);
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001093
1094 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001095 * Returns true if the string is external
1096 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001097 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098
v8.team.kasperl727e9952008-09-02 14:56:44 +00001099 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001100 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001101 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001102 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001103
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001104 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001105 public:
1106 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001107
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001108 protected:
1109 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001110
1111 /**
1112 * Internally V8 will call this Dispose method when the external string
1113 * resource is no longer needed. The default implementation will use the
1114 * delete operator. This method can be overridden in subclasses to
1115 * control how allocated external string resources are disposed.
1116 */
1117 virtual void Dispose() { delete this; }
1118
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001119 private:
1120 // Disallow copying and assigning.
1121 ExternalStringResourceBase(const ExternalStringResourceBase&);
1122 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001123
1124 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001125 };
1126
v8.team.kasperl727e9952008-09-02 14:56:44 +00001127 /**
1128 * An ExternalStringResource is a wrapper around a two-byte string
1129 * buffer that resides outside V8's heap. Implement an
1130 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001131 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001132 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001133 class V8EXPORT ExternalStringResource
1134 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 public:
1136 /**
1137 * Override the destructor to manage the life cycle of the underlying
1138 * buffer.
1139 */
1140 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001141
1142 /**
1143 * The string data from the underlying buffer.
1144 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001146
1147 /**
1148 * The length of the string. That is, the number of two-byte characters.
1149 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001151
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152 protected:
1153 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 };
1155
1156 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001157 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001158 * string buffer that resides outside V8's heap. Implement an
1159 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001160 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001161 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001162 * UTF-8, which would require special treatment internally in the
1163 * engine and, in the case of UTF-8, do not allow efficient indexing.
1164 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001165 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001167 class V8EXPORT ExternalAsciiStringResource
1168 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169 public:
1170 /**
1171 * Override the destructor to manage the life cycle of the underlying
1172 * buffer.
1173 */
1174 virtual ~ExternalAsciiStringResource() {}
1175 /** The string data from the underlying buffer.*/
1176 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001177 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178 virtual size_t length() const = 0;
1179 protected:
1180 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181 };
1182
1183 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001184 * Get the ExternalStringResource for an external string. Returns
1185 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001187 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188
1189 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001190 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001191 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001193 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1194 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001196 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197
1198 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001199 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001201 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202 * be careful to supply the length parameter.
1203 * If it is not given, the function calls
1204 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001205 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001207 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208
lrn@chromium.org34e60782011-09-15 07:25:40 +00001209 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001210 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211
1212 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001213 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214
v8.team.kasperl727e9952008-09-02 14:56:44 +00001215 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001216 * Creates a new string by concatenating the left and the right strings
1217 * passed in as parameters.
1218 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001219 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001220 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001221
1222 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001223 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001224 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001225 * resource will be disposed by calling its Dispose method. The caller of
1226 * this function should not otherwise delete or modify the resource. Neither
1227 * should the underlying buffer be deallocated or modified except through the
1228 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001229 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001230 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001231
ager@chromium.org6f10e412009-02-13 10:11:16 +00001232 /**
1233 * Associate an external string resource with this string by transforming it
1234 * in place so that existing references to this string in the JavaScript heap
1235 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001236 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001237 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001238 * The string is not modified if the operation fails. See NewExternal for
1239 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001240 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001241 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242
v8.team.kasperl727e9952008-09-02 14:56:44 +00001243 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001244 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001245 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001246 * resource will be disposed by calling its Dispose method. The caller of
1247 * this function should not otherwise delete or modify the resource. Neither
1248 * should the underlying buffer be deallocated or modified except through the
1249 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001250 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001251 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001252
ager@chromium.org6f10e412009-02-13 10:11:16 +00001253 /**
1254 * Associate an external string resource with this string by transforming it
1255 * in place so that existing references to this string in the JavaScript heap
1256 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001257 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001258 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001259 * The string is not modified if the operation fails. See NewExternal for
1260 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001261 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001262 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001264 /**
1265 * Returns true if this string can be made external.
1266 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001267 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001268
lrn@chromium.org34e60782011-09-15 07:25:40 +00001269 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001270 V8EXPORT static Local<String> NewUndetectable(const char* data,
1271 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272
lrn@chromium.org34e60782011-09-15 07:25:40 +00001273 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001274 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1275 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276
1277 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001278 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001279 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001280 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001281 * then the length() method returns 0 and the * operator returns
1282 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001283 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001284 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001285 public:
1286 explicit Utf8Value(Handle<v8::Value> obj);
1287 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001288 char* operator*() { return str_; }
1289 const char* operator*() const { return str_; }
1290 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001291 private:
1292 char* str_;
1293 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001294
1295 // Disallow copying and assigning.
1296 Utf8Value(const Utf8Value&);
1297 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001298 };
1299
1300 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001301 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001303 * If conversion to a string fails (eg. due to an exception in the toString()
1304 * method of the object) then the length() method returns 0 and the * operator
1305 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001307 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 public:
1309 explicit AsciiValue(Handle<v8::Value> obj);
1310 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001311 char* operator*() { return str_; }
1312 const char* operator*() const { return str_; }
1313 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 private:
1315 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001316 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001317
1318 // Disallow copying and assigning.
1319 AsciiValue(const AsciiValue&);
1320 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 };
1322
1323 /**
1324 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001325 * If conversion to a string fails (eg. due to an exception in the toString()
1326 * method of the object) then the length() method returns 0 and the * operator
1327 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001329 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330 public:
1331 explicit Value(Handle<v8::Value> obj);
1332 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001333 uint16_t* operator*() { return str_; }
1334 const uint16_t* operator*() const { return str_; }
1335 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 private:
1337 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001338 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001339
1340 // Disallow copying and assigning.
1341 Value(const Value&);
1342 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001344
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001345 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001346 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1347 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348};
1349
1350
1351/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001352 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001354class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001356 V8EXPORT double Value() const;
1357 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001358 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001360 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001361 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362};
1363
1364
1365/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001366 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001368class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001370 V8EXPORT static Local<Integer> New(int32_t value);
1371 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1372 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001373 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001375 V8EXPORT Integer();
1376 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377};
1378
1379
1380/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001381 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001383class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001385 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001387 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388};
1389
1390
1391/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001392 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001394class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001396 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001398 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399};
1400
1401
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001402enum PropertyAttribute {
1403 None = 0,
1404 ReadOnly = 1 << 0,
1405 DontEnum = 1 << 1,
1406 DontDelete = 1 << 2
1407};
1408
ager@chromium.org3811b432009-10-28 14:53:37 +00001409enum ExternalArrayType {
1410 kExternalByteArray = 1,
1411 kExternalUnsignedByteArray,
1412 kExternalShortArray,
1413 kExternalUnsignedShortArray,
1414 kExternalIntArray,
1415 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001416 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001417 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001418 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001419};
1420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001422 * Accessor[Getter|Setter] are used as callback functions when
1423 * setting|getting a particular property. See Object and ObjectTemplate's
1424 * method SetAccessor.
1425 */
1426typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1427 const AccessorInfo& info);
1428
1429
1430typedef void (*AccessorSetter)(Local<String> property,
1431 Local<Value> value,
1432 const AccessorInfo& info);
1433
1434
1435/**
1436 * Access control specifications.
1437 *
1438 * Some accessors should be accessible across contexts. These
1439 * accessors have an explicit access control parameter which specifies
1440 * the kind of cross-context access that should be allowed.
1441 *
1442 * Additionally, for security, accessors can prohibit overwriting by
1443 * accessors defined in JavaScript. For objects that have such
1444 * accessors either locally or in their prototype chain it is not
1445 * possible to overwrite the accessor by using __defineGetter__ or
1446 * __defineSetter__ from JavaScript code.
1447 */
1448enum AccessControl {
1449 DEFAULT = 0,
1450 ALL_CAN_READ = 1,
1451 ALL_CAN_WRITE = 1 << 1,
1452 PROHIBITS_OVERWRITING = 1 << 2
1453};
1454
1455
1456/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001457 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001459class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001461 V8EXPORT bool Set(Handle<Value> key,
1462 Handle<Value> value,
1463 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001464
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001465 V8EXPORT bool Set(uint32_t index,
1466 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001467
ager@chromium.orge2902be2009-06-08 12:21:35 +00001468 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001469 // overriding accessors or read-only properties.
1470 //
1471 // Note that if the object has an interceptor the property will be set
1472 // locally, but since the interceptor takes precedence the local property
1473 // will only be returned if the interceptor doesn't return a value.
1474 //
1475 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001476 V8EXPORT bool ForceSet(Handle<Value> key,
1477 Handle<Value> value,
1478 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001479
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001480 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001482 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001483
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001484 /**
1485 * Gets the property attributes of a property which can be None or
1486 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1487 * None when the property doesn't exist.
1488 */
1489 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1490
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 // TODO(1245389): Replace the type-specific versions of these
1492 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001493 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001494
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001495 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001496
1497 // Delete a property on this object bypassing interceptors and
1498 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001499 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001500
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001501 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001502
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001503 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001505 V8EXPORT bool SetAccessor(Handle<String> name,
1506 AccessorGetter getter,
1507 AccessorSetter setter = 0,
1508 Handle<Value> data = Handle<Value>(),
1509 AccessControl settings = DEFAULT,
1510 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001513 * Returns an array containing the names of the enumerable properties
1514 * of this object, including properties from prototype objects. The
1515 * array returned by this method contains the same values as would
1516 * be enumerated by a for-in statement over this object.
1517 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001518 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001519
1520 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001521 * This function has the same functionality as GetPropertyNames but
1522 * the returned array doesn't contain the names of properties from
1523 * prototype objects.
1524 */
1525 V8EXPORT Local<Array> GetOwnPropertyNames();
1526
1527 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528 * Get the prototype object. This does not skip objects marked to
1529 * be skipped by __proto__ and it does not consult the security
1530 * handler.
1531 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001532 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533
1534 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001535 * Set the prototype object. This does not skip objects marked to
1536 * be skipped by __proto__ and it does not consult the security
1537 * handler.
1538 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001539 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001540
1541 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001542 * Finds an instance of the given function template in the prototype
1543 * chain.
1544 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001545 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1546 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001547
1548 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549 * Call builtin Object.prototype.toString on this object.
1550 * This is different from Value::ToString() that may call
1551 * user-defined toString function. This one does not.
1552 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001553 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001555 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001556 * Returns the function invoked as a constructor for this object.
1557 * May be the null value.
1558 */
1559 V8EXPORT Local<Value> GetConstructor();
1560
1561 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001562 * Returns the name of the function invoked as a constructor for this object.
1563 */
1564 V8EXPORT Local<String> GetConstructorName();
1565
kasper.lund212ac232008-07-16 07:07:30 +00001566 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001567 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001568 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001569 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001570 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001571 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001573 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001574 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001575
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001576 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001577 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001578
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001580 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001581 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1582 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1583 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584
1585 /**
1586 * If result.IsEmpty() no real property was located in the prototype chain.
1587 * This means interceptors in the prototype chain are not called.
1588 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001589 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1590 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001591
1592 /**
1593 * If result.IsEmpty() no real property was located on the object or
1594 * in the prototype chain.
1595 * This means interceptors in the prototype chain are not called.
1596 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001597 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598
1599 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001600 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601
kasper.lund212ac232008-07-16 07:07:30 +00001602 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001603 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001605 /**
1606 * Turns on access check on the object if the object is an instance of
1607 * a template that has access check callbacks. If an object has no
1608 * access check info, the object cannot be accessed by anyone.
1609 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001610 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001611
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001612 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001613 * Returns the identity hash for this object. The current implementation
1614 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001615 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001616 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001617 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001618 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001619 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001620
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001621 /**
1622 * Access hidden properties on JavaScript objects. These properties are
1623 * hidden from the executing JavaScript and only accessible through the V8
1624 * C++ API. Hidden properties introduced by V8 internally (for example the
1625 * identity hash) are prefixed with "v8::".
1626 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001627 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1628 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1629 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001630
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001631 /**
1632 * Returns true if this is an instance of an api function (one
1633 * created from a function created from a function template) and has
1634 * been modified since it was created. Note that this method is
1635 * conservative and may return true for objects that haven't actually
1636 * been modified.
1637 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001638 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001639
1640 /**
1641 * Clone this object with a fast but shallow copy. Values will point
1642 * to the same values as the original object.
1643 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001644 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001646 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001647 * Returns the context in which the object was created.
1648 */
1649 V8EXPORT Local<Context> CreationContext();
1650
1651 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001652 * Set the backing store of the indexed properties to be managed by the
1653 * embedding layer. Access to the indexed properties will follow the rules
1654 * spelled out in CanvasPixelArray.
1655 * Note: The embedding program still owns the data and needs to ensure that
1656 * the backing store is preserved while V8 has a reference.
1657 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001658 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001659 V8EXPORT bool HasIndexedPropertiesInPixelData();
1660 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1661 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001662
ager@chromium.org3811b432009-10-28 14:53:37 +00001663 /**
1664 * Set the backing store of the indexed properties to be managed by the
1665 * embedding layer. Access to the indexed properties will follow the rules
1666 * spelled out for the CanvasArray subtypes in the WebGL specification.
1667 * Note: The embedding program still owns the data and needs to ensure that
1668 * the backing store is preserved while V8 has a reference.
1669 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001670 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1671 void* data,
1672 ExternalArrayType array_type,
1673 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001674 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1675 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1676 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1677 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001678
lrn@chromium.org1c092762011-05-09 09:42:16 +00001679 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001680 * Checks whether a callback is set by the
1681 * ObjectTemplate::SetCallAsFunctionHandler method.
1682 * When an Object is callable this method returns true.
1683 */
1684 V8EXPORT bool IsCallable();
1685
1686 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001687 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001688 * ObjectTemplate::SetCallAsFunctionHandler method.
1689 */
1690 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1691 int argc,
1692 Handle<Value> argv[]);
1693
1694 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001695 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001696 * ObjectTemplate::SetCallAsFunctionHandler method.
1697 * Note: This method behaves like the Function::NewInstance method.
1698 */
1699 V8EXPORT Local<Value> CallAsConstructor(int argc,
1700 Handle<Value> argv[]);
1701
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001702 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001703 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001704
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001706 V8EXPORT Object();
1707 V8EXPORT static void CheckCast(Value* obj);
1708 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1709 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001710
1711 /**
1712 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001713 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001714 */
1715 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716};
1717
1718
1719/**
1720 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1721 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001722class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001724 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725
ager@chromium.org3e875802009-06-29 08:26:34 +00001726 /**
1727 * Clones an element at index |index|. Returns an empty
1728 * handle if cloning fails (for any reason).
1729 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001730 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001731
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001732 /**
1733 * Creates a JavaScript array with the given length. If the length
1734 * is negative the returned array will have length 0.
1735 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001736 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001737
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001738 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001740 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001741 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742};
1743
1744
1745/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001746 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001748class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001750 V8EXPORT Local<Object> NewInstance() const;
1751 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1752 V8EXPORT Local<Value> Call(Handle<Object> recv,
1753 int argc,
1754 Handle<Value> argv[]);
1755 V8EXPORT void SetName(Handle<String> name);
1756 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001757
1758 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001759 * Name inferred from variable or property assignment of this function.
1760 * Used to facilitate debugging and profiling of JavaScript code written
1761 * in an OO style, where many functions are anonymous but are assigned
1762 * to object properties.
1763 */
1764 V8EXPORT Handle<Value> GetInferredName() const;
1765
1766 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001767 * Returns zero based line number of function body and
1768 * kLineOffsetNotFound if no information available.
1769 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001770 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001771 /**
1772 * Returns zero based column number of function body and
1773 * kLineOffsetNotFound if no information available.
1774 */
1775 V8EXPORT int GetScriptColumnNumber() const;
1776 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001777 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001778 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001779 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001780
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001782 V8EXPORT Function();
1783 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784};
1785
1786
1787/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001788 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1789 */
1790class Date : public Object {
1791 public:
1792 V8EXPORT static Local<Value> New(double time);
1793
1794 /**
1795 * A specialization of Value::NumberValue that is more efficient
1796 * because we know the structure of this object.
1797 */
1798 V8EXPORT double NumberValue() const;
1799
1800 static inline Date* Cast(v8::Value* obj);
1801
1802 /**
1803 * Notification that the embedder has changed the time zone,
1804 * daylight savings time, or other date / time configuration
1805 * parameters. V8 keeps a cache of various values used for
1806 * date / time computation. This notification will reset
1807 * those cached values for the current context so that date /
1808 * time configuration changes would be reflected in the Date
1809 * object.
1810 *
1811 * This API should not be called more than needed as it will
1812 * negatively impact the performance of date operations.
1813 */
1814 V8EXPORT static void DateTimeConfigurationChangeNotification();
1815
1816 private:
1817 V8EXPORT static void CheckCast(v8::Value* obj);
1818};
1819
1820
1821/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001822 * A Number object (ECMA-262, 4.3.21).
1823 */
1824class NumberObject : public Object {
1825 public:
1826 V8EXPORT static Local<Value> New(double value);
1827
1828 /**
1829 * Returns the Number held by the object.
1830 */
1831 V8EXPORT double NumberValue() const;
1832
1833 static inline NumberObject* Cast(v8::Value* obj);
1834
1835 private:
1836 V8EXPORT static void CheckCast(v8::Value* obj);
1837};
1838
1839
1840/**
1841 * A Boolean object (ECMA-262, 4.3.15).
1842 */
1843class BooleanObject : public Object {
1844 public:
1845 V8EXPORT static Local<Value> New(bool value);
1846
1847 /**
1848 * Returns the Boolean held by the object.
1849 */
1850 V8EXPORT bool BooleanValue() const;
1851
1852 static inline BooleanObject* Cast(v8::Value* obj);
1853
1854 private:
1855 V8EXPORT static void CheckCast(v8::Value* obj);
1856};
1857
1858
1859/**
1860 * A String object (ECMA-262, 4.3.18).
1861 */
1862class StringObject : public Object {
1863 public:
1864 V8EXPORT static Local<Value> New(Handle<String> value);
1865
1866 /**
1867 * Returns the String held by the object.
1868 */
1869 V8EXPORT Local<String> StringValue() const;
1870
1871 static inline StringObject* Cast(v8::Value* obj);
1872
1873 private:
1874 V8EXPORT static void CheckCast(v8::Value* obj);
1875};
1876
1877
1878/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001879 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1880 */
1881class RegExp : public Object {
1882 public:
1883 /**
1884 * Regular expression flag bits. They can be or'ed to enable a set
1885 * of flags.
1886 */
1887 enum Flags {
1888 kNone = 0,
1889 kGlobal = 1,
1890 kIgnoreCase = 2,
1891 kMultiline = 4
1892 };
1893
1894 /**
1895 * Creates a regular expression from the given pattern string and
1896 * the flags bit field. May throw a JavaScript exception as
1897 * described in ECMA-262, 15.10.4.1.
1898 *
1899 * For example,
1900 * RegExp::New(v8::String::New("foo"),
1901 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1902 * is equivalent to evaluating "/foo/gm".
1903 */
1904 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1905 Flags flags);
1906
1907 /**
1908 * Returns the value of the source property: a string representing
1909 * the regular expression.
1910 */
1911 V8EXPORT Local<String> GetSource() const;
1912
1913 /**
1914 * Returns the flags bit field.
1915 */
1916 V8EXPORT Flags GetFlags() const;
1917
1918 static inline RegExp* Cast(v8::Value* obj);
1919
1920 private:
1921 V8EXPORT static void CheckCast(v8::Value* obj);
1922};
1923
1924
1925/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001926 * A JavaScript value that wraps a C++ void*. This type of value is
1927 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001929 *
1930 * The Wrap function V8 will return the most optimal Value object wrapping the
1931 * C++ void*. The type of the value is not guaranteed to be an External object
1932 * and no assumptions about its type should be made. To access the wrapped
1933 * value Unwrap should be used, all other operations on that object will lead
1934 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001936class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001938 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001939 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001940
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001941 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001942 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001943 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001945 V8EXPORT External();
1946 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001947 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001948 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949};
1950
1951
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001952// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953
1954
1955/**
1956 * The superclass of object and function templates.
1957 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001958class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 public:
1960 /** Adds a property to each instance created by this template.*/
1961 void Set(Handle<String> name, Handle<Data> value,
1962 PropertyAttribute attributes = None);
1963 inline void Set(const char* name, Handle<Data> value);
1964 private:
1965 Template();
1966
1967 friend class ObjectTemplate;
1968 friend class FunctionTemplate;
1969};
1970
1971
1972/**
1973 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001974 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975 * including the receiver, the number and values of arguments, and
1976 * the holder of the function.
1977 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001978class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979 public:
1980 inline int Length() const;
1981 inline Local<Value> operator[](int i) const;
1982 inline Local<Function> Callee() const;
1983 inline Local<Object> This() const;
1984 inline Local<Object> Holder() const;
1985 inline bool IsConstructCall() const;
1986 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001987 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00001988
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001990 static const int kIsolateIndex = 0;
1991 static const int kDataIndex = -1;
1992 static const int kCalleeIndex = -2;
1993 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001994
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001996 inline Arguments(internal::Object** implicit_args,
1997 internal::Object** values,
1998 int length,
1999 bool is_construct_call);
2000 internal::Object** implicit_args_;
2001 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002003 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004};
2005
2006
2007/**
2008 * The information passed to an accessor callback about the context
2009 * of the property access.
2010 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002011class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002013 inline AccessorInfo(internal::Object** args)
2014 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002015 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 inline Local<Value> Data() const;
2017 inline Local<Object> This() const;
2018 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002019
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002021 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022};
2023
2024
2025typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2026
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 * NamedProperty[Getter|Setter] are used as interceptors on object.
2029 * See ObjectTemplate::SetNamedPropertyHandler.
2030 */
2031typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2032 const AccessorInfo& info);
2033
2034
2035/**
2036 * Returns the value if the setter intercepts the request.
2037 * Otherwise, returns an empty handle.
2038 */
2039typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2040 Local<Value> value,
2041 const AccessorInfo& info);
2042
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043/**
2044 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002045 * The result is an integer encoding property attributes (like v8::None,
2046 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002048typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2049 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050
2051
2052/**
2053 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002054 * The return value is true if the property could be deleted and false
2055 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 */
2057typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2058 const AccessorInfo& info);
2059
2060/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002061 * Returns an array containing the names of the properties the named
2062 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063 */
2064typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2065
v8.team.kasperl727e9952008-09-02 14:56:44 +00002066
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002068 * Returns the value of the property if the getter intercepts the
2069 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 */
2071typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2072 const AccessorInfo& info);
2073
2074
2075/**
2076 * Returns the value if the setter intercepts the request.
2077 * Otherwise, returns an empty handle.
2078 */
2079typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2080 Local<Value> value,
2081 const AccessorInfo& info);
2082
2083
2084/**
2085 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002086 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002088typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2089 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090
2091/**
2092 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002093 * The return value is true if the property could be deleted and false
2094 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 */
2096typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2097 const AccessorInfo& info);
2098
v8.team.kasperl727e9952008-09-02 14:56:44 +00002099/**
2100 * Returns an array containing the indices of the properties the
2101 * indexed property getter intercepts.
2102 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2104
2105
2106/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002107 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108 */
2109enum AccessType {
2110 ACCESS_GET,
2111 ACCESS_SET,
2112 ACCESS_HAS,
2113 ACCESS_DELETE,
2114 ACCESS_KEYS
2115};
2116
v8.team.kasperl727e9952008-09-02 14:56:44 +00002117
2118/**
2119 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002120 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002121 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002122typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 Local<Value> key,
2124 AccessType type,
2125 Local<Value> data);
2126
v8.team.kasperl727e9952008-09-02 14:56:44 +00002127
2128/**
2129 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002130 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002131 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002132typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 uint32_t index,
2134 AccessType type,
2135 Local<Value> data);
2136
2137
2138/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002139 * A FunctionTemplate is used to create functions at runtime. There
2140 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002141 * context. The lifetime of the created function is equal to the
2142 * lifetime of the context. So in case the embedder needs to create
2143 * temporary functions that can be collected using Scripts is
2144 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145 *
2146 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002147 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002149 * A FunctionTemplate has a corresponding instance template which is
2150 * used to create object instances when the function is used as a
2151 * constructor. Properties added to the instance template are added to
2152 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 *
2154 * A FunctionTemplate can have a prototype template. The prototype template
2155 * is used to create the prototype object of the function.
2156 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002157 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002159 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2161 * t->Set("func_property", v8::Number::New(1));
2162 *
2163 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2164 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2165 * proto_t->Set("proto_const", v8::Number::New(2));
2166 *
2167 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2168 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2169 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2170 * instance_t->Set("instance_property", Number::New(3));
2171 *
2172 * v8::Local<v8::Function> function = t->GetFunction();
2173 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002174 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 *
2176 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002177 * and "instance" for the instance object created above. The function
2178 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002180 * \code
2181 * func_property in function == true;
2182 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002184 * function.prototype.proto_method() invokes 'InvokeCallback'
2185 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002187 * instance instanceof function == true;
2188 * instance.instance_accessor calls 'InstanceAccessorCallback'
2189 * instance.instance_property == 3;
2190 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002192 * A FunctionTemplate can inherit from another one by calling the
2193 * FunctionTemplate::Inherit method. The following graph illustrates
2194 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002196 * \code
2197 * FunctionTemplate Parent -> Parent() . prototype -> { }
2198 * ^ ^
2199 * | Inherit(Parent) | .__proto__
2200 * | |
2201 * FunctionTemplate Child -> Child() . prototype -> { }
2202 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002204 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2205 * object of the Child() function has __proto__ pointing to the
2206 * Parent() function's prototype object. An instance of the Child
2207 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002209 * Let Parent be the FunctionTemplate initialized in the previous
2210 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002212 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 * Local<FunctionTemplate> parent = t;
2214 * Local<FunctionTemplate> child = FunctionTemplate::New();
2215 * child->Inherit(parent);
2216 *
2217 * Local<Function> child_function = child->GetFunction();
2218 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002219 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002221 * The Child function and Child instance will have the following
2222 * properties:
2223 *
2224 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002226 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002228 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002230class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 public:
2232 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002233 static Local<FunctionTemplate> New(
2234 InvocationCallback callback = 0,
2235 Handle<Value> data = Handle<Value>(),
2236 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 /** Returns the unique function instance in the current execution context.*/
2238 Local<Function> GetFunction();
2239
v8.team.kasperl727e9952008-09-02 14:56:44 +00002240 /**
2241 * Set the call-handler callback for a FunctionTemplate. This
2242 * callback is called whenever the function created from this
2243 * FunctionTemplate is called.
2244 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 void SetCallHandler(InvocationCallback callback,
2246 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 Local<ObjectTemplate> InstanceTemplate();
2250
2251 /** Causes the function template to inherit from a parent function template.*/
2252 void Inherit(Handle<FunctionTemplate> parent);
2253
2254 /**
2255 * A PrototypeTemplate is the template used to create the prototype object
2256 * of the function created by this template.
2257 */
2258 Local<ObjectTemplate> PrototypeTemplate();
2259
v8.team.kasperl727e9952008-09-02 14:56:44 +00002260
2261 /**
2262 * Set the class name of the FunctionTemplate. This is used for
2263 * printing objects created with the function created from the
2264 * FunctionTemplate as its constructor.
2265 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 void SetClassName(Handle<String> name);
2267
2268 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002269 * Determines whether the __proto__ accessor ignores instances of
2270 * the function template. If instances of the function template are
2271 * ignored, __proto__ skips all instances and instead returns the
2272 * next object in the prototype chain.
2273 *
2274 * Call with a value of true to make the __proto__ accessor ignore
2275 * instances of the function template. Call with a value of false
2276 * to make the __proto__ accessor not ignore instances of the
2277 * function template. By default, instances of a function template
2278 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 */
2280 void SetHiddenPrototype(bool value);
2281
2282 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002283 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2284 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002285 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002286 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002287
2288 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002289 * Returns true if the given object is an instance of this function
2290 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 */
2292 bool HasInstance(Handle<Value> object);
2293
2294 private:
2295 FunctionTemplate();
2296 void AddInstancePropertyAccessor(Handle<String> name,
2297 AccessorGetter getter,
2298 AccessorSetter setter,
2299 Handle<Value> data,
2300 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002301 PropertyAttribute attributes,
2302 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2304 NamedPropertySetter setter,
2305 NamedPropertyQuery query,
2306 NamedPropertyDeleter remover,
2307 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002308 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002309 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2310 IndexedPropertySetter setter,
2311 IndexedPropertyQuery query,
2312 IndexedPropertyDeleter remover,
2313 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002314 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2316 Handle<Value> data);
2317
2318 friend class Context;
2319 friend class ObjectTemplate;
2320};
2321
2322
2323/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002324 * An ObjectTemplate is used to create objects at runtime.
2325 *
2326 * Properties added to an ObjectTemplate are added to each object
2327 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002329class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002331 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 /** Creates a new instance of this template.*/
2335 Local<Object> NewInstance();
2336
2337 /**
2338 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002339 *
2340 * Whenever the property with the given name is accessed on objects
2341 * created from this ObjectTemplate the getter and setter callbacks
2342 * are called instead of getting and setting the property directly
2343 * on the JavaScript object.
2344 *
2345 * \param name The name of the property for which an accessor is added.
2346 * \param getter The callback to invoke when getting the property.
2347 * \param setter The callback to invoke when setting the property.
2348 * \param data A piece of data that will be passed to the getter and setter
2349 * callbacks whenever they are invoked.
2350 * \param settings Access control settings for the accessor. This is a bit
2351 * field consisting of one of more of
2352 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2353 * The default is to not allow cross-context access.
2354 * ALL_CAN_READ means that all cross-context reads are allowed.
2355 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2356 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2357 * cross-context access.
2358 * \param attribute The attributes of the property for which an accessor
2359 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002360 * \param signature The signature describes valid receivers for the accessor
2361 * and is used to perform implicit instance checks against them. If the
2362 * receiver is incompatible (i.e. is not an instance of the constructor as
2363 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2364 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365 */
2366 void SetAccessor(Handle<String> name,
2367 AccessorGetter getter,
2368 AccessorSetter setter = 0,
2369 Handle<Value> data = Handle<Value>(),
2370 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002371 PropertyAttribute attribute = None,
2372 Handle<AccessorSignature> signature =
2373 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374
2375 /**
2376 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002377 *
2378 * Whenever a named property is accessed on objects created from
2379 * this object template, the provided callback is invoked instead of
2380 * accessing the property directly on the JavaScript object.
2381 *
2382 * \param getter The callback to invoke when getting a property.
2383 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002384 * \param query The callback to invoke to check if a property is present,
2385 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002386 * \param deleter The callback to invoke when deleting a property.
2387 * \param enumerator The callback to invoke to enumerate all the named
2388 * properties of an object.
2389 * \param data A piece of data that will be passed to the callbacks
2390 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391 */
2392 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2393 NamedPropertySetter setter = 0,
2394 NamedPropertyQuery query = 0,
2395 NamedPropertyDeleter deleter = 0,
2396 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002397 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398
2399 /**
2400 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002401 *
2402 * Whenever an indexed property is accessed on objects created from
2403 * this object template, the provided callback is invoked instead of
2404 * accessing the property directly on the JavaScript object.
2405 *
2406 * \param getter The callback to invoke when getting a property.
2407 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002408 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002409 * \param deleter The callback to invoke when deleting a property.
2410 * \param enumerator The callback to invoke to enumerate all the indexed
2411 * properties of an object.
2412 * \param data A piece of data that will be passed to the callbacks
2413 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 */
2415 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2416 IndexedPropertySetter setter = 0,
2417 IndexedPropertyQuery query = 0,
2418 IndexedPropertyDeleter deleter = 0,
2419 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002420 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002421
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 /**
2423 * Sets the callback to be used when calling instances created from
2424 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002425 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 * function.
2427 */
2428 void SetCallAsFunctionHandler(InvocationCallback callback,
2429 Handle<Value> data = Handle<Value>());
2430
v8.team.kasperl727e9952008-09-02 14:56:44 +00002431 /**
2432 * Mark object instances of the template as undetectable.
2433 *
2434 * In many ways, undetectable objects behave as though they are not
2435 * there. They behave like 'undefined' in conditionals and when
2436 * printed. However, properties can be accessed and called as on
2437 * normal objects.
2438 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439 void MarkAsUndetectable();
2440
v8.team.kasperl727e9952008-09-02 14:56:44 +00002441 /**
2442 * Sets access check callbacks on the object template.
2443 *
2444 * When accessing properties on instances of this object template,
2445 * the access check callback will be called to determine whether or
2446 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002447 * The last parameter specifies whether access checks are turned
2448 * on by default on instances. If access checks are off by default,
2449 * they can be turned on on individual instances by calling
2450 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002451 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2453 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002454 Handle<Value> data = Handle<Value>(),
2455 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002456
kasper.lund212ac232008-07-16 07:07:30 +00002457 /**
2458 * Gets the number of internal fields for objects generated from
2459 * this template.
2460 */
2461 int InternalFieldCount();
2462
2463 /**
2464 * Sets the number of internal fields for objects generated from
2465 * this template.
2466 */
2467 void SetInternalFieldCount(int value);
2468
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002469 private:
2470 ObjectTemplate();
2471 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2472 friend class FunctionTemplate;
2473};
2474
2475
2476/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002477 * A Signature specifies which receivers and arguments are valid
2478 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002480class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481 public:
2482 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2483 Handle<FunctionTemplate>(),
2484 int argc = 0,
2485 Handle<FunctionTemplate> argv[] = 0);
2486 private:
2487 Signature();
2488};
2489
2490
2491/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002492 * An AccessorSignature specifies which receivers are valid parameters
2493 * to an accessor callback.
2494 */
2495class V8EXPORT AccessorSignature : public Data {
2496 public:
2497 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2498 Handle<FunctionTemplate>());
2499 private:
2500 AccessorSignature();
2501};
2502
2503
2504/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002505 * A utility for determining the type of objects based on the template
2506 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002508class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509 public:
2510 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2511 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2512 int match(Handle<Value> value);
2513 private:
2514 TypeSwitch();
2515};
2516
2517
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002518// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002520class V8EXPORT ExternalAsciiStringResourceImpl
2521 : public String::ExternalAsciiStringResource {
2522 public:
2523 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2524 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2525 : data_(data), length_(length) {}
2526 const char* data() const { return data_; }
2527 size_t length() const { return length_; }
2528
2529 private:
2530 const char* data_;
2531 size_t length_;
2532};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533
2534/**
2535 * Ignore
2536 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002537class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002539 // Note that the strings passed into this constructor must live as long
2540 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541 Extension(const char* name,
2542 const char* source = 0,
2543 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002544 const char** deps = 0,
2545 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546 virtual ~Extension() { }
2547 virtual v8::Handle<v8::FunctionTemplate>
2548 GetNativeFunction(v8::Handle<v8::String> name) {
2549 return v8::Handle<v8::FunctionTemplate>();
2550 }
2551
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002552 const char* name() const { return name_; }
2553 size_t source_length() const { return source_length_; }
2554 const String::ExternalAsciiStringResource* source() const {
2555 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 int dependency_count() { return dep_count_; }
2557 const char** dependencies() { return deps_; }
2558 void set_auto_enable(bool value) { auto_enable_ = value; }
2559 bool auto_enable() { return auto_enable_; }
2560
2561 private:
2562 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002563 size_t source_length_; // expected to initialize before source_
2564 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565 int dep_count_;
2566 const char** deps_;
2567 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002568
2569 // Disallow copying and assigning.
2570 Extension(const Extension&);
2571 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572};
2573
2574
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002575void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002576
2577
2578/**
2579 * Ignore
2580 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002581class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 public:
2583 inline DeclareExtension(Extension* extension) {
2584 RegisterExtension(extension);
2585 }
2586};
2587
2588
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002589// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590
2591
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002592Handle<Primitive> V8EXPORT Undefined();
2593Handle<Primitive> V8EXPORT Null();
2594Handle<Boolean> V8EXPORT True();
2595Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002597inline Handle<Primitive> Undefined(Isolate* isolate);
2598inline Handle<Primitive> Null(Isolate* isolate);
2599inline Handle<Boolean> True(Isolate* isolate);
2600inline Handle<Boolean> False(Isolate* isolate);
2601
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602
2603/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002604 * A set of constraints that specifies the limits of the runtime's memory use.
2605 * You must set the heap size before initializing the VM - the size cannot be
2606 * adjusted after the VM is initialized.
2607 *
2608 * If you are using threads then you should hold the V8::Locker lock while
2609 * setting the stack limit and you must set a non-default stack limit separately
2610 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002612class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613 public:
2614 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002615 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002617 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002619 int max_executable_size() { return max_executable_size_; }
2620 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002621 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002622 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002623 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2624 private:
2625 int max_young_space_size_;
2626 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002627 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 uint32_t* stack_limit_;
2629};
2630
2631
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002632bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633
2634
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002635// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636
2637
2638typedef void (*FatalErrorCallback)(const char* location, const char* message);
2639
2640
2641typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2642
2643
2644/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002645 * Schedules an exception to be thrown when returning to JavaScript. When an
2646 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002648 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002650Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651
2652/**
2653 * Create new error objects by calling the corresponding error object
2654 * constructor with the message.
2655 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002656class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 public:
2658 static Local<Value> RangeError(Handle<String> message);
2659 static Local<Value> ReferenceError(Handle<String> message);
2660 static Local<Value> SyntaxError(Handle<String> message);
2661 static Local<Value> TypeError(Handle<String> message);
2662 static Local<Value> Error(Handle<String> message);
2663};
2664
2665
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002666// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002668typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002670typedef void* (*CreateHistogramCallback)(const char* name,
2671 int min,
2672 int max,
2673 size_t buckets);
2674
2675typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2676
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002677// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002678 enum ObjectSpace {
2679 kObjectSpaceNewSpace = 1 << 0,
2680 kObjectSpaceOldPointerSpace = 1 << 1,
2681 kObjectSpaceOldDataSpace = 1 << 2,
2682 kObjectSpaceCodeSpace = 1 << 3,
2683 kObjectSpaceMapSpace = 1 << 4,
2684 kObjectSpaceLoSpace = 1 << 5,
2685
2686 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2687 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2688 kObjectSpaceLoSpace
2689 };
2690
2691 enum AllocationAction {
2692 kAllocationActionAllocate = 1 << 0,
2693 kAllocationActionFree = 1 << 1,
2694 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2695 };
2696
2697typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2698 AllocationAction action,
2699 int size);
2700
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002701// --- Leave Script Callback ---
2702typedef void (*CallCompletedCallback)();
2703
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002704// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2706 AccessType type,
2707 Local<Value> data);
2708
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002709// --- AllowCodeGenerationFromStrings callbacks ---
2710
2711/**
2712 * Callback to check if code generation from strings is allowed. See
2713 * Context::AllowCodeGenerationFromStrings.
2714 */
2715typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2716
2717// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718
2719/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002720 * Applications can register callback functions which will be called
2721 * before and after a garbage collection. Allocations are not
2722 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002723 * objects (set or delete properties for example) since it is possible
2724 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002726enum GCType {
2727 kGCTypeScavenge = 1 << 0,
2728 kGCTypeMarkSweepCompact = 1 << 1,
2729 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2730};
2731
2732enum GCCallbackFlags {
2733 kNoGCCallbackFlags = 0,
2734 kGCCallbackFlagCompacted = 1 << 0
2735};
2736
2737typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2738typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2739
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740typedef void (*GCCallback)();
2741
2742
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002744 * Collection of V8 heap information.
2745 *
2746 * Instances of this class can be passed to v8::V8::HeapStatistics to
2747 * get heap statistics from V8.
2748 */
2749class V8EXPORT HeapStatistics {
2750 public:
2751 HeapStatistics();
2752 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002753 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002754 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002755 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002756
2757 private:
2758 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002759 void set_total_heap_size_executable(size_t size) {
2760 total_heap_size_executable_ = size;
2761 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002762 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002763 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002764
2765 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002766 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002767 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002768 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002769
2770 friend class V8;
2771};
2772
2773
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002774class RetainedObjectInfo;
2775
ager@chromium.org3811b432009-10-28 14:53:37 +00002776/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002777 * Isolate represents an isolated instance of the V8 engine. V8
2778 * isolates have completely separate states. Objects from one isolate
2779 * must not be used in other isolates. When V8 is initialized a
2780 * default isolate is implicitly created and entered. The embedder
2781 * can create additional isolates and use them in parallel in multiple
2782 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002783 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002784 */
2785class V8EXPORT Isolate {
2786 public:
2787 /**
2788 * Stack-allocated class which sets the isolate for all operations
2789 * executed within a local scope.
2790 */
2791 class V8EXPORT Scope {
2792 public:
2793 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2794 isolate->Enter();
2795 }
2796
2797 ~Scope() { isolate_->Exit(); }
2798
2799 private:
2800 Isolate* const isolate_;
2801
2802 // Prevent copying of Scope objects.
2803 Scope(const Scope&);
2804 Scope& operator=(const Scope&);
2805 };
2806
2807 /**
2808 * Creates a new isolate. Does not change the currently entered
2809 * isolate.
2810 *
2811 * When an isolate is no longer used its resources should be freed
2812 * by calling Dispose(). Using the delete operator is not allowed.
2813 */
2814 static Isolate* New();
2815
2816 /**
2817 * Returns the entered isolate for the current thread or NULL in
2818 * case there is no current isolate.
2819 */
2820 static Isolate* GetCurrent();
2821
2822 /**
2823 * Methods below this point require holding a lock (using Locker) in
2824 * a multi-threaded environment.
2825 */
2826
2827 /**
2828 * Sets this isolate as the entered one for the current thread.
2829 * Saves the previously entered one (if any), so that it can be
2830 * restored when exiting. Re-entering an isolate is allowed.
2831 */
2832 void Enter();
2833
2834 /**
2835 * Exits this isolate by restoring the previously entered one in the
2836 * current thread. The isolate may still stay the same, if it was
2837 * entered more than once.
2838 *
2839 * Requires: this == Isolate::GetCurrent().
2840 */
2841 void Exit();
2842
2843 /**
2844 * Disposes the isolate. The isolate must not be entered by any
2845 * thread to be disposable.
2846 */
2847 void Dispose();
2848
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002849 /**
2850 * Associate embedder-specific data with the isolate
2851 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002852 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002853
2854 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002855 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002856 * Returns NULL if SetData has never been called.
2857 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002858 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002859
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002860 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002861 Isolate();
2862 Isolate(const Isolate&);
2863 ~Isolate();
2864 Isolate& operator=(const Isolate&);
2865 void* operator new(size_t size);
2866 void operator delete(void*, size_t);
2867};
2868
2869
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002870class StartupData {
2871 public:
2872 enum CompressionAlgorithm {
2873 kUncompressed,
2874 kBZip2
2875 };
2876
2877 const char* data;
2878 int compressed_size;
2879 int raw_size;
2880};
2881
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002882
2883/**
2884 * A helper class for driving V8 startup data decompression. It is based on
2885 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2886 * for an embedder to use this class, instead, API functions can be used
2887 * directly.
2888 *
2889 * For an example of the class usage, see the "shell.cc" sample application.
2890 */
2891class V8EXPORT StartupDataDecompressor { // NOLINT
2892 public:
2893 StartupDataDecompressor();
2894 virtual ~StartupDataDecompressor();
2895 int Decompress();
2896
2897 protected:
2898 virtual int DecompressData(char* raw_data,
2899 int* raw_data_size,
2900 const char* compressed_data,
2901 int compressed_data_size) = 0;
2902
2903 private:
2904 char** raw_data;
2905};
2906
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002907
2908/**
2909 * EntropySource is used as a callback function when v8 needs a source
2910 * of entropy.
2911 */
2912typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2913
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002914
2915/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002916 * ReturnAddressLocationResolver is used as a callback function when v8 is
2917 * resolving the location of a return address on the stack. Profilers that
2918 * change the return address on the stack can use this to resolve the stack
2919 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002920 *
2921 * \param return_addr_location points to a location on stack where a machine
2922 * return address resides.
2923 * \returns either return_addr_location, or else a pointer to the profiler's
2924 * copy of the original return address.
2925 *
2926 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00002927 */
2928typedef uintptr_t (*ReturnAddressLocationResolver)(
2929 uintptr_t return_addr_location);
2930
2931
2932/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002933 * FunctionEntryHook is the type of the profile entry hook called at entry to
2934 * any generated function when function-level profiling is enabled.
2935 *
2936 * \param function the address of the function that's being entered.
2937 * \param return_addr_location points to a location on stack where the machine
2938 * return address resides. This can be used to identify the caller of
2939 * \p function, and/or modified to divert execution when \p function exits.
2940 *
2941 * \note the entry hook must not cause garbage collection.
2942 */
2943typedef void (*FunctionEntryHook)(uintptr_t function,
2944 uintptr_t return_addr_location);
2945
2946
2947/**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002948 * Interface for iterating though all external resources in the heap.
2949 */
2950class V8EXPORT ExternalResourceVisitor { // NOLINT
2951 public:
2952 virtual ~ExternalResourceVisitor() {}
2953 virtual void VisitExternalString(Handle<String> string) {}
2954};
2955
2956
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002957/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 * Container class for static utility functions.
2959 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002960class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002962 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002963 static void SetFatalErrorHandler(FatalErrorCallback that);
2964
v8.team.kasperl727e9952008-09-02 14:56:44 +00002965 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002966 * Set the callback to invoke to check if code generation from
2967 * strings should be allowed.
2968 */
2969 static void SetAllowCodeGenerationFromStringsCallback(
2970 AllowCodeGenerationFromStringsCallback that);
2971
2972 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002973 * Ignore out-of-memory exceptions.
2974 *
2975 * V8 running out of memory is treated as a fatal error by default.
2976 * This means that the fatal error handler is called and that V8 is
2977 * terminated.
2978 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002979 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002980 * out-of-memory situation as a fatal error. This way, the contexts
2981 * that did not cause the out of memory problem might be able to
2982 * continue execution.
2983 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002984 static void IgnoreOutOfMemoryException();
2985
v8.team.kasperl727e9952008-09-02 14:56:44 +00002986 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002987 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002988 * fatal errors such as out-of-memory situations.
2989 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002990 static bool IsDead();
2991
2992 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002993 * The following 4 functions are to be used when V8 is built with
2994 * the 'compress_startup_data' flag enabled. In this case, the
2995 * embedder must decompress startup data prior to initializing V8.
2996 *
2997 * This is how interaction with V8 should look like:
2998 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2999 * v8::StartupData* compressed_data =
3000 * new v8::StartupData[compressed_data_count];
3001 * v8::V8::GetCompressedStartupData(compressed_data);
3002 * ... decompress data (compressed_data can be updated in-place) ...
3003 * v8::V8::SetDecompressedStartupData(compressed_data);
3004 * ... now V8 can be initialized
3005 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003006 *
3007 * A helper class StartupDataDecompressor is provided. It implements
3008 * the protocol of the interaction described above, and can be used in
3009 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003010 */
3011 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3012 static int GetCompressedStartupDataCount();
3013 static void GetCompressedStartupData(StartupData* compressed_data);
3014 static void SetDecompressedStartupData(StartupData* decompressed_data);
3015
3016 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003017 * Adds a message listener.
3018 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003019 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003020 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021 */
3022 static bool AddMessageListener(MessageCallback that,
3023 Handle<Value> data = Handle<Value>());
3024
3025 /**
3026 * Remove all message listeners from the specified callback function.
3027 */
3028 static void RemoveMessageListeners(MessageCallback that);
3029
3030 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003031 * Tells V8 to capture current stack trace when uncaught exception occurs
3032 * and report it to the message listeners. The option is off by default.
3033 */
3034 static void SetCaptureStackTraceForUncaughtExceptions(
3035 bool capture,
3036 int frame_limit = 10,
3037 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3038
3039 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003040 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003041 */
3042 static void SetFlagsFromString(const char* str, int length);
3043
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003044 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003045 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003046 */
3047 static void SetFlagsFromCommandLine(int* argc,
3048 char** argv,
3049 bool remove_flags);
3050
kasper.lund7276f142008-07-30 08:49:36 +00003051 /** Get the version string. */
3052 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053
3054 /**
3055 * Enables the host application to provide a mechanism for recording
3056 * statistics counters.
3057 */
3058 static void SetCounterFunction(CounterLookupCallback);
3059
3060 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003061 * Enables the host application to provide a mechanism for recording
3062 * histograms. The CreateHistogram function returns a
3063 * histogram which will later be passed to the AddHistogramSample
3064 * function.
3065 */
3066 static void SetCreateHistogramFunction(CreateHistogramCallback);
3067 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3068
3069 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003070 * Enables the computation of a sliding window of states. The sliding
3071 * window information is recorded in statistics counters.
3072 */
3073 static void EnableSlidingStateWindow();
3074
3075 /** Callback function for reporting failed access checks.*/
3076 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3077
3078 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003079 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003080 * garbage collection. Allocations are not allowed in the
3081 * callback function, you therefore cannot manipulate objects (set
3082 * or delete properties for example) since it is possible such
3083 * operations will result in the allocation of objects. It is possible
3084 * to specify the GCType filter for your callback. But it is not possible to
3085 * register the same callback function two times with different
3086 * GCType filters.
3087 */
3088 static void AddGCPrologueCallback(
3089 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3090
3091 /**
3092 * This function removes callback which was installed by
3093 * AddGCPrologueCallback function.
3094 */
3095 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3096
3097 /**
3098 * The function is deprecated. Please use AddGCPrologueCallback instead.
3099 * Enables the host application to receive a notification before a
3100 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003101 * callback function, you therefore cannot manipulate objects (set
3102 * or delete properties for example) since it is possible such
3103 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003104 */
3105 static void SetGlobalGCPrologueCallback(GCCallback);
3106
3107 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003108 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003109 * garbage collection. Allocations are not allowed in the
3110 * callback function, you therefore cannot manipulate objects (set
3111 * or delete properties for example) since it is possible such
3112 * operations will result in the allocation of objects. It is possible
3113 * to specify the GCType filter for your callback. But it is not possible to
3114 * register the same callback function two times with different
3115 * GCType filters.
3116 */
3117 static void AddGCEpilogueCallback(
3118 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3119
3120 /**
3121 * This function removes callback which was installed by
3122 * AddGCEpilogueCallback function.
3123 */
3124 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3125
3126 /**
3127 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3128 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003129 * major garbage collection. Allocations are not allowed in the
3130 * callback function, you therefore cannot manipulate objects (set
3131 * or delete properties for example) since it is possible such
3132 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003133 */
3134 static void SetGlobalGCEpilogueCallback(GCCallback);
3135
3136 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003137 * Enables the host application to provide a mechanism to be notified
3138 * and perform custom logging when V8 Allocates Executable Memory.
3139 */
3140 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3141 ObjectSpace space,
3142 AllocationAction action);
3143
3144 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003145 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003146 */
3147 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3148
3149 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003150 * Adds a callback to notify the host application when a script finished
3151 * running. If a script re-enters the runtime during executing, the
3152 * CallCompletedCallback is only invoked when the outer-most script
3153 * execution ends. Executing scripts inside the callback do not trigger
3154 * further callbacks.
3155 */
3156 static void AddCallCompletedCallback(CallCompletedCallback callback);
3157
3158 /**
3159 * Removes callback that was installed by AddCallCompletedCallback.
3160 */
3161 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3162
3163 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003164 * Allows the host application to group objects together. If one
3165 * object in the group is alive, all objects in the group are alive.
3166 * After each garbage collection, object groups are removed. It is
3167 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003168 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00003169 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003170 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003171 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003172 static void AddObjectGroup(Persistent<Value>* objects,
3173 size_t length,
3174 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003175
3176 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003177 * Allows the host application to declare implicit references between
3178 * the objects: if |parent| is alive, all |children| are alive too.
3179 * After each garbage collection, all implicit references
3180 * are removed. It is intended to be used in the before-garbage-collection
3181 * callback function.
3182 */
3183 static void AddImplicitReferences(Persistent<Object> parent,
3184 Persistent<Value>* children,
3185 size_t length);
3186
3187 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003188 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003189 * initialize from scratch. This function is called implicitly if
3190 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003191 */
3192 static bool Initialize();
3193
kasper.lund7276f142008-07-30 08:49:36 +00003194 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003195 * Allows the host application to provide a callback which can be used
3196 * as a source of entropy for random number generators.
3197 */
3198 static void SetEntropySource(EntropySource source);
3199
3200 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003201 * Allows the host application to provide a callback that allows v8 to
3202 * cooperate with a profiler that rewrites return addresses on stack.
3203 */
3204 static void SetReturnAddressLocationResolver(
3205 ReturnAddressLocationResolver return_address_resolver);
3206
3207 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003208 * Allows the host application to provide the address of a function that's
3209 * invoked on entry to every V8-generated function.
3210 * Note that \p entry_hook is invoked at the very start of each
3211 * generated function.
3212 *
3213 * \param entry_hook a function that will be invoked on entry to every
3214 * V8-generated function.
3215 * \returns true on success on supported platforms, false on failure.
3216 * \note Setting a new entry hook function when one is already active will
3217 * fail.
3218 */
3219 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3220
3221 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003222 * Adjusts the amount of registered external memory. Used to give
3223 * V8 an indication of the amount of externally allocated memory
3224 * that is kept alive by JavaScript objects. V8 uses this to decide
3225 * when to perform global garbage collections. Registering
3226 * externally allocated memory will trigger global garbage
3227 * collections more often than otherwise in an attempt to garbage
3228 * collect the JavaScript objects keeping the externally allocated
3229 * memory alive.
3230 *
3231 * \param change_in_bytes the change in externally allocated memory
3232 * that is kept alive by JavaScript objects.
3233 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003234 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003235 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3236 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003237
iposva@chromium.org245aa852009-02-10 00:49:54 +00003238 /**
3239 * Suspends recording of tick samples in the profiler.
3240 * When the V8 profiling mode is enabled (usually via command line
3241 * switches) this function suspends recording of tick samples.
3242 * Profiling ticks are discarded until ResumeProfiler() is called.
3243 *
3244 * See also the --prof and --prof_auto command line switches to
3245 * enable V8 profiling.
3246 */
3247 static void PauseProfiler();
3248
3249 /**
3250 * Resumes recording of tick samples in the profiler.
3251 * See also PauseProfiler().
3252 */
3253 static void ResumeProfiler();
3254
ager@chromium.org41826e72009-03-30 13:30:57 +00003255 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003256 * Return whether profiler is currently paused.
3257 */
3258 static bool IsProfilerPaused();
3259
3260 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003261 * Retrieve the V8 thread id of the calling thread.
3262 *
3263 * The thread id for a thread should only be retrieved after the V8
3264 * lock has been acquired with a Locker object with that thread.
3265 */
3266 static int GetCurrentThreadId();
3267
3268 /**
3269 * Forcefully terminate execution of a JavaScript thread. This can
3270 * be used to terminate long-running scripts.
3271 *
3272 * TerminateExecution should only be called when then V8 lock has
3273 * been acquired with a Locker object. Therefore, in order to be
3274 * able to terminate long-running threads, preemption must be
3275 * enabled to allow the user of TerminateExecution to acquire the
3276 * lock.
3277 *
3278 * The termination is achieved by throwing an exception that is
3279 * uncatchable by JavaScript exception handlers. Termination
3280 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003281 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003282 * exception handler that catches an exception should check if that
3283 * exception is a termination exception and immediately return if
3284 * that is the case. Returning immediately in that case will
3285 * continue the propagation of the termination exception if needed.
3286 *
3287 * The thread id passed to TerminateExecution must have been
3288 * obtained by calling GetCurrentThreadId on the thread in question.
3289 *
3290 * \param thread_id The thread id of the thread to terminate.
3291 */
3292 static void TerminateExecution(int thread_id);
3293
3294 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003295 * Forcefully terminate the current thread of JavaScript execution
3296 * in the given isolate. If no isolate is provided, the default
3297 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003298 *
3299 * This method can be used by any thread even if that thread has not
3300 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003301 *
3302 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003303 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003304 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003305
3306 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003307 * Is V8 terminating JavaScript execution.
3308 *
3309 * Returns true if JavaScript execution is currently terminating
3310 * because of a call to TerminateExecution. In that case there are
3311 * still JavaScript frames on the stack and the termination
3312 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003313 *
3314 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003315 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003316 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003317
3318 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003319 * Releases any resources used by v8 and stops any utility threads
3320 * that may be running. Note that disposing v8 is permanent, it
3321 * cannot be reinitialized.
3322 *
3323 * It should generally not be necessary to dispose v8 before exiting
3324 * a process, this should happen automatically. It is only necessary
3325 * to use if the process needs the resources taken up by v8.
3326 */
3327 static bool Dispose();
3328
ager@chromium.org3811b432009-10-28 14:53:37 +00003329 /**
3330 * Get statistics about the heap memory usage.
3331 */
3332 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003333
3334 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003335 * Iterates through all external resources referenced from current isolate
3336 * heap. This method is not expected to be used except for debugging purposes
3337 * and may be quite slow.
3338 */
3339 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3340
3341 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003342 * Optional notification that the embedder is idle.
3343 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003344 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003345 * Returns true if the embedder should stop calling IdleNotification
3346 * until real work has been done. This indicates that V8 has done
3347 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003348 *
3349 * The hint argument specifies the amount of work to be done in the function
3350 * on scale from 1 to 1000. There is no guarantee that the actual work will
3351 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003352 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003353 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003354
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003355 /**
3356 * Optional notification that the system is running low on memory.
3357 * V8 uses these notifications to attempt to free memory.
3358 */
3359 static void LowMemoryNotification();
3360
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003361 /**
3362 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003363 * these notifications to guide the GC heuristic. Returns the number
3364 * of context disposals - including this one - since the last time
3365 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003366 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003367 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003368
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003369 private:
3370 V8();
3371
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003372 static internal::Object** GlobalizeReference(internal::Object** handle);
3373 static void DisposeGlobal(internal::Object** global_handle);
3374 static void MakeWeak(internal::Object** global_handle,
3375 void* data,
3376 WeakReferenceCallback);
3377 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003378 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003379 static bool IsGlobalNearDeath(internal::Object** global_handle);
3380 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003381 static void SetWrapperClassId(internal::Object** global_handle,
3382 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003383
3384 template <class T> friend class Handle;
3385 template <class T> friend class Local;
3386 template <class T> friend class Persistent;
3387 friend class Context;
3388};
3389
3390
3391/**
3392 * An external exception handler.
3393 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003394class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003395 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003396 /**
3397 * Creates a new try/catch block and registers it with v8.
3398 */
3399 TryCatch();
3400
3401 /**
3402 * Unregisters and deletes this try/catch block.
3403 */
3404 ~TryCatch();
3405
3406 /**
3407 * Returns true if an exception has been caught by this try/catch block.
3408 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003409 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003410
3411 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003412 * For certain types of exceptions, it makes no sense to continue
3413 * execution.
3414 *
3415 * Currently, the only type of exception that can be caught by a
3416 * TryCatch handler and for which it does not make sense to continue
3417 * is termination exception. Such exceptions are thrown when the
3418 * TerminateExecution methods are called to terminate a long-running
3419 * script.
3420 *
3421 * If CanContinue returns false, the correct action is to perform
3422 * any C++ cleanup needed and then return.
3423 */
3424 bool CanContinue() const;
3425
3426 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003427 * Throws the exception caught by this TryCatch in a way that avoids
3428 * it being caught again by this same TryCatch. As with ThrowException
3429 * it is illegal to execute any JavaScript operations after calling
3430 * ReThrow; the caller must return immediately to where the exception
3431 * is caught.
3432 */
3433 Handle<Value> ReThrow();
3434
3435 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003436 * Returns the exception caught by this try/catch block. If no exception has
3437 * been caught an empty handle is returned.
3438 *
3439 * The returned handle is valid until this TryCatch block has been destroyed.
3440 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003441 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003442
3443 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003444 * Returns the .stack property of the thrown object. If no .stack
3445 * property is present an empty handle is returned.
3446 */
3447 Local<Value> StackTrace() const;
3448
3449 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003450 * Returns the message associated with this exception. If there is
3451 * no message associated an empty handle is returned.
3452 *
3453 * The returned handle is valid until this TryCatch block has been
3454 * destroyed.
3455 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003456 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003457
3458 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459 * Clears any exceptions that may have been caught by this try/catch block.
3460 * After this method has been called, HasCaught() will return false.
3461 *
3462 * It is not necessary to clear a try/catch block before using it again; if
3463 * another exception is thrown the previously caught exception will just be
3464 * overwritten. However, it is often a good idea since it makes it easier
3465 * to determine which operation threw a given exception.
3466 */
3467 void Reset();
3468
v8.team.kasperl727e9952008-09-02 14:56:44 +00003469 /**
3470 * Set verbosity of the external exception handler.
3471 *
3472 * By default, exceptions that are caught by an external exception
3473 * handler are not reported. Call SetVerbose with true on an
3474 * external exception handler to have exceptions caught by the
3475 * handler reported as if they were not caught.
3476 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003477 void SetVerbose(bool value);
3478
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003479 /**
3480 * Set whether or not this TryCatch should capture a Message object
3481 * which holds source information about where the exception
3482 * occurred. True by default.
3483 */
3484 void SetCaptureMessage(bool value);
3485
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003486 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003487 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003488 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003489 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003490 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003491 bool is_verbose_ : 1;
3492 bool can_continue_ : 1;
3493 bool capture_message_ : 1;
3494 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003495
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003496 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003497};
3498
3499
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003500// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003501
3502
3503/**
3504 * Ignore
3505 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003506class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003507 public:
3508 ExtensionConfiguration(int name_count, const char* names[])
3509 : name_count_(name_count), names_(names) { }
3510 private:
3511 friend class ImplementationUtilities;
3512 int name_count_;
3513 const char** names_;
3514};
3515
3516
3517/**
3518 * A sandboxed execution context with its own set of built-in objects
3519 * and functions.
3520 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003521class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003523 /**
3524 * Returns the global proxy object or global object itself for
3525 * detached contexts.
3526 *
3527 * Global proxy object is a thin wrapper whose prototype points to
3528 * actual context's global object with the properties like Object, etc.
3529 * This is done that way for security reasons (for more details see
3530 * https://wiki.mozilla.org/Gecko:SplitWindow).
3531 *
3532 * Please note that changes to global proxy object prototype most probably
3533 * would break VM---v8 expects only global object as a prototype of
3534 * global proxy object.
3535 *
3536 * If DetachGlobal() has been invoked, Global() would return actual global
3537 * object until global is reattached with ReattachGlobal().
3538 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003539 Local<Object> Global();
3540
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003541 /**
3542 * Detaches the global object from its context before
3543 * the global object can be reused to create a new context.
3544 */
3545 void DetachGlobal();
3546
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003547 /**
3548 * Reattaches a global object to a context. This can be used to
3549 * restore the connection between a global object and a context
3550 * after DetachGlobal has been called.
3551 *
3552 * \param global_object The global object to reattach to the
3553 * context. For this to work, the global object must be the global
3554 * object that was associated with this context before a call to
3555 * DetachGlobal.
3556 */
3557 void ReattachGlobal(Handle<Object> global_object);
3558
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003559 /** Creates a new context.
3560 *
3561 * Returns a persistent handle to the newly allocated context. This
3562 * persistent handle has to be disposed when the context is no
3563 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003564 *
3565 * \param extensions An optional extension configuration containing
3566 * the extensions to be installed in the newly created context.
3567 *
3568 * \param global_template An optional object template from which the
3569 * global object for the newly created context will be created.
3570 *
3571 * \param global_object An optional global object to be reused for
3572 * the newly created context. This global object must have been
3573 * created by a previous call to Context::New with the same global
3574 * template. The state of the global object will be completely reset
3575 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003576 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003577 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003578 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003579 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3580 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003581
kasper.lund44510672008-07-25 07:37:58 +00003582 /** Returns the last entered context. */
3583 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003584
kasper.lund44510672008-07-25 07:37:58 +00003585 /** Returns the context that is on the top of the stack. */
3586 static Local<Context> GetCurrent();
3587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003588 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003589 * Returns the context of the calling JavaScript code. That is the
3590 * context of the top-most JavaScript frame. If there are no
3591 * JavaScript frames an empty handle is returned.
3592 */
3593 static Local<Context> GetCalling();
3594
3595 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596 * Sets the security token for the context. To access an object in
3597 * another context, the security tokens must match.
3598 */
3599 void SetSecurityToken(Handle<Value> token);
3600
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003601 /** Restores the security token to the default value. */
3602 void UseDefaultSecurityToken();
3603
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003604 /** Returns the security token of this context.*/
3605 Handle<Value> GetSecurityToken();
3606
v8.team.kasperl727e9952008-09-02 14:56:44 +00003607 /**
3608 * Enter this context. After entering a context, all code compiled
3609 * and run is compiled and run in this context. If another context
3610 * is already entered, this old context is saved so it can be
3611 * restored when the new context is exited.
3612 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003613 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003614
3615 /**
3616 * Exit this context. Exiting the current context restores the
3617 * context that was in place when entering the current context.
3618 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003619 void Exit();
3620
v8.team.kasperl727e9952008-09-02 14:56:44 +00003621 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003622 bool HasOutOfMemoryException();
3623
v8.team.kasperl727e9952008-09-02 14:56:44 +00003624 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003625 static bool InContext();
3626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003627 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003628 * Associate an additional data object with the context. This is mainly used
3629 * with the debugger to provide additional information on the context through
3630 * the debugger API.
3631 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003632 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003633 Local<Value> GetData();
3634
3635 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003636 * Control whether code generation from strings is allowed. Calling
3637 * this method with false will disable 'eval' and the 'Function'
3638 * constructor for code running in this context. If 'eval' or the
3639 * 'Function' constructor are used an exception will be thrown.
3640 *
3641 * If code generation from strings is not allowed the
3642 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3643 * set before blocking the call to 'eval' or the 'Function'
3644 * constructor. If that callback returns true, the call will be
3645 * allowed, otherwise an exception will be thrown. If no callback is
3646 * set an exception will be thrown.
3647 */
3648 void AllowCodeGenerationFromStrings(bool allow);
3649
3650 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003651 * Returns true if code generation from strings is allowed for the context.
3652 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3653 */
3654 bool IsCodeGenerationFromStringsAllowed();
3655
3656 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003657 * Stack-allocated class which sets the execution context for all
3658 * operations executed within a local scope.
3659 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003660 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003661 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003662 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003663 context_->Enter();
3664 }
3665 inline ~Scope() { context_->Exit(); }
3666 private:
3667 Handle<Context> context_;
3668 };
3669
3670 private:
3671 friend class Value;
3672 friend class Script;
3673 friend class Object;
3674 friend class Function;
3675};
3676
3677
3678/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003679 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003680 * is allowed to use any given V8 isolate. See Isolate class
3681 * comments. The definition of 'using V8 isolate' includes
3682 * accessing handles or holding onto object pointers obtained
3683 * from V8 handles while in the particular V8 isolate. It is up
3684 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003685 * constraint is not violated. In addition to any other synchronization
3686 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3687 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003688 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003689 * v8::Locker is a scoped lock object. While it's
3690 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003691 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3692 * locked by at most one thread at any time. In other words, the scope of a
3693 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003694 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003695 * Sample usage:
3696* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003697 * ...
3698 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003699 * v8::Locker locker(isolate);
3700 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003701 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003702 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003703 * ...
3704 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003705 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003706 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003707 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003708 * by destroying the v8::Locker object as above or by constructing a
3709 * v8::Unlocker object:
3710 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003711 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003712 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003713 * isolate->Exit();
3714 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003715 * ...
3716 * // Code not using V8 goes here while V8 can run in another thread.
3717 * ...
3718 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003719 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003720 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003721 *
3722 * The Unlocker object is intended for use in a long-running callback
3723 * from V8, where you want to release the V8 lock for other threads to
3724 * use.
3725 *
3726 * The v8::Locker is a recursive lock. That is, you can lock more than
3727 * once in a given thread. This can be useful if you have code that can
3728 * be called either from code that holds the lock or from code that does
3729 * not. The Unlocker is not recursive so you can not have several
3730 * Unlockers on the stack at once, and you can not use an Unlocker in a
3731 * thread that is not inside a Locker's scope.
3732 *
3733 * An unlocker will unlock several lockers if it has to and reinstate
3734 * the correct depth of locking on its destruction. eg.:
3735 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003736 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003737 * // V8 not locked.
3738 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003739 * v8::Locker locker(isolate);
3740 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003741 * // V8 locked.
3742 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003743 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003744 * // V8 still locked (2 levels).
3745 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003746 * isolate->Exit();
3747 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003748 * // V8 not locked.
3749 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003750 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003751 * // V8 locked again (2 levels).
3752 * }
3753 * // V8 still locked (1 level).
3754 * }
3755 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003756 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003757 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003758 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003759 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003760class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003761 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003762 /**
3763 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3764 */
3765 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003767 private:
3768 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003769};
3770
3771
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003772class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003773 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003774 /**
3775 * Initialize Locker for a given Isolate. NULL means default isolate.
3776 */
3777 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003779
3780 /**
3781 * Start preemption.
3782 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003783 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003784 * that will switch between multiple threads that are in contention
3785 * for the V8 lock.
3786 */
3787 static void StartPreemption(int every_n_ms);
3788
3789 /**
3790 * Stop preemption.
3791 */
3792 static void StopPreemption();
3793
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003794 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003795 * Returns whether or not the locker for a given isolate, or default isolate
3796 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003797 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003798 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003799
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003800 /**
3801 * Returns whether v8::Locker is being used by this V8 instance.
3802 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003803 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003804
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003805 private:
3806 bool has_lock_;
3807 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003808 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003809
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003810 static bool active_;
3811
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003812 // Disallow copying and assigning.
3813 Locker(const Locker&);
3814 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003815};
3816
3817
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003818/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003819 * A struct for exporting HeapStats data from V8, using "push" model.
3820 */
3821struct HeapStatsUpdate;
3822
3823
3824/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003825 * An interface for exporting data from V8, using "push" model.
3826 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003827class V8EXPORT OutputStream { // NOLINT
3828 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003829 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003830 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003831 };
3832 enum WriteResult {
3833 kContinue = 0,
3834 kAbort = 1
3835 };
3836 virtual ~OutputStream() {}
3837 /** Notify about the end of stream. */
3838 virtual void EndOfStream() = 0;
3839 /** Get preferred output chunk size. Called only once. */
3840 virtual int GetChunkSize() { return 1024; }
3841 /** Get preferred output encoding. Called only once. */
3842 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3843 /**
3844 * Writes the next chunk of snapshot data into the stream. Writing
3845 * can be stopped by returning kAbort as function result. EndOfStream
3846 * will not be called in case writing was aborted.
3847 */
3848 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003849 /**
3850 * Writes the next chunk of heap stats data into the stream. Writing
3851 * can be stopped by returning kAbort as function result. EndOfStream
3852 * will not be called in case writing was aborted.
3853 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003854 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00003855 return kAbort;
3856 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003857};
3858
3859
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003860/**
3861 * An interface for reporting progress and controlling long-running
3862 * activities.
3863 */
3864class V8EXPORT ActivityControl { // NOLINT
3865 public:
3866 enum ControlOption {
3867 kContinue = 0,
3868 kAbort = 1
3869 };
3870 virtual ~ActivityControl() {}
3871 /**
3872 * Notify about current progress. The activity can be stopped by
3873 * returning kAbort as the callback result.
3874 */
3875 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3876};
3877
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003878
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003879// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003880
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003881
3882namespace internal {
3883
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003884const int kApiPointerSize = sizeof(void*); // NOLINT
3885const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003886
3887// Tag information for HeapObject.
3888const int kHeapObjectTag = 1;
3889const int kHeapObjectTagSize = 2;
3890const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3891
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003892// Tag information for Smi.
3893const int kSmiTag = 0;
3894const int kSmiTagSize = 1;
3895const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3896
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003897template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003898
3899// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003900template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003901 static const int kSmiShiftSize = 0;
3902 static const int kSmiValueSize = 31;
3903 static inline int SmiToInt(internal::Object* value) {
3904 int shift_bits = kSmiTagSize + kSmiShiftSize;
3905 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3906 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3907 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003908
3909 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3910 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003911 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003912 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003913};
3914
3915// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003916template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003917 static const int kSmiShiftSize = 31;
3918 static const int kSmiValueSize = 32;
3919 static inline int SmiToInt(internal::Object* value) {
3920 int shift_bits = kSmiTagSize + kSmiShiftSize;
3921 // Shift down and throw away top 32 bits.
3922 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3923 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003924
3925 // To maximize the range of pointers that can be encoded
3926 // in the available 32 bits, we require them to be 8 bytes aligned.
3927 // This gives 2 ^ (32 + 3) = 32G address space covered.
3928 // It might be not enough to cover stack allocated objects on some platforms.
3929 static const int kPointerAlignment = 3;
3930
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003931 static const uintptr_t kEncodablePointerMask =
3932 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003933
3934 static const int kPointerToSmiShift =
3935 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003936};
3937
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003938typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3939const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3940const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003941const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003942 PlatformSmiTagging::kEncodablePointerMask;
3943const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003944
3945/**
3946 * This class exports constants and functionality from within v8 that
3947 * is necessary to implement inline functions in the v8 api. Don't
3948 * depend on functions and constants defined here.
3949 */
3950class Internals {
3951 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003952 // These values match non-compiler-dependent values defined within
3953 // the implementation of v8.
3954 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003955 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003956 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003957
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003958 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003959 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003960 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003961 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003962 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003963
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003964 static const int kIsolateStateOffset = 0;
3965 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
3966 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
3967 static const int kUndefinedValueRootIndex = 5;
3968 static const int kNullValueRootIndex = 7;
3969 static const int kTrueValueRootIndex = 8;
3970 static const int kFalseValueRootIndex = 9;
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00003971 static const int kEmptySymbolRootIndex = 112;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003972
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003973 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003974 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003975 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003976 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003977
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003978 static const int kUndefinedOddballKind = 5;
3979 static const int kNullOddballKind = 3;
3980
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003981 static inline bool HasHeapObjectTag(internal::Object* value) {
3982 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3983 kHeapObjectTag);
3984 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003985
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003986 static inline bool HasSmiTag(internal::Object* value) {
3987 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3988 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003989
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003990 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003991 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003992 }
3993
3994 static inline int GetInstanceType(internal::Object* obj) {
3995 typedef internal::Object O;
3996 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3997 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3998 }
3999
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004000 static inline int GetOddballKind(internal::Object* obj) {
4001 typedef internal::Object O;
4002 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4003 }
4004
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004005 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004006 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004007 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
4008 }
4009
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004010 static inline void* GetExternalPointer(internal::Object* obj) {
4011 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004012 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004013 } else if (GetInstanceType(obj) == kForeignType) {
4014 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004015 } else {
4016 return NULL;
4017 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004018 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004019
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004020 static inline bool IsExternalTwoByteString(int instance_type) {
4021 int representation = (instance_type & kFullStringRepresentationMask);
4022 return representation == kExternalTwoByteRepresentationTag;
4023 }
4024
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004025 static inline bool IsInitialized(v8::Isolate* isolate) {
4026 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4027 return *reinterpret_cast<int*>(addr) == 1;
4028 }
4029
4030 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
4031 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4032 kIsolateEmbedderDataOffset;
4033 *reinterpret_cast<void**>(addr) = data;
4034 }
4035
4036 static inline void* GetEmbedderData(v8::Isolate* isolate) {
4037 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4038 kIsolateEmbedderDataOffset;
4039 return *reinterpret_cast<void**>(addr);
4040 }
4041
4042 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
4043 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4044 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4045 }
4046
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004047 template <typename T>
4048 static inline T ReadField(Object* ptr, int offset) {
4049 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4050 return *reinterpret_cast<T*>(addr);
4051 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004052
4053 static inline bool CanCastToHeapObject(void* o) { return false; }
4054 static inline bool CanCastToHeapObject(Context* o) { return true; }
4055 static inline bool CanCastToHeapObject(String* o) { return true; }
4056 static inline bool CanCastToHeapObject(Object* o) { return true; }
4057 static inline bool CanCastToHeapObject(Message* o) { return true; }
4058 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
4059 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004060};
4061
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004062} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004063
4064
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004065template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004066Local<T>::Local() : Handle<T>() { }
4067
4068
4069template <class T>
4070Local<T> Local<T>::New(Handle<T> that) {
4071 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004072 T* that_ptr = *that;
4073 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4074 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4075 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4076 reinterpret_cast<internal::HeapObject*>(*p))));
4077 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004078 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4079}
4080
4081
4082template <class T>
4083Persistent<T> Persistent<T>::New(Handle<T> that) {
4084 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004085 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004086 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4087}
4088
4089
4090template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004091bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004092 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004093 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004094}
4095
4096
4097template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004098bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004099 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004100 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004101}
4102
4103
4104template <class T>
4105void Persistent<T>::Dispose() {
4106 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004107 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108}
4109
4110
4111template <class T>
4112Persistent<T>::Persistent() : Handle<T>() { }
4113
4114template <class T>
4115void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004116 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4117 parameters,
4118 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004119}
4120
4121template <class T>
4122void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004123 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004124}
4125
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004126template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004127void Persistent<T>::MarkIndependent() {
4128 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4129}
4130
4131template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004132void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4133 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4134}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004135
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004136Arguments::Arguments(internal::Object** implicit_args,
4137 internal::Object** values, int length,
4138 bool is_construct_call)
4139 : implicit_args_(implicit_args),
4140 values_(values),
4141 length_(length),
4142 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004143
4144
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004145Local<Value> Arguments::operator[](int i) const {
4146 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4147 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4148}
4149
4150
4151Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004152 return Local<Function>(reinterpret_cast<Function*>(
4153 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004154}
4155
4156
4157Local<Object> Arguments::This() const {
4158 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4159}
4160
4161
4162Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004163 return Local<Object>(reinterpret_cast<Object*>(
4164 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004165}
4166
4167
4168Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004169 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004170}
4171
4172
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004173Isolate* Arguments::GetIsolate() const {
4174 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4175}
4176
4177
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004178bool Arguments::IsConstructCall() const {
4179 return is_construct_call_;
4180}
4181
4182
4183int Arguments::Length() const {
4184 return length_;
4185}
4186
4187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004188template <class T>
4189Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004190 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4191 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004192 return Local<T>(reinterpret_cast<T*>(after));
4193}
4194
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004195Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004196 return resource_name_;
4197}
4198
4199
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004200Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004201 return resource_line_offset_;
4202}
4203
4204
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004205Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004206 return resource_column_offset_;
4207}
4208
4209
4210Handle<Boolean> Boolean::New(bool value) {
4211 return value ? True() : False();
4212}
4213
4214
4215void Template::Set(const char* name, v8::Handle<Data> value) {
4216 Set(v8::String::New(name), value);
4217}
4218
4219
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004220Local<Value> Object::GetInternalField(int index) {
4221#ifndef V8_ENABLE_CHECKS
4222 Local<Value> quick_result = UncheckedGetInternalField(index);
4223 if (!quick_result.IsEmpty()) return quick_result;
4224#endif
4225 return CheckedGetInternalField(index);
4226}
4227
4228
4229Local<Value> Object::UncheckedGetInternalField(int index) {
4230 typedef internal::Object O;
4231 typedef internal::Internals I;
4232 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004233 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004234 // If the object is a plain JSObject, which is the common case,
4235 // we know where to find the internal fields and can return the
4236 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004237 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004238 O* value = I::ReadField<O*>(obj, offset);
4239 O** result = HandleScope::CreateHandle(value);
4240 return Local<Value>(reinterpret_cast<Value*>(result));
4241 } else {
4242 return Local<Value>();
4243 }
4244}
4245
4246
4247void* External::Unwrap(Handle<v8::Value> obj) {
4248#ifdef V8_ENABLE_CHECKS
4249 return FullUnwrap(obj);
4250#else
4251 return QuickUnwrap(obj);
4252#endif
4253}
4254
4255
4256void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4257 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004258 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004259 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004260}
4261
4262
4263void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004264 typedef internal::Object O;
4265 typedef internal::Internals I;
4266
4267 O* obj = *reinterpret_cast<O**>(this);
4268
4269 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4270 // If the object is a plain JSObject, which is the common case,
4271 // we know where to find the internal fields and can return the
4272 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004273 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004274 O* value = I::ReadField<O*>(obj, offset);
4275 return I::GetExternalPointer(value);
4276 }
4277
4278 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004279}
4280
4281
4282String* String::Cast(v8::Value* value) {
4283#ifdef V8_ENABLE_CHECKS
4284 CheckCast(value);
4285#endif
4286 return static_cast<String*>(value);
4287}
4288
4289
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004290Local<String> String::Empty(Isolate* isolate) {
4291 typedef internal::Object* S;
4292 typedef internal::Internals I;
4293 if (!I::IsInitialized(isolate)) return Empty();
4294 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4295 return Local<String>(reinterpret_cast<String*>(slot));
4296}
4297
4298
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004299String::ExternalStringResource* String::GetExternalStringResource() const {
4300 typedef internal::Object O;
4301 typedef internal::Internals I;
4302 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004303 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004304 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004305 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4306 result = reinterpret_cast<String::ExternalStringResource*>(value);
4307 } else {
4308 result = NULL;
4309 }
4310#ifdef V8_ENABLE_CHECKS
4311 VerifyExternalStringResource(result);
4312#endif
4313 return result;
4314}
4315
4316
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004317bool Value::IsUndefined() const {
4318#ifdef V8_ENABLE_CHECKS
4319 return FullIsUndefined();
4320#else
4321 return QuickIsUndefined();
4322#endif
4323}
4324
4325bool Value::QuickIsUndefined() const {
4326 typedef internal::Object O;
4327 typedef internal::Internals I;
4328 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4329 if (!I::HasHeapObjectTag(obj)) return false;
4330 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4331 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4332}
4333
4334
4335bool Value::IsNull() const {
4336#ifdef V8_ENABLE_CHECKS
4337 return FullIsNull();
4338#else
4339 return QuickIsNull();
4340#endif
4341}
4342
4343bool Value::QuickIsNull() const {
4344 typedef internal::Object O;
4345 typedef internal::Internals I;
4346 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4347 if (!I::HasHeapObjectTag(obj)) return false;
4348 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4349 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4350}
4351
4352
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004353bool Value::IsString() const {
4354#ifdef V8_ENABLE_CHECKS
4355 return FullIsString();
4356#else
4357 return QuickIsString();
4358#endif
4359}
4360
4361bool Value::QuickIsString() const {
4362 typedef internal::Object O;
4363 typedef internal::Internals I;
4364 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4365 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004366 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004367}
4368
4369
4370Number* Number::Cast(v8::Value* value) {
4371#ifdef V8_ENABLE_CHECKS
4372 CheckCast(value);
4373#endif
4374 return static_cast<Number*>(value);
4375}
4376
4377
4378Integer* Integer::Cast(v8::Value* value) {
4379#ifdef V8_ENABLE_CHECKS
4380 CheckCast(value);
4381#endif
4382 return static_cast<Integer*>(value);
4383}
4384
4385
4386Date* Date::Cast(v8::Value* value) {
4387#ifdef V8_ENABLE_CHECKS
4388 CheckCast(value);
4389#endif
4390 return static_cast<Date*>(value);
4391}
4392
4393
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004394StringObject* StringObject::Cast(v8::Value* value) {
4395#ifdef V8_ENABLE_CHECKS
4396 CheckCast(value);
4397#endif
4398 return static_cast<StringObject*>(value);
4399}
4400
4401
4402NumberObject* NumberObject::Cast(v8::Value* value) {
4403#ifdef V8_ENABLE_CHECKS
4404 CheckCast(value);
4405#endif
4406 return static_cast<NumberObject*>(value);
4407}
4408
4409
4410BooleanObject* BooleanObject::Cast(v8::Value* value) {
4411#ifdef V8_ENABLE_CHECKS
4412 CheckCast(value);
4413#endif
4414 return static_cast<BooleanObject*>(value);
4415}
4416
4417
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004418RegExp* RegExp::Cast(v8::Value* value) {
4419#ifdef V8_ENABLE_CHECKS
4420 CheckCast(value);
4421#endif
4422 return static_cast<RegExp*>(value);
4423}
4424
4425
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004426Object* Object::Cast(v8::Value* value) {
4427#ifdef V8_ENABLE_CHECKS
4428 CheckCast(value);
4429#endif
4430 return static_cast<Object*>(value);
4431}
4432
4433
4434Array* Array::Cast(v8::Value* value) {
4435#ifdef V8_ENABLE_CHECKS
4436 CheckCast(value);
4437#endif
4438 return static_cast<Array*>(value);
4439}
4440
4441
4442Function* Function::Cast(v8::Value* value) {
4443#ifdef V8_ENABLE_CHECKS
4444 CheckCast(value);
4445#endif
4446 return static_cast<Function*>(value);
4447}
4448
4449
4450External* External::Cast(v8::Value* value) {
4451#ifdef V8_ENABLE_CHECKS
4452 CheckCast(value);
4453#endif
4454 return static_cast<External*>(value);
4455}
4456
4457
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004458Isolate* AccessorInfo::GetIsolate() const {
4459 return *reinterpret_cast<Isolate**>(&args_[-3]);
4460}
4461
4462
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004463Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004464 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004465}
4466
4467
4468Local<Object> AccessorInfo::This() const {
4469 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4470}
4471
4472
4473Local<Object> AccessorInfo::Holder() const {
4474 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4475}
4476
4477
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004478Handle<Primitive> Undefined(Isolate* isolate) {
4479 typedef internal::Object* S;
4480 typedef internal::Internals I;
4481 if (!I::IsInitialized(isolate)) return Undefined();
4482 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4483 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4484}
4485
4486
4487Handle<Primitive> Null(Isolate* isolate) {
4488 typedef internal::Object* S;
4489 typedef internal::Internals I;
4490 if (!I::IsInitialized(isolate)) return Null();
4491 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4492 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4493}
4494
4495
4496Handle<Boolean> True(Isolate* isolate) {
4497 typedef internal::Object* S;
4498 typedef internal::Internals I;
4499 if (!I::IsInitialized(isolate)) return True();
4500 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4501 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4502}
4503
4504
4505Handle<Boolean> False(Isolate* isolate) {
4506 typedef internal::Object* S;
4507 typedef internal::Internals I;
4508 if (!I::IsInitialized(isolate)) return False();
4509 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4510 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4511}
4512
4513
4514void Isolate::SetData(void* data) {
4515 typedef internal::Internals I;
4516 I::SetEmbedderData(this, data);
4517}
4518
4519
4520void* Isolate::GetData() {
4521 typedef internal::Internals I;
4522 return I::GetEmbedderData(this);
4523}
4524
4525
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004526/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004527 * \example shell.cc
4528 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004529 * command-line and executes them.
4530 */
4531
4532
4533/**
4534 * \example process.cc
4535 */
4536
4537
4538} // namespace v8
4539
4540
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004541#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004542#undef TYPE_CHECK
4543
4544
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004545#endif // V8_H_