blob: 411c2f1169dd0051159e338e4634860a536afd72 [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
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000162 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 * 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
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000184 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 * 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
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000328 * Persistent<Number> will cause a compile-time 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.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000374 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 * 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 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000391 * Marks the reference to this object independent. Garbage collector
392 * is free to ignore any object groups containing this object.
393 * Weak callback for an independent handle should not
394 * assume that it will be preceded by a global GC prologue callback
395 * or followed by a global GC epilogue callback.
396 */
397 inline void MarkIndependent();
398
399 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 *Checks if the handle holds the only reference to an object.
401 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000402 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403
404 /**
405 * Returns true if the handle's reference is weak.
406 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000407 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000409 /**
410 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
411 * interface description in v8-profiler.h for details.
412 */
413 inline void SetWrapperClassId(uint16_t class_id);
414
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415 private:
416 friend class ImplementationUtilities;
417 friend class ObjectTemplate;
418};
419
420
v8.team.kasperl727e9952008-09-02 14:56:44 +0000421 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422 * A stack-allocated class that governs a number of local handles.
423 * After a handle scope has been created, all local handles will be
424 * allocated within that handle scope until either the handle scope is
425 * deleted or another handle scope is created. If there is already a
426 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000427 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 * new handles will again be allocated in the original handle scope.
429 *
430 * After the handle scope of a local handle has been deleted the
431 * garbage collector will no longer track the object stored in the
432 * handle and may deallocate it. The behavior of accessing a handle
433 * for which the handle scope has been deleted is undefined.
434 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000435class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000437 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000439 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440
441 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 * Closes the handle scope and returns the value as a handle in the
443 * previous scope, which is the new current scope after the call.
444 */
445 template <class T> Local<T> Close(Handle<T> value);
446
447 /**
448 * Counts the number of allocated handles.
449 */
450 static int NumberOfHandles();
451
452 /**
453 * Creates a new handle with the given value.
454 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000455 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000456 // Faster version, uses HeapObject to obtain the current Isolate.
457 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458
459 private:
460 // Make it impossible to create heap-allocated or illegal handle
461 // scopes by disallowing certain operations.
462 HandleScope(const HandleScope&);
463 void operator=(const HandleScope&);
464 void* operator new(size_t size);
465 void operator delete(void*, size_t);
466
ager@chromium.org3811b432009-10-28 14:53:37 +0000467 // This Data class is accessible internally as HandleScopeData through a
468 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000469 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000471 internal::Object** next;
472 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000473 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000476 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 }
478 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000479
lrn@chromium.org303ada72010-10-27 09:33:13 +0000480 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000482 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000483 internal::Object** prev_next_;
484 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000486 // Allow for the active closing of HandleScopes which allows to pass a handle
487 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000489 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 friend class ImplementationUtilities;
492};
493
494
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000495// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
497
498/**
499 * The superclass of values and API object templates.
500 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000501class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 private:
503 Data();
504};
505
506
507/**
508 * Pre-compilation data that can be associated with a script. This
509 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000510 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000511 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000513class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 public:
515 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000516
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000517 /**
518 * Pre-compiles the specified script (context-independent).
519 *
520 * \param input Pointer to UTF-8 script source code.
521 * \param length Length of UTF-8 script source code.
522 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000525 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000526 * Pre-compiles the specified script (context-independent).
527 *
528 * NOTE: Pre-compilation using this method cannot happen on another thread
529 * without using Lockers.
530 *
531 * \param source Script source code.
532 */
533 static ScriptData* PreCompile(Handle<String> source);
534
535 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000536 * Load previous pre-compilation data.
537 *
538 * \param data Pointer to data returned by a call to Data() of a previous
539 * ScriptData. Ownership is not transferred.
540 * \param length Length of data.
541 */
542 static ScriptData* New(const char* data, int length);
543
544 /**
545 * Returns the length of Data().
546 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000548
549 /**
550 * Returns a serialized representation of this ScriptData that can later be
551 * passed to New(). NOTE: Serialized data is platform-dependent.
552 */
553 virtual const char* Data() = 0;
554
555 /**
556 * Returns true if the source code could not be parsed.
557 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000558 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559};
560
561
562/**
563 * The origin, within a file, of a script.
564 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000565class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000567 inline ScriptOrigin(
568 Handle<Value> resource_name,
569 Handle<Integer> resource_line_offset = Handle<Integer>(),
570 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 : resource_name_(resource_name),
572 resource_line_offset_(resource_line_offset),
573 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000574 inline Handle<Value> ResourceName() const;
575 inline Handle<Integer> ResourceLineOffset() const;
576 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000578 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 Handle<Integer> resource_line_offset_;
580 Handle<Integer> resource_column_offset_;
581};
582
583
584/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000585 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000587class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 public:
589
590 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000591 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000592 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000593 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000594 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000595 * when New() returns
596 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
597 * using pre_data speeds compilation if it's done multiple times.
598 * Owned by caller, no references are kept when New() returns.
599 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000600 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000601 * available to compile event handlers.
602 * \return Compiled script object (context independent; when run it
603 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000605 static Local<Script> New(Handle<String> source,
606 ScriptOrigin* origin = NULL,
607 ScriptData* pre_data = NULL,
608 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609
mads.s.agercbaa0602008-08-14 13:41:48 +0000610 /**
611 * Compiles the specified script using the specified file name
612 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000613 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000614 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000615 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000616 * as the script's origin.
617 * \return Compiled script object (context independent; when run it
618 * will use the currently entered context).
619 */
620 static Local<Script> New(Handle<String> source,
621 Handle<Value> file_name);
622
623 /**
624 * Compiles the specified script (bound to current context).
625 *
626 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000627 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000628 * when Compile() returns
629 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
630 * using pre_data speeds compilation if it's done multiple times.
631 * Owned by caller, no references are kept when Compile() returns.
632 * \param script_data Arbitrary data associated with script. Using
633 * this has same effect as calling SetData(), but makes data available
634 * earlier (i.e. to compile event handlers).
635 * \return Compiled script object, bound to the context that was active
636 * when this function was called. When run it will always use this
637 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000638 */
639 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000640 ScriptOrigin* origin = NULL,
641 ScriptData* pre_data = NULL,
642 Handle<String> script_data = Handle<String>());
643
644 /**
645 * Compiles the specified script using the specified file name
646 * object (typically a string) as the script's origin.
647 *
648 * \param source Script source code.
649 * \param file_name File name to use as script's origin
650 * \param script_data Arbitrary data associated with script. Using
651 * this has same effect as calling SetData(), but makes data available
652 * earlier (i.e. to compile event handlers).
653 * \return Compiled script object, bound to the context that was active
654 * when this function was called. When run it will always use this
655 * context.
656 */
657 static Local<Script> Compile(Handle<String> source,
658 Handle<Value> file_name,
659 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000660
v8.team.kasperl727e9952008-09-02 14:56:44 +0000661 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000662 * Runs the script returning the resulting value. If the script is
663 * context independent (created using ::New) it will be run in the
664 * currently entered context. If it is context specific (created
665 * using ::Compile) it will be run in the context in which it was
666 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000667 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000669
670 /**
671 * Returns the script id value.
672 */
673 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000674
675 /**
676 * Associate an additional data object with the script. This is mainly used
677 * with the debugger as this data object is only available through the
678 * debugger API.
679 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000680 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681};
682
683
684/**
685 * An error message.
686 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000687class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000689 Local<String> Get() const;
690 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000692 /**
693 * Returns the resource name for the script from where the function causing
694 * the error originates.
695 */
ager@chromium.org32912102009-01-16 10:38:43 +0000696 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000698 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000699 * Returns the resource data for the script from where the function causing
700 * the error originates.
701 */
702 Handle<Value> GetScriptData() const;
703
704 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000705 * Exception stack trace. By default stack traces are not captured for
706 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
707 * to change this option.
708 */
709 Handle<StackTrace> GetStackTrace() const;
710
711 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000712 * Returns the number, 1-based, of the line where the error occurred.
713 */
ager@chromium.org32912102009-01-16 10:38:43 +0000714 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000716 /**
717 * Returns the index within the script of the first character where
718 * the error occurred.
719 */
ager@chromium.org32912102009-01-16 10:38:43 +0000720 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000721
722 /**
723 * Returns the index within the script of the last character where
724 * the error occurred.
725 */
ager@chromium.org32912102009-01-16 10:38:43 +0000726 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000727
728 /**
729 * Returns the index within the line of the first character where
730 * the error occurred.
731 */
ager@chromium.org32912102009-01-16 10:38:43 +0000732 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000733
734 /**
735 * Returns the index within the line of the last character where
736 * the error occurred.
737 */
ager@chromium.org32912102009-01-16 10:38:43 +0000738 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000739
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740 // TODO(1245381): Print to a string instead of on a FILE.
741 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000742
743 static const int kNoLineNumberInfo = 0;
744 static const int kNoColumnInfo = 0;
745};
746
747
748/**
749 * Representation of a JavaScript stack trace. The information collected is a
750 * snapshot of the execution stack and the information remains valid after
751 * execution continues.
752 */
753class V8EXPORT StackTrace {
754 public:
755 /**
756 * Flags that determine what information is placed captured for each
757 * StackFrame when grabbing the current stack trace.
758 */
759 enum StackTraceOptions {
760 kLineNumber = 1,
761 kColumnOffset = 1 << 1 | kLineNumber,
762 kScriptName = 1 << 2,
763 kFunctionName = 1 << 3,
764 kIsEval = 1 << 4,
765 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000766 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000767 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000768 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000769 };
770
771 /**
772 * Returns a StackFrame at a particular index.
773 */
774 Local<StackFrame> GetFrame(uint32_t index) const;
775
776 /**
777 * Returns the number of StackFrames.
778 */
779 int GetFrameCount() const;
780
781 /**
782 * Returns StackTrace as a v8::Array that contains StackFrame objects.
783 */
784 Local<Array> AsArray();
785
786 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000787 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000788 *
789 * \param frame_limit The maximum number of stack frames we want to capture.
790 * \param options Enumerates the set of things we will capture for each
791 * StackFrame.
792 */
793 static Local<StackTrace> CurrentStackTrace(
794 int frame_limit,
795 StackTraceOptions options = kOverview);
796};
797
798
799/**
800 * A single JavaScript stack frame.
801 */
802class V8EXPORT StackFrame {
803 public:
804 /**
805 * Returns the number, 1-based, of the line for the associate function call.
806 * This method will return Message::kNoLineNumberInfo if it is unable to
807 * retrieve the line number, or if kLineNumber was not passed as an option
808 * when capturing the StackTrace.
809 */
810 int GetLineNumber() const;
811
812 /**
813 * Returns the 1-based column offset on the line for the associated function
814 * call.
815 * This method will return Message::kNoColumnInfo if it is unable to retrieve
816 * the column number, or if kColumnOffset was not passed as an option when
817 * capturing the StackTrace.
818 */
819 int GetColumn() const;
820
821 /**
822 * Returns the name of the resource that contains the script for the
823 * function for this StackFrame.
824 */
825 Local<String> GetScriptName() const;
826
827 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000828 * Returns the name of the resource that contains the script for the
829 * function for this StackFrame or sourceURL value if the script name
830 * is undefined and its source ends with //@ sourceURL=... string.
831 */
832 Local<String> GetScriptNameOrSourceURL() const;
833
834 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000835 * Returns the name of the function associated with this stack frame.
836 */
837 Local<String> GetFunctionName() const;
838
839 /**
840 * Returns whether or not the associated function is compiled via a call to
841 * eval().
842 */
843 bool IsEval() const;
844
845 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000846 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000847 * constructor via "new".
848 */
849 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850};
851
852
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000853// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854
855
856/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000857 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000859class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 public:
861
862 /**
863 * Returns true if this value is the undefined value. See ECMA-262
864 * 4.3.10.
865 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000866 V8EXPORT bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
868 /**
869 * Returns true if this value is the null value. See ECMA-262
870 * 4.3.11.
871 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000872 V8EXPORT bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873
874 /**
875 * Returns true if this value is true.
876 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000877 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878
879 /**
880 * Returns true if this value is false.
881 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000882 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
884 /**
885 * Returns true if this value is an instance of the String type.
886 * See ECMA-262 8.4.
887 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000888 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
890 /**
891 * Returns true if this value is a function.
892 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000893 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894
895 /**
896 * Returns true if this value is an array.
897 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000898 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
v8.team.kasperl727e9952008-09-02 14:56:44 +0000900 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 * Returns true if this value is an object.
902 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000903 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904
v8.team.kasperl727e9952008-09-02 14:56:44 +0000905 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 * Returns true if this value is boolean.
907 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000908 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000909
v8.team.kasperl727e9952008-09-02 14:56:44 +0000910 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 * Returns true if this value is a number.
912 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000913 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914
v8.team.kasperl727e9952008-09-02 14:56:44 +0000915 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 * Returns true if this value is external.
917 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000918 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919
v8.team.kasperl727e9952008-09-02 14:56:44 +0000920 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 * Returns true if this value is a 32-bit signed integer.
922 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000923 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000925 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000926 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000927 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000928 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000929
930 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000931 * Returns true if this value is a Date.
932 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000933 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000934
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000935 /**
936 * Returns true if this value is a RegExp.
937 */
938 V8EXPORT bool IsRegExp() const;
939
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000940 V8EXPORT Local<Boolean> ToBoolean() const;
941 V8EXPORT Local<Number> ToNumber() const;
942 V8EXPORT Local<String> ToString() const;
943 V8EXPORT Local<String> ToDetailString() const;
944 V8EXPORT Local<Object> ToObject() const;
945 V8EXPORT Local<Integer> ToInteger() const;
946 V8EXPORT Local<Uint32> ToUint32() const;
947 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948
949 /**
950 * Attempts to convert a string to an array index.
951 * Returns an empty handle if the conversion fails.
952 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000953 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000955 V8EXPORT bool BooleanValue() const;
956 V8EXPORT double NumberValue() const;
957 V8EXPORT int64_t IntegerValue() const;
958 V8EXPORT uint32_t Uint32Value() const;
959 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960
961 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000962 V8EXPORT bool Equals(Handle<Value> that) const;
963 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000964
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000965 private:
966 inline bool QuickIsString() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000967 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968};
969
970
971/**
972 * The superclass of primitive values. See ECMA-262 4.3.2.
973 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000974class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975
976
977/**
978 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
979 * or false value.
980 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000981class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000983 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 static inline Handle<Boolean> New(bool value);
985};
986
987
988/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000989 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000991class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000993
994 /**
995 * Returns the number of characters in this string.
996 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000997 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998
v8.team.kasperl727e9952008-09-02 14:56:44 +0000999 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001000 * Returns the number of bytes in the UTF-8 encoded
1001 * representation of this string.
1002 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001003 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001004
1005 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001006 * Write the contents of the string to an external buffer.
1007 * If no arguments are given, expects the buffer to be large
1008 * enough to hold the entire string and NULL terminator. Copies
1009 * the contents of the string and the NULL terminator into the
1010 * buffer.
1011 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001012 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1013 * before the end of the buffer.
1014 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001015 * Copies up to length characters into the output buffer.
1016 * Only null-terminates if there is enough space in the buffer.
1017 *
1018 * \param buffer The buffer into which the string will be copied.
1019 * \param start The starting position within the string at which
1020 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001021 * \param length The number of characters to copy from the string. For
1022 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001023 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001024 * \param hints Various hints that might affect performance of this or
1025 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001026 * \return The number of characters copied to the buffer excluding the null
1027 * terminator. For WriteUtf8: The number of bytes copied to the buffer
1028 * including the null terminator.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001029 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001030 enum WriteHints {
1031 NO_HINTS = 0,
1032 HINT_MANY_WRITES_EXPECTED = 1
1033 };
1034
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001035 V8EXPORT int Write(uint16_t* buffer,
1036 int start = 0,
1037 int length = -1,
1038 WriteHints hints = NO_HINTS) const; // UTF-16
1039 V8EXPORT int WriteAscii(char* buffer,
1040 int start = 0,
1041 int length = -1,
1042 WriteHints hints = NO_HINTS) const; // ASCII
1043 V8EXPORT int WriteUtf8(char* buffer,
1044 int length = -1,
1045 int* nchars_ref = NULL,
1046 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047
v8.team.kasperl727e9952008-09-02 14:56:44 +00001048 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001049 * A zero length string.
1050 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001051 V8EXPORT static v8::Local<v8::String> Empty();
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001052
1053 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001054 * Returns true if the string is external
1055 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001056 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057
v8.team.kasperl727e9952008-09-02 14:56:44 +00001058 /**
1059 * Returns true if the string is both external and ascii
1060 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001061 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001062
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001063 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001064 public:
1065 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001066
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001067 protected:
1068 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001069
1070 /**
1071 * Internally V8 will call this Dispose method when the external string
1072 * resource is no longer needed. The default implementation will use the
1073 * delete operator. This method can be overridden in subclasses to
1074 * control how allocated external string resources are disposed.
1075 */
1076 virtual void Dispose() { delete this; }
1077
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001078 private:
1079 // Disallow copying and assigning.
1080 ExternalStringResourceBase(const ExternalStringResourceBase&);
1081 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001082
1083 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001084 };
1085
v8.team.kasperl727e9952008-09-02 14:56:44 +00001086 /**
1087 * An ExternalStringResource is a wrapper around a two-byte string
1088 * buffer that resides outside V8's heap. Implement an
1089 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001090 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001091 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001092 class V8EXPORT ExternalStringResource
1093 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 public:
1095 /**
1096 * Override the destructor to manage the life cycle of the underlying
1097 * buffer.
1098 */
1099 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001100
1101 /**
1102 * The string data from the underlying buffer.
1103 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001105
1106 /**
1107 * The length of the string. That is, the number of two-byte characters.
1108 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 protected:
1112 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 };
1114
1115 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001116 * An ExternalAsciiStringResource is a wrapper around an ascii
1117 * string buffer that resides outside V8's heap. Implement an
1118 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001119 * underlying buffer. Note that the string data must be immutable
1120 * and that the data must be strict 7-bit ASCII, not Latin1 or
1121 * UTF-8, which would require special treatment internally in the
1122 * engine and, in the case of UTF-8, do not allow efficient indexing.
1123 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001124 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001126 class V8EXPORT ExternalAsciiStringResource
1127 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128 public:
1129 /**
1130 * Override the destructor to manage the life cycle of the underlying
1131 * buffer.
1132 */
1133 virtual ~ExternalAsciiStringResource() {}
1134 /** The string data from the underlying buffer.*/
1135 virtual const char* data() const = 0;
1136 /** The number of ascii characters in the string.*/
1137 virtual size_t length() const = 0;
1138 protected:
1139 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 };
1141
1142 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001143 * Get the ExternalStringResource for an external string. Returns
1144 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001146 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147
1148 /**
1149 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001150 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001152 V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001154 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155
1156 /**
1157 * Allocates a new string from either utf-8 encoded or ascii data.
1158 * The second parameter 'length' gives the buffer length.
1159 * If the data is utf-8 encoded, the caller must
1160 * be careful to supply the length parameter.
1161 * If it is not given, the function calls
1162 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001163 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001165 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166
1167 /** Allocates a new string from utf16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001168 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169
1170 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001171 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172
v8.team.kasperl727e9952008-09-02 14:56:44 +00001173 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001174 * Creates a new string by concatenating the left and the right strings
1175 * passed in as parameters.
1176 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001177 V8EXPORT static Local<String> Concat(Handle<String> left,
1178 Handle<String>right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001179
1180 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001181 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001182 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001183 * resource will be disposed by calling its Dispose method. The caller of
1184 * this function should not otherwise delete or modify the resource. Neither
1185 * should the underlying buffer be deallocated or modified except through the
1186 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001187 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001188 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001189
ager@chromium.org6f10e412009-02-13 10:11:16 +00001190 /**
1191 * Associate an external string resource with this string by transforming it
1192 * in place so that existing references to this string in the JavaScript heap
1193 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001194 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001195 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001196 * The string is not modified if the operation fails. See NewExternal for
1197 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001198 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001199 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200
v8.team.kasperl727e9952008-09-02 14:56:44 +00001201 /**
1202 * Creates a new external string using the ascii data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001203 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001204 * resource will be disposed by calling its Dispose method. The caller of
1205 * this function should not otherwise delete or modify the resource. Neither
1206 * should the underlying buffer be deallocated or modified except through the
1207 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001208 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001209 V8EXPORT static Local<String> NewExternal(
1210 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001211
ager@chromium.org6f10e412009-02-13 10:11:16 +00001212 /**
1213 * Associate an external string resource with this string by transforming it
1214 * in place so that existing references to this string in the JavaScript heap
1215 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001216 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001217 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001218 * The string is not modified if the operation fails. See NewExternal for
1219 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001220 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001221 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001223 /**
1224 * Returns true if this string can be made external.
1225 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001226 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001227
kasper.lund7276f142008-07-30 08:49:36 +00001228 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001229 V8EXPORT static Local<String> NewUndetectable(const char* data,
1230 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231
kasper.lund7276f142008-07-30 08:49:36 +00001232 /** Creates an undetectable string from the supplied utf-16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001233 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1234 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235
1236 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001237 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001238 * you want to print the object. If conversion to a string fails
1239 * (eg. due to an exception in the toString() method of the object)
1240 * then the length() method returns 0 and the * operator returns
1241 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001242 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001243 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001244 public:
1245 explicit Utf8Value(Handle<v8::Value> obj);
1246 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001247 char* operator*() { return str_; }
1248 const char* operator*() const { return str_; }
1249 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001250 private:
1251 char* str_;
1252 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001253
1254 // Disallow copying and assigning.
1255 Utf8Value(const Utf8Value&);
1256 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001257 };
1258
1259 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 * Converts an object to an ascii string.
1261 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001262 * If conversion to a string fails (eg. due to an exception in the toString()
1263 * method of the object) then the length() method returns 0 and the * operator
1264 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001266 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 public:
1268 explicit AsciiValue(Handle<v8::Value> obj);
1269 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001270 char* operator*() { return str_; }
1271 const char* operator*() const { return str_; }
1272 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 private:
1274 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001275 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001276
1277 // Disallow copying and assigning.
1278 AsciiValue(const AsciiValue&);
1279 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 };
1281
1282 /**
1283 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001284 * If conversion to a string fails (eg. due to an exception in the toString()
1285 * method of the object) then the length() method returns 0 and the * operator
1286 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001288 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 public:
1290 explicit Value(Handle<v8::Value> obj);
1291 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001292 uint16_t* operator*() { return str_; }
1293 const uint16_t* operator*() const { return str_; }
1294 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295 private:
1296 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001297 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001298
1299 // Disallow copying and assigning.
1300 Value(const Value&);
1301 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001303
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001304 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001305 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1306 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307};
1308
1309
1310/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001311 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001313class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001315 V8EXPORT double Value() const;
1316 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001317 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001319 V8EXPORT Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001320 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321};
1322
1323
1324/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001325 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001327class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001329 V8EXPORT static Local<Integer> New(int32_t value);
1330 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1331 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001332 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001334 V8EXPORT Integer();
1335 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336};
1337
1338
1339/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001340 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001342class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001344 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001346 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347};
1348
1349
1350/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001351 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001353class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001355 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001357 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358};
1359
1360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361enum PropertyAttribute {
1362 None = 0,
1363 ReadOnly = 1 << 0,
1364 DontEnum = 1 << 1,
1365 DontDelete = 1 << 2
1366};
1367
ager@chromium.org3811b432009-10-28 14:53:37 +00001368enum ExternalArrayType {
1369 kExternalByteArray = 1,
1370 kExternalUnsignedByteArray,
1371 kExternalShortArray,
1372 kExternalUnsignedShortArray,
1373 kExternalIntArray,
1374 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001375 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001376 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001377 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001378};
1379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001381 * Accessor[Getter|Setter] are used as callback functions when
1382 * setting|getting a particular property. See Object and ObjectTemplate's
1383 * method SetAccessor.
1384 */
1385typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1386 const AccessorInfo& info);
1387
1388
1389typedef void (*AccessorSetter)(Local<String> property,
1390 Local<Value> value,
1391 const AccessorInfo& info);
1392
1393
1394/**
1395 * Access control specifications.
1396 *
1397 * Some accessors should be accessible across contexts. These
1398 * accessors have an explicit access control parameter which specifies
1399 * the kind of cross-context access that should be allowed.
1400 *
1401 * Additionally, for security, accessors can prohibit overwriting by
1402 * accessors defined in JavaScript. For objects that have such
1403 * accessors either locally or in their prototype chain it is not
1404 * possible to overwrite the accessor by using __defineGetter__ or
1405 * __defineSetter__ from JavaScript code.
1406 */
1407enum AccessControl {
1408 DEFAULT = 0,
1409 ALL_CAN_READ = 1,
1410 ALL_CAN_WRITE = 1 << 1,
1411 PROHIBITS_OVERWRITING = 1 << 2
1412};
1413
1414
1415/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001416 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001418class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001420 V8EXPORT bool Set(Handle<Value> key,
1421 Handle<Value> value,
1422 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001423
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001424 V8EXPORT bool Set(uint32_t index,
1425 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001426
ager@chromium.orge2902be2009-06-08 12:21:35 +00001427 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001428 // overriding accessors or read-only properties.
1429 //
1430 // Note that if the object has an interceptor the property will be set
1431 // locally, but since the interceptor takes precedence the local property
1432 // will only be returned if the interceptor doesn't return a value.
1433 //
1434 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001435 V8EXPORT bool ForceSet(Handle<Value> key,
1436 Handle<Value> value,
1437 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001438
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001439 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001441 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001442
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 // TODO(1245389): Replace the type-specific versions of these
1444 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001445 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001446
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001447 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001448
1449 // Delete a property on this object bypassing interceptors and
1450 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001451 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001452
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001453 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001454
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001455 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001457 V8EXPORT bool SetAccessor(Handle<String> name,
1458 AccessorGetter getter,
1459 AccessorSetter setter = 0,
1460 Handle<Value> data = Handle<Value>(),
1461 AccessControl settings = DEFAULT,
1462 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001463
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001465 * Returns an array containing the names of the enumerable properties
1466 * of this object, including properties from prototype objects. The
1467 * array returned by this method contains the same values as would
1468 * be enumerated by a for-in statement over this object.
1469 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001470 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001471
1472 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 * Get the prototype object. This does not skip objects marked to
1474 * be skipped by __proto__ and it does not consult the security
1475 * handler.
1476 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001477 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478
1479 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001480 * Set the prototype object. This does not skip objects marked to
1481 * be skipped by __proto__ and it does not consult the security
1482 * handler.
1483 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001484 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001485
1486 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001487 * Finds an instance of the given function template in the prototype
1488 * chain.
1489 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001490 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1491 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001492
1493 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494 * Call builtin Object.prototype.toString on this object.
1495 * This is different from Value::ToString() that may call
1496 * user-defined toString function. This one does not.
1497 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001498 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001500 /**
1501 * Returns the name of the function invoked as a constructor for this object.
1502 */
1503 V8EXPORT Local<String> GetConstructorName();
1504
kasper.lund212ac232008-07-16 07:07:30 +00001505 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001506 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001507 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001508 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001509 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001510 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001512 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001513 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001514
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001515 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001516 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001517
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001519 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001520 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1521 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1522 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523
1524 /**
1525 * If result.IsEmpty() no real property was located in the prototype chain.
1526 * This means interceptors in the prototype chain are not called.
1527 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001528 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1529 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001530
1531 /**
1532 * If result.IsEmpty() no real property was located on the object or
1533 * in the prototype chain.
1534 * This means interceptors in the prototype chain are not called.
1535 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001536 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537
1538 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001539 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540
kasper.lund212ac232008-07-16 07:07:30 +00001541 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001542 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001544 /**
1545 * Turns on access check on the object if the object is an instance of
1546 * a template that has access check callbacks. If an object has no
1547 * access check info, the object cannot be accessed by anyone.
1548 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001549 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001550
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001551 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001552 * Returns the identity hash for this object. The current implementation
1553 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001554 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001555 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001556 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001557 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001558 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001559
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001560 /**
1561 * Access hidden properties on JavaScript objects. These properties are
1562 * hidden from the executing JavaScript and only accessible through the V8
1563 * C++ API. Hidden properties introduced by V8 internally (for example the
1564 * identity hash) are prefixed with "v8::".
1565 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001566 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1567 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1568 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001569
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001570 /**
1571 * Returns true if this is an instance of an api function (one
1572 * created from a function created from a function template) and has
1573 * been modified since it was created. Note that this method is
1574 * conservative and may return true for objects that haven't actually
1575 * been modified.
1576 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001577 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001578
1579 /**
1580 * Clone this object with a fast but shallow copy. Values will point
1581 * to the same values as the original object.
1582 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001583 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001585 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001586 * Returns the context in which the object was created.
1587 */
1588 V8EXPORT Local<Context> CreationContext();
1589
1590 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001591 * Set the backing store of the indexed properties to be managed by the
1592 * embedding layer. Access to the indexed properties will follow the rules
1593 * spelled out in CanvasPixelArray.
1594 * Note: The embedding program still owns the data and needs to ensure that
1595 * the backing store is preserved while V8 has a reference.
1596 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001597 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001598 V8EXPORT bool HasIndexedPropertiesInPixelData();
1599 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1600 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001601
ager@chromium.org3811b432009-10-28 14:53:37 +00001602 /**
1603 * Set the backing store of the indexed properties to be managed by the
1604 * embedding layer. Access to the indexed properties will follow the rules
1605 * spelled out for the CanvasArray subtypes in the WebGL specification.
1606 * Note: The embedding program still owns the data and needs to ensure that
1607 * the backing store is preserved while V8 has a reference.
1608 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001609 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1610 void* data,
1611 ExternalArrayType array_type,
1612 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001613 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1614 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1615 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1616 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001617
lrn@chromium.org1c092762011-05-09 09:42:16 +00001618 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001619 * Checks whether a callback is set by the
1620 * ObjectTemplate::SetCallAsFunctionHandler method.
1621 * When an Object is callable this method returns true.
1622 */
1623 V8EXPORT bool IsCallable();
1624
1625 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00001626 * Call an Object as a function if a callback is set by the
1627 * ObjectTemplate::SetCallAsFunctionHandler method.
1628 */
1629 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1630 int argc,
1631 Handle<Value> argv[]);
1632
1633 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001634 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001635 * ObjectTemplate::SetCallAsFunctionHandler method.
1636 * Note: This method behaves like the Function::NewInstance method.
1637 */
1638 V8EXPORT Local<Value> CallAsConstructor(int argc,
1639 Handle<Value> argv[]);
1640
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001641 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001642 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001644 V8EXPORT Object();
1645 V8EXPORT static void CheckCast(Value* obj);
1646 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1647 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001648
1649 /**
1650 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001651 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001652 */
1653 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654};
1655
1656
1657/**
1658 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1659 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001660class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001662 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663
ager@chromium.org3e875802009-06-29 08:26:34 +00001664 /**
1665 * Clones an element at index |index|. Returns an empty
1666 * handle if cloning fails (for any reason).
1667 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001668 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001669
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001670 /**
1671 * Creates a JavaScript array with the given length. If the length
1672 * is negative the returned array will have length 0.
1673 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001674 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001675
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001676 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001678 V8EXPORT Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001679 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680};
1681
1682
1683/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001684 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001686class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001688 V8EXPORT Local<Object> NewInstance() const;
1689 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1690 V8EXPORT Local<Value> Call(Handle<Object> recv,
1691 int argc,
1692 Handle<Value> argv[]);
1693 V8EXPORT void SetName(Handle<String> name);
1694 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001695
1696 /**
1697 * Returns zero based line number of function body and
1698 * kLineOffsetNotFound if no information available.
1699 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001700 V8EXPORT int GetScriptLineNumber() const;
1701 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001702 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001703 V8EXPORT static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001705 V8EXPORT Function();
1706 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707};
1708
1709
1710/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001711 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1712 */
1713class Date : public Object {
1714 public:
1715 V8EXPORT static Local<Value> New(double time);
1716
1717 /**
1718 * A specialization of Value::NumberValue that is more efficient
1719 * because we know the structure of this object.
1720 */
1721 V8EXPORT double NumberValue() const;
1722
1723 static inline Date* Cast(v8::Value* obj);
1724
1725 /**
1726 * Notification that the embedder has changed the time zone,
1727 * daylight savings time, or other date / time configuration
1728 * parameters. V8 keeps a cache of various values used for
1729 * date / time computation. This notification will reset
1730 * those cached values for the current context so that date /
1731 * time configuration changes would be reflected in the Date
1732 * object.
1733 *
1734 * This API should not be called more than needed as it will
1735 * negatively impact the performance of date operations.
1736 */
1737 V8EXPORT static void DateTimeConfigurationChangeNotification();
1738
1739 private:
1740 V8EXPORT static void CheckCast(v8::Value* obj);
1741};
1742
1743
1744/**
1745 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1746 */
1747class RegExp : public Object {
1748 public:
1749 /**
1750 * Regular expression flag bits. They can be or'ed to enable a set
1751 * of flags.
1752 */
1753 enum Flags {
1754 kNone = 0,
1755 kGlobal = 1,
1756 kIgnoreCase = 2,
1757 kMultiline = 4
1758 };
1759
1760 /**
1761 * Creates a regular expression from the given pattern string and
1762 * the flags bit field. May throw a JavaScript exception as
1763 * described in ECMA-262, 15.10.4.1.
1764 *
1765 * For example,
1766 * RegExp::New(v8::String::New("foo"),
1767 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1768 * is equivalent to evaluating "/foo/gm".
1769 */
1770 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1771 Flags flags);
1772
1773 /**
1774 * Returns the value of the source property: a string representing
1775 * the regular expression.
1776 */
1777 V8EXPORT Local<String> GetSource() const;
1778
1779 /**
1780 * Returns the flags bit field.
1781 */
1782 V8EXPORT Flags GetFlags() const;
1783
1784 static inline RegExp* Cast(v8::Value* obj);
1785
1786 private:
1787 V8EXPORT static void CheckCast(v8::Value* obj);
1788};
1789
1790
1791/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001792 * A JavaScript value that wraps a C++ void*. This type of value is
1793 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001795 *
1796 * The Wrap function V8 will return the most optimal Value object wrapping the
1797 * C++ void*. The type of the value is not guaranteed to be an External object
1798 * and no assumptions about its type should be made. To access the wrapped
1799 * value Unwrap should be used, all other operations on that object will lead
1800 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001802class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001804 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001805 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001806
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001807 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001808 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001809 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001811 V8EXPORT External();
1812 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001813 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001814 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815};
1816
1817
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001818// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819
1820
1821/**
1822 * The superclass of object and function templates.
1823 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001824class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 public:
1826 /** Adds a property to each instance created by this template.*/
1827 void Set(Handle<String> name, Handle<Data> value,
1828 PropertyAttribute attributes = None);
1829 inline void Set(const char* name, Handle<Data> value);
1830 private:
1831 Template();
1832
1833 friend class ObjectTemplate;
1834 friend class FunctionTemplate;
1835};
1836
1837
1838/**
1839 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001840 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841 * including the receiver, the number and values of arguments, and
1842 * the holder of the function.
1843 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001844class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845 public:
1846 inline int Length() const;
1847 inline Local<Value> operator[](int i) const;
1848 inline Local<Function> Callee() const;
1849 inline Local<Object> This() const;
1850 inline Local<Object> Holder() const;
1851 inline bool IsConstructCall() const;
1852 inline Local<Value> Data() const;
1853 private:
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001854 static const int kDataIndex = 0;
1855 static const int kCalleeIndex = -1;
1856 static const int kHolderIndex = -2;
1857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001859 inline Arguments(internal::Object** implicit_args,
1860 internal::Object** values,
1861 int length,
1862 bool is_construct_call);
1863 internal::Object** implicit_args_;
1864 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001866 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867};
1868
1869
1870/**
1871 * The information passed to an accessor callback about the context
1872 * of the property access.
1873 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001874class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001876 inline AccessorInfo(internal::Object** args)
1877 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 inline Local<Value> Data() const;
1879 inline Local<Object> This() const;
1880 inline Local<Object> Holder() const;
1881 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001882 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883};
1884
1885
1886typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889 * NamedProperty[Getter|Setter] are used as interceptors on object.
1890 * See ObjectTemplate::SetNamedPropertyHandler.
1891 */
1892typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1893 const AccessorInfo& info);
1894
1895
1896/**
1897 * Returns the value if the setter intercepts the request.
1898 * Otherwise, returns an empty handle.
1899 */
1900typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1901 Local<Value> value,
1902 const AccessorInfo& info);
1903
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904/**
1905 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001906 * The result is an integer encoding property attributes (like v8::None,
1907 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001909typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1910 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911
1912
1913/**
1914 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001915 * The return value is true if the property could be deleted and false
1916 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 */
1918typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1919 const AccessorInfo& info);
1920
1921/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001922 * Returns an array containing the names of the properties the named
1923 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 */
1925typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1926
v8.team.kasperl727e9952008-09-02 14:56:44 +00001927
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001929 * Returns the value of the property if the getter intercepts the
1930 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 */
1932typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1933 const AccessorInfo& info);
1934
1935
1936/**
1937 * Returns the value if the setter intercepts the request.
1938 * Otherwise, returns an empty handle.
1939 */
1940typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1941 Local<Value> value,
1942 const AccessorInfo& info);
1943
1944
1945/**
1946 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001947 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001949typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
1950 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951
1952/**
1953 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001954 * The return value is true if the property could be deleted and false
1955 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956 */
1957typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1958 const AccessorInfo& info);
1959
v8.team.kasperl727e9952008-09-02 14:56:44 +00001960/**
1961 * Returns an array containing the indices of the properties the
1962 * indexed property getter intercepts.
1963 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1965
1966
1967/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001968 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969 */
1970enum AccessType {
1971 ACCESS_GET,
1972 ACCESS_SET,
1973 ACCESS_HAS,
1974 ACCESS_DELETE,
1975 ACCESS_KEYS
1976};
1977
v8.team.kasperl727e9952008-09-02 14:56:44 +00001978
1979/**
1980 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001981 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001982 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001983typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001984 Local<Value> key,
1985 AccessType type,
1986 Local<Value> data);
1987
v8.team.kasperl727e9952008-09-02 14:56:44 +00001988
1989/**
1990 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001991 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001992 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001993typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 uint32_t index,
1995 AccessType type,
1996 Local<Value> data);
1997
1998
1999/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002000 * A FunctionTemplate is used to create functions at runtime. There
2001 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002002 * context. The lifetime of the created function is equal to the
2003 * lifetime of the context. So in case the embedder needs to create
2004 * temporary functions that can be collected using Scripts is
2005 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 *
2007 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002008 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002010 * A FunctionTemplate has a corresponding instance template which is
2011 * used to create object instances when the function is used as a
2012 * constructor. Properties added to the instance template are added to
2013 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 *
2015 * A FunctionTemplate can have a prototype template. The prototype template
2016 * is used to create the prototype object of the function.
2017 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002018 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002020 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2022 * t->Set("func_property", v8::Number::New(1));
2023 *
2024 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2025 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2026 * proto_t->Set("proto_const", v8::Number::New(2));
2027 *
2028 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2029 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2030 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2031 * instance_t->Set("instance_property", Number::New(3));
2032 *
2033 * v8::Local<v8::Function> function = t->GetFunction();
2034 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002035 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036 *
2037 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002038 * and "instance" for the instance object created above. The function
2039 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002041 * \code
2042 * func_property in function == true;
2043 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002045 * function.prototype.proto_method() invokes 'InvokeCallback'
2046 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002048 * instance instanceof function == true;
2049 * instance.instance_accessor calls 'InstanceAccessorCallback'
2050 * instance.instance_property == 3;
2051 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002053 * A FunctionTemplate can inherit from another one by calling the
2054 * FunctionTemplate::Inherit method. The following graph illustrates
2055 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002057 * \code
2058 * FunctionTemplate Parent -> Parent() . prototype -> { }
2059 * ^ ^
2060 * | Inherit(Parent) | .__proto__
2061 * | |
2062 * FunctionTemplate Child -> Child() . prototype -> { }
2063 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002065 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2066 * object of the Child() function has __proto__ pointing to the
2067 * Parent() function's prototype object. An instance of the Child
2068 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002070 * Let Parent be the FunctionTemplate initialized in the previous
2071 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002073 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 * Local<FunctionTemplate> parent = t;
2075 * Local<FunctionTemplate> child = FunctionTemplate::New();
2076 * child->Inherit(parent);
2077 *
2078 * Local<Function> child_function = child->GetFunction();
2079 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002080 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002082 * The Child function and Child instance will have the following
2083 * properties:
2084 *
2085 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002087 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002089 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002091class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 public:
2093 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002094 static Local<FunctionTemplate> New(
2095 InvocationCallback callback = 0,
2096 Handle<Value> data = Handle<Value>(),
2097 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 /** Returns the unique function instance in the current execution context.*/
2099 Local<Function> GetFunction();
2100
v8.team.kasperl727e9952008-09-02 14:56:44 +00002101 /**
2102 * Set the call-handler callback for a FunctionTemplate. This
2103 * callback is called whenever the function created from this
2104 * FunctionTemplate is called.
2105 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 void SetCallHandler(InvocationCallback callback,
2107 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108
v8.team.kasperl727e9952008-09-02 14:56:44 +00002109 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 Local<ObjectTemplate> InstanceTemplate();
2111
2112 /** Causes the function template to inherit from a parent function template.*/
2113 void Inherit(Handle<FunctionTemplate> parent);
2114
2115 /**
2116 * A PrototypeTemplate is the template used to create the prototype object
2117 * of the function created by this template.
2118 */
2119 Local<ObjectTemplate> PrototypeTemplate();
2120
v8.team.kasperl727e9952008-09-02 14:56:44 +00002121
2122 /**
2123 * Set the class name of the FunctionTemplate. This is used for
2124 * printing objects created with the function created from the
2125 * FunctionTemplate as its constructor.
2126 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127 void SetClassName(Handle<String> name);
2128
2129 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002130 * Determines whether the __proto__ accessor ignores instances of
2131 * the function template. If instances of the function template are
2132 * ignored, __proto__ skips all instances and instead returns the
2133 * next object in the prototype chain.
2134 *
2135 * Call with a value of true to make the __proto__ accessor ignore
2136 * instances of the function template. Call with a value of false
2137 * to make the __proto__ accessor not ignore instances of the
2138 * function template. By default, instances of a function template
2139 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 */
2141 void SetHiddenPrototype(bool value);
2142
2143 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002144 * Returns true if the given object is an instance of this function
2145 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 */
2147 bool HasInstance(Handle<Value> object);
2148
2149 private:
2150 FunctionTemplate();
2151 void AddInstancePropertyAccessor(Handle<String> name,
2152 AccessorGetter getter,
2153 AccessorSetter setter,
2154 Handle<Value> data,
2155 AccessControl settings,
2156 PropertyAttribute attributes);
2157 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2158 NamedPropertySetter setter,
2159 NamedPropertyQuery query,
2160 NamedPropertyDeleter remover,
2161 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002162 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002163 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2164 IndexedPropertySetter setter,
2165 IndexedPropertyQuery query,
2166 IndexedPropertyDeleter remover,
2167 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002168 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2170 Handle<Value> data);
2171
2172 friend class Context;
2173 friend class ObjectTemplate;
2174};
2175
2176
2177/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002178 * An ObjectTemplate is used to create objects at runtime.
2179 *
2180 * Properties added to an ObjectTemplate are added to each object
2181 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002183class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002185 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188 /** Creates a new instance of this template.*/
2189 Local<Object> NewInstance();
2190
2191 /**
2192 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002193 *
2194 * Whenever the property with the given name is accessed on objects
2195 * created from this ObjectTemplate the getter and setter callbacks
2196 * are called instead of getting and setting the property directly
2197 * on the JavaScript object.
2198 *
2199 * \param name The name of the property for which an accessor is added.
2200 * \param getter The callback to invoke when getting the property.
2201 * \param setter The callback to invoke when setting the property.
2202 * \param data A piece of data that will be passed to the getter and setter
2203 * callbacks whenever they are invoked.
2204 * \param settings Access control settings for the accessor. This is a bit
2205 * field consisting of one of more of
2206 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2207 * The default is to not allow cross-context access.
2208 * ALL_CAN_READ means that all cross-context reads are allowed.
2209 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2210 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2211 * cross-context access.
2212 * \param attribute The attributes of the property for which an accessor
2213 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214 */
2215 void SetAccessor(Handle<String> name,
2216 AccessorGetter getter,
2217 AccessorSetter setter = 0,
2218 Handle<Value> data = Handle<Value>(),
2219 AccessControl settings = DEFAULT,
2220 PropertyAttribute attribute = None);
2221
2222 /**
2223 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002224 *
2225 * Whenever a named property is accessed on objects created from
2226 * this object template, the provided callback is invoked instead of
2227 * accessing the property directly on the JavaScript object.
2228 *
2229 * \param getter The callback to invoke when getting a property.
2230 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002231 * \param query The callback to invoke to check if a property is present,
2232 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002233 * \param deleter The callback to invoke when deleting a property.
2234 * \param enumerator The callback to invoke to enumerate all the named
2235 * properties of an object.
2236 * \param data A piece of data that will be passed to the callbacks
2237 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 */
2239 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2240 NamedPropertySetter setter = 0,
2241 NamedPropertyQuery query = 0,
2242 NamedPropertyDeleter deleter = 0,
2243 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002244 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245
2246 /**
2247 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248 *
2249 * Whenever an indexed property is accessed on objects created from
2250 * this object template, the provided callback is invoked instead of
2251 * accessing the property directly on the JavaScript object.
2252 *
2253 * \param getter The callback to invoke when getting a property.
2254 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002255 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002256 * \param deleter The callback to invoke when deleting a property.
2257 * \param enumerator The callback to invoke to enumerate all the indexed
2258 * properties of an object.
2259 * \param data A piece of data that will be passed to the callbacks
2260 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261 */
2262 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2263 IndexedPropertySetter setter = 0,
2264 IndexedPropertyQuery query = 0,
2265 IndexedPropertyDeleter deleter = 0,
2266 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002267 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 /**
2270 * Sets the callback to be used when calling instances created from
2271 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002272 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 * function.
2274 */
2275 void SetCallAsFunctionHandler(InvocationCallback callback,
2276 Handle<Value> data = Handle<Value>());
2277
v8.team.kasperl727e9952008-09-02 14:56:44 +00002278 /**
2279 * Mark object instances of the template as undetectable.
2280 *
2281 * In many ways, undetectable objects behave as though they are not
2282 * there. They behave like 'undefined' in conditionals and when
2283 * printed. However, properties can be accessed and called as on
2284 * normal objects.
2285 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 void MarkAsUndetectable();
2287
v8.team.kasperl727e9952008-09-02 14:56:44 +00002288 /**
2289 * Sets access check callbacks on the object template.
2290 *
2291 * When accessing properties on instances of this object template,
2292 * the access check callback will be called to determine whether or
2293 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002294 * The last parameter specifies whether access checks are turned
2295 * on by default on instances. If access checks are off by default,
2296 * they can be turned on on individual instances by calling
2297 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002298 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2300 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002301 Handle<Value> data = Handle<Value>(),
2302 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303
kasper.lund212ac232008-07-16 07:07:30 +00002304 /**
2305 * Gets the number of internal fields for objects generated from
2306 * this template.
2307 */
2308 int InternalFieldCount();
2309
2310 /**
2311 * Sets the number of internal fields for objects generated from
2312 * this template.
2313 */
2314 void SetInternalFieldCount(int value);
2315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 private:
2317 ObjectTemplate();
2318 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2319 friend class FunctionTemplate;
2320};
2321
2322
2323/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002324 * A Signature specifies which receivers and arguments a function can
2325 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002327class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 public:
2329 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2330 Handle<FunctionTemplate>(),
2331 int argc = 0,
2332 Handle<FunctionTemplate> argv[] = 0);
2333 private:
2334 Signature();
2335};
2336
2337
2338/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002339 * A utility for determining the type of objects based on the template
2340 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002341 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002342class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 public:
2344 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2345 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2346 int match(Handle<Value> value);
2347 private:
2348 TypeSwitch();
2349};
2350
2351
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002352// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353
2354
2355/**
2356 * Ignore
2357 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002358class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359 public:
2360 Extension(const char* name,
2361 const char* source = 0,
2362 int dep_count = 0,
2363 const char** deps = 0);
2364 virtual ~Extension() { }
2365 virtual v8::Handle<v8::FunctionTemplate>
2366 GetNativeFunction(v8::Handle<v8::String> name) {
2367 return v8::Handle<v8::FunctionTemplate>();
2368 }
2369
2370 const char* name() { return name_; }
2371 const char* source() { return source_; }
2372 int dependency_count() { return dep_count_; }
2373 const char** dependencies() { return deps_; }
2374 void set_auto_enable(bool value) { auto_enable_ = value; }
2375 bool auto_enable() { return auto_enable_; }
2376
2377 private:
2378 const char* name_;
2379 const char* source_;
2380 int dep_count_;
2381 const char** deps_;
2382 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002383
2384 // Disallow copying and assigning.
2385 Extension(const Extension&);
2386 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387};
2388
2389
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002390void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391
2392
2393/**
2394 * Ignore
2395 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002396class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 public:
2398 inline DeclareExtension(Extension* extension) {
2399 RegisterExtension(extension);
2400 }
2401};
2402
2403
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002404// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405
2406
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002407Handle<Primitive> V8EXPORT Undefined();
2408Handle<Primitive> V8EXPORT Null();
2409Handle<Boolean> V8EXPORT True();
2410Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002411
2412
2413/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002414 * A set of constraints that specifies the limits of the runtime's memory use.
2415 * You must set the heap size before initializing the VM - the size cannot be
2416 * adjusted after the VM is initialized.
2417 *
2418 * If you are using threads then you should hold the V8::Locker lock while
2419 * setting the stack limit and you must set a non-default stack limit separately
2420 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002422class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002423 public:
2424 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002425 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002427 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002429 int max_executable_size() { return max_executable_size_; }
2430 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002431 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002432 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2434 private:
2435 int max_young_space_size_;
2436 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002437 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 uint32_t* stack_limit_;
2439};
2440
2441
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002442bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443
2444
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002445// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446
2447
2448typedef void (*FatalErrorCallback)(const char* location, const char* message);
2449
2450
2451typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2452
2453
2454/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002455 * Schedules an exception to be thrown when returning to JavaScript. When an
2456 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002457 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002458 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002460Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461
2462/**
2463 * Create new error objects by calling the corresponding error object
2464 * constructor with the message.
2465 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002466class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 public:
2468 static Local<Value> RangeError(Handle<String> message);
2469 static Local<Value> ReferenceError(Handle<String> message);
2470 static Local<Value> SyntaxError(Handle<String> message);
2471 static Local<Value> TypeError(Handle<String> message);
2472 static Local<Value> Error(Handle<String> message);
2473};
2474
2475
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002476// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002478typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002480typedef void* (*CreateHistogramCallback)(const char* name,
2481 int min,
2482 int max,
2483 size_t buckets);
2484
2485typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2486
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002487// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002488 enum ObjectSpace {
2489 kObjectSpaceNewSpace = 1 << 0,
2490 kObjectSpaceOldPointerSpace = 1 << 1,
2491 kObjectSpaceOldDataSpace = 1 << 2,
2492 kObjectSpaceCodeSpace = 1 << 3,
2493 kObjectSpaceMapSpace = 1 << 4,
2494 kObjectSpaceLoSpace = 1 << 5,
2495
2496 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2497 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2498 kObjectSpaceLoSpace
2499 };
2500
2501 enum AllocationAction {
2502 kAllocationActionAllocate = 1 << 0,
2503 kAllocationActionFree = 1 << 1,
2504 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2505 };
2506
2507typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2508 AllocationAction action,
2509 int size);
2510
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002511// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2513 AccessType type,
2514 Local<Value> data);
2515
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002516// --- AllowCodeGenerationFromStrings callbacks ---
2517
2518/**
2519 * Callback to check if code generation from strings is allowed. See
2520 * Context::AllowCodeGenerationFromStrings.
2521 */
2522typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2523
2524// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525
2526/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002527 * Applications can register callback functions which will be called
2528 * before and after a garbage collection. Allocations are not
2529 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002530 * objects (set or delete properties for example) since it is possible
2531 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002532 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002533enum GCType {
2534 kGCTypeScavenge = 1 << 0,
2535 kGCTypeMarkSweepCompact = 1 << 1,
2536 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2537};
2538
2539enum GCCallbackFlags {
2540 kNoGCCallbackFlags = 0,
2541 kGCCallbackFlagCompacted = 1 << 0
2542};
2543
2544typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2545typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2546
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547typedef void (*GCCallback)();
2548
2549
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002551 * Profiler modules.
2552 *
2553 * In V8, profiler consists of several modules: CPU profiler, and different
2554 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002555 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2556 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002557 */
2558enum ProfilerModules {
2559 PROFILER_MODULE_NONE = 0,
2560 PROFILER_MODULE_CPU = 1,
2561 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002562 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2563 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002564};
2565
2566
2567/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002568 * Collection of V8 heap information.
2569 *
2570 * Instances of this class can be passed to v8::V8::HeapStatistics to
2571 * get heap statistics from V8.
2572 */
2573class V8EXPORT HeapStatistics {
2574 public:
2575 HeapStatistics();
2576 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002577 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002578 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002579 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002580
2581 private:
2582 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002583 void set_total_heap_size_executable(size_t size) {
2584 total_heap_size_executable_ = size;
2585 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002586 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002587 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002588
2589 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002590 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002591 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002592 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002593
2594 friend class V8;
2595};
2596
2597
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002598class RetainedObjectInfo;
2599
ager@chromium.org3811b432009-10-28 14:53:37 +00002600/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002601 * Isolate represents an isolated instance of the V8 engine. V8
2602 * isolates have completely separate states. Objects from one isolate
2603 * must not be used in other isolates. When V8 is initialized a
2604 * default isolate is implicitly created and entered. The embedder
2605 * can create additional isolates and use them in parallel in multiple
2606 * threads. An isolate can be entered by at most one thread at any
2607 * given time. The Locker/Unlocker API can be used to synchronize.
2608 */
2609class V8EXPORT Isolate {
2610 public:
2611 /**
2612 * Stack-allocated class which sets the isolate for all operations
2613 * executed within a local scope.
2614 */
2615 class V8EXPORT Scope {
2616 public:
2617 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2618 isolate->Enter();
2619 }
2620
2621 ~Scope() { isolate_->Exit(); }
2622
2623 private:
2624 Isolate* const isolate_;
2625
2626 // Prevent copying of Scope objects.
2627 Scope(const Scope&);
2628 Scope& operator=(const Scope&);
2629 };
2630
2631 /**
2632 * Creates a new isolate. Does not change the currently entered
2633 * isolate.
2634 *
2635 * When an isolate is no longer used its resources should be freed
2636 * by calling Dispose(). Using the delete operator is not allowed.
2637 */
2638 static Isolate* New();
2639
2640 /**
2641 * Returns the entered isolate for the current thread or NULL in
2642 * case there is no current isolate.
2643 */
2644 static Isolate* GetCurrent();
2645
2646 /**
2647 * Methods below this point require holding a lock (using Locker) in
2648 * a multi-threaded environment.
2649 */
2650
2651 /**
2652 * Sets this isolate as the entered one for the current thread.
2653 * Saves the previously entered one (if any), so that it can be
2654 * restored when exiting. Re-entering an isolate is allowed.
2655 */
2656 void Enter();
2657
2658 /**
2659 * Exits this isolate by restoring the previously entered one in the
2660 * current thread. The isolate may still stay the same, if it was
2661 * entered more than once.
2662 *
2663 * Requires: this == Isolate::GetCurrent().
2664 */
2665 void Exit();
2666
2667 /**
2668 * Disposes the isolate. The isolate must not be entered by any
2669 * thread to be disposable.
2670 */
2671 void Dispose();
2672
2673 private:
2674
2675 Isolate();
2676 Isolate(const Isolate&);
2677 ~Isolate();
2678 Isolate& operator=(const Isolate&);
2679 void* operator new(size_t size);
2680 void operator delete(void*, size_t);
2681};
2682
2683
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002684class StartupData {
2685 public:
2686 enum CompressionAlgorithm {
2687 kUncompressed,
2688 kBZip2
2689 };
2690
2691 const char* data;
2692 int compressed_size;
2693 int raw_size;
2694};
2695
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002696/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697 * Container class for static utility functions.
2698 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002699class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002701 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 static void SetFatalErrorHandler(FatalErrorCallback that);
2703
v8.team.kasperl727e9952008-09-02 14:56:44 +00002704 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002705 * Set the callback to invoke to check if code generation from
2706 * strings should be allowed.
2707 */
2708 static void SetAllowCodeGenerationFromStringsCallback(
2709 AllowCodeGenerationFromStringsCallback that);
2710
2711 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002712 * Ignore out-of-memory exceptions.
2713 *
2714 * V8 running out of memory is treated as a fatal error by default.
2715 * This means that the fatal error handler is called and that V8 is
2716 * terminated.
2717 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002718 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002719 * out-of-memory situation as a fatal error. This way, the contexts
2720 * that did not cause the out of memory problem might be able to
2721 * continue execution.
2722 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723 static void IgnoreOutOfMemoryException();
2724
v8.team.kasperl727e9952008-09-02 14:56:44 +00002725 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002726 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002727 * fatal errors such as out-of-memory situations.
2728 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729 static bool IsDead();
2730
2731 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002732 * The following 4 functions are to be used when V8 is built with
2733 * the 'compress_startup_data' flag enabled. In this case, the
2734 * embedder must decompress startup data prior to initializing V8.
2735 *
2736 * This is how interaction with V8 should look like:
2737 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2738 * v8::StartupData* compressed_data =
2739 * new v8::StartupData[compressed_data_count];
2740 * v8::V8::GetCompressedStartupData(compressed_data);
2741 * ... decompress data (compressed_data can be updated in-place) ...
2742 * v8::V8::SetDecompressedStartupData(compressed_data);
2743 * ... now V8 can be initialized
2744 * ... make sure the decompressed data stays valid until V8 shutdown
2745 */
2746 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2747 static int GetCompressedStartupDataCount();
2748 static void GetCompressedStartupData(StartupData* compressed_data);
2749 static void SetDecompressedStartupData(StartupData* decompressed_data);
2750
2751 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002752 * Adds a message listener.
2753 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002754 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00002755 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002756 */
2757 static bool AddMessageListener(MessageCallback that,
2758 Handle<Value> data = Handle<Value>());
2759
2760 /**
2761 * Remove all message listeners from the specified callback function.
2762 */
2763 static void RemoveMessageListeners(MessageCallback that);
2764
2765 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002766 * Tells V8 to capture current stack trace when uncaught exception occurs
2767 * and report it to the message listeners. The option is off by default.
2768 */
2769 static void SetCaptureStackTraceForUncaughtExceptions(
2770 bool capture,
2771 int frame_limit = 10,
2772 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2773
2774 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002775 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776 */
2777 static void SetFlagsFromString(const char* str, int length);
2778
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002779 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002780 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002781 */
2782 static void SetFlagsFromCommandLine(int* argc,
2783 char** argv,
2784 bool remove_flags);
2785
kasper.lund7276f142008-07-30 08:49:36 +00002786 /** Get the version string. */
2787 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788
2789 /**
2790 * Enables the host application to provide a mechanism for recording
2791 * statistics counters.
2792 */
2793 static void SetCounterFunction(CounterLookupCallback);
2794
2795 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002796 * Enables the host application to provide a mechanism for recording
2797 * histograms. The CreateHistogram function returns a
2798 * histogram which will later be passed to the AddHistogramSample
2799 * function.
2800 */
2801 static void SetCreateHistogramFunction(CreateHistogramCallback);
2802 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2803
2804 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805 * Enables the computation of a sliding window of states. The sliding
2806 * window information is recorded in statistics counters.
2807 */
2808 static void EnableSlidingStateWindow();
2809
2810 /** Callback function for reporting failed access checks.*/
2811 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2812
2813 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002814 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002815 * garbage collection. Allocations are not allowed in the
2816 * callback function, you therefore cannot manipulate objects (set
2817 * or delete properties for example) since it is possible such
2818 * operations will result in the allocation of objects. It is possible
2819 * to specify the GCType filter for your callback. But it is not possible to
2820 * register the same callback function two times with different
2821 * GCType filters.
2822 */
2823 static void AddGCPrologueCallback(
2824 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2825
2826 /**
2827 * This function removes callback which was installed by
2828 * AddGCPrologueCallback function.
2829 */
2830 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2831
2832 /**
2833 * The function is deprecated. Please use AddGCPrologueCallback instead.
2834 * Enables the host application to receive a notification before a
2835 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002836 * 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.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839 */
2840 static void SetGlobalGCPrologueCallback(GCCallback);
2841
2842 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002843 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002844 * garbage collection. Allocations are not allowed in the
2845 * callback function, you therefore cannot manipulate objects (set
2846 * or delete properties for example) since it is possible such
2847 * operations will result in the allocation of objects. It is possible
2848 * to specify the GCType filter for your callback. But it is not possible to
2849 * register the same callback function two times with different
2850 * GCType filters.
2851 */
2852 static void AddGCEpilogueCallback(
2853 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2854
2855 /**
2856 * This function removes callback which was installed by
2857 * AddGCEpilogueCallback function.
2858 */
2859 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2860
2861 /**
2862 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2863 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002864 * major garbage collection. Allocations are not allowed in the
2865 * callback function, you therefore cannot manipulate objects (set
2866 * or delete properties for example) since it is possible such
2867 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868 */
2869 static void SetGlobalGCEpilogueCallback(GCCallback);
2870
2871 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002872 * Enables the host application to provide a mechanism to be notified
2873 * and perform custom logging when V8 Allocates Executable Memory.
2874 */
2875 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
2876 ObjectSpace space,
2877 AllocationAction action);
2878
2879 /**
2880 * This function removes callback which was installed by
2881 * AddMemoryAllocationCallback function.
2882 */
2883 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
2884
2885 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002886 * Allows the host application to group objects together. If one
2887 * object in the group is alive, all objects in the group are alive.
2888 * After each garbage collection, object groups are removed. It is
2889 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002890 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002891 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002892 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002893 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002894 static void AddObjectGroup(Persistent<Value>* objects,
2895 size_t length,
2896 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002897
2898 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002899 * Allows the host application to declare implicit references between
2900 * the objects: if |parent| is alive, all |children| are alive too.
2901 * After each garbage collection, all implicit references
2902 * are removed. It is intended to be used in the before-garbage-collection
2903 * callback function.
2904 */
2905 static void AddImplicitReferences(Persistent<Object> parent,
2906 Persistent<Value>* children,
2907 size_t length);
2908
2909 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002910 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002911 * initialize from scratch. This function is called implicitly if
2912 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002913 */
2914 static bool Initialize();
2915
kasper.lund7276f142008-07-30 08:49:36 +00002916 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002917 * Adjusts the amount of registered external memory. Used to give
2918 * V8 an indication of the amount of externally allocated memory
2919 * that is kept alive by JavaScript objects. V8 uses this to decide
2920 * when to perform global garbage collections. Registering
2921 * externally allocated memory will trigger global garbage
2922 * collections more often than otherwise in an attempt to garbage
2923 * collect the JavaScript objects keeping the externally allocated
2924 * memory alive.
2925 *
2926 * \param change_in_bytes the change in externally allocated memory
2927 * that is kept alive by JavaScript objects.
2928 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002929 */
2930 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2931
iposva@chromium.org245aa852009-02-10 00:49:54 +00002932 /**
2933 * Suspends recording of tick samples in the profiler.
2934 * When the V8 profiling mode is enabled (usually via command line
2935 * switches) this function suspends recording of tick samples.
2936 * Profiling ticks are discarded until ResumeProfiler() is called.
2937 *
2938 * See also the --prof and --prof_auto command line switches to
2939 * enable V8 profiling.
2940 */
2941 static void PauseProfiler();
2942
2943 /**
2944 * Resumes recording of tick samples in the profiler.
2945 * See also PauseProfiler().
2946 */
2947 static void ResumeProfiler();
2948
ager@chromium.org41826e72009-03-30 13:30:57 +00002949 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002950 * Return whether profiler is currently paused.
2951 */
2952 static bool IsProfilerPaused();
2953
2954 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002955 * Resumes specified profiler modules. Can be called several times to
2956 * mark the opening of a profiler events block with the given tag.
2957 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002958 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2959 * See ProfilerModules enum.
2960 *
2961 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002962 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002963 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002964 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002965
2966 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002967 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2968 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2969 * same tag value. There is no need for blocks to be properly nested.
2970 * The profiler is paused when the last opened block is closed.
2971 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002972 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2973 * See ProfilerModules enum.
2974 *
2975 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002976 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002977 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002978 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002979
2980 /**
2981 * Returns active (resumed) profiler modules.
2982 * See ProfilerModules enum.
2983 *
2984 * \returns active profiler modules.
2985 */
2986 static int GetActiveProfilerModules();
2987
2988 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002989 * If logging is performed into a memory buffer (via --logfile=*), allows to
2990 * retrieve previously written messages. This can be used for retrieving
2991 * profiler log data in the application. This function is thread-safe.
2992 *
2993 * Caller provides a destination buffer that must exist during GetLogLines
2994 * call. Only whole log lines are copied into the buffer.
2995 *
2996 * \param from_pos specified a point in a buffer to read from, 0 is the
2997 * beginning of a buffer. It is assumed that caller updates its current
2998 * position using returned size value from the previous call.
2999 * \param dest_buf destination buffer for log data.
3000 * \param max_size size of the destination buffer.
3001 * \returns actual size of log data copied into buffer.
3002 */
3003 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
3004
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003005 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003006 * The minimum allowed size for a log lines buffer. If the size of
3007 * the buffer given will not be enough to hold a line of the maximum
3008 * length, an attempt to find a log line end in GetLogLines will
3009 * fail, and an empty result will be returned.
3010 */
3011 static const int kMinimumSizeForLogLinesBuffer = 2048;
3012
3013 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003014 * Retrieve the V8 thread id of the calling thread.
3015 *
3016 * The thread id for a thread should only be retrieved after the V8
3017 * lock has been acquired with a Locker object with that thread.
3018 */
3019 static int GetCurrentThreadId();
3020
3021 /**
3022 * Forcefully terminate execution of a JavaScript thread. This can
3023 * be used to terminate long-running scripts.
3024 *
3025 * TerminateExecution should only be called when then V8 lock has
3026 * been acquired with a Locker object. Therefore, in order to be
3027 * able to terminate long-running threads, preemption must be
3028 * enabled to allow the user of TerminateExecution to acquire the
3029 * lock.
3030 *
3031 * The termination is achieved by throwing an exception that is
3032 * uncatchable by JavaScript exception handlers. Termination
3033 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003034 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003035 * exception handler that catches an exception should check if that
3036 * exception is a termination exception and immediately return if
3037 * that is the case. Returning immediately in that case will
3038 * continue the propagation of the termination exception if needed.
3039 *
3040 * The thread id passed to TerminateExecution must have been
3041 * obtained by calling GetCurrentThreadId on the thread in question.
3042 *
3043 * \param thread_id The thread id of the thread to terminate.
3044 */
3045 static void TerminateExecution(int thread_id);
3046
3047 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003048 * Forcefully terminate the current thread of JavaScript execution
3049 * in the given isolate. If no isolate is provided, the default
3050 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003051 *
3052 * This method can be used by any thread even if that thread has not
3053 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003054 *
3055 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003056 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003057 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003058
3059 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003060 * Is V8 terminating JavaScript execution.
3061 *
3062 * Returns true if JavaScript execution is currently terminating
3063 * because of a call to TerminateExecution. In that case there are
3064 * still JavaScript frames on the stack and the termination
3065 * exception is still active.
3066 */
3067 static bool IsExecutionTerminating();
3068
3069 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003070 * Releases any resources used by v8 and stops any utility threads
3071 * that may be running. Note that disposing v8 is permanent, it
3072 * cannot be reinitialized.
3073 *
3074 * It should generally not be necessary to dispose v8 before exiting
3075 * a process, this should happen automatically. It is only necessary
3076 * to use if the process needs the resources taken up by v8.
3077 */
3078 static bool Dispose();
3079
ager@chromium.org3811b432009-10-28 14:53:37 +00003080 /**
3081 * Get statistics about the heap memory usage.
3082 */
3083 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003084
3085 /**
3086 * Optional notification that the embedder is idle.
3087 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003088 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003089 * Returns true if the embedder should stop calling IdleNotification
3090 * until real work has been done. This indicates that V8 has done
3091 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003092 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003093 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003094
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003095 /**
3096 * Optional notification that the system is running low on memory.
3097 * V8 uses these notifications to attempt to free memory.
3098 */
3099 static void LowMemoryNotification();
3100
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003101 /**
3102 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003103 * these notifications to guide the GC heuristic. Returns the number
3104 * of context disposals - including this one - since the last time
3105 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003106 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003107 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109 private:
3110 V8();
3111
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003112 static internal::Object** GlobalizeReference(internal::Object** handle);
3113 static void DisposeGlobal(internal::Object** global_handle);
3114 static void MakeWeak(internal::Object** global_handle,
3115 void* data,
3116 WeakReferenceCallback);
3117 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003118 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003119 static bool IsGlobalNearDeath(internal::Object** global_handle);
3120 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003121 static void SetWrapperClassId(internal::Object** global_handle,
3122 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123
3124 template <class T> friend class Handle;
3125 template <class T> friend class Local;
3126 template <class T> friend class Persistent;
3127 friend class Context;
3128};
3129
3130
3131/**
3132 * An external exception handler.
3133 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003134class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135 public:
3136
3137 /**
3138 * Creates a new try/catch block and registers it with v8.
3139 */
3140 TryCatch();
3141
3142 /**
3143 * Unregisters and deletes this try/catch block.
3144 */
3145 ~TryCatch();
3146
3147 /**
3148 * Returns true if an exception has been caught by this try/catch block.
3149 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003150 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003151
3152 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003153 * For certain types of exceptions, it makes no sense to continue
3154 * execution.
3155 *
3156 * Currently, the only type of exception that can be caught by a
3157 * TryCatch handler and for which it does not make sense to continue
3158 * is termination exception. Such exceptions are thrown when the
3159 * TerminateExecution methods are called to terminate a long-running
3160 * script.
3161 *
3162 * If CanContinue returns false, the correct action is to perform
3163 * any C++ cleanup needed and then return.
3164 */
3165 bool CanContinue() const;
3166
3167 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003168 * Throws the exception caught by this TryCatch in a way that avoids
3169 * it being caught again by this same TryCatch. As with ThrowException
3170 * it is illegal to execute any JavaScript operations after calling
3171 * ReThrow; the caller must return immediately to where the exception
3172 * is caught.
3173 */
3174 Handle<Value> ReThrow();
3175
3176 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003177 * Returns the exception caught by this try/catch block. If no exception has
3178 * been caught an empty handle is returned.
3179 *
3180 * The returned handle is valid until this TryCatch block has been destroyed.
3181 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003182 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003183
3184 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003185 * Returns the .stack property of the thrown object. If no .stack
3186 * property is present an empty handle is returned.
3187 */
3188 Local<Value> StackTrace() const;
3189
3190 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003191 * Returns the message associated with this exception. If there is
3192 * no message associated an empty handle is returned.
3193 *
3194 * The returned handle is valid until this TryCatch block has been
3195 * destroyed.
3196 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003197 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003198
3199 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003200 * Clears any exceptions that may have been caught by this try/catch block.
3201 * After this method has been called, HasCaught() will return false.
3202 *
3203 * It is not necessary to clear a try/catch block before using it again; if
3204 * another exception is thrown the previously caught exception will just be
3205 * overwritten. However, it is often a good idea since it makes it easier
3206 * to determine which operation threw a given exception.
3207 */
3208 void Reset();
3209
v8.team.kasperl727e9952008-09-02 14:56:44 +00003210 /**
3211 * Set verbosity of the external exception handler.
3212 *
3213 * By default, exceptions that are caught by an external exception
3214 * handler are not reported. Call SetVerbose with true on an
3215 * external exception handler to have exceptions caught by the
3216 * handler reported as if they were not caught.
3217 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 void SetVerbose(bool value);
3219
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003220 /**
3221 * Set whether or not this TryCatch should capture a Message object
3222 * which holds source information about where the exception
3223 * occurred. True by default.
3224 */
3225 void SetCaptureMessage(bool value);
3226
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003227 private:
3228 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003229 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003230 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003231 bool is_verbose_ : 1;
3232 bool can_continue_ : 1;
3233 bool capture_message_ : 1;
3234 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003235
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003236 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003237};
3238
3239
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003240// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241
3242
3243/**
3244 * Ignore
3245 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003246class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003247 public:
3248 ExtensionConfiguration(int name_count, const char* names[])
3249 : name_count_(name_count), names_(names) { }
3250 private:
3251 friend class ImplementationUtilities;
3252 int name_count_;
3253 const char** names_;
3254};
3255
3256
3257/**
3258 * A sandboxed execution context with its own set of built-in objects
3259 * and functions.
3260 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003261class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003262 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003263 /**
3264 * Returns the global proxy object or global object itself for
3265 * detached contexts.
3266 *
3267 * Global proxy object is a thin wrapper whose prototype points to
3268 * actual context's global object with the properties like Object, etc.
3269 * This is done that way for security reasons (for more details see
3270 * https://wiki.mozilla.org/Gecko:SplitWindow).
3271 *
3272 * Please note that changes to global proxy object prototype most probably
3273 * would break VM---v8 expects only global object as a prototype of
3274 * global proxy object.
3275 *
3276 * If DetachGlobal() has been invoked, Global() would return actual global
3277 * object until global is reattached with ReattachGlobal().
3278 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003279 Local<Object> Global();
3280
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003281 /**
3282 * Detaches the global object from its context before
3283 * the global object can be reused to create a new context.
3284 */
3285 void DetachGlobal();
3286
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003287 /**
3288 * Reattaches a global object to a context. This can be used to
3289 * restore the connection between a global object and a context
3290 * after DetachGlobal has been called.
3291 *
3292 * \param global_object The global object to reattach to the
3293 * context. For this to work, the global object must be the global
3294 * object that was associated with this context before a call to
3295 * DetachGlobal.
3296 */
3297 void ReattachGlobal(Handle<Object> global_object);
3298
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003299 /** Creates a new context.
3300 *
3301 * Returns a persistent handle to the newly allocated context. This
3302 * persistent handle has to be disposed when the context is no
3303 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003304 *
3305 * \param extensions An optional extension configuration containing
3306 * the extensions to be installed in the newly created context.
3307 *
3308 * \param global_template An optional object template from which the
3309 * global object for the newly created context will be created.
3310 *
3311 * \param global_object An optional global object to be reused for
3312 * the newly created context. This global object must have been
3313 * created by a previous call to Context::New with the same global
3314 * template. The state of the global object will be completely reset
3315 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003316 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003317 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003318 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003319 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3320 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003321
kasper.lund44510672008-07-25 07:37:58 +00003322 /** Returns the last entered context. */
3323 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003324
kasper.lund44510672008-07-25 07:37:58 +00003325 /** Returns the context that is on the top of the stack. */
3326 static Local<Context> GetCurrent();
3327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003328 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003329 * Returns the context of the calling JavaScript code. That is the
3330 * context of the top-most JavaScript frame. If there are no
3331 * JavaScript frames an empty handle is returned.
3332 */
3333 static Local<Context> GetCalling();
3334
3335 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003336 * Sets the security token for the context. To access an object in
3337 * another context, the security tokens must match.
3338 */
3339 void SetSecurityToken(Handle<Value> token);
3340
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003341 /** Restores the security token to the default value. */
3342 void UseDefaultSecurityToken();
3343
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003344 /** Returns the security token of this context.*/
3345 Handle<Value> GetSecurityToken();
3346
v8.team.kasperl727e9952008-09-02 14:56:44 +00003347 /**
3348 * Enter this context. After entering a context, all code compiled
3349 * and run is compiled and run in this context. If another context
3350 * is already entered, this old context is saved so it can be
3351 * restored when the new context is exited.
3352 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003353 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003354
3355 /**
3356 * Exit this context. Exiting the current context restores the
3357 * context that was in place when entering the current context.
3358 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359 void Exit();
3360
v8.team.kasperl727e9952008-09-02 14:56:44 +00003361 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003362 bool HasOutOfMemoryException();
3363
v8.team.kasperl727e9952008-09-02 14:56:44 +00003364 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365 static bool InContext();
3366
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003367 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003368 * Associate an additional data object with the context. This is mainly used
3369 * with the debugger to provide additional information on the context through
3370 * the debugger API.
3371 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003372 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003373 Local<Value> GetData();
3374
3375 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003376 * Control whether code generation from strings is allowed. Calling
3377 * this method with false will disable 'eval' and the 'Function'
3378 * constructor for code running in this context. If 'eval' or the
3379 * 'Function' constructor are used an exception will be thrown.
3380 *
3381 * If code generation from strings is not allowed the
3382 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3383 * set before blocking the call to 'eval' or the 'Function'
3384 * constructor. If that callback returns true, the call will be
3385 * allowed, otherwise an exception will be thrown. If no callback is
3386 * set an exception will be thrown.
3387 */
3388 void AllowCodeGenerationFromStrings(bool allow);
3389
3390 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003391 * Stack-allocated class which sets the execution context for all
3392 * operations executed within a local scope.
3393 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003394 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003395 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003396 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003397 context_->Enter();
3398 }
3399 inline ~Scope() { context_->Exit(); }
3400 private:
3401 Handle<Context> context_;
3402 };
3403
3404 private:
3405 friend class Value;
3406 friend class Script;
3407 friend class Object;
3408 friend class Function;
3409};
3410
3411
3412/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003413 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003414 * is allowed to use any given V8 isolate. See Isolate class
3415 * comments. The definition of 'using V8 isolate' includes
3416 * accessing handles or holding onto object pointers obtained
3417 * from V8 handles while in the particular V8 isolate. It is up
3418 * to the user of V8 to ensure (perhaps with locking) that this
3419 * constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003420 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003421 * v8::Locker is a scoped lock object. While it's
3422 * active (i.e. between its construction and destruction) the current thread is
3423 * allowed to use the locked isolate. V8 guarantees that an isolate can be locked
3424 * by at most one thread at any time. In other words, the scope of a v8::Locker is
3425 * a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003426 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003427 * Sample usage:
3428* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003429 * ...
3430 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003431 * v8::Locker locker(isolate);
3432 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003433 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003434 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003435 * ...
3436 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003437 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003438 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003439 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003440 * by destroying the v8::Locker object as above or by constructing a
3441 * v8::Unlocker object:
3442 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003443 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003444 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003445 * isolate->Exit();
3446 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003447 * ...
3448 * // Code not using V8 goes here while V8 can run in another thread.
3449 * ...
3450 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003451 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003452 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003453 *
3454 * The Unlocker object is intended for use in a long-running callback
3455 * from V8, where you want to release the V8 lock for other threads to
3456 * use.
3457 *
3458 * The v8::Locker is a recursive lock. That is, you can lock more than
3459 * once in a given thread. This can be useful if you have code that can
3460 * be called either from code that holds the lock or from code that does
3461 * not. The Unlocker is not recursive so you can not have several
3462 * Unlockers on the stack at once, and you can not use an Unlocker in a
3463 * thread that is not inside a Locker's scope.
3464 *
3465 * An unlocker will unlock several lockers if it has to and reinstate
3466 * the correct depth of locking on its destruction. eg.:
3467 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003468 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003469 * // V8 not locked.
3470 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003471 * v8::Locker locker(isolate);
3472 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473 * // V8 locked.
3474 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003475 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003476 * // V8 still locked (2 levels).
3477 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003478 * isolate->Exit();
3479 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003480 * // V8 not locked.
3481 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003482 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003483 * // V8 locked again (2 levels).
3484 * }
3485 * // V8 still locked (1 level).
3486 * }
3487 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003488 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003489 *
3490 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003491 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003492class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003493 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003494 /**
3495 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3496 */
3497 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003498 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003499 private:
3500 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003501};
3502
3503
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003504class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003505 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003506 /**
3507 * Initialize Locker for a given Isolate. NULL means default isolate.
3508 */
3509 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003510 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003511
3512 /**
3513 * Start preemption.
3514 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003515 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003516 * that will switch between multiple threads that are in contention
3517 * for the V8 lock.
3518 */
3519 static void StartPreemption(int every_n_ms);
3520
3521 /**
3522 * Stop preemption.
3523 */
3524 static void StopPreemption();
3525
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003526 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00003527 * Returns whether or not the locker for a given isolate, or default isolate if NULL is given,
3528 * is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003529 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003530 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003531
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003532 /**
3533 * Returns whether v8::Locker is being used by this V8 instance.
3534 */
3535 static bool IsActive() { return active_; }
3536
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003537 private:
3538 bool has_lock_;
3539 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003540 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003541
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003542 static bool active_;
3543
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003544 // Disallow copying and assigning.
3545 Locker(const Locker&);
3546 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003547};
3548
3549
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003550/**
3551 * An interface for exporting data from V8, using "push" model.
3552 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003553class V8EXPORT OutputStream { // NOLINT
3554 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003555 enum OutputEncoding {
3556 kAscii = 0 // 7-bit ASCII.
3557 };
3558 enum WriteResult {
3559 kContinue = 0,
3560 kAbort = 1
3561 };
3562 virtual ~OutputStream() {}
3563 /** Notify about the end of stream. */
3564 virtual void EndOfStream() = 0;
3565 /** Get preferred output chunk size. Called only once. */
3566 virtual int GetChunkSize() { return 1024; }
3567 /** Get preferred output encoding. Called only once. */
3568 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3569 /**
3570 * Writes the next chunk of snapshot data into the stream. Writing
3571 * can be stopped by returning kAbort as function result. EndOfStream
3572 * will not be called in case writing was aborted.
3573 */
3574 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
3575};
3576
3577
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003578/**
3579 * An interface for reporting progress and controlling long-running
3580 * activities.
3581 */
3582class V8EXPORT ActivityControl { // NOLINT
3583 public:
3584 enum ControlOption {
3585 kContinue = 0,
3586 kAbort = 1
3587 };
3588 virtual ~ActivityControl() {}
3589 /**
3590 * Notify about current progress. The activity can be stopped by
3591 * returning kAbort as the callback result.
3592 */
3593 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3594};
3595
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003597// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003598
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003599
3600namespace internal {
3601
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003602static const int kApiPointerSize = sizeof(void*); // NOLINT
3603static const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003604
3605// Tag information for HeapObject.
3606const int kHeapObjectTag = 1;
3607const int kHeapObjectTagSize = 2;
3608const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3609
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003610// Tag information for Smi.
3611const int kSmiTag = 0;
3612const int kSmiTagSize = 1;
3613const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3614
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003615template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003616
3617// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003618template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003619 static const int kSmiShiftSize = 0;
3620 static const int kSmiValueSize = 31;
3621 static inline int SmiToInt(internal::Object* value) {
3622 int shift_bits = kSmiTagSize + kSmiShiftSize;
3623 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3624 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3625 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003626
3627 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3628 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003629 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003630 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003631};
3632
3633// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003634template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003635 static const int kSmiShiftSize = 31;
3636 static const int kSmiValueSize = 32;
3637 static inline int SmiToInt(internal::Object* value) {
3638 int shift_bits = kSmiTagSize + kSmiShiftSize;
3639 // Shift down and throw away top 32 bits.
3640 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3641 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003642
3643 // To maximize the range of pointers that can be encoded
3644 // in the available 32 bits, we require them to be 8 bytes aligned.
3645 // This gives 2 ^ (32 + 3) = 32G address space covered.
3646 // It might be not enough to cover stack allocated objects on some platforms.
3647 static const int kPointerAlignment = 3;
3648
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003649 static const uintptr_t kEncodablePointerMask =
3650 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003651
3652 static const int kPointerToSmiShift =
3653 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003654};
3655
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003656typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3657const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3658const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003659const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003660 PlatformSmiTagging::kEncodablePointerMask;
3661const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003662
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003663template <size_t ptr_size> struct InternalConstants;
3664
3665// Internal constants for 32-bit systems.
3666template <> struct InternalConstants<4> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003667 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003668};
3669
3670// Internal constants for 64-bit systems.
3671template <> struct InternalConstants<8> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003672 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003673};
3674
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003675/**
3676 * This class exports constants and functionality from within v8 that
3677 * is necessary to implement inline functions in the v8 api. Don't
3678 * depend on functions and constants defined here.
3679 */
3680class Internals {
3681 public:
3682
3683 // These values match non-compiler-dependent values defined within
3684 // the implementation of v8.
3685 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003686 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003687 static const int kStringResourceOffset =
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003688 InternalConstants<kApiPointerSize>::kStringResourceOffset;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003689
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003690 static const int kProxyProxyOffset = kApiPointerSize;
3691 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003692 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003693 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003694
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003695 static const int kJSObjectType = 0xa2;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003696 static const int kFirstNonstringType = 0x80;
3697 static const int kProxyType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003698
3699 static inline bool HasHeapObjectTag(internal::Object* value) {
3700 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3701 kHeapObjectTag);
3702 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003703
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003704 static inline bool HasSmiTag(internal::Object* value) {
3705 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3706 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003707
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003708 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003709 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003710 }
3711
3712 static inline int GetInstanceType(internal::Object* obj) {
3713 typedef internal::Object O;
3714 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3715 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3716 }
3717
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003718 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003719 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003720 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3721 }
3722
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003723 static inline void* GetExternalPointer(internal::Object* obj) {
3724 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003725 return GetExternalPointerFromSmi(obj);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003726 } else if (GetInstanceType(obj) == kProxyType) {
3727 return ReadField<void*>(obj, kProxyProxyOffset);
3728 } else {
3729 return NULL;
3730 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003731 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003732
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003733 static inline bool IsExternalTwoByteString(int instance_type) {
3734 int representation = (instance_type & kFullStringRepresentationMask);
3735 return representation == kExternalTwoByteRepresentationTag;
3736 }
3737
3738 template <typename T>
3739 static inline T ReadField(Object* ptr, int offset) {
3740 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3741 return *reinterpret_cast<T*>(addr);
3742 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003743
3744 static inline bool CanCastToHeapObject(void* o) { return false; }
3745 static inline bool CanCastToHeapObject(Context* o) { return true; }
3746 static inline bool CanCastToHeapObject(String* o) { return true; }
3747 static inline bool CanCastToHeapObject(Object* o) { return true; }
3748 static inline bool CanCastToHeapObject(Message* o) { return true; }
3749 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
3750 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003751};
3752
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003753} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003754
3755
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003756template <class T>
3757Handle<T>::Handle() : val_(0) { }
3758
3759
3760template <class T>
3761Local<T>::Local() : Handle<T>() { }
3762
3763
3764template <class T>
3765Local<T> Local<T>::New(Handle<T> that) {
3766 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003767 T* that_ptr = *that;
3768 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
3769 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
3770 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
3771 reinterpret_cast<internal::HeapObject*>(*p))));
3772 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003773 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3774}
3775
3776
3777template <class T>
3778Persistent<T> Persistent<T>::New(Handle<T> that) {
3779 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003780 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003781 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3782}
3783
3784
3785template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003786bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003787 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003788 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003789}
3790
3791
3792template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003793bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003794 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003795 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003796}
3797
3798
3799template <class T>
3800void Persistent<T>::Dispose() {
3801 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003802 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003803}
3804
3805
3806template <class T>
3807Persistent<T>::Persistent() : Handle<T>() { }
3808
3809template <class T>
3810void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003811 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3812 parameters,
3813 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003814}
3815
3816template <class T>
3817void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003818 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003819}
3820
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003821template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003822void Persistent<T>::MarkIndependent() {
3823 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
3824}
3825
3826template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003827void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
3828 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
3829}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003830
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003831Arguments::Arguments(internal::Object** implicit_args,
3832 internal::Object** values, int length,
3833 bool is_construct_call)
3834 : implicit_args_(implicit_args),
3835 values_(values),
3836 length_(length),
3837 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003838
3839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840Local<Value> Arguments::operator[](int i) const {
3841 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3842 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3843}
3844
3845
3846Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003847 return Local<Function>(reinterpret_cast<Function*>(
3848 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003849}
3850
3851
3852Local<Object> Arguments::This() const {
3853 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3854}
3855
3856
3857Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003858 return Local<Object>(reinterpret_cast<Object*>(
3859 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003860}
3861
3862
3863Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003864 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003865}
3866
3867
3868bool Arguments::IsConstructCall() const {
3869 return is_construct_call_;
3870}
3871
3872
3873int Arguments::Length() const {
3874 return length_;
3875}
3876
3877
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003878template <class T>
3879Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003880 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3881 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 return Local<T>(reinterpret_cast<T*>(after));
3883}
3884
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003885Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003886 return resource_name_;
3887}
3888
3889
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003890Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003891 return resource_line_offset_;
3892}
3893
3894
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003895Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896 return resource_column_offset_;
3897}
3898
3899
3900Handle<Boolean> Boolean::New(bool value) {
3901 return value ? True() : False();
3902}
3903
3904
3905void Template::Set(const char* name, v8::Handle<Data> value) {
3906 Set(v8::String::New(name), value);
3907}
3908
3909
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003910Local<Value> Object::GetInternalField(int index) {
3911#ifndef V8_ENABLE_CHECKS
3912 Local<Value> quick_result = UncheckedGetInternalField(index);
3913 if (!quick_result.IsEmpty()) return quick_result;
3914#endif
3915 return CheckedGetInternalField(index);
3916}
3917
3918
3919Local<Value> Object::UncheckedGetInternalField(int index) {
3920 typedef internal::Object O;
3921 typedef internal::Internals I;
3922 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003923 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003924 // If the object is a plain JSObject, which is the common case,
3925 // we know where to find the internal fields and can return the
3926 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003927 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003928 O* value = I::ReadField<O*>(obj, offset);
3929 O** result = HandleScope::CreateHandle(value);
3930 return Local<Value>(reinterpret_cast<Value*>(result));
3931 } else {
3932 return Local<Value>();
3933 }
3934}
3935
3936
3937void* External::Unwrap(Handle<v8::Value> obj) {
3938#ifdef V8_ENABLE_CHECKS
3939 return FullUnwrap(obj);
3940#else
3941 return QuickUnwrap(obj);
3942#endif
3943}
3944
3945
3946void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3947 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003948 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003949 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003950}
3951
3952
3953void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003954 typedef internal::Object O;
3955 typedef internal::Internals I;
3956
3957 O* obj = *reinterpret_cast<O**>(this);
3958
3959 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3960 // If the object is a plain JSObject, which is the common case,
3961 // we know where to find the internal fields and can return the
3962 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003963 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003964 O* value = I::ReadField<O*>(obj, offset);
3965 return I::GetExternalPointer(value);
3966 }
3967
3968 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003969}
3970
3971
3972String* String::Cast(v8::Value* value) {
3973#ifdef V8_ENABLE_CHECKS
3974 CheckCast(value);
3975#endif
3976 return static_cast<String*>(value);
3977}
3978
3979
3980String::ExternalStringResource* String::GetExternalStringResource() const {
3981 typedef internal::Object O;
3982 typedef internal::Internals I;
3983 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003984 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003985 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003986 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3987 result = reinterpret_cast<String::ExternalStringResource*>(value);
3988 } else {
3989 result = NULL;
3990 }
3991#ifdef V8_ENABLE_CHECKS
3992 VerifyExternalStringResource(result);
3993#endif
3994 return result;
3995}
3996
3997
3998bool Value::IsString() const {
3999#ifdef V8_ENABLE_CHECKS
4000 return FullIsString();
4001#else
4002 return QuickIsString();
4003#endif
4004}
4005
4006bool Value::QuickIsString() const {
4007 typedef internal::Object O;
4008 typedef internal::Internals I;
4009 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4010 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004011 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004012}
4013
4014
4015Number* Number::Cast(v8::Value* value) {
4016#ifdef V8_ENABLE_CHECKS
4017 CheckCast(value);
4018#endif
4019 return static_cast<Number*>(value);
4020}
4021
4022
4023Integer* Integer::Cast(v8::Value* value) {
4024#ifdef V8_ENABLE_CHECKS
4025 CheckCast(value);
4026#endif
4027 return static_cast<Integer*>(value);
4028}
4029
4030
4031Date* Date::Cast(v8::Value* value) {
4032#ifdef V8_ENABLE_CHECKS
4033 CheckCast(value);
4034#endif
4035 return static_cast<Date*>(value);
4036}
4037
4038
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004039RegExp* RegExp::Cast(v8::Value* value) {
4040#ifdef V8_ENABLE_CHECKS
4041 CheckCast(value);
4042#endif
4043 return static_cast<RegExp*>(value);
4044}
4045
4046
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004047Object* Object::Cast(v8::Value* value) {
4048#ifdef V8_ENABLE_CHECKS
4049 CheckCast(value);
4050#endif
4051 return static_cast<Object*>(value);
4052}
4053
4054
4055Array* Array::Cast(v8::Value* value) {
4056#ifdef V8_ENABLE_CHECKS
4057 CheckCast(value);
4058#endif
4059 return static_cast<Array*>(value);
4060}
4061
4062
4063Function* Function::Cast(v8::Value* value) {
4064#ifdef V8_ENABLE_CHECKS
4065 CheckCast(value);
4066#endif
4067 return static_cast<Function*>(value);
4068}
4069
4070
4071External* External::Cast(v8::Value* value) {
4072#ifdef V8_ENABLE_CHECKS
4073 CheckCast(value);
4074#endif
4075 return static_cast<External*>(value);
4076}
4077
4078
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004079Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004080 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004081}
4082
4083
4084Local<Object> AccessorInfo::This() const {
4085 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4086}
4087
4088
4089Local<Object> AccessorInfo::Holder() const {
4090 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4091}
4092
4093
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004094/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004095 * \example shell.cc
4096 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004097 * command-line and executes them.
4098 */
4099
4100
4101/**
4102 * \example process.cc
4103 */
4104
4105
4106} // namespace v8
4107
4108
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004109#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004110#undef TYPE_CHECK
4111
4112
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004113#endif // V8_H_