blob: 457633a6e80f4f15226ea92ce519101c49c37987 [file] [log] [blame]
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001// Copyright 2011 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
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000065// Setup for Linux shared library export. There is no need to distinguish
66// between building or using the V8 shared library, but we should not
67// export symbols when we are building a static library.
68#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000070#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000071#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000072#endif // defined(__GNUC__) && (__GNUC__ >= 4)
73
74#endif // _WIN32
75
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000077 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 */
79namespace v8 {
80
81class Context;
82class String;
83class Value;
84class Utils;
85class Number;
86class Object;
87class Array;
88class Int32;
89class Uint32;
90class External;
91class Primitive;
92class Boolean;
93class Integer;
94class Function;
95class Date;
96class ImplementationUtilities;
97class Signature;
98template <class T> class Handle;
99template <class T> class Local;
100template <class T> class Persistent;
101class FunctionTemplate;
102class ObjectTemplate;
103class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000104class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000105class StackTrace;
106class StackFrame;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000108namespace internal {
109
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000110class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000111class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000112class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000113class HeapObject;
114class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000115}
116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000118// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119
120
121/**
122 * A weak reference callback function.
123 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000124 * This callback should either explicitly invoke Dispose on |object| if
125 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
126 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127 * \param object the weak global object to be reclaimed by the garbage collector
128 * \param parameter the value passed in when making the weak global object
129 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000130typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 void* parameter);
132
133
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000134// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000136#define TYPE_CHECK(T, S) \
137 while (false) { \
138 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 }
140
141/**
142 * An object reference managed by the v8 garbage collector.
143 *
144 * All objects returned from v8 have to be tracked by the garbage
145 * collector so that it knows that the objects are still alive. Also,
146 * because the garbage collector may move objects, it is unsafe to
147 * point directly to an object. Instead, all objects are stored in
148 * handles which are known by the garbage collector and updated
149 * whenever an object moves. Handles should always be passed by value
150 * (except in cases like out-parameters) and they should never be
151 * allocated on the heap.
152 *
153 * There are two types of handles: local and persistent handles.
154 * Local handles are light-weight and transient and typically used in
155 * local operations. They are managed by HandleScopes. Persistent
156 * handles can be used when storing objects across several independent
157 * operations and have to be explicitly deallocated when they're no
158 * longer used.
159 *
160 * It is safe to extract the object stored in the handle by
161 * dereferencing the handle (for instance, to extract the Object* from
162 * an Handle<Object>); the value will still be governed by a handle
163 * behind the scenes and the same rules apply to these values as to
164 * their handles.
165 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000166template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 public:
168
169 /**
170 * Creates an empty handle.
171 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000172 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173
174 /**
175 * Creates a new handle for the specified value.
176 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000177 inline explicit Handle(T* val) : val_(val) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178
179 /**
180 * Creates a handle for the contents of the specified handle. This
181 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000182 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 * incompatible handles, for instance from a Handle<String> to a
184 * Handle<Number> it will cause a compiletime error. Assigning
185 * between compatible handles, for instance assigning a
186 * Handle<String> to a variable declared as Handle<Value>, is legal
187 * because String is a subclass of Value.
188 */
189 template <class S> inline Handle(Handle<S> that)
190 : val_(reinterpret_cast<T*>(*that)) {
191 /**
192 * This check fails when trying to convert between incompatible
193 * handles. For example, converting from a Handle<String> to a
194 * Handle<Number>.
195 */
196 TYPE_CHECK(T, S);
197 }
198
199 /**
200 * Returns true if the handle is empty.
201 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000202 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000204 inline T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000206 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207
208 /**
209 * Sets the handle to be empty. IsEmpty() will then return true.
210 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000211 inline void Clear() { this->val_ = 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212
213 /**
214 * Checks whether two handles are the same.
215 * Returns true if both are empty, or if the objects
216 * to which they refer are identical.
217 * The handles' references are not checked.
218 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000219 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000220 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
221 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 if (a == 0) return b == 0;
223 if (b == 0) return false;
224 return *a == *b;
225 }
226
227 /**
228 * Checks whether two handles are different.
229 * Returns true if only one of the handles is empty, or if
230 * the objects to which they refer are different.
231 * The handles' references are not checked.
232 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000233 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234 return !operator==(that);
235 }
236
237 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000238#ifdef V8_ENABLE_CHECKS
239 // If we're going to perform the type check then we have to check
240 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000242#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 return Handle<T>(T::Cast(*that));
244 }
245
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000246 template <class S> inline Handle<S> As() {
247 return Handle<S>::Cast(*this);
248 }
249
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 private:
251 T* val_;
252};
253
254
255/**
256 * A light-weight stack-allocated object handle. All operations
257 * that return objects from within v8 return them in local handles. They
258 * are created within HandleScopes, and all local handles allocated within a
259 * handle scope are destroyed when the handle scope is destroyed. Hence it
260 * is not necessary to explicitly deallocate local handles.
261 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000262template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000264 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 template <class S> inline Local(Local<S> that)
266 : Handle<T>(reinterpret_cast<T*>(*that)) {
267 /**
268 * This check fails when trying to convert between incompatible
269 * handles. For example, converting from a Handle<String> to a
270 * Handle<Number>.
271 */
272 TYPE_CHECK(T, S);
273 }
274 template <class S> inline Local(S* that) : Handle<T>(that) { }
275 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000276#ifdef V8_ENABLE_CHECKS
277 // If we're going to perform the type check then we have to check
278 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000280#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 return Local<T>(T::Cast(*that));
282 }
283
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000284 template <class S> inline Local<S> As() {
285 return Local<S>::Cast(*this);
286 }
287
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 /** Create a local handle for the content of another handle.
289 * The referee is kept alive by the local handle even when
290 * the original handle is destroyed/disposed.
291 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000292 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293};
294
295
296/**
297 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000298 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 * allocated, a Persistent handle remains valid until it is explicitly
300 * disposed.
301 *
302 * A persistent handle contains a reference to a storage cell within
303 * the v8 engine which holds an object value and which is updated by
304 * the garbage collector whenever the object is moved. A new storage
305 * cell can be created using Persistent::New and existing handles can
306 * be disposed using Persistent::Dispose. Since persistent handles
307 * are passed by value you may have many persistent handle objects
308 * that point to the same storage cell. For instance, if you pass a
309 * persistent handle as an argument to a function you will not get two
310 * different storage cells but rather two references to the same
311 * storage cell.
312 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000313template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 public:
315
316 /**
317 * Creates an empty persistent handle that doesn't point to any
318 * storage cell.
319 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000320 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321
322 /**
323 * Creates a persistent handle for the same storage cell as the
324 * specified handle. This constructor allows you to pass persistent
325 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000326 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000328 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 * between compatible persistent handles, for instance assigning a
330 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000331 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 */
333 template <class S> inline Persistent(Persistent<S> that)
334 : Handle<T>(reinterpret_cast<T*>(*that)) {
335 /**
336 * This check fails when trying to convert between incompatible
337 * handles. For example, converting from a Handle<String> to a
338 * Handle<Number>.
339 */
340 TYPE_CHECK(T, S);
341 }
342
343 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
344
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000345 /**
346 * "Casts" a plain handle which is known to be a persistent handle
347 * to a persistent handle.
348 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 template <class S> explicit inline Persistent(Handle<S> that)
350 : Handle<T>(*that) { }
351
352 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000353#ifdef V8_ENABLE_CHECKS
354 // If we're going to perform the type check then we have to check
355 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000357#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 return Persistent<T>(T::Cast(*that));
359 }
360
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000361 template <class S> inline Persistent<S> As() {
362 return Persistent<S>::Cast(*this);
363 }
364
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000366 * Creates a new persistent handle for an existing local or
367 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000369 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370
371 /**
372 * Releases the storage cell referenced by this persistent handle.
373 * Does not remove the reference to the cell from any handles.
374 * This handle's reference, and any any other references to the storage
375 * cell remain and IsEmpty will still return false.
376 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000377 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378
379 /**
380 * Make the reference to this object weak. When only weak handles
381 * refer to the object, the garbage collector will perform a
382 * callback to the given V8::WeakReferenceCallback function, passing
383 * it the object reference and the given parameters.
384 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000385 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386
387 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000388 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389
390 /**
391 *Checks if the handle holds the only reference to an object.
392 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000393 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394
395 /**
396 * Returns true if the handle's reference is weak.
397 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000398 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000400 /**
401 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
402 * interface description in v8-profiler.h for details.
403 */
404 inline void SetWrapperClassId(uint16_t class_id);
405
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 private:
407 friend class ImplementationUtilities;
408 friend class ObjectTemplate;
409};
410
411
v8.team.kasperl727e9952008-09-02 14:56:44 +0000412 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 * A stack-allocated class that governs a number of local handles.
414 * After a handle scope has been created, all local handles will be
415 * allocated within that handle scope until either the handle scope is
416 * deleted or another handle scope is created. If there is already a
417 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000418 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 * new handles will again be allocated in the original handle scope.
420 *
421 * After the handle scope of a local handle has been deleted the
422 * garbage collector will no longer track the object stored in the
423 * handle and may deallocate it. The behavior of accessing a handle
424 * for which the handle scope has been deleted is undefined.
425 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000426class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000428 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000430 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431
432 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 * Closes the handle scope and returns the value as a handle in the
434 * previous scope, which is the new current scope after the call.
435 */
436 template <class T> Local<T> Close(Handle<T> value);
437
438 /**
439 * Counts the number of allocated handles.
440 */
441 static int NumberOfHandles();
442
443 /**
444 * Creates a new handle with the given value.
445 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000446 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000447 // Faster version, uses HeapObject to obtain the current Isolate.
448 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449
450 private:
451 // Make it impossible to create heap-allocated or illegal handle
452 // scopes by disallowing certain operations.
453 HandleScope(const HandleScope&);
454 void operator=(const HandleScope&);
455 void* operator new(size_t size);
456 void operator delete(void*, size_t);
457
ager@chromium.org3811b432009-10-28 14:53:37 +0000458 // This Data class is accessible internally as HandleScopeData through a
459 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000460 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000462 internal::Object** next;
463 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000464 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000467 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 }
469 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000470
lrn@chromium.org303ada72010-10-27 09:33:13 +0000471 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000473 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000474 internal::Object** prev_next_;
475 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000477 // Allow for the active closing of HandleScopes which allows to pass a handle
478 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000480 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482 friend class ImplementationUtilities;
483};
484
485
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000486// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487
488
489/**
490 * The superclass of values and API object templates.
491 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000492class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493 private:
494 Data();
495};
496
497
498/**
499 * Pre-compilation data that can be associated with a script. This
500 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000501 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000502 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000504class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 public:
506 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000507
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000508 /**
509 * Pre-compiles the specified script (context-independent).
510 *
511 * \param input Pointer to UTF-8 script source code.
512 * \param length Length of UTF-8 script source code.
513 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000516 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000517 * Pre-compiles the specified script (context-independent).
518 *
519 * NOTE: Pre-compilation using this method cannot happen on another thread
520 * without using Lockers.
521 *
522 * \param source Script source code.
523 */
524 static ScriptData* PreCompile(Handle<String> source);
525
526 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000527 * Load previous pre-compilation data.
528 *
529 * \param data Pointer to data returned by a call to Data() of a previous
530 * ScriptData. Ownership is not transferred.
531 * \param length Length of data.
532 */
533 static ScriptData* New(const char* data, int length);
534
535 /**
536 * Returns the length of Data().
537 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000539
540 /**
541 * Returns a serialized representation of this ScriptData that can later be
542 * passed to New(). NOTE: Serialized data is platform-dependent.
543 */
544 virtual const char* Data() = 0;
545
546 /**
547 * Returns true if the source code could not be parsed.
548 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000549 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550};
551
552
553/**
554 * The origin, within a file, of a script.
555 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000556class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000558 inline ScriptOrigin(
559 Handle<Value> resource_name,
560 Handle<Integer> resource_line_offset = Handle<Integer>(),
561 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 : resource_name_(resource_name),
563 resource_line_offset_(resource_line_offset),
564 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000565 inline Handle<Value> ResourceName() const;
566 inline Handle<Integer> ResourceLineOffset() const;
567 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000569 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 Handle<Integer> resource_line_offset_;
571 Handle<Integer> resource_column_offset_;
572};
573
574
575/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000576 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000578class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 public:
580
581 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000582 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000583 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000584 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000585 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000586 * when New() returns
587 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
588 * using pre_data speeds compilation if it's done multiple times.
589 * Owned by caller, no references are kept when New() returns.
590 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000591 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000592 * available to compile event handlers.
593 * \return Compiled script object (context independent; when run it
594 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000596 static Local<Script> New(Handle<String> source,
597 ScriptOrigin* origin = NULL,
598 ScriptData* pre_data = NULL,
599 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600
mads.s.agercbaa0602008-08-14 13:41:48 +0000601 /**
602 * Compiles the specified script using the specified file name
603 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000604 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000605 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000606 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000607 * as the script's origin.
608 * \return Compiled script object (context independent; when run it
609 * will use the currently entered context).
610 */
611 static Local<Script> New(Handle<String> source,
612 Handle<Value> file_name);
613
614 /**
615 * Compiles the specified script (bound to current context).
616 *
617 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000618 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000619 * when Compile() returns
620 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
621 * using pre_data speeds compilation if it's done multiple times.
622 * Owned by caller, no references are kept when Compile() returns.
623 * \param script_data Arbitrary data associated with script. Using
624 * this has same effect as calling SetData(), but makes data available
625 * earlier (i.e. to compile event handlers).
626 * \return Compiled script object, bound to the context that was active
627 * when this function was called. When run it will always use this
628 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000629 */
630 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000631 ScriptOrigin* origin = NULL,
632 ScriptData* pre_data = NULL,
633 Handle<String> script_data = Handle<String>());
634
635 /**
636 * Compiles the specified script using the specified file name
637 * object (typically a string) as the script's origin.
638 *
639 * \param source Script source code.
640 * \param file_name File name to use as script's origin
641 * \param script_data Arbitrary data associated with script. Using
642 * this has same effect as calling SetData(), but makes data available
643 * earlier (i.e. to compile event handlers).
644 * \return Compiled script object, bound to the context that was active
645 * when this function was called. When run it will always use this
646 * context.
647 */
648 static Local<Script> Compile(Handle<String> source,
649 Handle<Value> file_name,
650 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000651
v8.team.kasperl727e9952008-09-02 14:56:44 +0000652 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000653 * Runs the script returning the resulting value. If the script is
654 * context independent (created using ::New) it will be run in the
655 * currently entered context. If it is context specific (created
656 * using ::Compile) it will be run in the context in which it was
657 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000658 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000660
661 /**
662 * Returns the script id value.
663 */
664 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000665
666 /**
667 * Associate an additional data object with the script. This is mainly used
668 * with the debugger as this data object is only available through the
669 * debugger API.
670 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000671 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672};
673
674
675/**
676 * An error message.
677 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000678class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000680 Local<String> Get() const;
681 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000683 /**
684 * Returns the resource name for the script from where the function causing
685 * the error originates.
686 */
ager@chromium.org32912102009-01-16 10:38:43 +0000687 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000689 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000690 * Returns the resource data for the script from where the function causing
691 * the error originates.
692 */
693 Handle<Value> GetScriptData() const;
694
695 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000696 * Exception stack trace. By default stack traces are not captured for
697 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
698 * to change this option.
699 */
700 Handle<StackTrace> GetStackTrace() const;
701
702 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000703 * Returns the number, 1-based, of the line where the error occurred.
704 */
ager@chromium.org32912102009-01-16 10:38:43 +0000705 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000707 /**
708 * Returns the index within the script of the first character where
709 * the error occurred.
710 */
ager@chromium.org32912102009-01-16 10:38:43 +0000711 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000712
713 /**
714 * Returns the index within the script of the last character where
715 * the error occurred.
716 */
ager@chromium.org32912102009-01-16 10:38:43 +0000717 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000718
719 /**
720 * Returns the index within the line of the first character where
721 * the error occurred.
722 */
ager@chromium.org32912102009-01-16 10:38:43 +0000723 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000724
725 /**
726 * Returns the index within the line of the last character where
727 * the error occurred.
728 */
ager@chromium.org32912102009-01-16 10:38:43 +0000729 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000730
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731 // TODO(1245381): Print to a string instead of on a FILE.
732 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000733
734 static const int kNoLineNumberInfo = 0;
735 static const int kNoColumnInfo = 0;
736};
737
738
739/**
740 * Representation of a JavaScript stack trace. The information collected is a
741 * snapshot of the execution stack and the information remains valid after
742 * execution continues.
743 */
744class V8EXPORT StackTrace {
745 public:
746 /**
747 * Flags that determine what information is placed captured for each
748 * StackFrame when grabbing the current stack trace.
749 */
750 enum StackTraceOptions {
751 kLineNumber = 1,
752 kColumnOffset = 1 << 1 | kLineNumber,
753 kScriptName = 1 << 2,
754 kFunctionName = 1 << 3,
755 kIsEval = 1 << 4,
756 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000757 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000758 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000759 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000760 };
761
762 /**
763 * Returns a StackFrame at a particular index.
764 */
765 Local<StackFrame> GetFrame(uint32_t index) const;
766
767 /**
768 * Returns the number of StackFrames.
769 */
770 int GetFrameCount() const;
771
772 /**
773 * Returns StackTrace as a v8::Array that contains StackFrame objects.
774 */
775 Local<Array> AsArray();
776
777 /**
778 * Grab a snapshot of the the current JavaScript execution stack.
779 *
780 * \param frame_limit The maximum number of stack frames we want to capture.
781 * \param options Enumerates the set of things we will capture for each
782 * StackFrame.
783 */
784 static Local<StackTrace> CurrentStackTrace(
785 int frame_limit,
786 StackTraceOptions options = kOverview);
787};
788
789
790/**
791 * A single JavaScript stack frame.
792 */
793class V8EXPORT StackFrame {
794 public:
795 /**
796 * Returns the number, 1-based, of the line for the associate function call.
797 * This method will return Message::kNoLineNumberInfo if it is unable to
798 * retrieve the line number, or if kLineNumber was not passed as an option
799 * when capturing the StackTrace.
800 */
801 int GetLineNumber() const;
802
803 /**
804 * Returns the 1-based column offset on the line for the associated function
805 * call.
806 * This method will return Message::kNoColumnInfo if it is unable to retrieve
807 * the column number, or if kColumnOffset was not passed as an option when
808 * capturing the StackTrace.
809 */
810 int GetColumn() const;
811
812 /**
813 * Returns the name of the resource that contains the script for the
814 * function for this StackFrame.
815 */
816 Local<String> GetScriptName() const;
817
818 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000819 * Returns the name of the resource that contains the script for the
820 * function for this StackFrame or sourceURL value if the script name
821 * is undefined and its source ends with //@ sourceURL=... string.
822 */
823 Local<String> GetScriptNameOrSourceURL() const;
824
825 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000826 * Returns the name of the function associated with this stack frame.
827 */
828 Local<String> GetFunctionName() const;
829
830 /**
831 * Returns whether or not the associated function is compiled via a call to
832 * eval().
833 */
834 bool IsEval() const;
835
836 /**
837 * Returns whther or not the associated function is called as a
838 * constructor via "new".
839 */
840 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841};
842
843
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000844// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845
846
847/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000848 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000850class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851 public:
852
853 /**
854 * Returns true if this value is the undefined value. See ECMA-262
855 * 4.3.10.
856 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000857 V8EXPORT bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858
859 /**
860 * Returns true if this value is the null value. See ECMA-262
861 * 4.3.11.
862 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000863 V8EXPORT bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864
865 /**
866 * Returns true if this value is true.
867 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000868 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
870 /**
871 * Returns true if this value is false.
872 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000873 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874
875 /**
876 * Returns true if this value is an instance of the String type.
877 * See ECMA-262 8.4.
878 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000879 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880
881 /**
882 * Returns true if this value is a function.
883 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000884 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885
886 /**
887 * Returns true if this value is an array.
888 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000889 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890
v8.team.kasperl727e9952008-09-02 14:56:44 +0000891 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 * Returns true if this value is an object.
893 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000894 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
v8.team.kasperl727e9952008-09-02 14:56:44 +0000896 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 * Returns true if this value is boolean.
898 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000899 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000900
v8.team.kasperl727e9952008-09-02 14:56:44 +0000901 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 * Returns true if this value is a number.
903 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000904 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905
v8.team.kasperl727e9952008-09-02 14:56:44 +0000906 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 * Returns true if this value is external.
908 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000909 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910
v8.team.kasperl727e9952008-09-02 14:56:44 +0000911 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 * Returns true if this value is a 32-bit signed integer.
913 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000914 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000916 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000917 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000918 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000919 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000920
921 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000922 * Returns true if this value is a Date.
923 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000924 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000925
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000926 /**
927 * Returns true if this value is a RegExp.
928 */
929 V8EXPORT bool IsRegExp() const;
930
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000931 V8EXPORT Local<Boolean> ToBoolean() const;
932 V8EXPORT Local<Number> ToNumber() const;
933 V8EXPORT Local<String> ToString() const;
934 V8EXPORT Local<String> ToDetailString() const;
935 V8EXPORT Local<Object> ToObject() const;
936 V8EXPORT Local<Integer> ToInteger() const;
937 V8EXPORT Local<Uint32> ToUint32() const;
938 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939
940 /**
941 * Attempts to convert a string to an array index.
942 * Returns an empty handle if the conversion fails.
943 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000944 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000946 V8EXPORT bool BooleanValue() const;
947 V8EXPORT double NumberValue() const;
948 V8EXPORT int64_t IntegerValue() const;
949 V8EXPORT uint32_t Uint32Value() const;
950 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951
952 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000953 V8EXPORT bool Equals(Handle<Value> that) const;
954 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000955
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000956 private:
957 inline bool QuickIsString() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000958 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959};
960
961
962/**
963 * The superclass of primitive values. See ECMA-262 4.3.2.
964 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000965class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966
967
968/**
969 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
970 * or false value.
971 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000972class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000974 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 static inline Handle<Boolean> New(bool value);
976};
977
978
979/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000980 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000982class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000984
985 /**
986 * Returns the number of characters in this string.
987 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000988 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989
v8.team.kasperl727e9952008-09-02 14:56:44 +0000990 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000991 * Returns the number of bytes in the UTF-8 encoded
992 * representation of this string.
993 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000994 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000995
996 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000997 * Write the contents of the string to an external buffer.
998 * If no arguments are given, expects the buffer to be large
999 * enough to hold the entire string and NULL terminator. Copies
1000 * the contents of the string and the NULL terminator into the
1001 * buffer.
1002 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001003 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1004 * before the end of the buffer.
1005 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001006 * Copies up to length characters into the output buffer.
1007 * Only null-terminates if there is enough space in the buffer.
1008 *
1009 * \param buffer The buffer into which the string will be copied.
1010 * \param start The starting position within the string at which
1011 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001012 * \param length The number of characters to copy from the string. For
1013 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001014 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001015 * \param hints Various hints that might affect performance of this or
1016 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001017 * \return The number of characters copied to the buffer excluding the null
1018 * terminator. For WriteUtf8: The number of bytes copied to the buffer
1019 * including the null terminator.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001020 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001021 enum WriteHints {
1022 NO_HINTS = 0,
1023 HINT_MANY_WRITES_EXPECTED = 1
1024 };
1025
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001026 V8EXPORT int Write(uint16_t* buffer,
1027 int start = 0,
1028 int length = -1,
1029 WriteHints hints = NO_HINTS) const; // UTF-16
1030 V8EXPORT int WriteAscii(char* buffer,
1031 int start = 0,
1032 int length = -1,
1033 WriteHints hints = NO_HINTS) const; // ASCII
1034 V8EXPORT int WriteUtf8(char* buffer,
1035 int length = -1,
1036 int* nchars_ref = NULL,
1037 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038
v8.team.kasperl727e9952008-09-02 14:56:44 +00001039 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001040 * A zero length string.
1041 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001042 V8EXPORT static v8::Local<v8::String> Empty();
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001043
1044 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001045 * Returns true if the string is external
1046 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001047 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048
v8.team.kasperl727e9952008-09-02 14:56:44 +00001049 /**
1050 * Returns true if the string is both external and ascii
1051 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001052 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001053
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001054 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001055 public:
1056 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001057
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001058 protected:
1059 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001060
1061 /**
1062 * Internally V8 will call this Dispose method when the external string
1063 * resource is no longer needed. The default implementation will use the
1064 * delete operator. This method can be overridden in subclasses to
1065 * control how allocated external string resources are disposed.
1066 */
1067 virtual void Dispose() { delete this; }
1068
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001069 private:
1070 // Disallow copying and assigning.
1071 ExternalStringResourceBase(const ExternalStringResourceBase&);
1072 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001073
1074 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001075 };
1076
v8.team.kasperl727e9952008-09-02 14:56:44 +00001077 /**
1078 * An ExternalStringResource is a wrapper around a two-byte string
1079 * buffer that resides outside V8's heap. Implement an
1080 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001081 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001082 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001083 class V8EXPORT ExternalStringResource
1084 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 public:
1086 /**
1087 * Override the destructor to manage the life cycle of the underlying
1088 * buffer.
1089 */
1090 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001091
1092 /**
1093 * The string data from the underlying buffer.
1094 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001096
1097 /**
1098 * The length of the string. That is, the number of two-byte characters.
1099 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102 protected:
1103 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 };
1105
1106 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001107 * An ExternalAsciiStringResource is a wrapper around an ascii
1108 * string buffer that resides outside V8's heap. Implement an
1109 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001110 * underlying buffer. Note that the string data must be immutable
1111 * and that the data must be strict 7-bit ASCII, not Latin1 or
1112 * UTF-8, which would require special treatment internally in the
1113 * engine and, in the case of UTF-8, do not allow efficient indexing.
1114 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001115 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001117 class V8EXPORT ExternalAsciiStringResource
1118 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119 public:
1120 /**
1121 * Override the destructor to manage the life cycle of the underlying
1122 * buffer.
1123 */
1124 virtual ~ExternalAsciiStringResource() {}
1125 /** The string data from the underlying buffer.*/
1126 virtual const char* data() const = 0;
1127 /** The number of ascii characters in the string.*/
1128 virtual size_t length() const = 0;
1129 protected:
1130 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 };
1132
1133 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001134 * Get the ExternalStringResource for an external string. Returns
1135 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001137 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138
1139 /**
1140 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001141 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001143 V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001145 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146
1147 /**
1148 * Allocates a new string from either utf-8 encoded or ascii data.
1149 * The second parameter 'length' gives the buffer length.
1150 * If the data is utf-8 encoded, the caller must
1151 * be careful to supply the length parameter.
1152 * If it is not given, the function calls
1153 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001154 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001156 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157
1158 /** Allocates a new string from utf16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001159 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160
1161 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001162 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163
v8.team.kasperl727e9952008-09-02 14:56:44 +00001164 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001165 * Creates a new string by concatenating the left and the right strings
1166 * passed in as parameters.
1167 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001168 V8EXPORT static Local<String> Concat(Handle<String> left,
1169 Handle<String>right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001170
1171 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001172 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001173 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001174 * resource will be disposed by calling its Dispose method. The caller of
1175 * this function should not otherwise delete or modify the resource. Neither
1176 * should the underlying buffer be deallocated or modified except through the
1177 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001178 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001179 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001180
ager@chromium.org6f10e412009-02-13 10:11:16 +00001181 /**
1182 * Associate an external string resource with this string by transforming it
1183 * in place so that existing references to this string in the JavaScript heap
1184 * will use the external string resource. The external string resource's
1185 * character contents needs to be equivalent to this string.
1186 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001187 * The string is not modified if the operation fails. See NewExternal for
1188 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001189 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001190 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191
v8.team.kasperl727e9952008-09-02 14:56:44 +00001192 /**
1193 * Creates a new external string using the ascii data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001194 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001195 * resource will be disposed by calling its Dispose method. The caller of
1196 * this function should not otherwise delete or modify the resource. Neither
1197 * should the underlying buffer be deallocated or modified except through the
1198 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001199 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001200 V8EXPORT static Local<String> NewExternal(
1201 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001202
ager@chromium.org6f10e412009-02-13 10:11:16 +00001203 /**
1204 * Associate an external string resource with this string by transforming it
1205 * in place so that existing references to this string in the JavaScript heap
1206 * will use the external string resource. The external string resource's
1207 * character contents needs to be equivalent to this string.
1208 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001209 * The string is not modified if the operation fails. See NewExternal for
1210 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001211 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001212 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001214 /**
1215 * Returns true if this string can be made external.
1216 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001217 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001218
kasper.lund7276f142008-07-30 08:49:36 +00001219 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001220 V8EXPORT static Local<String> NewUndetectable(const char* data,
1221 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222
kasper.lund7276f142008-07-30 08:49:36 +00001223 /** Creates an undetectable string from the supplied utf-16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001224 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1225 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226
1227 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001228 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001229 * you want to print the object. If conversion to a string fails
1230 * (eg. due to an exception in the toString() method of the object)
1231 * then the length() method returns 0 and the * operator returns
1232 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001233 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001234 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001235 public:
1236 explicit Utf8Value(Handle<v8::Value> obj);
1237 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001238 char* operator*() { return str_; }
1239 const char* operator*() const { return str_; }
1240 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001241 private:
1242 char* str_;
1243 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001244
1245 // Disallow copying and assigning.
1246 Utf8Value(const Utf8Value&);
1247 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001248 };
1249
1250 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251 * Converts an object to an ascii string.
1252 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001253 * If conversion to a string fails (eg. due to an exception in the toString()
1254 * method of the object) then the length() method returns 0 and the * operator
1255 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001257 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258 public:
1259 explicit AsciiValue(Handle<v8::Value> obj);
1260 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001261 char* operator*() { return str_; }
1262 const char* operator*() const { return str_; }
1263 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 private:
1265 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001266 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001267
1268 // Disallow copying and assigning.
1269 AsciiValue(const AsciiValue&);
1270 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 };
1272
1273 /**
1274 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001275 * If conversion to a string fails (eg. due to an exception in the toString()
1276 * method of the object) then the length() method returns 0 and the * operator
1277 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001279 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 public:
1281 explicit Value(Handle<v8::Value> obj);
1282 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001283 uint16_t* operator*() { return str_; }
1284 const uint16_t* operator*() const { return str_; }
1285 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 private:
1287 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001288 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001289
1290 // Disallow copying and assigning.
1291 Value(const Value&);
1292 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001294
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001295 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001296 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1297 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298};
1299
1300
1301/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001302 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001304class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001306 V8EXPORT double Value() const;
1307 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001308 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001310 V8EXPORT Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001311 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312};
1313
1314
1315/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001316 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001318class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001320 V8EXPORT static Local<Integer> New(int32_t value);
1321 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1322 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001323 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001325 V8EXPORT Integer();
1326 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327};
1328
1329
1330/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001331 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001333class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001335 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001337 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338};
1339
1340
1341/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001342 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001344class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001346 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001348 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349};
1350
1351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352enum PropertyAttribute {
1353 None = 0,
1354 ReadOnly = 1 << 0,
1355 DontEnum = 1 << 1,
1356 DontDelete = 1 << 2
1357};
1358
ager@chromium.org3811b432009-10-28 14:53:37 +00001359enum ExternalArrayType {
1360 kExternalByteArray = 1,
1361 kExternalUnsignedByteArray,
1362 kExternalShortArray,
1363 kExternalUnsignedShortArray,
1364 kExternalIntArray,
1365 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001366 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001367 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001368 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001369};
1370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001372 * Accessor[Getter|Setter] are used as callback functions when
1373 * setting|getting a particular property. See Object and ObjectTemplate's
1374 * method SetAccessor.
1375 */
1376typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1377 const AccessorInfo& info);
1378
1379
1380typedef void (*AccessorSetter)(Local<String> property,
1381 Local<Value> value,
1382 const AccessorInfo& info);
1383
1384
1385/**
1386 * Access control specifications.
1387 *
1388 * Some accessors should be accessible across contexts. These
1389 * accessors have an explicit access control parameter which specifies
1390 * the kind of cross-context access that should be allowed.
1391 *
1392 * Additionally, for security, accessors can prohibit overwriting by
1393 * accessors defined in JavaScript. For objects that have such
1394 * accessors either locally or in their prototype chain it is not
1395 * possible to overwrite the accessor by using __defineGetter__ or
1396 * __defineSetter__ from JavaScript code.
1397 */
1398enum AccessControl {
1399 DEFAULT = 0,
1400 ALL_CAN_READ = 1,
1401 ALL_CAN_WRITE = 1 << 1,
1402 PROHIBITS_OVERWRITING = 1 << 2
1403};
1404
1405
1406/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001407 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001409class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001411 V8EXPORT bool Set(Handle<Value> key,
1412 Handle<Value> value,
1413 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001414
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001415 V8EXPORT bool Set(uint32_t index,
1416 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001417
ager@chromium.orge2902be2009-06-08 12:21:35 +00001418 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001419 // overriding accessors or read-only properties.
1420 //
1421 // Note that if the object has an interceptor the property will be set
1422 // locally, but since the interceptor takes precedence the local property
1423 // will only be returned if the interceptor doesn't return a value.
1424 //
1425 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001426 V8EXPORT bool ForceSet(Handle<Value> key,
1427 Handle<Value> value,
1428 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001429
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001430 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001432 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001433
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 // TODO(1245389): Replace the type-specific versions of these
1435 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001436 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001437
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001438 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001439
1440 // Delete a property on this object bypassing interceptors and
1441 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001442 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001443
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001444 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001445
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001446 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001448 V8EXPORT bool SetAccessor(Handle<String> name,
1449 AccessorGetter getter,
1450 AccessorSetter setter = 0,
1451 Handle<Value> data = Handle<Value>(),
1452 AccessControl settings = DEFAULT,
1453 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001454
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001456 * Returns an array containing the names of the enumerable properties
1457 * of this object, including properties from prototype objects. The
1458 * array returned by this method contains the same values as would
1459 * be enumerated by a for-in statement over this object.
1460 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001461 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001462
1463 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464 * Get the prototype object. This does not skip objects marked to
1465 * be skipped by __proto__ and it does not consult the security
1466 * handler.
1467 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001468 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
1470 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001471 * Set the prototype object. This does not skip objects marked to
1472 * be skipped by __proto__ and it does not consult the security
1473 * handler.
1474 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001475 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001476
1477 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001478 * Finds an instance of the given function template in the prototype
1479 * chain.
1480 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001481 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1482 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001483
1484 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 * Call builtin Object.prototype.toString on this object.
1486 * This is different from Value::ToString() that may call
1487 * user-defined toString function. This one does not.
1488 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001489 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001491 /**
1492 * Returns the name of the function invoked as a constructor for this object.
1493 */
1494 V8EXPORT Local<String> GetConstructorName();
1495
kasper.lund212ac232008-07-16 07:07:30 +00001496 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001497 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001498 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001499 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001500 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001501 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001503 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001504 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001505
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001506 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001507 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001508
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001510 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001511 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1512 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1513 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001514
1515 /**
1516 * If result.IsEmpty() no real property was located in the prototype chain.
1517 * This means interceptors in the prototype chain are not called.
1518 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001519 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1520 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001521
1522 /**
1523 * If result.IsEmpty() no real property was located on the object or
1524 * in the prototype chain.
1525 * This means interceptors in the prototype chain are not called.
1526 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001527 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528
1529 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001530 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531
kasper.lund212ac232008-07-16 07:07:30 +00001532 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001533 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001535 /**
1536 * Turns on access check on the object if the object is an instance of
1537 * a template that has access check callbacks. If an object has no
1538 * access check info, the object cannot be accessed by anyone.
1539 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001540 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001541
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001542 /**
1543 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001544 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001545 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001546 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001547 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001548 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001549 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001550
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001551 /**
1552 * Access hidden properties on JavaScript objects. These properties are
1553 * hidden from the executing JavaScript and only accessible through the V8
1554 * C++ API. Hidden properties introduced by V8 internally (for example the
1555 * identity hash) are prefixed with "v8::".
1556 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001557 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1558 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1559 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001560
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001561 /**
1562 * Returns true if this is an instance of an api function (one
1563 * created from a function created from a function template) and has
1564 * been modified since it was created. Note that this method is
1565 * conservative and may return true for objects that haven't actually
1566 * been modified.
1567 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001568 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001569
1570 /**
1571 * Clone this object with a fast but shallow copy. Values will point
1572 * to the same values as the original object.
1573 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001574 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001576 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001577 * Returns the context in which the object was created.
1578 */
1579 V8EXPORT Local<Context> CreationContext();
1580
1581 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001582 * Set the backing store of the indexed properties to be managed by the
1583 * embedding layer. Access to the indexed properties will follow the rules
1584 * spelled out in CanvasPixelArray.
1585 * Note: The embedding program still owns the data and needs to ensure that
1586 * the backing store is preserved while V8 has a reference.
1587 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001588 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001589 V8EXPORT bool HasIndexedPropertiesInPixelData();
1590 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1591 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001592
ager@chromium.org3811b432009-10-28 14:53:37 +00001593 /**
1594 * Set the backing store of the indexed properties to be managed by the
1595 * embedding layer. Access to the indexed properties will follow the rules
1596 * spelled out for the CanvasArray subtypes in the WebGL specification.
1597 * Note: The embedding program still owns the data and needs to ensure that
1598 * the backing store is preserved while V8 has a reference.
1599 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001600 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1601 void* data,
1602 ExternalArrayType array_type,
1603 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001604 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1605 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1606 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1607 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001608
lrn@chromium.org1c092762011-05-09 09:42:16 +00001609 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001610 * Checks whether a callback is set by the
1611 * ObjectTemplate::SetCallAsFunctionHandler method.
1612 * When an Object is callable this method returns true.
1613 */
1614 V8EXPORT bool IsCallable();
1615
1616 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00001617 * Call an Object as a function if a callback is set by the
1618 * ObjectTemplate::SetCallAsFunctionHandler method.
1619 */
1620 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1621 int argc,
1622 Handle<Value> argv[]);
1623
1624 /**
1625 * Call an Object as a consturctor if a callback is set by the
1626 * ObjectTemplate::SetCallAsFunctionHandler method.
1627 * Note: This method behaves like the Function::NewInstance method.
1628 */
1629 V8EXPORT Local<Value> CallAsConstructor(int argc,
1630 Handle<Value> argv[]);
1631
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001632 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001633 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001635 V8EXPORT Object();
1636 V8EXPORT static void CheckCast(Value* obj);
1637 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1638 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001639
1640 /**
1641 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001642 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001643 */
1644 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645};
1646
1647
1648/**
1649 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1650 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001651class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001653 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654
ager@chromium.org3e875802009-06-29 08:26:34 +00001655 /**
1656 * Clones an element at index |index|. Returns an empty
1657 * handle if cloning fails (for any reason).
1658 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001659 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001660
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001661 /**
1662 * Creates a JavaScript array with the given length. If the length
1663 * is negative the returned array will have length 0.
1664 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001665 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001666
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001667 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001669 V8EXPORT Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001670 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671};
1672
1673
1674/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001675 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001677class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001679 V8EXPORT Local<Object> NewInstance() const;
1680 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1681 V8EXPORT Local<Value> Call(Handle<Object> recv,
1682 int argc,
1683 Handle<Value> argv[]);
1684 V8EXPORT void SetName(Handle<String> name);
1685 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001686
1687 /**
1688 * Returns zero based line number of function body and
1689 * kLineOffsetNotFound if no information available.
1690 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001691 V8EXPORT int GetScriptLineNumber() const;
1692 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001693 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001694 V8EXPORT static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001695 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001696 V8EXPORT Function();
1697 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698};
1699
1700
1701/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001702 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1703 */
1704class Date : public Object {
1705 public:
1706 V8EXPORT static Local<Value> New(double time);
1707
1708 /**
1709 * A specialization of Value::NumberValue that is more efficient
1710 * because we know the structure of this object.
1711 */
1712 V8EXPORT double NumberValue() const;
1713
1714 static inline Date* Cast(v8::Value* obj);
1715
1716 /**
1717 * Notification that the embedder has changed the time zone,
1718 * daylight savings time, or other date / time configuration
1719 * parameters. V8 keeps a cache of various values used for
1720 * date / time computation. This notification will reset
1721 * those cached values for the current context so that date /
1722 * time configuration changes would be reflected in the Date
1723 * object.
1724 *
1725 * This API should not be called more than needed as it will
1726 * negatively impact the performance of date operations.
1727 */
1728 V8EXPORT static void DateTimeConfigurationChangeNotification();
1729
1730 private:
1731 V8EXPORT static void CheckCast(v8::Value* obj);
1732};
1733
1734
1735/**
1736 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1737 */
1738class RegExp : public Object {
1739 public:
1740 /**
1741 * Regular expression flag bits. They can be or'ed to enable a set
1742 * of flags.
1743 */
1744 enum Flags {
1745 kNone = 0,
1746 kGlobal = 1,
1747 kIgnoreCase = 2,
1748 kMultiline = 4
1749 };
1750
1751 /**
1752 * Creates a regular expression from the given pattern string and
1753 * the flags bit field. May throw a JavaScript exception as
1754 * described in ECMA-262, 15.10.4.1.
1755 *
1756 * For example,
1757 * RegExp::New(v8::String::New("foo"),
1758 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1759 * is equivalent to evaluating "/foo/gm".
1760 */
1761 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1762 Flags flags);
1763
1764 /**
1765 * Returns the value of the source property: a string representing
1766 * the regular expression.
1767 */
1768 V8EXPORT Local<String> GetSource() const;
1769
1770 /**
1771 * Returns the flags bit field.
1772 */
1773 V8EXPORT Flags GetFlags() const;
1774
1775 static inline RegExp* Cast(v8::Value* obj);
1776
1777 private:
1778 V8EXPORT static void CheckCast(v8::Value* obj);
1779};
1780
1781
1782/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001783 * A JavaScript value that wraps a C++ void*. This type of value is
1784 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001786 *
1787 * The Wrap function V8 will return the most optimal Value object wrapping the
1788 * C++ void*. The type of the value is not guaranteed to be an External object
1789 * and no assumptions about its type should be made. To access the wrapped
1790 * value Unwrap should be used, all other operations on that object will lead
1791 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001793class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001795 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001796 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001797
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001798 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001799 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001800 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001802 V8EXPORT External();
1803 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001804 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001805 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806};
1807
1808
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001809// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810
1811
1812/**
1813 * The superclass of object and function templates.
1814 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001815class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 public:
1817 /** Adds a property to each instance created by this template.*/
1818 void Set(Handle<String> name, Handle<Data> value,
1819 PropertyAttribute attributes = None);
1820 inline void Set(const char* name, Handle<Data> value);
1821 private:
1822 Template();
1823
1824 friend class ObjectTemplate;
1825 friend class FunctionTemplate;
1826};
1827
1828
1829/**
1830 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001831 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832 * including the receiver, the number and values of arguments, and
1833 * the holder of the function.
1834 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001835class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 public:
1837 inline int Length() const;
1838 inline Local<Value> operator[](int i) const;
1839 inline Local<Function> Callee() const;
1840 inline Local<Object> This() const;
1841 inline Local<Object> Holder() const;
1842 inline bool IsConstructCall() const;
1843 inline Local<Value> Data() const;
1844 private:
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001845 static const int kDataIndex = 0;
1846 static const int kCalleeIndex = -1;
1847 static const int kHolderIndex = -2;
1848
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001850 inline Arguments(internal::Object** implicit_args,
1851 internal::Object** values,
1852 int length,
1853 bool is_construct_call);
1854 internal::Object** implicit_args_;
1855 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001857 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858};
1859
1860
1861/**
1862 * The information passed to an accessor callback about the context
1863 * of the property access.
1864 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001865class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001867 inline AccessorInfo(internal::Object** args)
1868 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869 inline Local<Value> Data() const;
1870 inline Local<Object> This() const;
1871 inline Local<Object> Holder() const;
1872 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001873 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874};
1875
1876
1877typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1878
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 * NamedProperty[Getter|Setter] are used as interceptors on object.
1881 * See ObjectTemplate::SetNamedPropertyHandler.
1882 */
1883typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1884 const AccessorInfo& info);
1885
1886
1887/**
1888 * Returns the value if the setter intercepts the request.
1889 * Otherwise, returns an empty handle.
1890 */
1891typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1892 Local<Value> value,
1893 const AccessorInfo& info);
1894
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895/**
1896 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001897 * The result is an integer encoding property attributes (like v8::None,
1898 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001900typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1901 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902
1903
1904/**
1905 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001906 * The return value is true if the property could be deleted and false
1907 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908 */
1909typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1910 const AccessorInfo& info);
1911
1912/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001913 * Returns an array containing the names of the properties the named
1914 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 */
1916typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1917
v8.team.kasperl727e9952008-09-02 14:56:44 +00001918
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001920 * Returns the value of the property if the getter intercepts the
1921 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 */
1923typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1924 const AccessorInfo& info);
1925
1926
1927/**
1928 * Returns the value if the setter intercepts the request.
1929 * Otherwise, returns an empty handle.
1930 */
1931typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1932 Local<Value> value,
1933 const AccessorInfo& info);
1934
1935
1936/**
1937 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001938 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001940typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
1941 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942
1943/**
1944 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001945 * The return value is true if the property could be deleted and false
1946 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 */
1948typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1949 const AccessorInfo& info);
1950
v8.team.kasperl727e9952008-09-02 14:56:44 +00001951/**
1952 * Returns an array containing the indices of the properties the
1953 * indexed property getter intercepts.
1954 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1956
1957
1958/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001959 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 */
1961enum AccessType {
1962 ACCESS_GET,
1963 ACCESS_SET,
1964 ACCESS_HAS,
1965 ACCESS_DELETE,
1966 ACCESS_KEYS
1967};
1968
v8.team.kasperl727e9952008-09-02 14:56:44 +00001969
1970/**
1971 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001972 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001973 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001974typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975 Local<Value> key,
1976 AccessType type,
1977 Local<Value> data);
1978
v8.team.kasperl727e9952008-09-02 14:56:44 +00001979
1980/**
1981 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001982 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001983 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001984typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985 uint32_t index,
1986 AccessType type,
1987 Local<Value> data);
1988
1989
1990/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001991 * A FunctionTemplate is used to create functions at runtime. There
1992 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001993 * context. The lifetime of the created function is equal to the
1994 * lifetime of the context. So in case the embedder needs to create
1995 * temporary functions that can be collected using Scripts is
1996 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 *
1998 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001999 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002001 * A FunctionTemplate has a corresponding instance template which is
2002 * used to create object instances when the function is used as a
2003 * constructor. Properties added to the instance template are added to
2004 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005 *
2006 * A FunctionTemplate can have a prototype template. The prototype template
2007 * is used to create the prototype object of the function.
2008 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002009 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002011 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2013 * t->Set("func_property", v8::Number::New(1));
2014 *
2015 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2016 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2017 * proto_t->Set("proto_const", v8::Number::New(2));
2018 *
2019 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2020 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2021 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2022 * instance_t->Set("instance_property", Number::New(3));
2023 *
2024 * v8::Local<v8::Function> function = t->GetFunction();
2025 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002026 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 *
2028 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002029 * and "instance" for the instance object created above. The function
2030 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002032 * \code
2033 * func_property in function == true;
2034 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002036 * function.prototype.proto_method() invokes 'InvokeCallback'
2037 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002039 * instance instanceof function == true;
2040 * instance.instance_accessor calls 'InstanceAccessorCallback'
2041 * instance.instance_property == 3;
2042 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002044 * A FunctionTemplate can inherit from another one by calling the
2045 * FunctionTemplate::Inherit method. The following graph illustrates
2046 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002048 * \code
2049 * FunctionTemplate Parent -> Parent() . prototype -> { }
2050 * ^ ^
2051 * | Inherit(Parent) | .__proto__
2052 * | |
2053 * FunctionTemplate Child -> Child() . prototype -> { }
2054 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002056 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2057 * object of the Child() function has __proto__ pointing to the
2058 * Parent() function's prototype object. An instance of the Child
2059 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002061 * Let Parent be the FunctionTemplate initialized in the previous
2062 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002064 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 * Local<FunctionTemplate> parent = t;
2066 * Local<FunctionTemplate> child = FunctionTemplate::New();
2067 * child->Inherit(parent);
2068 *
2069 * Local<Function> child_function = child->GetFunction();
2070 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002071 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002073 * The Child function and Child instance will have the following
2074 * properties:
2075 *
2076 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002078 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002080 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002082class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 public:
2084 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002085 static Local<FunctionTemplate> New(
2086 InvocationCallback callback = 0,
2087 Handle<Value> data = Handle<Value>(),
2088 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 /** Returns the unique function instance in the current execution context.*/
2090 Local<Function> GetFunction();
2091
v8.team.kasperl727e9952008-09-02 14:56:44 +00002092 /**
2093 * Set the call-handler callback for a FunctionTemplate. This
2094 * callback is called whenever the function created from this
2095 * FunctionTemplate is called.
2096 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 void SetCallHandler(InvocationCallback callback,
2098 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099
v8.team.kasperl727e9952008-09-02 14:56:44 +00002100 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 Local<ObjectTemplate> InstanceTemplate();
2102
2103 /** Causes the function template to inherit from a parent function template.*/
2104 void Inherit(Handle<FunctionTemplate> parent);
2105
2106 /**
2107 * A PrototypeTemplate is the template used to create the prototype object
2108 * of the function created by this template.
2109 */
2110 Local<ObjectTemplate> PrototypeTemplate();
2111
v8.team.kasperl727e9952008-09-02 14:56:44 +00002112
2113 /**
2114 * Set the class name of the FunctionTemplate. This is used for
2115 * printing objects created with the function created from the
2116 * FunctionTemplate as its constructor.
2117 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118 void SetClassName(Handle<String> name);
2119
2120 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002121 * Determines whether the __proto__ accessor ignores instances of
2122 * the function template. If instances of the function template are
2123 * ignored, __proto__ skips all instances and instead returns the
2124 * next object in the prototype chain.
2125 *
2126 * Call with a value of true to make the __proto__ accessor ignore
2127 * instances of the function template. Call with a value of false
2128 * to make the __proto__ accessor not ignore instances of the
2129 * function template. By default, instances of a function template
2130 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 */
2132 void SetHiddenPrototype(bool value);
2133
2134 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002135 * Returns true if the given object is an instance of this function
2136 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 */
2138 bool HasInstance(Handle<Value> object);
2139
2140 private:
2141 FunctionTemplate();
2142 void AddInstancePropertyAccessor(Handle<String> name,
2143 AccessorGetter getter,
2144 AccessorSetter setter,
2145 Handle<Value> data,
2146 AccessControl settings,
2147 PropertyAttribute attributes);
2148 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2149 NamedPropertySetter setter,
2150 NamedPropertyQuery query,
2151 NamedPropertyDeleter remover,
2152 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002153 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2155 IndexedPropertySetter setter,
2156 IndexedPropertyQuery query,
2157 IndexedPropertyDeleter remover,
2158 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002159 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2161 Handle<Value> data);
2162
2163 friend class Context;
2164 friend class ObjectTemplate;
2165};
2166
2167
2168/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002169 * An ObjectTemplate is used to create objects at runtime.
2170 *
2171 * Properties added to an ObjectTemplate are added to each object
2172 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002174class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002176 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002178
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 /** Creates a new instance of this template.*/
2180 Local<Object> NewInstance();
2181
2182 /**
2183 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002184 *
2185 * Whenever the property with the given name is accessed on objects
2186 * created from this ObjectTemplate the getter and setter callbacks
2187 * are called instead of getting and setting the property directly
2188 * on the JavaScript object.
2189 *
2190 * \param name The name of the property for which an accessor is added.
2191 * \param getter The callback to invoke when getting the property.
2192 * \param setter The callback to invoke when setting the property.
2193 * \param data A piece of data that will be passed to the getter and setter
2194 * callbacks whenever they are invoked.
2195 * \param settings Access control settings for the accessor. This is a bit
2196 * field consisting of one of more of
2197 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2198 * The default is to not allow cross-context access.
2199 * ALL_CAN_READ means that all cross-context reads are allowed.
2200 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2201 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2202 * cross-context access.
2203 * \param attribute The attributes of the property for which an accessor
2204 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 */
2206 void SetAccessor(Handle<String> name,
2207 AccessorGetter getter,
2208 AccessorSetter setter = 0,
2209 Handle<Value> data = Handle<Value>(),
2210 AccessControl settings = DEFAULT,
2211 PropertyAttribute attribute = None);
2212
2213 /**
2214 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002215 *
2216 * Whenever a named property is accessed on objects created from
2217 * this object template, the provided callback is invoked instead of
2218 * accessing the property directly on the JavaScript object.
2219 *
2220 * \param getter The callback to invoke when getting a property.
2221 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002222 * \param query The callback to invoke to check if a property is present,
2223 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002224 * \param deleter The callback to invoke when deleting a property.
2225 * \param enumerator The callback to invoke to enumerate all the named
2226 * properties of an object.
2227 * \param data A piece of data that will be passed to the callbacks
2228 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 */
2230 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2231 NamedPropertySetter setter = 0,
2232 NamedPropertyQuery query = 0,
2233 NamedPropertyDeleter deleter = 0,
2234 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002235 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236
2237 /**
2238 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002239 *
2240 * Whenever an indexed property is accessed on objects created from
2241 * this object template, the provided callback is invoked instead of
2242 * accessing the property directly on the JavaScript object.
2243 *
2244 * \param getter The callback to invoke when getting a property.
2245 * \param setter The callback to invoke when setting a property.
2246 * \param query The callback to invoke to check is an object has a property.
2247 * \param deleter The callback to invoke when deleting a property.
2248 * \param enumerator The callback to invoke to enumerate all the indexed
2249 * properties of an object.
2250 * \param data A piece of data that will be passed to the callbacks
2251 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 */
2253 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2254 IndexedPropertySetter setter = 0,
2255 IndexedPropertyQuery query = 0,
2256 IndexedPropertyDeleter deleter = 0,
2257 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002258 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002259
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 /**
2261 * Sets the callback to be used when calling instances created from
2262 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002263 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 * function.
2265 */
2266 void SetCallAsFunctionHandler(InvocationCallback callback,
2267 Handle<Value> data = Handle<Value>());
2268
v8.team.kasperl727e9952008-09-02 14:56:44 +00002269 /**
2270 * Mark object instances of the template as undetectable.
2271 *
2272 * In many ways, undetectable objects behave as though they are not
2273 * there. They behave like 'undefined' in conditionals and when
2274 * printed. However, properties can be accessed and called as on
2275 * normal objects.
2276 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 void MarkAsUndetectable();
2278
v8.team.kasperl727e9952008-09-02 14:56:44 +00002279 /**
2280 * Sets access check callbacks on the object template.
2281 *
2282 * When accessing properties on instances of this object template,
2283 * the access check callback will be called to determine whether or
2284 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002285 * The last parameter specifies whether access checks are turned
2286 * on by default on instances. If access checks are off by default,
2287 * they can be turned on on individual instances by calling
2288 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002289 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2291 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002292 Handle<Value> data = Handle<Value>(),
2293 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294
kasper.lund212ac232008-07-16 07:07:30 +00002295 /**
2296 * Gets the number of internal fields for objects generated from
2297 * this template.
2298 */
2299 int InternalFieldCount();
2300
2301 /**
2302 * Sets the number of internal fields for objects generated from
2303 * this template.
2304 */
2305 void SetInternalFieldCount(int value);
2306
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002307 private:
2308 ObjectTemplate();
2309 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2310 friend class FunctionTemplate;
2311};
2312
2313
2314/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002315 * A Signature specifies which receivers and arguments a function can
2316 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002318class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 public:
2320 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2321 Handle<FunctionTemplate>(),
2322 int argc = 0,
2323 Handle<FunctionTemplate> argv[] = 0);
2324 private:
2325 Signature();
2326};
2327
2328
2329/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002330 * A utility for determining the type of objects based on the template
2331 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002333class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 public:
2335 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2336 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2337 int match(Handle<Value> value);
2338 private:
2339 TypeSwitch();
2340};
2341
2342
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002343// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344
2345
2346/**
2347 * Ignore
2348 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002349class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 public:
2351 Extension(const char* name,
2352 const char* source = 0,
2353 int dep_count = 0,
2354 const char** deps = 0);
2355 virtual ~Extension() { }
2356 virtual v8::Handle<v8::FunctionTemplate>
2357 GetNativeFunction(v8::Handle<v8::String> name) {
2358 return v8::Handle<v8::FunctionTemplate>();
2359 }
2360
2361 const char* name() { return name_; }
2362 const char* source() { return source_; }
2363 int dependency_count() { return dep_count_; }
2364 const char** dependencies() { return deps_; }
2365 void set_auto_enable(bool value) { auto_enable_ = value; }
2366 bool auto_enable() { return auto_enable_; }
2367
2368 private:
2369 const char* name_;
2370 const char* source_;
2371 int dep_count_;
2372 const char** deps_;
2373 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002374
2375 // Disallow copying and assigning.
2376 Extension(const Extension&);
2377 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378};
2379
2380
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002381void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382
2383
2384/**
2385 * Ignore
2386 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002387class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388 public:
2389 inline DeclareExtension(Extension* extension) {
2390 RegisterExtension(extension);
2391 }
2392};
2393
2394
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002395// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396
2397
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002398Handle<Primitive> V8EXPORT Undefined();
2399Handle<Primitive> V8EXPORT Null();
2400Handle<Boolean> V8EXPORT True();
2401Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402
2403
2404/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002405 * A set of constraints that specifies the limits of the runtime's memory use.
2406 * You must set the heap size before initializing the VM - the size cannot be
2407 * adjusted after the VM is initialized.
2408 *
2409 * If you are using threads then you should hold the V8::Locker lock while
2410 * setting the stack limit and you must set a non-default stack limit separately
2411 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002412 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002413class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 public:
2415 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002416 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002418 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002420 int max_executable_size() { return max_executable_size_; }
2421 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002422 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002423 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2425 private:
2426 int max_young_space_size_;
2427 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002428 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 uint32_t* stack_limit_;
2430};
2431
2432
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002433bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434
2435
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002436// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437
2438
2439typedef void (*FatalErrorCallback)(const char* location, const char* message);
2440
2441
2442typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2443
2444
2445/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002446 * Schedules an exception to be thrown when returning to JavaScript. When an
2447 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002449 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002451Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452
2453/**
2454 * Create new error objects by calling the corresponding error object
2455 * constructor with the message.
2456 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002457class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458 public:
2459 static Local<Value> RangeError(Handle<String> message);
2460 static Local<Value> ReferenceError(Handle<String> message);
2461 static Local<Value> SyntaxError(Handle<String> message);
2462 static Local<Value> TypeError(Handle<String> message);
2463 static Local<Value> Error(Handle<String> message);
2464};
2465
2466
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002467// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002469typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002471typedef void* (*CreateHistogramCallback)(const char* name,
2472 int min,
2473 int max,
2474 size_t buckets);
2475
2476typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2477
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002478// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002479 enum ObjectSpace {
2480 kObjectSpaceNewSpace = 1 << 0,
2481 kObjectSpaceOldPointerSpace = 1 << 1,
2482 kObjectSpaceOldDataSpace = 1 << 2,
2483 kObjectSpaceCodeSpace = 1 << 3,
2484 kObjectSpaceMapSpace = 1 << 4,
2485 kObjectSpaceLoSpace = 1 << 5,
2486
2487 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2488 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2489 kObjectSpaceLoSpace
2490 };
2491
2492 enum AllocationAction {
2493 kAllocationActionAllocate = 1 << 0,
2494 kAllocationActionFree = 1 << 1,
2495 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2496 };
2497
2498typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2499 AllocationAction action,
2500 int size);
2501
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002502// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2504 AccessType type,
2505 Local<Value> data);
2506
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002507// --- AllowCodeGenerationFromStrings callbacks ---
2508
2509/**
2510 * Callback to check if code generation from strings is allowed. See
2511 * Context::AllowCodeGenerationFromStrings.
2512 */
2513typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2514
2515// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516
2517/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002518 * Applications can register callback functions which will be called
2519 * before and after a garbage collection. Allocations are not
2520 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002521 * objects (set or delete properties for example) since it is possible
2522 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002524enum GCType {
2525 kGCTypeScavenge = 1 << 0,
2526 kGCTypeMarkSweepCompact = 1 << 1,
2527 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2528};
2529
2530enum GCCallbackFlags {
2531 kNoGCCallbackFlags = 0,
2532 kGCCallbackFlagCompacted = 1 << 0
2533};
2534
2535typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2536typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538typedef void (*GCCallback)();
2539
2540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002542 * Profiler modules.
2543 *
2544 * In V8, profiler consists of several modules: CPU profiler, and different
2545 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002546 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2547 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002548 */
2549enum ProfilerModules {
2550 PROFILER_MODULE_NONE = 0,
2551 PROFILER_MODULE_CPU = 1,
2552 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002553 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2554 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002555};
2556
2557
2558/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002559 * Collection of V8 heap information.
2560 *
2561 * Instances of this class can be passed to v8::V8::HeapStatistics to
2562 * get heap statistics from V8.
2563 */
2564class V8EXPORT HeapStatistics {
2565 public:
2566 HeapStatistics();
2567 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002568 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002569 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002570 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002571
2572 private:
2573 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002574 void set_total_heap_size_executable(size_t size) {
2575 total_heap_size_executable_ = size;
2576 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002577 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002578 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002579
2580 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002581 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002582 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002583 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002584
2585 friend class V8;
2586};
2587
2588
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002589class RetainedObjectInfo;
2590
ager@chromium.org3811b432009-10-28 14:53:37 +00002591/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002592 * Isolate represents an isolated instance of the V8 engine. V8
2593 * isolates have completely separate states. Objects from one isolate
2594 * must not be used in other isolates. When V8 is initialized a
2595 * default isolate is implicitly created and entered. The embedder
2596 * can create additional isolates and use them in parallel in multiple
2597 * threads. An isolate can be entered by at most one thread at any
2598 * given time. The Locker/Unlocker API can be used to synchronize.
2599 */
2600class V8EXPORT Isolate {
2601 public:
2602 /**
2603 * Stack-allocated class which sets the isolate for all operations
2604 * executed within a local scope.
2605 */
2606 class V8EXPORT Scope {
2607 public:
2608 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2609 isolate->Enter();
2610 }
2611
2612 ~Scope() { isolate_->Exit(); }
2613
2614 private:
2615 Isolate* const isolate_;
2616
2617 // Prevent copying of Scope objects.
2618 Scope(const Scope&);
2619 Scope& operator=(const Scope&);
2620 };
2621
2622 /**
2623 * Creates a new isolate. Does not change the currently entered
2624 * isolate.
2625 *
2626 * When an isolate is no longer used its resources should be freed
2627 * by calling Dispose(). Using the delete operator is not allowed.
2628 */
2629 static Isolate* New();
2630
2631 /**
2632 * Returns the entered isolate for the current thread or NULL in
2633 * case there is no current isolate.
2634 */
2635 static Isolate* GetCurrent();
2636
2637 /**
2638 * Methods below this point require holding a lock (using Locker) in
2639 * a multi-threaded environment.
2640 */
2641
2642 /**
2643 * Sets this isolate as the entered one for the current thread.
2644 * Saves the previously entered one (if any), so that it can be
2645 * restored when exiting. Re-entering an isolate is allowed.
2646 */
2647 void Enter();
2648
2649 /**
2650 * Exits this isolate by restoring the previously entered one in the
2651 * current thread. The isolate may still stay the same, if it was
2652 * entered more than once.
2653 *
2654 * Requires: this == Isolate::GetCurrent().
2655 */
2656 void Exit();
2657
2658 /**
2659 * Disposes the isolate. The isolate must not be entered by any
2660 * thread to be disposable.
2661 */
2662 void Dispose();
2663
2664 private:
2665
2666 Isolate();
2667 Isolate(const Isolate&);
2668 ~Isolate();
2669 Isolate& operator=(const Isolate&);
2670 void* operator new(size_t size);
2671 void operator delete(void*, size_t);
2672};
2673
2674
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002675class StartupData {
2676 public:
2677 enum CompressionAlgorithm {
2678 kUncompressed,
2679 kBZip2
2680 };
2681
2682 const char* data;
2683 int compressed_size;
2684 int raw_size;
2685};
2686
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002687/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 * Container class for static utility functions.
2689 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002690class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002692 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693 static void SetFatalErrorHandler(FatalErrorCallback that);
2694
v8.team.kasperl727e9952008-09-02 14:56:44 +00002695 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002696 * Set the callback to invoke to check if code generation from
2697 * strings should be allowed.
2698 */
2699 static void SetAllowCodeGenerationFromStringsCallback(
2700 AllowCodeGenerationFromStringsCallback that);
2701
2702 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002703 * Ignore out-of-memory exceptions.
2704 *
2705 * V8 running out of memory is treated as a fatal error by default.
2706 * This means that the fatal error handler is called and that V8 is
2707 * terminated.
2708 *
2709 * IgnoreOutOfMemoryException can be used to not treat a
2710 * out-of-memory situation as a fatal error. This way, the contexts
2711 * that did not cause the out of memory problem might be able to
2712 * continue execution.
2713 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714 static void IgnoreOutOfMemoryException();
2715
v8.team.kasperl727e9952008-09-02 14:56:44 +00002716 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002717 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002718 * fatal errors such as out-of-memory situations.
2719 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720 static bool IsDead();
2721
2722 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002723 * The following 4 functions are to be used when V8 is built with
2724 * the 'compress_startup_data' flag enabled. In this case, the
2725 * embedder must decompress startup data prior to initializing V8.
2726 *
2727 * This is how interaction with V8 should look like:
2728 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2729 * v8::StartupData* compressed_data =
2730 * new v8::StartupData[compressed_data_count];
2731 * v8::V8::GetCompressedStartupData(compressed_data);
2732 * ... decompress data (compressed_data can be updated in-place) ...
2733 * v8::V8::SetDecompressedStartupData(compressed_data);
2734 * ... now V8 can be initialized
2735 * ... make sure the decompressed data stays valid until V8 shutdown
2736 */
2737 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2738 static int GetCompressedStartupDataCount();
2739 static void GetCompressedStartupData(StartupData* compressed_data);
2740 static void SetDecompressedStartupData(StartupData* decompressed_data);
2741
2742 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002743 * Adds a message listener.
2744 *
2745 * The same message listener can be added more than once and it that
2746 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747 */
2748 static bool AddMessageListener(MessageCallback that,
2749 Handle<Value> data = Handle<Value>());
2750
2751 /**
2752 * Remove all message listeners from the specified callback function.
2753 */
2754 static void RemoveMessageListeners(MessageCallback that);
2755
2756 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002757 * Tells V8 to capture current stack trace when uncaught exception occurs
2758 * and report it to the message listeners. The option is off by default.
2759 */
2760 static void SetCaptureStackTraceForUncaughtExceptions(
2761 bool capture,
2762 int frame_limit = 10,
2763 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2764
2765 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002766 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 */
2768 static void SetFlagsFromString(const char* str, int length);
2769
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002770 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002771 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002772 */
2773 static void SetFlagsFromCommandLine(int* argc,
2774 char** argv,
2775 bool remove_flags);
2776
kasper.lund7276f142008-07-30 08:49:36 +00002777 /** Get the version string. */
2778 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002779
2780 /**
2781 * Enables the host application to provide a mechanism for recording
2782 * statistics counters.
2783 */
2784 static void SetCounterFunction(CounterLookupCallback);
2785
2786 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002787 * Enables the host application to provide a mechanism for recording
2788 * histograms. The CreateHistogram function returns a
2789 * histogram which will later be passed to the AddHistogramSample
2790 * function.
2791 */
2792 static void SetCreateHistogramFunction(CreateHistogramCallback);
2793 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2794
2795 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002796 * Enables the computation of a sliding window of states. The sliding
2797 * window information is recorded in statistics counters.
2798 */
2799 static void EnableSlidingStateWindow();
2800
2801 /** Callback function for reporting failed access checks.*/
2802 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2803
2804 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002805 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002806 * garbage collection. Allocations are not allowed in the
2807 * callback function, you therefore cannot manipulate objects (set
2808 * or delete properties for example) since it is possible such
2809 * operations will result in the allocation of objects. It is possible
2810 * to specify the GCType filter for your callback. But it is not possible to
2811 * register the same callback function two times with different
2812 * GCType filters.
2813 */
2814 static void AddGCPrologueCallback(
2815 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2816
2817 /**
2818 * This function removes callback which was installed by
2819 * AddGCPrologueCallback function.
2820 */
2821 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2822
2823 /**
2824 * The function is deprecated. Please use AddGCPrologueCallback instead.
2825 * Enables the host application to receive a notification before a
2826 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002827 * callback function, you therefore cannot manipulate objects (set
2828 * or delete properties for example) since it is possible such
2829 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830 */
2831 static void SetGlobalGCPrologueCallback(GCCallback);
2832
2833 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002834 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002835 * garbage collection. Allocations are not allowed in the
2836 * callback function, you therefore cannot manipulate objects (set
2837 * or delete properties for example) since it is possible such
2838 * operations will result in the allocation of objects. It is possible
2839 * to specify the GCType filter for your callback. But it is not possible to
2840 * register the same callback function two times with different
2841 * GCType filters.
2842 */
2843 static void AddGCEpilogueCallback(
2844 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2845
2846 /**
2847 * This function removes callback which was installed by
2848 * AddGCEpilogueCallback function.
2849 */
2850 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2851
2852 /**
2853 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2854 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002855 * major garbage collection. Allocations are not allowed in the
2856 * callback function, you therefore cannot manipulate objects (set
2857 * or delete properties for example) since it is possible such
2858 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002859 */
2860 static void SetGlobalGCEpilogueCallback(GCCallback);
2861
2862 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002863 * Enables the host application to provide a mechanism to be notified
2864 * and perform custom logging when V8 Allocates Executable Memory.
2865 */
2866 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
2867 ObjectSpace space,
2868 AllocationAction action);
2869
2870 /**
2871 * This function removes callback which was installed by
2872 * AddMemoryAllocationCallback function.
2873 */
2874 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
2875
2876 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002877 * Allows the host application to group objects together. If one
2878 * object in the group is alive, all objects in the group are alive.
2879 * After each garbage collection, object groups are removed. It is
2880 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002881 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002882 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002883 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002884 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002885 static void AddObjectGroup(Persistent<Value>* objects,
2886 size_t length,
2887 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888
2889 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002890 * Allows the host application to declare implicit references between
2891 * the objects: if |parent| is alive, all |children| are alive too.
2892 * After each garbage collection, all implicit references
2893 * are removed. It is intended to be used in the before-garbage-collection
2894 * callback function.
2895 */
2896 static void AddImplicitReferences(Persistent<Object> parent,
2897 Persistent<Value>* children,
2898 size_t length);
2899
2900 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002901 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002902 * initialize from scratch. This function is called implicitly if
2903 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002904 */
2905 static bool Initialize();
2906
kasper.lund7276f142008-07-30 08:49:36 +00002907 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002908 * Adjusts the amount of registered external memory. Used to give
2909 * V8 an indication of the amount of externally allocated memory
2910 * that is kept alive by JavaScript objects. V8 uses this to decide
2911 * when to perform global garbage collections. Registering
2912 * externally allocated memory will trigger global garbage
2913 * collections more often than otherwise in an attempt to garbage
2914 * collect the JavaScript objects keeping the externally allocated
2915 * memory alive.
2916 *
2917 * \param change_in_bytes the change in externally allocated memory
2918 * that is kept alive by JavaScript objects.
2919 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002920 */
2921 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2922
iposva@chromium.org245aa852009-02-10 00:49:54 +00002923 /**
2924 * Suspends recording of tick samples in the profiler.
2925 * When the V8 profiling mode is enabled (usually via command line
2926 * switches) this function suspends recording of tick samples.
2927 * Profiling ticks are discarded until ResumeProfiler() is called.
2928 *
2929 * See also the --prof and --prof_auto command line switches to
2930 * enable V8 profiling.
2931 */
2932 static void PauseProfiler();
2933
2934 /**
2935 * Resumes recording of tick samples in the profiler.
2936 * See also PauseProfiler().
2937 */
2938 static void ResumeProfiler();
2939
ager@chromium.org41826e72009-03-30 13:30:57 +00002940 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002941 * Return whether profiler is currently paused.
2942 */
2943 static bool IsProfilerPaused();
2944
2945 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002946 * Resumes specified profiler modules. Can be called several times to
2947 * mark the opening of a profiler events block with the given tag.
2948 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002949 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2950 * See ProfilerModules enum.
2951 *
2952 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002953 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002954 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002955 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002956
2957 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002958 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2959 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2960 * same tag value. There is no need for blocks to be properly nested.
2961 * The profiler is paused when the last opened block is closed.
2962 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002963 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2964 * See ProfilerModules enum.
2965 *
2966 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002967 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002968 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002969 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002970
2971 /**
2972 * Returns active (resumed) profiler modules.
2973 * See ProfilerModules enum.
2974 *
2975 * \returns active profiler modules.
2976 */
2977 static int GetActiveProfilerModules();
2978
2979 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002980 * If logging is performed into a memory buffer (via --logfile=*), allows to
2981 * retrieve previously written messages. This can be used for retrieving
2982 * profiler log data in the application. This function is thread-safe.
2983 *
2984 * Caller provides a destination buffer that must exist during GetLogLines
2985 * call. Only whole log lines are copied into the buffer.
2986 *
2987 * \param from_pos specified a point in a buffer to read from, 0 is the
2988 * beginning of a buffer. It is assumed that caller updates its current
2989 * position using returned size value from the previous call.
2990 * \param dest_buf destination buffer for log data.
2991 * \param max_size size of the destination buffer.
2992 * \returns actual size of log data copied into buffer.
2993 */
2994 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2995
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002996 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002997 * The minimum allowed size for a log lines buffer. If the size of
2998 * the buffer given will not be enough to hold a line of the maximum
2999 * length, an attempt to find a log line end in GetLogLines will
3000 * fail, and an empty result will be returned.
3001 */
3002 static const int kMinimumSizeForLogLinesBuffer = 2048;
3003
3004 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003005 * Retrieve the V8 thread id of the calling thread.
3006 *
3007 * The thread id for a thread should only be retrieved after the V8
3008 * lock has been acquired with a Locker object with that thread.
3009 */
3010 static int GetCurrentThreadId();
3011
3012 /**
3013 * Forcefully terminate execution of a JavaScript thread. This can
3014 * be used to terminate long-running scripts.
3015 *
3016 * TerminateExecution should only be called when then V8 lock has
3017 * been acquired with a Locker object. Therefore, in order to be
3018 * able to terminate long-running threads, preemption must be
3019 * enabled to allow the user of TerminateExecution to acquire the
3020 * lock.
3021 *
3022 * The termination is achieved by throwing an exception that is
3023 * uncatchable by JavaScript exception handlers. Termination
3024 * exceptions act as if they were caught by a C++ TryCatch exception
3025 * handlers. If forceful termination is used, any C++ TryCatch
3026 * exception handler that catches an exception should check if that
3027 * exception is a termination exception and immediately return if
3028 * that is the case. Returning immediately in that case will
3029 * continue the propagation of the termination exception if needed.
3030 *
3031 * The thread id passed to TerminateExecution must have been
3032 * obtained by calling GetCurrentThreadId on the thread in question.
3033 *
3034 * \param thread_id The thread id of the thread to terminate.
3035 */
3036 static void TerminateExecution(int thread_id);
3037
3038 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003039 * Forcefully terminate the current thread of JavaScript execution
3040 * in the given isolate. If no isolate is provided, the default
3041 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003042 *
3043 * This method can be used by any thread even if that thread has not
3044 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003045 *
3046 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003047 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003048 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003049
3050 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003051 * Is V8 terminating JavaScript execution.
3052 *
3053 * Returns true if JavaScript execution is currently terminating
3054 * because of a call to TerminateExecution. In that case there are
3055 * still JavaScript frames on the stack and the termination
3056 * exception is still active.
3057 */
3058 static bool IsExecutionTerminating();
3059
3060 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003061 * Releases any resources used by v8 and stops any utility threads
3062 * that may be running. Note that disposing v8 is permanent, it
3063 * cannot be reinitialized.
3064 *
3065 * It should generally not be necessary to dispose v8 before exiting
3066 * a process, this should happen automatically. It is only necessary
3067 * to use if the process needs the resources taken up by v8.
3068 */
3069 static bool Dispose();
3070
ager@chromium.org3811b432009-10-28 14:53:37 +00003071 /**
3072 * Get statistics about the heap memory usage.
3073 */
3074 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003075
3076 /**
3077 * Optional notification that the embedder is idle.
3078 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003079 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003080 * Returns true if the embedder should stop calling IdleNotification
3081 * until real work has been done. This indicates that V8 has done
3082 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003083 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003084 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003085
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003086 /**
3087 * Optional notification that the system is running low on memory.
3088 * V8 uses these notifications to attempt to free memory.
3089 */
3090 static void LowMemoryNotification();
3091
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003092 /**
3093 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003094 * these notifications to guide the GC heuristic. Returns the number
3095 * of context disposals - including this one - since the last time
3096 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003097 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003098 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003099
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003100 private:
3101 V8();
3102
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003103 static internal::Object** GlobalizeReference(internal::Object** handle);
3104 static void DisposeGlobal(internal::Object** global_handle);
3105 static void MakeWeak(internal::Object** global_handle,
3106 void* data,
3107 WeakReferenceCallback);
3108 static void ClearWeak(internal::Object** global_handle);
3109 static bool IsGlobalNearDeath(internal::Object** global_handle);
3110 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003111 static void SetWrapperClassId(internal::Object** global_handle,
3112 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003113
3114 template <class T> friend class Handle;
3115 template <class T> friend class Local;
3116 template <class T> friend class Persistent;
3117 friend class Context;
3118};
3119
3120
3121/**
3122 * An external exception handler.
3123 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003124class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003125 public:
3126
3127 /**
3128 * Creates a new try/catch block and registers it with v8.
3129 */
3130 TryCatch();
3131
3132 /**
3133 * Unregisters and deletes this try/catch block.
3134 */
3135 ~TryCatch();
3136
3137 /**
3138 * Returns true if an exception has been caught by this try/catch block.
3139 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003140 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141
3142 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003143 * For certain types of exceptions, it makes no sense to continue
3144 * execution.
3145 *
3146 * Currently, the only type of exception that can be caught by a
3147 * TryCatch handler and for which it does not make sense to continue
3148 * is termination exception. Such exceptions are thrown when the
3149 * TerminateExecution methods are called to terminate a long-running
3150 * script.
3151 *
3152 * If CanContinue returns false, the correct action is to perform
3153 * any C++ cleanup needed and then return.
3154 */
3155 bool CanContinue() const;
3156
3157 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003158 * Throws the exception caught by this TryCatch in a way that avoids
3159 * it being caught again by this same TryCatch. As with ThrowException
3160 * it is illegal to execute any JavaScript operations after calling
3161 * ReThrow; the caller must return immediately to where the exception
3162 * is caught.
3163 */
3164 Handle<Value> ReThrow();
3165
3166 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003167 * Returns the exception caught by this try/catch block. If no exception has
3168 * been caught an empty handle is returned.
3169 *
3170 * The returned handle is valid until this TryCatch block has been destroyed.
3171 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003172 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003173
3174 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003175 * Returns the .stack property of the thrown object. If no .stack
3176 * property is present an empty handle is returned.
3177 */
3178 Local<Value> StackTrace() const;
3179
3180 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003181 * Returns the message associated with this exception. If there is
3182 * no message associated an empty handle is returned.
3183 *
3184 * The returned handle is valid until this TryCatch block has been
3185 * destroyed.
3186 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003187 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003188
3189 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003190 * Clears any exceptions that may have been caught by this try/catch block.
3191 * After this method has been called, HasCaught() will return false.
3192 *
3193 * It is not necessary to clear a try/catch block before using it again; if
3194 * another exception is thrown the previously caught exception will just be
3195 * overwritten. However, it is often a good idea since it makes it easier
3196 * to determine which operation threw a given exception.
3197 */
3198 void Reset();
3199
v8.team.kasperl727e9952008-09-02 14:56:44 +00003200 /**
3201 * Set verbosity of the external exception handler.
3202 *
3203 * By default, exceptions that are caught by an external exception
3204 * handler are not reported. Call SetVerbose with true on an
3205 * external exception handler to have exceptions caught by the
3206 * handler reported as if they were not caught.
3207 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003208 void SetVerbose(bool value);
3209
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003210 /**
3211 * Set whether or not this TryCatch should capture a Message object
3212 * which holds source information about where the exception
3213 * occurred. True by default.
3214 */
3215 void SetCaptureMessage(bool value);
3216
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003217 private:
3218 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003219 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003220 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003221 bool is_verbose_ : 1;
3222 bool can_continue_ : 1;
3223 bool capture_message_ : 1;
3224 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003225
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003226 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003227};
3228
3229
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003230// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003231
3232
3233/**
3234 * Ignore
3235 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003236class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003237 public:
3238 ExtensionConfiguration(int name_count, const char* names[])
3239 : name_count_(name_count), names_(names) { }
3240 private:
3241 friend class ImplementationUtilities;
3242 int name_count_;
3243 const char** names_;
3244};
3245
3246
3247/**
3248 * A sandboxed execution context with its own set of built-in objects
3249 * and functions.
3250 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003251class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003252 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003253 /**
3254 * Returns the global proxy object or global object itself for
3255 * detached contexts.
3256 *
3257 * Global proxy object is a thin wrapper whose prototype points to
3258 * actual context's global object with the properties like Object, etc.
3259 * This is done that way for security reasons (for more details see
3260 * https://wiki.mozilla.org/Gecko:SplitWindow).
3261 *
3262 * Please note that changes to global proxy object prototype most probably
3263 * would break VM---v8 expects only global object as a prototype of
3264 * global proxy object.
3265 *
3266 * If DetachGlobal() has been invoked, Global() would return actual global
3267 * object until global is reattached with ReattachGlobal().
3268 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003269 Local<Object> Global();
3270
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003271 /**
3272 * Detaches the global object from its context before
3273 * the global object can be reused to create a new context.
3274 */
3275 void DetachGlobal();
3276
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003277 /**
3278 * Reattaches a global object to a context. This can be used to
3279 * restore the connection between a global object and a context
3280 * after DetachGlobal has been called.
3281 *
3282 * \param global_object The global object to reattach to the
3283 * context. For this to work, the global object must be the global
3284 * object that was associated with this context before a call to
3285 * DetachGlobal.
3286 */
3287 void ReattachGlobal(Handle<Object> global_object);
3288
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003289 /** Creates a new context.
3290 *
3291 * Returns a persistent handle to the newly allocated context. This
3292 * persistent handle has to be disposed when the context is no
3293 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003294 *
3295 * \param extensions An optional extension configuration containing
3296 * the extensions to be installed in the newly created context.
3297 *
3298 * \param global_template An optional object template from which the
3299 * global object for the newly created context will be created.
3300 *
3301 * \param global_object An optional global object to be reused for
3302 * the newly created context. This global object must have been
3303 * created by a previous call to Context::New with the same global
3304 * template. The state of the global object will be completely reset
3305 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003306 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003307 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003308 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003309 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3310 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003311
kasper.lund44510672008-07-25 07:37:58 +00003312 /** Returns the last entered context. */
3313 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003314
kasper.lund44510672008-07-25 07:37:58 +00003315 /** Returns the context that is on the top of the stack. */
3316 static Local<Context> GetCurrent();
3317
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003318 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003319 * Returns the context of the calling JavaScript code. That is the
3320 * context of the top-most JavaScript frame. If there are no
3321 * JavaScript frames an empty handle is returned.
3322 */
3323 static Local<Context> GetCalling();
3324
3325 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003326 * Sets the security token for the context. To access an object in
3327 * another context, the security tokens must match.
3328 */
3329 void SetSecurityToken(Handle<Value> token);
3330
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003331 /** Restores the security token to the default value. */
3332 void UseDefaultSecurityToken();
3333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003334 /** Returns the security token of this context.*/
3335 Handle<Value> GetSecurityToken();
3336
v8.team.kasperl727e9952008-09-02 14:56:44 +00003337 /**
3338 * Enter this context. After entering a context, all code compiled
3339 * and run is compiled and run in this context. If another context
3340 * is already entered, this old context is saved so it can be
3341 * restored when the new context is exited.
3342 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003343 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003344
3345 /**
3346 * Exit this context. Exiting the current context restores the
3347 * context that was in place when entering the current context.
3348 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003349 void Exit();
3350
v8.team.kasperl727e9952008-09-02 14:56:44 +00003351 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003352 bool HasOutOfMemoryException();
3353
v8.team.kasperl727e9952008-09-02 14:56:44 +00003354 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003355 static bool InContext();
3356
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003357 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003358 * Associate an additional data object with the context. This is mainly used
3359 * with the debugger to provide additional information on the context through
3360 * the debugger API.
3361 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003362 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003363 Local<Value> GetData();
3364
3365 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003366 * Control whether code generation from strings is allowed. Calling
3367 * this method with false will disable 'eval' and the 'Function'
3368 * constructor for code running in this context. If 'eval' or the
3369 * 'Function' constructor are used an exception will be thrown.
3370 *
3371 * If code generation from strings is not allowed the
3372 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3373 * set before blocking the call to 'eval' or the 'Function'
3374 * constructor. If that callback returns true, the call will be
3375 * allowed, otherwise an exception will be thrown. If no callback is
3376 * set an exception will be thrown.
3377 */
3378 void AllowCodeGenerationFromStrings(bool allow);
3379
3380 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003381 * Stack-allocated class which sets the execution context for all
3382 * operations executed within a local scope.
3383 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003384 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003385 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003386 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003387 context_->Enter();
3388 }
3389 inline ~Scope() { context_->Exit(); }
3390 private:
3391 Handle<Context> context_;
3392 };
3393
3394 private:
3395 friend class Value;
3396 friend class Script;
3397 friend class Object;
3398 friend class Function;
3399};
3400
3401
3402/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003403 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003404 * is allowed to use any given V8 isolate. See Isolate class
3405 * comments. The definition of 'using V8 isolate' includes
3406 * accessing handles or holding onto object pointers obtained
3407 * from V8 handles while in the particular V8 isolate. It is up
3408 * to the user of V8 to ensure (perhaps with locking) that this
3409 * constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003410 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003411 * v8::Locker is a scoped lock object. While it's
3412 * active (i.e. between its construction and destruction) the current thread is
3413 * allowed to use the locked isolate. V8 guarantees that an isolate can be locked
3414 * by at most one thread at any time. In other words, the scope of a v8::Locker is
3415 * a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003416 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003417 * Sample usage:
3418* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419 * ...
3420 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003421 * v8::Locker locker(isolate);
3422 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003423 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003424 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003425 * ...
3426 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003427 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003428 *
3429 * If you wish to stop using V8 in a thread A you can do this by either
3430 * by destroying the v8::Locker object as above or by constructing a
3431 * v8::Unlocker object:
3432 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003433 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003434 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003435 * isolate->Exit();
3436 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003437 * ...
3438 * // Code not using V8 goes here while V8 can run in another thread.
3439 * ...
3440 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003441 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003442 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443 *
3444 * The Unlocker object is intended for use in a long-running callback
3445 * from V8, where you want to release the V8 lock for other threads to
3446 * use.
3447 *
3448 * The v8::Locker is a recursive lock. That is, you can lock more than
3449 * once in a given thread. This can be useful if you have code that can
3450 * be called either from code that holds the lock or from code that does
3451 * not. The Unlocker is not recursive so you can not have several
3452 * Unlockers on the stack at once, and you can not use an Unlocker in a
3453 * thread that is not inside a Locker's scope.
3454 *
3455 * An unlocker will unlock several lockers if it has to and reinstate
3456 * the correct depth of locking on its destruction. eg.:
3457 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003458 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459 * // V8 not locked.
3460 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003461 * v8::Locker locker(isolate);
3462 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003463 * // V8 locked.
3464 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003465 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003466 * // V8 still locked (2 levels).
3467 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003468 * isolate->Exit();
3469 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003470 * // V8 not locked.
3471 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003472 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473 * // V8 locked again (2 levels).
3474 * }
3475 * // V8 still locked (1 level).
3476 * }
3477 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003478 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003479 *
3480 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003481 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003482class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003483 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003484 /**
3485 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3486 */
3487 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003489 private:
3490 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003491};
3492
3493
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003494class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003495 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003496 /**
3497 * Initialize Locker for a given Isolate. NULL means default isolate.
3498 */
3499 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003500 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003501
3502 /**
3503 * Start preemption.
3504 *
3505 * When preemption is started, a timer is fired every n milli seconds
3506 * that will switch between multiple threads that are in contention
3507 * for the V8 lock.
3508 */
3509 static void StartPreemption(int every_n_ms);
3510
3511 /**
3512 * Stop preemption.
3513 */
3514 static void StopPreemption();
3515
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003516 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00003517 * Returns whether or not the locker for a given isolate, or default isolate if NULL is given,
3518 * is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003519 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003520 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003521
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003522 /**
3523 * Returns whether v8::Locker is being used by this V8 instance.
3524 */
3525 static bool IsActive() { return active_; }
3526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003527 private:
3528 bool has_lock_;
3529 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003530 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003531
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003532 static bool active_;
3533
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003534 // Disallow copying and assigning.
3535 Locker(const Locker&);
3536 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003537};
3538
3539
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003540/**
3541 * An interface for exporting data from V8, using "push" model.
3542 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003543class V8EXPORT OutputStream { // NOLINT
3544 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003545 enum OutputEncoding {
3546 kAscii = 0 // 7-bit ASCII.
3547 };
3548 enum WriteResult {
3549 kContinue = 0,
3550 kAbort = 1
3551 };
3552 virtual ~OutputStream() {}
3553 /** Notify about the end of stream. */
3554 virtual void EndOfStream() = 0;
3555 /** Get preferred output chunk size. Called only once. */
3556 virtual int GetChunkSize() { return 1024; }
3557 /** Get preferred output encoding. Called only once. */
3558 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3559 /**
3560 * Writes the next chunk of snapshot data into the stream. Writing
3561 * can be stopped by returning kAbort as function result. EndOfStream
3562 * will not be called in case writing was aborted.
3563 */
3564 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
3565};
3566
3567
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003568/**
3569 * An interface for reporting progress and controlling long-running
3570 * activities.
3571 */
3572class V8EXPORT ActivityControl { // NOLINT
3573 public:
3574 enum ControlOption {
3575 kContinue = 0,
3576 kAbort = 1
3577 };
3578 virtual ~ActivityControl() {}
3579 /**
3580 * Notify about current progress. The activity can be stopped by
3581 * returning kAbort as the callback result.
3582 */
3583 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3584};
3585
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003587// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003588
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003589
3590namespace internal {
3591
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003592static const int kApiPointerSize = sizeof(void*); // NOLINT
3593static const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003594
3595// Tag information for HeapObject.
3596const int kHeapObjectTag = 1;
3597const int kHeapObjectTagSize = 2;
3598const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3599
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003600// Tag information for Smi.
3601const int kSmiTag = 0;
3602const int kSmiTagSize = 1;
3603const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3604
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003605template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003606
3607// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003608template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003609 static const int kSmiShiftSize = 0;
3610 static const int kSmiValueSize = 31;
3611 static inline int SmiToInt(internal::Object* value) {
3612 int shift_bits = kSmiTagSize + kSmiShiftSize;
3613 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3614 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3615 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003616
3617 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3618 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003619 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003620 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003621};
3622
3623// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003624template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003625 static const int kSmiShiftSize = 31;
3626 static const int kSmiValueSize = 32;
3627 static inline int SmiToInt(internal::Object* value) {
3628 int shift_bits = kSmiTagSize + kSmiShiftSize;
3629 // Shift down and throw away top 32 bits.
3630 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3631 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003632
3633 // To maximize the range of pointers that can be encoded
3634 // in the available 32 bits, we require them to be 8 bytes aligned.
3635 // This gives 2 ^ (32 + 3) = 32G address space covered.
3636 // It might be not enough to cover stack allocated objects on some platforms.
3637 static const int kPointerAlignment = 3;
3638
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003639 static const uintptr_t kEncodablePointerMask =
3640 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003641
3642 static const int kPointerToSmiShift =
3643 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003644};
3645
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003646typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3647const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3648const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003649const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003650 PlatformSmiTagging::kEncodablePointerMask;
3651const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003652
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003653template <size_t ptr_size> struct InternalConstants;
3654
3655// Internal constants for 32-bit systems.
3656template <> struct InternalConstants<4> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003657 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003658};
3659
3660// Internal constants for 64-bit systems.
3661template <> struct InternalConstants<8> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003662 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003663};
3664
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003665/**
3666 * This class exports constants and functionality from within v8 that
3667 * is necessary to implement inline functions in the v8 api. Don't
3668 * depend on functions and constants defined here.
3669 */
3670class Internals {
3671 public:
3672
3673 // These values match non-compiler-dependent values defined within
3674 // the implementation of v8.
3675 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003676 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003677 static const int kStringResourceOffset =
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003678 InternalConstants<kApiPointerSize>::kStringResourceOffset;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003679
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003680 static const int kProxyProxyOffset = kApiPointerSize;
3681 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003682 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003683 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003684
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00003685 static const int kJSObjectType = 0xa1;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003686 static const int kFirstNonstringType = 0x80;
3687 static const int kProxyType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003688
3689 static inline bool HasHeapObjectTag(internal::Object* value) {
3690 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3691 kHeapObjectTag);
3692 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003693
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003694 static inline bool HasSmiTag(internal::Object* value) {
3695 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3696 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003697
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003698 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003699 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003700 }
3701
3702 static inline int GetInstanceType(internal::Object* obj) {
3703 typedef internal::Object O;
3704 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3705 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3706 }
3707
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003708 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003709 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003710 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3711 }
3712
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003713 static inline void* GetExternalPointer(internal::Object* obj) {
3714 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003715 return GetExternalPointerFromSmi(obj);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003716 } else if (GetInstanceType(obj) == kProxyType) {
3717 return ReadField<void*>(obj, kProxyProxyOffset);
3718 } else {
3719 return NULL;
3720 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003721 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003722
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003723 static inline bool IsExternalTwoByteString(int instance_type) {
3724 int representation = (instance_type & kFullStringRepresentationMask);
3725 return representation == kExternalTwoByteRepresentationTag;
3726 }
3727
3728 template <typename T>
3729 static inline T ReadField(Object* ptr, int offset) {
3730 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3731 return *reinterpret_cast<T*>(addr);
3732 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003733
3734 static inline bool CanCastToHeapObject(void* o) { return false; }
3735 static inline bool CanCastToHeapObject(Context* o) { return true; }
3736 static inline bool CanCastToHeapObject(String* o) { return true; }
3737 static inline bool CanCastToHeapObject(Object* o) { return true; }
3738 static inline bool CanCastToHeapObject(Message* o) { return true; }
3739 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
3740 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003741};
3742
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003743} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003744
3745
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003746template <class T>
3747Handle<T>::Handle() : val_(0) { }
3748
3749
3750template <class T>
3751Local<T>::Local() : Handle<T>() { }
3752
3753
3754template <class T>
3755Local<T> Local<T>::New(Handle<T> that) {
3756 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003757 T* that_ptr = *that;
3758 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
3759 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
3760 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
3761 reinterpret_cast<internal::HeapObject*>(*p))));
3762 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003763 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3764}
3765
3766
3767template <class T>
3768Persistent<T> Persistent<T>::New(Handle<T> that) {
3769 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003770 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003771 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3772}
3773
3774
3775template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003776bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003777 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003778 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003779}
3780
3781
3782template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003783bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003785 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003786}
3787
3788
3789template <class T>
3790void Persistent<T>::Dispose() {
3791 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003792 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003793}
3794
3795
3796template <class T>
3797Persistent<T>::Persistent() : Handle<T>() { }
3798
3799template <class T>
3800void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003801 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3802 parameters,
3803 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804}
3805
3806template <class T>
3807void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003808 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809}
3810
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003811template <class T>
3812void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
3813 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
3814}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003815
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003816Arguments::Arguments(internal::Object** implicit_args,
3817 internal::Object** values, int length,
3818 bool is_construct_call)
3819 : implicit_args_(implicit_args),
3820 values_(values),
3821 length_(length),
3822 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003823
3824
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003825Local<Value> Arguments::operator[](int i) const {
3826 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3827 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3828}
3829
3830
3831Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003832 return Local<Function>(reinterpret_cast<Function*>(
3833 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834}
3835
3836
3837Local<Object> Arguments::This() const {
3838 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3839}
3840
3841
3842Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003843 return Local<Object>(reinterpret_cast<Object*>(
3844 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003845}
3846
3847
3848Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003849 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003850}
3851
3852
3853bool Arguments::IsConstructCall() const {
3854 return is_construct_call_;
3855}
3856
3857
3858int Arguments::Length() const {
3859 return length_;
3860}
3861
3862
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003863template <class T>
3864Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003865 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3866 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003867 return Local<T>(reinterpret_cast<T*>(after));
3868}
3869
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003870Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871 return resource_name_;
3872}
3873
3874
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003875Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003876 return resource_line_offset_;
3877}
3878
3879
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003880Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003881 return resource_column_offset_;
3882}
3883
3884
3885Handle<Boolean> Boolean::New(bool value) {
3886 return value ? True() : False();
3887}
3888
3889
3890void Template::Set(const char* name, v8::Handle<Data> value) {
3891 Set(v8::String::New(name), value);
3892}
3893
3894
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003895Local<Value> Object::GetInternalField(int index) {
3896#ifndef V8_ENABLE_CHECKS
3897 Local<Value> quick_result = UncheckedGetInternalField(index);
3898 if (!quick_result.IsEmpty()) return quick_result;
3899#endif
3900 return CheckedGetInternalField(index);
3901}
3902
3903
3904Local<Value> Object::UncheckedGetInternalField(int index) {
3905 typedef internal::Object O;
3906 typedef internal::Internals I;
3907 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003908 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003909 // If the object is a plain JSObject, which is the common case,
3910 // we know where to find the internal fields and can return the
3911 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003912 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003913 O* value = I::ReadField<O*>(obj, offset);
3914 O** result = HandleScope::CreateHandle(value);
3915 return Local<Value>(reinterpret_cast<Value*>(result));
3916 } else {
3917 return Local<Value>();
3918 }
3919}
3920
3921
3922void* External::Unwrap(Handle<v8::Value> obj) {
3923#ifdef V8_ENABLE_CHECKS
3924 return FullUnwrap(obj);
3925#else
3926 return QuickUnwrap(obj);
3927#endif
3928}
3929
3930
3931void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3932 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003933 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003934 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003935}
3936
3937
3938void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003939 typedef internal::Object O;
3940 typedef internal::Internals I;
3941
3942 O* obj = *reinterpret_cast<O**>(this);
3943
3944 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3945 // If the object is a plain JSObject, which is the common case,
3946 // we know where to find the internal fields and can return the
3947 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003948 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003949 O* value = I::ReadField<O*>(obj, offset);
3950 return I::GetExternalPointer(value);
3951 }
3952
3953 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003954}
3955
3956
3957String* String::Cast(v8::Value* value) {
3958#ifdef V8_ENABLE_CHECKS
3959 CheckCast(value);
3960#endif
3961 return static_cast<String*>(value);
3962}
3963
3964
3965String::ExternalStringResource* String::GetExternalStringResource() const {
3966 typedef internal::Object O;
3967 typedef internal::Internals I;
3968 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003969 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003970 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003971 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3972 result = reinterpret_cast<String::ExternalStringResource*>(value);
3973 } else {
3974 result = NULL;
3975 }
3976#ifdef V8_ENABLE_CHECKS
3977 VerifyExternalStringResource(result);
3978#endif
3979 return result;
3980}
3981
3982
3983bool Value::IsString() const {
3984#ifdef V8_ENABLE_CHECKS
3985 return FullIsString();
3986#else
3987 return QuickIsString();
3988#endif
3989}
3990
3991bool Value::QuickIsString() const {
3992 typedef internal::Object O;
3993 typedef internal::Internals I;
3994 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3995 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003996 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003997}
3998
3999
4000Number* Number::Cast(v8::Value* value) {
4001#ifdef V8_ENABLE_CHECKS
4002 CheckCast(value);
4003#endif
4004 return static_cast<Number*>(value);
4005}
4006
4007
4008Integer* Integer::Cast(v8::Value* value) {
4009#ifdef V8_ENABLE_CHECKS
4010 CheckCast(value);
4011#endif
4012 return static_cast<Integer*>(value);
4013}
4014
4015
4016Date* Date::Cast(v8::Value* value) {
4017#ifdef V8_ENABLE_CHECKS
4018 CheckCast(value);
4019#endif
4020 return static_cast<Date*>(value);
4021}
4022
4023
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004024RegExp* RegExp::Cast(v8::Value* value) {
4025#ifdef V8_ENABLE_CHECKS
4026 CheckCast(value);
4027#endif
4028 return static_cast<RegExp*>(value);
4029}
4030
4031
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004032Object* Object::Cast(v8::Value* value) {
4033#ifdef V8_ENABLE_CHECKS
4034 CheckCast(value);
4035#endif
4036 return static_cast<Object*>(value);
4037}
4038
4039
4040Array* Array::Cast(v8::Value* value) {
4041#ifdef V8_ENABLE_CHECKS
4042 CheckCast(value);
4043#endif
4044 return static_cast<Array*>(value);
4045}
4046
4047
4048Function* Function::Cast(v8::Value* value) {
4049#ifdef V8_ENABLE_CHECKS
4050 CheckCast(value);
4051#endif
4052 return static_cast<Function*>(value);
4053}
4054
4055
4056External* External::Cast(v8::Value* value) {
4057#ifdef V8_ENABLE_CHECKS
4058 CheckCast(value);
4059#endif
4060 return static_cast<External*>(value);
4061}
4062
4063
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004064Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004065 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004066}
4067
4068
4069Local<Object> AccessorInfo::This() const {
4070 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4071}
4072
4073
4074Local<Object> AccessorInfo::Holder() const {
4075 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4076}
4077
4078
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004079/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004080 * \example shell.cc
4081 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004082 * command-line and executes them.
4083 */
4084
4085
4086/**
4087 * \example process.cc
4088 */
4089
4090
4091} // namespace v8
4092
4093
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004094#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004095#undef TYPE_CHECK
4096
4097
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004098#endif // V8_H_