blob: f300884965233b2d92eac5099e0731ab2bad7c97 [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000065// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000066#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
67 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070#else
71#define V8EXPORT
72#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000075#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076
77#endif // _WIN32
78
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000079#if defined(__GNUC__) && !defined(DEBUG)
80#define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
81#elif defined(_MSC_VER) && !defined(DEBUG)
82#define V8_INLINE(declarator) __forceinline declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000083#else
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000084#define V8_INLINE(declarator) inline declarator
85#endif
86
87#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
88#define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
89#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
90#define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
91#else
92#define V8_DEPRECATED(declarator) declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000093#endif
94
danno@chromium.orgca29dd82013-04-26 11:59:48 +000095#if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))
96 #define V8_UNLIKELY(condition) __builtin_expect((condition), 0)
97 #define V8_LIKELY(condition) __builtin_expect((condition), 1)
98#else
99 #define V8_UNLIKELY(condition) (condition)
100 #define V8_LIKELY(condition) (condition)
101#endif
102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000104 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 */
106namespace v8 {
107
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000108class AccessorInfo;
109class AccessorSignature;
110class Array;
111class Boolean;
112class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113class Context;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000114class CpuProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000115class Data;
116class Date;
117class DeclaredAccessorDescriptor;
118class External;
119class Function;
120class FunctionTemplate;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000121class HeapProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000122class ImplementationUtilities;
123class Int32;
124class Integer;
125class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000127class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128class Object;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000129class ObjectOperationDescriptor;
130class ObjectTemplate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131class Primitive;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000132class RawOperationDescriptor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133class Signature;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000134class StackFrame;
135class StackTrace;
136class String;
137class StringObject;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000138class Symbol;
139class SymbolObject;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000140class Uint32;
141class Utils;
142class Value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143template <class T> class Handle;
144template <class T> class Local;
145template <class T> class Persistent;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000147namespace internal {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000148class Arguments;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000149class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000150class HeapObject;
151class Isolate;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000152class Object;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000153}
154
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000156/**
157 * General purpose unique identifier.
158 */
159class UniqueId {
160 public:
161 explicit UniqueId(intptr_t data)
162 : data_(data) {}
163
164 bool operator==(const UniqueId& other) const {
165 return data_ == other.data_;
166 }
167
168 bool operator!=(const UniqueId& other) const {
169 return data_ != other.data_;
170 }
171
172 bool operator<(const UniqueId& other) const {
173 return data_ < other.data_;
174 }
175
176 private:
177 intptr_t data_;
178};
179
180
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000181// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182
183
184/**
185 * A weak reference callback function.
186 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000187 * This callback should either explicitly invoke Dispose on |object| if
188 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
189 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 * \param object the weak global object to be reclaimed by the garbage collector
191 * \param parameter the value passed in when making the weak global object
192 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000193typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 void* parameter);
195
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000196// TODO(svenpanne) Temporary definition until Chrome is in sync.
197typedef void (*NearDeathCallback)(Isolate* isolate,
198 Persistent<Value> object,
199 void* parameter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000201// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000203#define TYPE_CHECK(T, S) \
204 while (false) { \
205 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 }
207
208/**
209 * An object reference managed by the v8 garbage collector.
210 *
211 * All objects returned from v8 have to be tracked by the garbage
212 * collector so that it knows that the objects are still alive. Also,
213 * because the garbage collector may move objects, it is unsafe to
214 * point directly to an object. Instead, all objects are stored in
215 * handles which are known by the garbage collector and updated
216 * whenever an object moves. Handles should always be passed by value
217 * (except in cases like out-parameters) and they should never be
218 * allocated on the heap.
219 *
220 * There are two types of handles: local and persistent handles.
221 * Local handles are light-weight and transient and typically used in
222 * local operations. They are managed by HandleScopes. Persistent
223 * handles can be used when storing objects across several independent
224 * operations and have to be explicitly deallocated when they're no
225 * longer used.
226 *
227 * It is safe to extract the object stored in the handle by
228 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000229 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 * behind the scenes and the same rules apply to these values as to
231 * their handles.
232 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000233template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 /**
236 * Creates an empty handle.
237 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000238 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239
240 /**
241 * Creates a new handle for the specified value.
242 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000243 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244
245 /**
246 * Creates a handle for the contents of the specified handle. This
247 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000248 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000250 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 * between compatible handles, for instance assigning a
252 * Handle<String> to a variable declared as Handle<Value>, is legal
253 * because String is a subclass of Value.
254 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000255 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 : val_(reinterpret_cast<T*>(*that)) {
257 /**
258 * This check fails when trying to convert between incompatible
259 * handles. For example, converting from a Handle<String> to a
260 * Handle<Number>.
261 */
262 TYPE_CHECK(T, S);
263 }
264
265 /**
266 * Returns true if the handle is empty.
267 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000268 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 /**
271 * Sets the handle to be empty. IsEmpty() will then return true.
272 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000273 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000274
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000275 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000276
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000277 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278
279 /**
280 * Checks whether two handles are the same.
281 * Returns true if both are empty, or if the objects
282 * to which they refer are identical.
283 * The handles' references are not checked.
284 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000285 template <class S> V8_INLINE(bool operator==(Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000286 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
287 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 if (a == 0) return b == 0;
289 if (b == 0) return false;
290 return *a == *b;
291 }
292
293 /**
294 * Checks whether two handles are different.
295 * Returns true if only one of the handles is empty, or if
296 * the objects to which they refer are different.
297 * The handles' references are not checked.
298 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000299 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300 return !operator==(that);
301 }
302
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000303 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000304#ifdef V8_ENABLE_CHECKS
305 // If we're going to perform the type check then we have to check
306 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000308#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 return Handle<T>(T::Cast(*that));
310 }
311
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000312 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000313 return Handle<S>::Cast(*this);
314 }
315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 private:
317 T* val_;
318};
319
320
321/**
322 * A light-weight stack-allocated object handle. All operations
323 * that return objects from within v8 return them in local handles. They
324 * are created within HandleScopes, and all local handles allocated within a
325 * handle scope are destroyed when the handle scope is destroyed. Hence it
326 * is not necessary to explicitly deallocate local handles.
327 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000328template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000330 V8_INLINE(Local());
331 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 : Handle<T>(reinterpret_cast<T*>(*that)) {
333 /**
334 * This check fails when trying to convert between incompatible
335 * handles. For example, converting from a Handle<String> to a
336 * Handle<Number>.
337 */
338 TYPE_CHECK(T, S);
339 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000340 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
341 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000342#ifdef V8_ENABLE_CHECKS
343 // If we're going to perform the type check then we have to check
344 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000346#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 return Local<T>(T::Cast(*that));
348 }
349
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000350 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000351 return Local<S>::Cast(*this);
352 }
353
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000354 /**
355 * Create a local handle for the content of another handle.
356 * The referee is kept alive by the local handle even when
357 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000359 V8_INLINE(static Local<T> New(Handle<T> that));
360 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361};
362
363
364/**
365 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000366 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 * allocated, a Persistent handle remains valid until it is explicitly
368 * disposed.
369 *
370 * A persistent handle contains a reference to a storage cell within
371 * the v8 engine which holds an object value and which is updated by
372 * the garbage collector whenever the object is moved. A new storage
373 * cell can be created using Persistent::New and existing handles can
374 * be disposed using Persistent::Dispose. Since persistent handles
375 * are passed by value you may have many persistent handle objects
376 * that point to the same storage cell. For instance, if you pass a
377 * persistent handle as an argument to a function you will not get two
378 * different storage cells but rather two references to the same
379 * storage cell.
380 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000381template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 /**
384 * Creates an empty persistent handle that doesn't point to any
385 * storage cell.
386 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000387 V8_INLINE(Persistent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388
389 /**
390 * Creates a persistent handle for the same storage cell as the
391 * specified handle. This constructor allows you to pass persistent
392 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000393 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000395 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 * between compatible persistent handles, for instance assigning a
397 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000398 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000400 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 : Handle<T>(reinterpret_cast<T*>(*that)) {
402 /**
403 * This check fails when trying to convert between incompatible
404 * handles. For example, converting from a Handle<String> to a
405 * Handle<Number>.
406 */
407 TYPE_CHECK(T, S);
408 }
409
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000410 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000412 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000413 * A constructor that creates a new global cell pointing to that. In contrast
414 * to the copy constructor, this creates a new persistent handle which needs
415 * to be separately disposed.
416 */
417 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
418 : Handle<T>(New(isolate, that)) { }
419
420 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000421 * "Casts" a plain handle which is known to be a persistent handle
422 * to a persistent handle.
423 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000424 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 : Handle<T>(*that) { }
426
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000427 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000428#ifdef V8_ENABLE_CHECKS
429 // If we're going to perform the type check then we have to check
430 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000432#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 return Persistent<T>(T::Cast(*that));
434 }
435
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000436 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000437 return Persistent<S>::Cast(*this);
438 }
439
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000440 /** Deprecated. Use Isolate version instead. */
441 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
442
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000444 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000446 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
447
448 /** Deprecated. Use Isolate version instead. */
449 V8_DEPRECATED(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450
451 /**
452 * Releases the storage cell referenced by this persistent handle.
453 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000454 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 * cell remain and IsEmpty will still return false.
456 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000457 V8_INLINE(void Dispose(Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000459 /** Deprecated. Use Isolate version instead. */
460 V8_DEPRECATED(void MakeWeak(void* parameters,
461 WeakReferenceCallback callback));
462
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 /**
464 * Make the reference to this object weak. When only weak handles
465 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000466 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 * it the object reference and the given parameters.
468 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000469 V8_INLINE(void MakeWeak(Isolate* isolate,
470 void* parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000471 NearDeathCallback callback));
472
473 /** Deprecated. Use Isolate version instead. */
474 V8_DEPRECATED(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000476 /** Clears the weak reference to this object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000477 V8_INLINE(void ClearWeak(Isolate* isolate));
478
479 /** Deprecated. Use Isolate version instead. */
480 V8_DEPRECATED(void MarkIndependent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481
482 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000483 * Marks the reference to this object independent. Garbage collector is free
484 * to ignore any object groups containing this object. Weak callback for an
485 * independent handle should not assume that it will be preceded by a global
486 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000487 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000488 V8_INLINE(void MarkIndependent(Isolate* isolate));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000489
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000490 /** Deprecated. Use Isolate version instead. */
491 V8_DEPRECATED(void MarkPartiallyDependent());
492
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000493 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000494 * Marks the reference to this object partially dependent. Partially dependent
495 * handles only depend on other partially dependent handles and these
496 * dependencies are provided through object groups. It provides a way to build
497 * smaller object groups for young objects that represent only a subset of all
498 * external dependencies. This mark is automatically cleared after each
499 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000500 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000501 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000502
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000503 /** Deprecated. Use Isolate version instead. */
504 V8_DEPRECATED(bool IsIndependent() const);
505
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000506 /** Returns true if this handle was previously marked as independent. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000507 V8_INLINE(bool IsIndependent(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000508
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000509 /** Deprecated. Use Isolate version instead. */
510 V8_DEPRECATED(bool IsNearDeath() const);
511
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000512 /** Checks if the handle holds the only reference to an object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000513 V8_INLINE(bool IsNearDeath(Isolate* isolate) const);
514
515 /** Deprecated. Use Isolate version instead. */
516 V8_DEPRECATED(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000518 /** Returns true if the handle's reference is weak. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000519 V8_INLINE(bool IsWeak(Isolate* isolate) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000521 /** Deprecated. Use Isolate version instead. */
522 V8_DEPRECATED(void SetWrapperClassId(uint16_t class_id));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000523
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000524 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000525 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
526 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000527 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000528 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id));
529
530 /** Deprecated. Use Isolate version instead. */
531 V8_DEPRECATED(uint16_t WrapperClassId() const);
532
533 /**
534 * Returns the class ID previously assigned to this handle or 0 if no class ID
535 * was previously assigned.
536 */
537 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000538
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 private:
540 friend class ImplementationUtilities;
541 friend class ObjectTemplate;
542};
543
544
v8.team.kasperl727e9952008-09-02 14:56:44 +0000545 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 * A stack-allocated class that governs a number of local handles.
547 * After a handle scope has been created, all local handles will be
548 * allocated within that handle scope until either the handle scope is
549 * deleted or another handle scope is created. If there is already a
550 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000551 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 * new handles will again be allocated in the original handle scope.
553 *
554 * After the handle scope of a local handle has been deleted the
555 * garbage collector will no longer track the object stored in the
556 * handle and may deallocate it. The behavior of accessing a handle
557 * for which the handle scope has been deleted is undefined.
558 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000559class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000561 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000562 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000564 HandleScope(Isolate* isolate);
565
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000566 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567
568 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 * Closes the handle scope and returns the value as a handle in the
570 * previous scope, which is the new current scope after the call.
571 */
572 template <class T> Local<T> Close(Handle<T> value);
573
574 /**
575 * Counts the number of allocated handles.
576 */
577 static int NumberOfHandles();
578
579 /**
580 * Creates a new handle with the given value.
581 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000582 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000583 static internal::Object** CreateHandle(internal::Isolate* isolate,
584 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000585 // Faster version, uses HeapObject to obtain the current Isolate.
586 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587
588 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000589 // Make it hard to create heap-allocated or illegal handle scopes by
590 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 HandleScope(const HandleScope&);
592 void operator=(const HandleScope&);
593 void* operator new(size_t size);
594 void operator delete(void*, size_t);
595
ager@chromium.org3811b432009-10-28 14:53:37 +0000596 // This Data class is accessible internally as HandleScopeData through a
597 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000598 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000600 internal::Object** next;
601 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000602 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000603 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000605 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 }
607 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000608
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000609 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000610 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000612 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000613 internal::Object** prev_next_;
614 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000616 // Allow for the active closing of HandleScopes which allows to pass a handle
617 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000619 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 friend class ImplementationUtilities;
622};
623
624
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000625// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626
627
628/**
629 * The superclass of values and API object templates.
630 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000631class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 private:
633 Data();
634};
635
636
637/**
638 * Pre-compilation data that can be associated with a script. This
639 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000640 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000641 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000643class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644 public:
645 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000646
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000647 /**
648 * Pre-compiles the specified script (context-independent).
649 *
650 * \param input Pointer to UTF-8 script source code.
651 * \param length Length of UTF-8 script source code.
652 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000655 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000656 * Pre-compiles the specified script (context-independent).
657 *
658 * NOTE: Pre-compilation using this method cannot happen on another thread
659 * without using Lockers.
660 *
661 * \param source Script source code.
662 */
663 static ScriptData* PreCompile(Handle<String> source);
664
665 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000666 * Load previous pre-compilation data.
667 *
668 * \param data Pointer to data returned by a call to Data() of a previous
669 * ScriptData. Ownership is not transferred.
670 * \param length Length of data.
671 */
672 static ScriptData* New(const char* data, int length);
673
674 /**
675 * Returns the length of Data().
676 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000678
679 /**
680 * Returns a serialized representation of this ScriptData that can later be
681 * passed to New(). NOTE: Serialized data is platform-dependent.
682 */
683 virtual const char* Data() = 0;
684
685 /**
686 * Returns true if the source code could not be parsed.
687 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000688 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689};
690
691
692/**
693 * The origin, within a file, of a script.
694 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000695class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000697 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000698 Handle<Value> resource_name,
699 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000700 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 : resource_name_(resource_name),
702 resource_line_offset_(resource_line_offset),
703 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000704 V8_INLINE(Handle<Value> ResourceName() const);
705 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
706 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000708 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 Handle<Integer> resource_line_offset_;
710 Handle<Integer> resource_column_offset_;
711};
712
713
714/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000715 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000717class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000720 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000721 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000722 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000723 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000724 * when New() returns
725 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
726 * using pre_data speeds compilation if it's done multiple times.
727 * Owned by caller, no references are kept when New() returns.
728 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000729 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000730 * available to compile event handlers.
731 * \return Compiled script object (context independent; when run it
732 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000734 static Local<Script> New(Handle<String> source,
735 ScriptOrigin* origin = NULL,
736 ScriptData* pre_data = NULL,
737 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738
mads.s.agercbaa0602008-08-14 13:41:48 +0000739 /**
740 * Compiles the specified script using the specified file name
741 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000742 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000743 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000744 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000745 * as the script's origin.
746 * \return Compiled script object (context independent; when run it
747 * will use the currently entered context).
748 */
749 static Local<Script> New(Handle<String> source,
750 Handle<Value> file_name);
751
752 /**
753 * Compiles the specified script (bound to current context).
754 *
755 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000756 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000757 * when Compile() returns
758 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
759 * using pre_data speeds compilation if it's done multiple times.
760 * Owned by caller, no references are kept when Compile() returns.
761 * \param script_data Arbitrary data associated with script. Using
762 * this has same effect as calling SetData(), but makes data available
763 * earlier (i.e. to compile event handlers).
764 * \return Compiled script object, bound to the context that was active
765 * when this function was called. When run it will always use this
766 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000767 */
768 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000769 ScriptOrigin* origin = NULL,
770 ScriptData* pre_data = NULL,
771 Handle<String> script_data = Handle<String>());
772
773 /**
774 * Compiles the specified script using the specified file name
775 * object (typically a string) as the script's origin.
776 *
777 * \param source Script source code.
778 * \param file_name File name to use as script's origin
779 * \param script_data Arbitrary data associated with script. Using
780 * this has same effect as calling SetData(), but makes data available
781 * earlier (i.e. to compile event handlers).
782 * \return Compiled script object, bound to the context that was active
783 * when this function was called. When run it will always use this
784 * context.
785 */
786 static Local<Script> Compile(Handle<String> source,
787 Handle<Value> file_name,
788 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000789
v8.team.kasperl727e9952008-09-02 14:56:44 +0000790 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000791 * Runs the script returning the resulting value. If the script is
792 * context independent (created using ::New) it will be run in the
793 * currently entered context. If it is context specific (created
794 * using ::Compile) it will be run in the context in which it was
795 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000796 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000798
799 /**
800 * Returns the script id value.
801 */
802 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000803
804 /**
805 * Associate an additional data object with the script. This is mainly used
806 * with the debugger as this data object is only available through the
807 * debugger API.
808 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000809 void SetData(Handle<String> data);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000810
811 /**
812 * Returns the name value of one Script.
813 */
814 Handle<Value> GetScriptName();
815
816 /**
817 * Returns zero based line number of the code_pos location in the script.
818 * -1 will be returned if no information available.
819 */
820 int GetLineNumber(int code_pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821};
822
823
824/**
825 * An error message.
826 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000827class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000829 Local<String> Get() const;
830 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000832 /**
833 * Returns the resource name for the script from where the function causing
834 * the error originates.
835 */
ager@chromium.org32912102009-01-16 10:38:43 +0000836 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000838 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000839 * Returns the resource data for the script from where the function causing
840 * the error originates.
841 */
842 Handle<Value> GetScriptData() const;
843
844 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000845 * Exception stack trace. By default stack traces are not captured for
846 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
847 * to change this option.
848 */
849 Handle<StackTrace> GetStackTrace() const;
850
851 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000852 * Returns the number, 1-based, of the line where the error occurred.
853 */
ager@chromium.org32912102009-01-16 10:38:43 +0000854 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000856 /**
857 * Returns the index within the script of the first character where
858 * the error occurred.
859 */
ager@chromium.org32912102009-01-16 10:38:43 +0000860 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000861
862 /**
863 * Returns the index within the script of the last character where
864 * the error occurred.
865 */
ager@chromium.org32912102009-01-16 10:38:43 +0000866 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000867
868 /**
869 * Returns the index within the line of the first character where
870 * the error occurred.
871 */
ager@chromium.org32912102009-01-16 10:38:43 +0000872 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000873
874 /**
875 * Returns the index within the line of the last character where
876 * the error occurred.
877 */
ager@chromium.org32912102009-01-16 10:38:43 +0000878 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000879
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880 // TODO(1245381): Print to a string instead of on a FILE.
881 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000882
883 static const int kNoLineNumberInfo = 0;
884 static const int kNoColumnInfo = 0;
885};
886
887
888/**
889 * Representation of a JavaScript stack trace. The information collected is a
890 * snapshot of the execution stack and the information remains valid after
891 * execution continues.
892 */
893class V8EXPORT StackTrace {
894 public:
895 /**
896 * Flags that determine what information is placed captured for each
897 * StackFrame when grabbing the current stack trace.
898 */
899 enum StackTraceOptions {
900 kLineNumber = 1,
901 kColumnOffset = 1 << 1 | kLineNumber,
902 kScriptName = 1 << 2,
903 kFunctionName = 1 << 3,
904 kIsEval = 1 << 4,
905 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000906 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000907 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000908 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000909 };
910
911 /**
912 * Returns a StackFrame at a particular index.
913 */
914 Local<StackFrame> GetFrame(uint32_t index) const;
915
916 /**
917 * Returns the number of StackFrames.
918 */
919 int GetFrameCount() const;
920
921 /**
922 * Returns StackTrace as a v8::Array that contains StackFrame objects.
923 */
924 Local<Array> AsArray();
925
926 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000927 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000928 *
929 * \param frame_limit The maximum number of stack frames we want to capture.
930 * \param options Enumerates the set of things we will capture for each
931 * StackFrame.
932 */
933 static Local<StackTrace> CurrentStackTrace(
934 int frame_limit,
935 StackTraceOptions options = kOverview);
936};
937
938
939/**
940 * A single JavaScript stack frame.
941 */
942class V8EXPORT StackFrame {
943 public:
944 /**
945 * Returns the number, 1-based, of the line for the associate function call.
946 * This method will return Message::kNoLineNumberInfo if it is unable to
947 * retrieve the line number, or if kLineNumber was not passed as an option
948 * when capturing the StackTrace.
949 */
950 int GetLineNumber() const;
951
952 /**
953 * Returns the 1-based column offset on the line for the associated function
954 * call.
955 * This method will return Message::kNoColumnInfo if it is unable to retrieve
956 * the column number, or if kColumnOffset was not passed as an option when
957 * capturing the StackTrace.
958 */
959 int GetColumn() const;
960
961 /**
962 * Returns the name of the resource that contains the script for the
963 * function for this StackFrame.
964 */
965 Local<String> GetScriptName() const;
966
967 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000968 * Returns the name of the resource that contains the script for the
969 * function for this StackFrame or sourceURL value if the script name
970 * is undefined and its source ends with //@ sourceURL=... string.
971 */
972 Local<String> GetScriptNameOrSourceURL() const;
973
974 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000975 * Returns the name of the function associated with this stack frame.
976 */
977 Local<String> GetFunctionName() const;
978
979 /**
980 * Returns whether or not the associated function is compiled via a call to
981 * eval().
982 */
983 bool IsEval() const;
984
985 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000986 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000987 * constructor via "new".
988 */
989 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990};
991
992
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000993// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994
995
996/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000997 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000999class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 /**
1002 * Returns true if this value is the undefined value. See ECMA-262
1003 * 4.3.10.
1004 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001005 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006
1007 /**
1008 * Returns true if this value is the null value. See ECMA-262
1009 * 4.3.11.
1010 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001011 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012
1013 /**
1014 * Returns true if this value is true.
1015 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001016 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017
1018 /**
1019 * Returns true if this value is false.
1020 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001021 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022
1023 /**
1024 * Returns true if this value is an instance of the String type.
1025 * See ECMA-262 8.4.
1026 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001027 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028
1029 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001030 * Returns true if this value is a symbol.
1031 * This is an experimental feature.
1032 */
1033 bool IsSymbol() const;
1034
1035 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 * Returns true if this value is a function.
1037 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001038 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039
1040 /**
1041 * Returns true if this value is an array.
1042 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001043 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044
v8.team.kasperl727e9952008-09-02 14:56:44 +00001045 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 * Returns true if this value is an object.
1047 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001048 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049
v8.team.kasperl727e9952008-09-02 14:56:44 +00001050 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 * Returns true if this value is boolean.
1052 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001053 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001054
v8.team.kasperl727e9952008-09-02 14:56:44 +00001055 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 * Returns true if this value is a number.
1057 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001058 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059
v8.team.kasperl727e9952008-09-02 14:56:44 +00001060 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 * Returns true if this value is external.
1062 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001063 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064
v8.team.kasperl727e9952008-09-02 14:56:44 +00001065 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 * Returns true if this value is a 32-bit signed integer.
1067 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001068 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001070 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001071 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001072 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001073 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001074
1075 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001076 * Returns true if this value is a Date.
1077 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001078 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001079
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001080 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001081 * Returns true if this value is a Boolean object.
1082 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001083 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001084
1085 /**
1086 * Returns true if this value is a Number object.
1087 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001088 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001089
1090 /**
1091 * Returns true if this value is a String object.
1092 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001093 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001094
1095 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001096 * Returns true if this value is a Symbol object.
1097 * This is an experimental feature.
1098 */
1099 bool IsSymbolObject() const;
1100
1101 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001102 * Returns true if this value is a NativeError.
1103 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001104 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001105
1106 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001107 * Returns true if this value is a RegExp.
1108 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001109 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001110
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001111 Local<Boolean> ToBoolean() const;
1112 Local<Number> ToNumber() const;
1113 Local<String> ToString() const;
1114 Local<String> ToDetailString() const;
1115 Local<Object> ToObject() const;
1116 Local<Integer> ToInteger() const;
1117 Local<Uint32> ToUint32() const;
1118 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119
1120 /**
1121 * Attempts to convert a string to an array index.
1122 * Returns an empty handle if the conversion fails.
1123 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001124 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001126 bool BooleanValue() const;
1127 double NumberValue() const;
1128 int64_t IntegerValue() const;
1129 uint32_t Uint32Value() const;
1130 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131
1132 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001133 bool Equals(Handle<Value> that) const;
1134 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001135
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001136 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001137 V8_INLINE(bool QuickIsUndefined() const);
1138 V8_INLINE(bool QuickIsNull() const);
1139 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001140 bool FullIsUndefined() const;
1141 bool FullIsNull() const;
1142 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143};
1144
1145
1146/**
1147 * The superclass of primitive values. See ECMA-262 4.3.2.
1148 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001149class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150
1151
1152/**
1153 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1154 * or false value.
1155 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001156class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001158 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001159 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160};
1161
1162
1163/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001164 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001166class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001168 enum Encoding {
1169 UNKNOWN_ENCODING = 0x1,
1170 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001171 ASCII_ENCODING = 0x4,
1172 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001173 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001174 /**
1175 * Returns the number of characters in this string.
1176 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001177 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178
v8.team.kasperl727e9952008-09-02 14:56:44 +00001179 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001180 * Returns the number of bytes in the UTF-8 encoded
1181 * representation of this string.
1182 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001183 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001184
1185 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001186 * This function is no longer useful.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001187 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001188 // TODO(dcarney): deprecate
1189 V8_INLINE(bool MayContainNonAscii()) const { return true; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001190
1191 /**
1192 * Returns whether this string contains only one byte data.
1193 */
1194 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001195
1196 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001197 * Write the contents of the string to an external buffer.
1198 * If no arguments are given, expects the buffer to be large
1199 * enough to hold the entire string and NULL terminator. Copies
1200 * the contents of the string and the NULL terminator into the
1201 * buffer.
1202 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001203 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1204 * before the end of the buffer.
1205 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001206 * Copies up to length characters into the output buffer.
1207 * Only null-terminates if there is enough space in the buffer.
1208 *
1209 * \param buffer The buffer into which the string will be copied.
1210 * \param start The starting position within the string at which
1211 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001212 * \param length The number of characters to copy from the string. For
1213 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001214 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001215 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001216 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001217 * \return The number of characters copied to the buffer excluding the null
1218 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001219 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001220 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001221 enum WriteOptions {
1222 NO_OPTIONS = 0,
1223 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001224 NO_NULL_TERMINATION = 2,
1225 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001226 };
1227
lrn@chromium.org34e60782011-09-15 07:25:40 +00001228 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001229 int Write(uint16_t* buffer,
1230 int start = 0,
1231 int length = -1,
1232 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001233 // ASCII characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001234 int WriteAscii(char* buffer,
1235 int start = 0,
1236 int length = -1,
1237 int options = NO_OPTIONS) const;
1238 // One byte characters.
1239 int WriteOneByte(uint8_t* buffer,
1240 int start = 0,
1241 int length = -1,
1242 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001243 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001244 int WriteUtf8(char* buffer,
1245 int length = -1,
1246 int* nchars_ref = NULL,
1247 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248
v8.team.kasperl727e9952008-09-02 14:56:44 +00001249 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001250 * A zero length string.
1251 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001252 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001253 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001254
1255 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001256 * Returns true if the string is external
1257 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001258 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259
v8.team.kasperl727e9952008-09-02 14:56:44 +00001260 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001261 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001262 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001263 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001264
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001265 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001266 public:
1267 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001268
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001269 protected:
1270 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001271
1272 /**
1273 * Internally V8 will call this Dispose method when the external string
1274 * resource is no longer needed. The default implementation will use the
1275 * delete operator. This method can be overridden in subclasses to
1276 * control how allocated external string resources are disposed.
1277 */
1278 virtual void Dispose() { delete this; }
1279
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001280 private:
1281 // Disallow copying and assigning.
1282 ExternalStringResourceBase(const ExternalStringResourceBase&);
1283 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001284
1285 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001286 };
1287
v8.team.kasperl727e9952008-09-02 14:56:44 +00001288 /**
1289 * An ExternalStringResource is a wrapper around a two-byte string
1290 * buffer that resides outside V8's heap. Implement an
1291 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001292 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001293 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001294 class V8EXPORT ExternalStringResource
1295 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 public:
1297 /**
1298 * Override the destructor to manage the life cycle of the underlying
1299 * buffer.
1300 */
1301 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001302
1303 /**
1304 * The string data from the underlying buffer.
1305 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001307
1308 /**
1309 * The length of the string. That is, the number of two-byte characters.
1310 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001312
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 protected:
1314 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 };
1316
1317 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001318 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001319 * string buffer that resides outside V8's heap. Implement an
1320 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001321 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001322 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001323 * UTF-8, which would require special treatment internally in the
1324 * engine and, in the case of UTF-8, do not allow efficient indexing.
1325 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001326 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001328 class V8EXPORT ExternalAsciiStringResource
1329 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330 public:
1331 /**
1332 * Override the destructor to manage the life cycle of the underlying
1333 * buffer.
1334 */
1335 virtual ~ExternalAsciiStringResource() {}
1336 /** The string data from the underlying buffer.*/
1337 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001338 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 virtual size_t length() const = 0;
1340 protected:
1341 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 };
1343
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001344 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001347 * If the string is an external string, return the ExternalStringResourceBase
1348 * regardless of the encoding, otherwise return NULL. The encoding of the
1349 * string is returned in encoding_out.
1350 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001351 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1352 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001353
1354 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001355 * Get the ExternalStringResource for an external string. Returns
1356 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001358 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359
1360 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001361 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001362 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001364 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001366 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001368 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001370 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001371 * The second parameter 'length' gives the buffer length. If omitted,
1372 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001374 V8_INLINE(static Local<String> New(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001376 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001377 /** Allocates a new string from 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001378 V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001380 // TODO(dcarney): deprecate
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001381 /**
1382 * Creates an internalized string (historically called a "symbol",
1383 * not to be confused with ES6 symbols). Returns one if it exists already.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001384 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001385 V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
1386
1387 enum NewStringType {
1388 kNormalString, kInternalizedString, kUndetectableString
1389 };
1390
1391 /** Allocates a new string from UTF-8 data.*/
1392 static Local<String> NewFromUtf8(Isolate* isolate,
1393 const char* data,
1394 NewStringType type = kNormalString,
1395 int length = -1);
1396
1397 /** Allocates a new string from Latin-1 data.*/
1398 static Local<String> NewFromOneByte(
1399 Isolate* isolate,
1400 const uint8_t* data,
1401 NewStringType type = kNormalString,
1402 int length = -1);
1403
1404 /** Allocates a new string from UTF-16 data.*/
1405 static Local<String> NewFromTwoByte(
1406 Isolate* isolate,
1407 const uint16_t* data,
1408 NewStringType type = kNormalString,
1409 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410
v8.team.kasperl727e9952008-09-02 14:56:44 +00001411 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001412 * Creates a new string by concatenating the left and the right strings
1413 * passed in as parameters.
1414 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001415 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001416
1417 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001418 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001419 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001420 * resource will be disposed by calling its Dispose method. The caller of
1421 * this function should not otherwise delete or modify the resource. Neither
1422 * should the underlying buffer be deallocated or modified except through the
1423 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001424 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001425 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001426
ager@chromium.org6f10e412009-02-13 10:11:16 +00001427 /**
1428 * Associate an external string resource with this string by transforming it
1429 * in place so that existing references to this string in the JavaScript heap
1430 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001431 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001432 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001433 * The string is not modified if the operation fails. See NewExternal for
1434 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001435 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001436 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437
v8.team.kasperl727e9952008-09-02 14:56:44 +00001438 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001439 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001440 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001441 * resource will be disposed by calling its Dispose method. The caller of
1442 * this function should not otherwise delete or modify the resource. Neither
1443 * should the underlying buffer be deallocated or modified except through the
1444 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001445 */
1446 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001447
ager@chromium.org6f10e412009-02-13 10:11:16 +00001448 /**
1449 * Associate an external string resource with this string by transforming it
1450 * in place so that existing references to this string in the JavaScript heap
1451 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001452 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001453 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001454 * The string is not modified if the operation fails. See NewExternal for
1455 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001456 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001457 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001459 /**
1460 * Returns true if this string can be made external.
1461 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001462 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001463
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001464 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001465 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001466 V8_INLINE(
1467 static Local<String> NewUndetectable(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001469 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001470 /** Creates an undetectable string from the supplied 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001471 V8_INLINE(static Local<String> NewUndetectable(
1472 const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473
1474 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001475 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001476 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001477 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001478 * then the length() method returns 0 and the * operator returns
1479 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001480 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001481 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001482 public:
1483 explicit Utf8Value(Handle<v8::Value> obj);
1484 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001485 char* operator*() { return str_; }
1486 const char* operator*() const { return str_; }
1487 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001488 private:
1489 char* str_;
1490 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001491
1492 // Disallow copying and assigning.
1493 Utf8Value(const Utf8Value&);
1494 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001495 };
1496
1497 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001498 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001500 * If conversion to a string fails (eg. due to an exception in the toString()
1501 * method of the object) then the length() method returns 0 and the * operator
1502 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001504 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505 public:
1506 explicit AsciiValue(Handle<v8::Value> obj);
1507 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001508 char* operator*() { return str_; }
1509 const char* operator*() const { return str_; }
1510 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511 private:
1512 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001513 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001514
1515 // Disallow copying and assigning.
1516 AsciiValue(const AsciiValue&);
1517 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 };
1519
1520 /**
1521 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001522 * If conversion to a string fails (eg. due to an exception in the toString()
1523 * method of the object) then the length() method returns 0 and the * operator
1524 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001526 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 public:
1528 explicit Value(Handle<v8::Value> obj);
1529 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001530 uint16_t* operator*() { return str_; }
1531 const uint16_t* operator*() const { return str_; }
1532 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533 private:
1534 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001535 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001536
1537 // Disallow copying and assigning.
1538 Value(const Value&);
1539 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001541
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001542 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001543 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1544 Encoding encoding) const;
1545 void VerifyExternalStringResource(ExternalStringResource* val) const;
1546 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547};
1548
1549
1550/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001551 * A JavaScript symbol (ECMA-262 edition 6)
1552 *
1553 * This is an experimental feature. Use at your own risk.
1554 */
1555class V8EXPORT Symbol : public Primitive {
1556 public:
1557 // Returns the print name string of the symbol, or undefined if none.
1558 Local<Value> Name() const;
1559
1560 // Create a symbol without a print name.
1561 static Local<Symbol> New(Isolate* isolate);
1562
1563 // Create a symbol with a print name.
1564 static Local<Symbol> New(Isolate *isolate, const char* data, int length = -1);
1565
1566 V8_INLINE(static Symbol* Cast(v8::Value* obj));
1567 private:
1568 Symbol();
1569 static void CheckCast(v8::Value* obj);
1570};
1571
1572
1573/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001574 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001576class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001578 double Value() const;
1579 static Local<Number> New(double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001580 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001582 Number();
1583 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584};
1585
1586
1587/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001588 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001590class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001592 static Local<Integer> New(int32_t value);
1593 static Local<Integer> NewFromUnsigned(uint32_t value);
1594 static Local<Integer> New(int32_t value, Isolate*);
1595 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1596 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001597 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001599 Integer();
1600 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601};
1602
1603
1604/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001605 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001607class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001609 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001611 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612};
1613
1614
1615/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001616 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001618class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001620 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001622 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001623};
1624
1625
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626enum PropertyAttribute {
1627 None = 0,
1628 ReadOnly = 1 << 0,
1629 DontEnum = 1 << 1,
1630 DontDelete = 1 << 2
1631};
1632
ager@chromium.org3811b432009-10-28 14:53:37 +00001633enum ExternalArrayType {
1634 kExternalByteArray = 1,
1635 kExternalUnsignedByteArray,
1636 kExternalShortArray,
1637 kExternalUnsignedShortArray,
1638 kExternalIntArray,
1639 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001640 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001641 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001642 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001643};
1644
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001646 * Accessor[Getter|Setter] are used as callback functions when
1647 * setting|getting a particular property. See Object and ObjectTemplate's
1648 * method SetAccessor.
1649 */
1650typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1651 const AccessorInfo& info);
1652
1653
1654typedef void (*AccessorSetter)(Local<String> property,
1655 Local<Value> value,
1656 const AccessorInfo& info);
1657
1658
1659/**
1660 * Access control specifications.
1661 *
1662 * Some accessors should be accessible across contexts. These
1663 * accessors have an explicit access control parameter which specifies
1664 * the kind of cross-context access that should be allowed.
1665 *
1666 * Additionally, for security, accessors can prohibit overwriting by
1667 * accessors defined in JavaScript. For objects that have such
1668 * accessors either locally or in their prototype chain it is not
1669 * possible to overwrite the accessor by using __defineGetter__ or
1670 * __defineSetter__ from JavaScript code.
1671 */
1672enum AccessControl {
1673 DEFAULT = 0,
1674 ALL_CAN_READ = 1,
1675 ALL_CAN_WRITE = 1 << 1,
1676 PROHIBITS_OVERWRITING = 1 << 2
1677};
1678
1679
1680/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001681 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001683class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001685 bool Set(Handle<Value> key,
1686 Handle<Value> value,
1687 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001688
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001689 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001690
ager@chromium.orge2902be2009-06-08 12:21:35 +00001691 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001692 // overriding accessors or read-only properties.
1693 //
1694 // Note that if the object has an interceptor the property will be set
1695 // locally, but since the interceptor takes precedence the local property
1696 // will only be returned if the interceptor doesn't return a value.
1697 //
1698 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001699 bool ForceSet(Handle<Value> key,
1700 Handle<Value> value,
1701 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001702
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001703 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001705 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001706
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001707 /**
1708 * Gets the property attributes of a property which can be None or
1709 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1710 * None when the property doesn't exist.
1711 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001712 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001713
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001714 bool Has(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001715
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001716 bool Delete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001717
1718 // Delete a property on this object bypassing interceptors and
1719 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001720 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001721
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001722 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001723
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001724 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001726 bool SetAccessor(Handle<String> name,
1727 AccessorGetter getter,
1728 AccessorSetter setter = 0,
1729 Handle<Value> data = Handle<Value>(),
1730 AccessControl settings = DEFAULT,
1731 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001732
ulan@chromium.org750145a2013-03-07 15:14:13 +00001733 // This function is not yet stable and should not be used at this time.
1734 bool SetAccessor(Handle<String> name,
1735 Handle<DeclaredAccessorDescriptor> descriptor,
1736 AccessControl settings = DEFAULT,
1737 PropertyAttribute attribute = None);
1738
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001740 * Returns an array containing the names of the enumerable properties
1741 * of this object, including properties from prototype objects. The
1742 * array returned by this method contains the same values as would
1743 * be enumerated by a for-in statement over this object.
1744 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001745 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001746
1747 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001748 * This function has the same functionality as GetPropertyNames but
1749 * the returned array doesn't contain the names of properties from
1750 * prototype objects.
1751 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001752 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001753
1754 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 * Get the prototype object. This does not skip objects marked to
1756 * be skipped by __proto__ and it does not consult the security
1757 * handler.
1758 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001759 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760
1761 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001762 * Set the prototype object. This does not skip objects marked to
1763 * be skipped by __proto__ and it does not consult the security
1764 * handler.
1765 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001766 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001767
1768 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001769 * Finds an instance of the given function template in the prototype
1770 * chain.
1771 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001772 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001773
1774 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 * Call builtin Object.prototype.toString on this object.
1776 * This is different from Value::ToString() that may call
1777 * user-defined toString function. This one does not.
1778 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001779 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001781 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001782 * Returns the function invoked as a constructor for this object.
1783 * May be the null value.
1784 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001785 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001786
1787 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001788 * Returns the name of the function invoked as a constructor for this object.
1789 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001790 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001791
kasper.lund212ac232008-07-16 07:07:30 +00001792 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001793 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001794
1795 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001796 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001797
kasper.lund212ac232008-07-16 07:07:30 +00001798 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001799 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001800
1801 /**
1802 * Gets a 2-byte-aligned native pointer from an internal field. This field
1803 * must have been set by SetAlignedPointerInInternalField, everything else
1804 * leads to undefined behavior.
1805 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001806 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001807
1808 /**
1809 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1810 * a field, GetAlignedPointerFromInternalField must be used, everything else
1811 * leads to undefined behavior.
1812 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001813 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001814
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001816 bool HasOwnProperty(Handle<String> key);
1817 bool HasRealNamedProperty(Handle<String> key);
1818 bool HasRealIndexedProperty(uint32_t index);
1819 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820
1821 /**
1822 * If result.IsEmpty() no real property was located in the prototype chain.
1823 * This means interceptors in the prototype chain are not called.
1824 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001825 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001826
1827 /**
1828 * If result.IsEmpty() no real property was located on the object or
1829 * in the prototype chain.
1830 * This means interceptors in the prototype chain are not called.
1831 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001832 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833
1834 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001835 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836
kasper.lund212ac232008-07-16 07:07:30 +00001837 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001838 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001840 /**
1841 * Turns on access check on the object if the object is an instance of
1842 * a template that has access check callbacks. If an object has no
1843 * access check info, the object cannot be accessed by anyone.
1844 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001845 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001846
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001847 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001848 * Returns the identity hash for this object. The current implementation
1849 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001850 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001851 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001852 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001853 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001854 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001855
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001856 /**
1857 * Access hidden properties on JavaScript objects. These properties are
1858 * hidden from the executing JavaScript and only accessible through the V8
1859 * C++ API. Hidden properties introduced by V8 internally (for example the
1860 * identity hash) are prefixed with "v8::".
1861 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001862 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1863 Local<Value> GetHiddenValue(Handle<String> key);
1864 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001865
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001866 /**
1867 * Returns true if this is an instance of an api function (one
1868 * created from a function created from a function template) and has
1869 * been modified since it was created. Note that this method is
1870 * conservative and may return true for objects that haven't actually
1871 * been modified.
1872 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001873 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001874
1875 /**
1876 * Clone this object with a fast but shallow copy. Values will point
1877 * to the same values as the original object.
1878 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001879 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001881 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001882 * Returns the context in which the object was created.
1883 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001884 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001885
1886 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001887 * Set the backing store of the indexed properties to be managed by the
1888 * embedding layer. Access to the indexed properties will follow the rules
1889 * spelled out in CanvasPixelArray.
1890 * Note: The embedding program still owns the data and needs to ensure that
1891 * the backing store is preserved while V8 has a reference.
1892 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001893 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1894 bool HasIndexedPropertiesInPixelData();
1895 uint8_t* GetIndexedPropertiesPixelData();
1896 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001897
ager@chromium.org3811b432009-10-28 14:53:37 +00001898 /**
1899 * Set the backing store of the indexed properties to be managed by the
1900 * embedding layer. Access to the indexed properties will follow the rules
1901 * spelled out for the CanvasArray subtypes in the WebGL specification.
1902 * Note: The embedding program still owns the data and needs to ensure that
1903 * the backing store is preserved while V8 has a reference.
1904 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001905 void SetIndexedPropertiesToExternalArrayData(void* data,
1906 ExternalArrayType array_type,
1907 int number_of_elements);
1908 bool HasIndexedPropertiesInExternalArrayData();
1909 void* GetIndexedPropertiesExternalArrayData();
1910 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1911 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001912
lrn@chromium.org1c092762011-05-09 09:42:16 +00001913 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001914 * Checks whether a callback is set by the
1915 * ObjectTemplate::SetCallAsFunctionHandler method.
1916 * When an Object is callable this method returns true.
1917 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001918 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001919
1920 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001921 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001922 * ObjectTemplate::SetCallAsFunctionHandler method.
1923 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001924 Local<Value> CallAsFunction(Handle<Object> recv,
1925 int argc,
1926 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001927
1928 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001929 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001930 * ObjectTemplate::SetCallAsFunctionHandler method.
1931 * Note: This method behaves like the Function::NewInstance method.
1932 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001933 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001934
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001935 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001936 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001937
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001939 Object();
1940 static void CheckCast(Value* obj);
1941 Local<Value> SlowGetInternalField(int index);
1942 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943};
1944
1945
1946/**
1947 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1948 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001949class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001951 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952
ager@chromium.org3e875802009-06-29 08:26:34 +00001953 /**
1954 * Clones an element at index |index|. Returns an empty
1955 * handle if cloning fails (for any reason).
1956 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001957 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001958
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001959 /**
1960 * Creates a JavaScript array with the given length. If the length
1961 * is negative the returned array will have length 0.
1962 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001963 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001964
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001965 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001967 Array();
1968 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969};
1970
1971
1972/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001973 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001975class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001977 Local<Object> NewInstance() const;
1978 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1979 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1980 void SetName(Handle<String> name);
1981 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001982
1983 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001984 * Name inferred from variable or property assignment of this function.
1985 * Used to facilitate debugging and profiling of JavaScript code written
1986 * in an OO style, where many functions are anonymous but are assigned
1987 * to object properties.
1988 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001989 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001990
1991 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001992 * Returns zero based line number of function body and
1993 * kLineOffsetNotFound if no information available.
1994 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001995 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001996 /**
1997 * Returns zero based column number of function body and
1998 * kLineOffsetNotFound if no information available.
1999 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002000 int GetScriptColumnNumber() const;
2001 Handle<Value> GetScriptId() const;
2002 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002003 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002004 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002005
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002007 Function();
2008 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009};
2010
2011
2012/**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002013 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
2014 * This API is experimental and may change significantly.
2015 */
2016class V8EXPORT ArrayBuffer : public Object {
2017 public:
2018 /**
2019 * Data length in bytes.
2020 */
2021 size_t ByteLength() const;
2022 /**
2023 * Raw pointer to the array buffer data
2024 */
2025 void* Data() const;
2026
2027 /**
2028 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
2029 * Allocated memory will be owned by a created ArrayBuffer and
2030 * will be deallocated when it is garbage-collected.
2031 */
2032 static Local<ArrayBuffer> New(size_t byte_length);
2033
2034 /**
2035 * Create a new ArrayBuffer over an existing memory block.
2036 * The memory block will not be reclaimed when a created ArrayBuffer
2037 * is garbage-collected.
2038 */
2039 static Local<ArrayBuffer> New(void* data, size_t byte_length);
2040
2041 V8_INLINE(static ArrayBuffer* Cast(Value* obj));
2042
2043 private:
2044 ArrayBuffer();
2045 static void CheckCast(Value* obj);
2046};
2047
2048
2049/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002050 * An instance of the built-in Date constructor (ECMA-262, 15.9).
2051 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002052class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002053 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002054 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002055
2056 /**
2057 * A specialization of Value::NumberValue that is more efficient
2058 * because we know the structure of this object.
2059 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002060 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002061
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002062 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002063
2064 /**
2065 * Notification that the embedder has changed the time zone,
2066 * daylight savings time, or other date / time configuration
2067 * parameters. V8 keeps a cache of various values used for
2068 * date / time computation. This notification will reset
2069 * those cached values for the current context so that date /
2070 * time configuration changes would be reflected in the Date
2071 * object.
2072 *
2073 * This API should not be called more than needed as it will
2074 * negatively impact the performance of date operations.
2075 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002076 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002077
2078 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002079 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002080};
2081
2082
2083/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002084 * A Number object (ECMA-262, 4.3.21).
2085 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002086class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002087 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002088 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002089
2090 /**
2091 * Returns the Number held by the object.
2092 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002093 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002094
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002095 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002096
2097 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002098 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002099};
2100
2101
2102/**
2103 * A Boolean object (ECMA-262, 4.3.15).
2104 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002105class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002106 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002107 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002108
2109 /**
2110 * Returns the Boolean held by the object.
2111 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002112 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002113
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002114 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002115
2116 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002117 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002118};
2119
2120
2121/**
2122 * A String object (ECMA-262, 4.3.18).
2123 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002124class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002125 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002126 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002127
2128 /**
2129 * Returns the String held by the object.
2130 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002131 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002132
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002133 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002134
2135 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002136 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002137};
2138
2139
2140/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002141 * A Symbol object (ECMA-262 edition 6).
2142 *
2143 * This is an experimental feature. Use at your own risk.
2144 */
2145class V8EXPORT SymbolObject : public Object {
2146 public:
2147 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2148
2149 /**
2150 * Returns the Symbol held by the object.
2151 */
2152 Local<Symbol> SymbolValue() const;
2153
2154 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2155
2156 private:
2157 static void CheckCast(v8::Value* obj);
2158};
2159
2160
2161/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002162 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2163 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002164class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002165 public:
2166 /**
2167 * Regular expression flag bits. They can be or'ed to enable a set
2168 * of flags.
2169 */
2170 enum Flags {
2171 kNone = 0,
2172 kGlobal = 1,
2173 kIgnoreCase = 2,
2174 kMultiline = 4
2175 };
2176
2177 /**
2178 * Creates a regular expression from the given pattern string and
2179 * the flags bit field. May throw a JavaScript exception as
2180 * described in ECMA-262, 15.10.4.1.
2181 *
2182 * For example,
2183 * RegExp::New(v8::String::New("foo"),
2184 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2185 * is equivalent to evaluating "/foo/gm".
2186 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002187 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002188
2189 /**
2190 * Returns the value of the source property: a string representing
2191 * the regular expression.
2192 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002193 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002194
2195 /**
2196 * Returns the flags bit field.
2197 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002198 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002199
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002200 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002201
2202 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002203 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002204};
2205
2206
2207/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002208 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2209 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002211class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002213 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002214 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002215 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002217 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218};
2219
2220
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002221// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222
2223
2224/**
2225 * The superclass of object and function templates.
2226 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002227class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 public:
2229 /** Adds a property to each instance created by this template.*/
2230 void Set(Handle<String> name, Handle<Data> value,
2231 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002232 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 private:
2234 Template();
2235
2236 friend class ObjectTemplate;
2237 friend class FunctionTemplate;
2238};
2239
2240
2241/**
2242 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002243 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 * including the receiver, the number and values of arguments, and
2245 * the holder of the function.
2246 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002247class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002249 V8_INLINE(int Length() const);
2250 V8_INLINE(Local<Value> operator[](int i) const);
2251 V8_INLINE(Local<Function> Callee() const);
2252 V8_INLINE(Local<Object> This() const);
2253 V8_INLINE(Local<Object> Holder() const);
2254 V8_INLINE(bool IsConstructCall() const);
2255 V8_INLINE(Local<Value> Data() const);
2256 V8_INLINE(Isolate* GetIsolate() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002257
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002259 static const int kIsolateIndex = 0;
2260 static const int kDataIndex = -1;
2261 static const int kCalleeIndex = -2;
2262 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 friend class ImplementationUtilities;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002265 V8_INLINE(Arguments(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002266 internal::Object** values,
2267 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002268 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002269 internal::Object** implicit_args_;
2270 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002272 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273};
2274
2275
2276/**
2277 * The information passed to an accessor callback about the context
2278 * of the property access.
2279 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002280class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002282 V8_INLINE(AccessorInfo(internal::Object** args))
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002283 : args_(args) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002284 V8_INLINE(Isolate* GetIsolate() const);
2285 V8_INLINE(Local<Value> Data() const);
2286 V8_INLINE(Local<Object> This() const);
2287 V8_INLINE(Local<Object> Holder() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002290 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291};
2292
2293
2294typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2295
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297 * NamedProperty[Getter|Setter] are used as interceptors on object.
2298 * See ObjectTemplate::SetNamedPropertyHandler.
2299 */
2300typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2301 const AccessorInfo& info);
2302
2303
2304/**
2305 * Returns the value if the setter intercepts the request.
2306 * Otherwise, returns an empty handle.
2307 */
2308typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2309 Local<Value> value,
2310 const AccessorInfo& info);
2311
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312/**
2313 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002314 * The result is an integer encoding property attributes (like v8::None,
2315 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002317typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2318 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319
2320
2321/**
2322 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002323 * The return value is true if the property could be deleted and false
2324 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325 */
2326typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2327 const AccessorInfo& info);
2328
2329/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002330 * Returns an array containing the names of the properties the named
2331 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332 */
2333typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2334
v8.team.kasperl727e9952008-09-02 14:56:44 +00002335
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002337 * Returns the value of the property if the getter intercepts the
2338 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339 */
2340typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2341 const AccessorInfo& info);
2342
2343
2344/**
2345 * Returns the value if the setter intercepts the request.
2346 * Otherwise, returns an empty handle.
2347 */
2348typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2349 Local<Value> value,
2350 const AccessorInfo& info);
2351
2352
2353/**
2354 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002355 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002356 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002357typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2358 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359
2360/**
2361 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002362 * The return value is true if the property could be deleted and false
2363 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364 */
2365typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2366 const AccessorInfo& info);
2367
v8.team.kasperl727e9952008-09-02 14:56:44 +00002368/**
2369 * Returns an array containing the indices of the properties the
2370 * indexed property getter intercepts.
2371 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2373
2374
2375/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002376 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377 */
2378enum AccessType {
2379 ACCESS_GET,
2380 ACCESS_SET,
2381 ACCESS_HAS,
2382 ACCESS_DELETE,
2383 ACCESS_KEYS
2384};
2385
v8.team.kasperl727e9952008-09-02 14:56:44 +00002386
2387/**
2388 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002389 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002390 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002391typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002392 Local<Value> key,
2393 AccessType type,
2394 Local<Value> data);
2395
v8.team.kasperl727e9952008-09-02 14:56:44 +00002396
2397/**
2398 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002399 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002400 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002401typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402 uint32_t index,
2403 AccessType type,
2404 Local<Value> data);
2405
2406
2407/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002408 * A FunctionTemplate is used to create functions at runtime. There
2409 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002410 * context. The lifetime of the created function is equal to the
2411 * lifetime of the context. So in case the embedder needs to create
2412 * temporary functions that can be collected using Scripts is
2413 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 *
2415 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002416 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002418 * A FunctionTemplate has a corresponding instance template which is
2419 * used to create object instances when the function is used as a
2420 * constructor. Properties added to the instance template are added to
2421 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 *
2423 * A FunctionTemplate can have a prototype template. The prototype template
2424 * is used to create the prototype object of the function.
2425 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002426 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002428 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2430 * t->Set("func_property", v8::Number::New(1));
2431 *
2432 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2433 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2434 * proto_t->Set("proto_const", v8::Number::New(2));
2435 *
2436 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2437 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2438 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2439 * instance_t->Set("instance_property", Number::New(3));
2440 *
2441 * v8::Local<v8::Function> function = t->GetFunction();
2442 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002443 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 *
2445 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002446 * and "instance" for the instance object created above. The function
2447 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002449 * \code
2450 * func_property in function == true;
2451 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002453 * function.prototype.proto_method() invokes 'InvokeCallback'
2454 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002455 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002456 * instance instanceof function == true;
2457 * instance.instance_accessor calls 'InstanceAccessorCallback'
2458 * instance.instance_property == 3;
2459 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002461 * A FunctionTemplate can inherit from another one by calling the
2462 * FunctionTemplate::Inherit method. The following graph illustrates
2463 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002465 * \code
2466 * FunctionTemplate Parent -> Parent() . prototype -> { }
2467 * ^ ^
2468 * | Inherit(Parent) | .__proto__
2469 * | |
2470 * FunctionTemplate Child -> Child() . prototype -> { }
2471 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002473 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2474 * object of the Child() function has __proto__ pointing to the
2475 * Parent() function's prototype object. An instance of the Child
2476 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002478 * Let Parent be the FunctionTemplate initialized in the previous
2479 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002481 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482 * Local<FunctionTemplate> parent = t;
2483 * Local<FunctionTemplate> child = FunctionTemplate::New();
2484 * child->Inherit(parent);
2485 *
2486 * Local<Function> child_function = child->GetFunction();
2487 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002488 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002490 * The Child function and Child instance will have the following
2491 * properties:
2492 *
2493 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002495 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002497 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002499class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500 public:
2501 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002502 static Local<FunctionTemplate> New(
2503 InvocationCallback callback = 0,
2504 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002505 Handle<Signature> signature = Handle<Signature>(),
2506 int length = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507 /** Returns the unique function instance in the current execution context.*/
2508 Local<Function> GetFunction();
2509
v8.team.kasperl727e9952008-09-02 14:56:44 +00002510 /**
2511 * Set the call-handler callback for a FunctionTemplate. This
2512 * callback is called whenever the function created from this
2513 * FunctionTemplate is called.
2514 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515 void SetCallHandler(InvocationCallback callback,
2516 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002518 /** Set the predefined length property for the FunctionTemplate. */
2519 void SetLength(int length);
2520
v8.team.kasperl727e9952008-09-02 14:56:44 +00002521 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 Local<ObjectTemplate> InstanceTemplate();
2523
2524 /** Causes the function template to inherit from a parent function template.*/
2525 void Inherit(Handle<FunctionTemplate> parent);
2526
2527 /**
2528 * A PrototypeTemplate is the template used to create the prototype object
2529 * of the function created by this template.
2530 */
2531 Local<ObjectTemplate> PrototypeTemplate();
2532
v8.team.kasperl727e9952008-09-02 14:56:44 +00002533 /**
2534 * Set the class name of the FunctionTemplate. This is used for
2535 * printing objects created with the function created from the
2536 * FunctionTemplate as its constructor.
2537 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 void SetClassName(Handle<String> name);
2539
2540 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002541 * Determines whether the __proto__ accessor ignores instances of
2542 * the function template. If instances of the function template are
2543 * ignored, __proto__ skips all instances and instead returns the
2544 * next object in the prototype chain.
2545 *
2546 * Call with a value of true to make the __proto__ accessor ignore
2547 * instances of the function template. Call with a value of false
2548 * to make the __proto__ accessor not ignore instances of the
2549 * function template. By default, instances of a function template
2550 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002551 */
2552 void SetHiddenPrototype(bool value);
2553
2554 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002555 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2556 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002557 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002558 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002559
2560 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002561 * Returns true if the given object is an instance of this function
2562 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 */
2564 bool HasInstance(Handle<Value> object);
2565
2566 private:
2567 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2569 NamedPropertySetter setter,
2570 NamedPropertyQuery query,
2571 NamedPropertyDeleter remover,
2572 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002573 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2575 IndexedPropertySetter setter,
2576 IndexedPropertyQuery query,
2577 IndexedPropertyDeleter remover,
2578 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002579 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2581 Handle<Value> data);
2582
2583 friend class Context;
2584 friend class ObjectTemplate;
2585};
2586
2587
2588/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002589 * An ObjectTemplate is used to create objects at runtime.
2590 *
2591 * Properties added to an ObjectTemplate are added to each object
2592 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002594class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002596 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002598
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599 /** Creates a new instance of this template.*/
2600 Local<Object> NewInstance();
2601
2602 /**
2603 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002604 *
2605 * Whenever the property with the given name is accessed on objects
2606 * created from this ObjectTemplate the getter and setter callbacks
2607 * are called instead of getting and setting the property directly
2608 * on the JavaScript object.
2609 *
2610 * \param name The name of the property for which an accessor is added.
2611 * \param getter The callback to invoke when getting the property.
2612 * \param setter The callback to invoke when setting the property.
2613 * \param data A piece of data that will be passed to the getter and setter
2614 * callbacks whenever they are invoked.
2615 * \param settings Access control settings for the accessor. This is a bit
2616 * field consisting of one of more of
2617 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2618 * The default is to not allow cross-context access.
2619 * ALL_CAN_READ means that all cross-context reads are allowed.
2620 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2621 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2622 * cross-context access.
2623 * \param attribute The attributes of the property for which an accessor
2624 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002625 * \param signature The signature describes valid receivers for the accessor
2626 * and is used to perform implicit instance checks against them. If the
2627 * receiver is incompatible (i.e. is not an instance of the constructor as
2628 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2629 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 */
2631 void SetAccessor(Handle<String> name,
2632 AccessorGetter getter,
2633 AccessorSetter setter = 0,
2634 Handle<Value> data = Handle<Value>(),
2635 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002636 PropertyAttribute attribute = None,
2637 Handle<AccessorSignature> signature =
2638 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639
ulan@chromium.org750145a2013-03-07 15:14:13 +00002640 // This function is not yet stable and should not be used at this time.
2641 bool SetAccessor(Handle<String> name,
2642 Handle<DeclaredAccessorDescriptor> descriptor,
2643 AccessControl settings = DEFAULT,
2644 PropertyAttribute attribute = None,
2645 Handle<AccessorSignature> signature =
2646 Handle<AccessorSignature>());
2647
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 /**
2649 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002650 *
2651 * Whenever a named property is accessed on objects created from
2652 * this object template, the provided callback is invoked instead of
2653 * accessing the property directly on the JavaScript object.
2654 *
2655 * \param getter The callback to invoke when getting a property.
2656 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002657 * \param query The callback to invoke to check if a property is present,
2658 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002659 * \param deleter The callback to invoke when deleting a property.
2660 * \param enumerator The callback to invoke to enumerate all the named
2661 * properties of an object.
2662 * \param data A piece of data that will be passed to the callbacks
2663 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 */
2665 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2666 NamedPropertySetter setter = 0,
2667 NamedPropertyQuery query = 0,
2668 NamedPropertyDeleter deleter = 0,
2669 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002670 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671
2672 /**
2673 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002674 *
2675 * Whenever an indexed property is accessed on objects created from
2676 * this object template, the provided callback is invoked instead of
2677 * accessing the property directly on the JavaScript object.
2678 *
2679 * \param getter The callback to invoke when getting a property.
2680 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002681 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002682 * \param deleter The callback to invoke when deleting a property.
2683 * \param enumerator The callback to invoke to enumerate all the indexed
2684 * properties of an object.
2685 * \param data A piece of data that will be passed to the callbacks
2686 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 */
2688 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2689 IndexedPropertySetter setter = 0,
2690 IndexedPropertyQuery query = 0,
2691 IndexedPropertyDeleter deleter = 0,
2692 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002693 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002694
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 /**
2696 * Sets the callback to be used when calling instances created from
2697 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002698 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 * function.
2700 */
2701 void SetCallAsFunctionHandler(InvocationCallback callback,
2702 Handle<Value> data = Handle<Value>());
2703
v8.team.kasperl727e9952008-09-02 14:56:44 +00002704 /**
2705 * Mark object instances of the template as undetectable.
2706 *
2707 * In many ways, undetectable objects behave as though they are not
2708 * there. They behave like 'undefined' in conditionals and when
2709 * printed. However, properties can be accessed and called as on
2710 * normal objects.
2711 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002712 void MarkAsUndetectable();
2713
v8.team.kasperl727e9952008-09-02 14:56:44 +00002714 /**
2715 * Sets access check callbacks on the object template.
2716 *
2717 * When accessing properties on instances of this object template,
2718 * the access check callback will be called to determine whether or
2719 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002720 * The last parameter specifies whether access checks are turned
2721 * on by default on instances. If access checks are off by default,
2722 * they can be turned on on individual instances by calling
2723 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002724 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2726 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002727 Handle<Value> data = Handle<Value>(),
2728 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729
kasper.lund212ac232008-07-16 07:07:30 +00002730 /**
2731 * Gets the number of internal fields for objects generated from
2732 * this template.
2733 */
2734 int InternalFieldCount();
2735
2736 /**
2737 * Sets the number of internal fields for objects generated from
2738 * this template.
2739 */
2740 void SetInternalFieldCount(int value);
2741
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002742 private:
2743 ObjectTemplate();
2744 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2745 friend class FunctionTemplate;
2746};
2747
2748
2749/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002750 * A Signature specifies which receivers and arguments are valid
2751 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002753class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002754 public:
2755 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2756 Handle<FunctionTemplate>(),
2757 int argc = 0,
2758 Handle<FunctionTemplate> argv[] = 0);
2759 private:
2760 Signature();
2761};
2762
2763
2764/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002765 * An AccessorSignature specifies which receivers are valid parameters
2766 * to an accessor callback.
2767 */
2768class V8EXPORT AccessorSignature : public Data {
2769 public:
2770 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2771 Handle<FunctionTemplate>());
2772 private:
2773 AccessorSignature();
2774};
2775
2776
ulan@chromium.org750145a2013-03-07 15:14:13 +00002777class V8EXPORT DeclaredAccessorDescriptor : public Data {
2778 private:
2779 DeclaredAccessorDescriptor();
2780};
2781
2782
2783class V8EXPORT ObjectOperationDescriptor : public Data {
2784 public:
2785 // This function is not yet stable and should not be used at this time.
2786 static Local<RawOperationDescriptor> NewInternalFieldDereference(
2787 Isolate* isolate,
2788 int internal_field);
2789 private:
2790 ObjectOperationDescriptor();
2791};
2792
2793
2794enum DeclaredAccessorDescriptorDataType {
2795 kDescriptorBoolType,
2796 kDescriptorInt8Type, kDescriptorUint8Type,
2797 kDescriptorInt16Type, kDescriptorUint16Type,
2798 kDescriptorInt32Type, kDescriptorUint32Type,
2799 kDescriptorFloatType, kDescriptorDoubleType
2800};
2801
2802
2803class V8EXPORT RawOperationDescriptor : public Data {
2804 public:
2805 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
2806 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
2807 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
2808 int16_t byte_offset);
2809 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
2810 void* compare_value);
2811 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
2812 Isolate* isolate,
2813 DeclaredAccessorDescriptorDataType data_type,
2814 uint8_t bool_offset = 0);
2815 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
2816 uint8_t bitmask,
2817 uint8_t compare_value);
2818 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
2819 Isolate* isolate,
2820 uint16_t bitmask,
2821 uint16_t compare_value);
2822 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
2823 Isolate* isolate,
2824 uint32_t bitmask,
2825 uint32_t compare_value);
2826
2827 private:
2828 RawOperationDescriptor();
2829};
2830
2831
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002832/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002833 * A utility for determining the type of objects based on the template
2834 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002836class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837 public:
2838 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2839 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2840 int match(Handle<Value> value);
2841 private:
2842 TypeSwitch();
2843};
2844
2845
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002846// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002848class V8EXPORT ExternalAsciiStringResourceImpl
2849 : public String::ExternalAsciiStringResource {
2850 public:
2851 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2852 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2853 : data_(data), length_(length) {}
2854 const char* data() const { return data_; }
2855 size_t length() const { return length_; }
2856
2857 private:
2858 const char* data_;
2859 size_t length_;
2860};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861
2862/**
2863 * Ignore
2864 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002865class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002866 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002867 // Note that the strings passed into this constructor must live as long
2868 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002869 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002870 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002871 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002872 const char** deps = 0,
2873 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874 virtual ~Extension() { }
2875 virtual v8::Handle<v8::FunctionTemplate>
2876 GetNativeFunction(v8::Handle<v8::String> name) {
2877 return v8::Handle<v8::FunctionTemplate>();
2878 }
2879
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002880 const char* name() const { return name_; }
2881 size_t source_length() const { return source_length_; }
2882 const String::ExternalAsciiStringResource* source() const {
2883 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002884 int dependency_count() { return dep_count_; }
2885 const char** dependencies() { return deps_; }
2886 void set_auto_enable(bool value) { auto_enable_ = value; }
2887 bool auto_enable() { return auto_enable_; }
2888
2889 private:
2890 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002891 size_t source_length_; // expected to initialize before source_
2892 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002893 int dep_count_;
2894 const char** deps_;
2895 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002896
2897 // Disallow copying and assigning.
2898 Extension(const Extension&);
2899 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900};
2901
2902
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002903void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002904
2905
2906/**
2907 * Ignore
2908 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002909class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002911 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002912 RegisterExtension(extension);
2913 }
2914};
2915
2916
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002917// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918
2919
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002920Handle<Primitive> V8EXPORT Undefined();
2921Handle<Primitive> V8EXPORT Null();
2922Handle<Boolean> V8EXPORT True();
2923Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002924
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002925V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
2926V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
2927V8_INLINE(Handle<Boolean> True(Isolate* isolate));
2928V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002929
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002930
2931/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002932 * A set of constraints that specifies the limits of the runtime's memory use.
2933 * You must set the heap size before initializing the VM - the size cannot be
2934 * adjusted after the VM is initialized.
2935 *
2936 * If you are using threads then you should hold the V8::Locker lock while
2937 * setting the stack limit and you must set a non-default stack limit separately
2938 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002939 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002940class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941 public:
2942 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002943 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002945 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002946 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002947 int max_executable_size() { return max_executable_size_; }
2948 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002949 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002950 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2952 private:
2953 int max_young_space_size_;
2954 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002955 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002956 uint32_t* stack_limit_;
2957};
2958
2959
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002960bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961
2962
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002963// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964
2965
2966typedef void (*FatalErrorCallback)(const char* location, const char* message);
2967
2968
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002969typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970
2971
2972/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002973 * Schedules an exception to be thrown when returning to JavaScript. When an
2974 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002975 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002976 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002977 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002978Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002979
2980/**
2981 * Create new error objects by calling the corresponding error object
2982 * constructor with the message.
2983 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002984class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002985 public:
2986 static Local<Value> RangeError(Handle<String> message);
2987 static Local<Value> ReferenceError(Handle<String> message);
2988 static Local<Value> SyntaxError(Handle<String> message);
2989 static Local<Value> TypeError(Handle<String> message);
2990 static Local<Value> Error(Handle<String> message);
2991};
2992
2993
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002994// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002996typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002997
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002998typedef void* (*CreateHistogramCallback)(const char* name,
2999 int min,
3000 int max,
3001 size_t buckets);
3002
3003typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
3004
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003005// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003006 enum ObjectSpace {
3007 kObjectSpaceNewSpace = 1 << 0,
3008 kObjectSpaceOldPointerSpace = 1 << 1,
3009 kObjectSpaceOldDataSpace = 1 << 2,
3010 kObjectSpaceCodeSpace = 1 << 3,
3011 kObjectSpaceMapSpace = 1 << 4,
3012 kObjectSpaceLoSpace = 1 << 5,
3013
3014 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
3015 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
3016 kObjectSpaceLoSpace
3017 };
3018
3019 enum AllocationAction {
3020 kAllocationActionAllocate = 1 << 0,
3021 kAllocationActionFree = 1 << 1,
3022 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
3023 };
3024
3025typedef void (*MemoryAllocationCallback)(ObjectSpace space,
3026 AllocationAction action,
3027 int size);
3028
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003029// --- Leave Script Callback ---
3030typedef void (*CallCompletedCallback)();
3031
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003032// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003033typedef void (*FailedAccessCheckCallback)(Local<Object> target,
3034 AccessType type,
3035 Local<Value> data);
3036
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003037// --- AllowCodeGenerationFromStrings callbacks ---
3038
3039/**
3040 * Callback to check if code generation from strings is allowed. See
3041 * Context::AllowCodeGenerationFromStrings.
3042 */
3043typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
3044
3045// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046
3047/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003048 * Applications can register callback functions which will be called
3049 * before and after a garbage collection. Allocations are not
3050 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003051 * objects (set or delete properties for example) since it is possible
3052 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003054enum GCType {
3055 kGCTypeScavenge = 1 << 0,
3056 kGCTypeMarkSweepCompact = 1 << 1,
3057 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
3058};
3059
3060enum GCCallbackFlags {
3061 kNoGCCallbackFlags = 0,
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003062 kGCCallbackFlagCompacted = 1 << 0,
3063 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003064};
3065
3066typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
3067typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
3068
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069typedef void (*GCCallback)();
3070
3071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003072/**
ager@chromium.org3811b432009-10-28 14:53:37 +00003073 * Collection of V8 heap information.
3074 *
3075 * Instances of this class can be passed to v8::V8::HeapStatistics to
3076 * get heap statistics from V8.
3077 */
3078class V8EXPORT HeapStatistics {
3079 public:
3080 HeapStatistics();
3081 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003082 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003083 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003084 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003085 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003086
3087 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003088 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003089 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003090 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003091 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003092 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003093
3094 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003095 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003096};
3097
3098
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003099class RetainedObjectInfo;
3100
ager@chromium.org3811b432009-10-28 14:53:37 +00003101/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003102 * Isolate represents an isolated instance of the V8 engine. V8
3103 * isolates have completely separate states. Objects from one isolate
3104 * must not be used in other isolates. When V8 is initialized a
3105 * default isolate is implicitly created and entered. The embedder
3106 * can create additional isolates and use them in parallel in multiple
3107 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003108 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003109 */
3110class V8EXPORT Isolate {
3111 public:
3112 /**
3113 * Stack-allocated class which sets the isolate for all operations
3114 * executed within a local scope.
3115 */
3116 class V8EXPORT Scope {
3117 public:
3118 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3119 isolate->Enter();
3120 }
3121
3122 ~Scope() { isolate_->Exit(); }
3123
3124 private:
3125 Isolate* const isolate_;
3126
3127 // Prevent copying of Scope objects.
3128 Scope(const Scope&);
3129 Scope& operator=(const Scope&);
3130 };
3131
3132 /**
3133 * Creates a new isolate. Does not change the currently entered
3134 * isolate.
3135 *
3136 * When an isolate is no longer used its resources should be freed
3137 * by calling Dispose(). Using the delete operator is not allowed.
3138 */
3139 static Isolate* New();
3140
3141 /**
3142 * Returns the entered isolate for the current thread or NULL in
3143 * case there is no current isolate.
3144 */
3145 static Isolate* GetCurrent();
3146
3147 /**
3148 * Methods below this point require holding a lock (using Locker) in
3149 * a multi-threaded environment.
3150 */
3151
3152 /**
3153 * Sets this isolate as the entered one for the current thread.
3154 * Saves the previously entered one (if any), so that it can be
3155 * restored when exiting. Re-entering an isolate is allowed.
3156 */
3157 void Enter();
3158
3159 /**
3160 * Exits this isolate by restoring the previously entered one in the
3161 * current thread. The isolate may still stay the same, if it was
3162 * entered more than once.
3163 *
3164 * Requires: this == Isolate::GetCurrent().
3165 */
3166 void Exit();
3167
3168 /**
3169 * Disposes the isolate. The isolate must not be entered by any
3170 * thread to be disposable.
3171 */
3172 void Dispose();
3173
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003174 /**
3175 * Associate embedder-specific data with the isolate
3176 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003177 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003178
3179 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003180 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003181 * Returns NULL if SetData has never been called.
3182 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003183 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003184
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003185 /**
3186 * Get statistics about the heap memory usage.
3187 */
3188 void GetHeapStatistics(HeapStatistics* heap_statistics);
3189
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003190 /**
3191 * Adjusts the amount of registered external memory. Used to give V8 an
3192 * indication of the amount of externally allocated memory that is kept alive
3193 * by JavaScript objects. V8 uses this to decide when to perform global
3194 * garbage collections. Registering externally allocated memory will trigger
3195 * global garbage collections more often than it would otherwise in an attempt
3196 * to garbage collect the JavaScript objects that keep the externally
3197 * allocated memory alive.
3198 *
3199 * \param change_in_bytes the change in externally allocated memory that is
3200 * kept alive by JavaScript objects.
3201 * \returns the adjusted value.
3202 */
3203 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3204
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003205 /**
3206 * Returns heap profiler for this isolate. Will return NULL until the isolate
3207 * is initialized.
3208 */
3209 HeapProfiler* GetHeapProfiler();
3210
3211 /**
3212 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3213 * is initialized.
3214 */
3215 CpuProfiler* GetCpuProfiler();
3216
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003217 /** Returns the context that is on the top of the stack. */
3218 Local<Context> GetCurrentContext();
3219
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003220 /**
3221 * Allows the host application to group objects together. If one
3222 * object in the group is alive, all objects in the group are alive.
3223 * After each garbage collection, object groups are removed. It is
3224 * intended to be used in the before-garbage-collection callback
3225 * function, for instance to simulate DOM tree connections among JS
3226 * wrapper objects. Object groups for all dependent handles need to
3227 * be provided for kGCTypeMarkSweepCompact collections, for all other
3228 * garbage collection types it is sufficient to provide object groups
3229 * for partially dependent handles only.
3230 */
3231 void SetObjectGroupId(const Persistent<Value>& object,
3232 UniqueId id);
3233
3234 /**
3235 * Allows the host application to declare implicit references from an object
3236 * group to an object. If the objects of the object group are alive, the child
3237 * object is alive too. After each garbage collection, all implicit references
3238 * are removed. It is intended to be used in the before-garbage-collection
3239 * callback function.
3240 */
3241 void SetReferenceFromGroup(UniqueId id,
3242 const Persistent<Value>& child);
3243
3244 /**
3245 * Allows the host application to declare implicit references from an object
3246 * to another object. If the parent object is alive, the child object is alive
3247 * too. After each garbage collection, all implicit references are removed. It
3248 * is intended to be used in the before-garbage-collection callback function.
3249 */
3250 void SetReference(const Persistent<Object>& parent,
3251 const Persistent<Value>& child);
3252
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003253 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003254 Isolate();
3255 Isolate(const Isolate&);
3256 ~Isolate();
3257 Isolate& operator=(const Isolate&);
3258 void* operator new(size_t size);
3259 void operator delete(void*, size_t);
3260};
3261
3262
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003263class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003264 public:
3265 enum CompressionAlgorithm {
3266 kUncompressed,
3267 kBZip2
3268 };
3269
3270 const char* data;
3271 int compressed_size;
3272 int raw_size;
3273};
3274
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003275
3276/**
3277 * A helper class for driving V8 startup data decompression. It is based on
3278 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3279 * for an embedder to use this class, instead, API functions can be used
3280 * directly.
3281 *
3282 * For an example of the class usage, see the "shell.cc" sample application.
3283 */
3284class V8EXPORT StartupDataDecompressor { // NOLINT
3285 public:
3286 StartupDataDecompressor();
3287 virtual ~StartupDataDecompressor();
3288 int Decompress();
3289
3290 protected:
3291 virtual int DecompressData(char* raw_data,
3292 int* raw_data_size,
3293 const char* compressed_data,
3294 int compressed_data_size) = 0;
3295
3296 private:
3297 char** raw_data;
3298};
3299
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003300
3301/**
3302 * EntropySource is used as a callback function when v8 needs a source
3303 * of entropy.
3304 */
3305typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3306
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003307
3308/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003309 * ReturnAddressLocationResolver is used as a callback function when v8 is
3310 * resolving the location of a return address on the stack. Profilers that
3311 * change the return address on the stack can use this to resolve the stack
3312 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003313 *
3314 * \param return_addr_location points to a location on stack where a machine
3315 * return address resides.
3316 * \returns either return_addr_location, or else a pointer to the profiler's
3317 * copy of the original return address.
3318 *
3319 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003320 */
3321typedef uintptr_t (*ReturnAddressLocationResolver)(
3322 uintptr_t return_addr_location);
3323
3324
3325/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003326 * FunctionEntryHook is the type of the profile entry hook called at entry to
3327 * any generated function when function-level profiling is enabled.
3328 *
3329 * \param function the address of the function that's being entered.
3330 * \param return_addr_location points to a location on stack where the machine
3331 * return address resides. This can be used to identify the caller of
3332 * \p function, and/or modified to divert execution when \p function exits.
3333 *
3334 * \note the entry hook must not cause garbage collection.
3335 */
3336typedef void (*FunctionEntryHook)(uintptr_t function,
3337 uintptr_t return_addr_location);
3338
3339
3340/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003341 * A JIT code event is issued each time code is added, moved or removed.
3342 *
3343 * \note removal events are not currently issued.
3344 */
3345struct JitCodeEvent {
3346 enum EventType {
3347 CODE_ADDED,
3348 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003349 CODE_REMOVED,
3350 CODE_ADD_LINE_POS_INFO,
3351 CODE_START_LINE_INFO_RECORDING,
3352 CODE_END_LINE_INFO_RECORDING
3353 };
3354 // Definition of the code position type. The "POSITION" type means the place
3355 // in the source code which are of interest when making stack traces to
3356 // pin-point the source location of a stack frame as close as possible.
3357 // The "STATEMENT_POSITION" means the place at the beginning of each
3358 // statement, and is used to indicate possible break locations.
3359 enum PositionType {
3360 POSITION,
3361 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003362 };
3363
3364 // Type of event.
3365 EventType type;
3366 // Start of the instructions.
3367 void* code_start;
3368 // Size of the instructions.
3369 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003370 // Script info for CODE_ADDED event.
3371 Handle<Script> script;
3372 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
3373 // code line information which is returned from the
3374 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
3375 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
3376 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003377
3378 union {
3379 // Only valid for CODE_ADDED.
3380 struct {
3381 // Name of the object associated with the code, note that the string is
3382 // not zero-terminated.
3383 const char* str;
3384 // Number of chars in str.
3385 size_t len;
3386 } name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003387
3388 // Only valid for CODE_ADD_LINE_POS_INFO
3389 struct {
3390 // PC offset
3391 size_t offset;
3392 // Code postion
3393 size_t pos;
3394 // The position type.
3395 PositionType position_type;
3396 } line_info;
3397
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003398 // New location of instructions. Only valid for CODE_MOVED.
3399 void* new_code_start;
3400 };
3401};
3402
3403/**
3404 * Option flags passed to the SetJitCodeEventHandler function.
3405 */
3406enum JitCodeEventOptions {
3407 kJitCodeEventDefault = 0,
3408 // Generate callbacks for already existent code.
3409 kJitCodeEventEnumExisting = 1
3410};
3411
3412
3413/**
3414 * Callback function passed to SetJitCodeEventHandler.
3415 *
3416 * \param event code add, move or removal event.
3417 */
3418typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3419
3420
3421/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003422 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003423 */
3424class V8EXPORT ExternalResourceVisitor { // NOLINT
3425 public:
3426 virtual ~ExternalResourceVisitor() {}
3427 virtual void VisitExternalString(Handle<String> string) {}
3428};
3429
3430
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003431/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003432 * Interface for iterating through all the persistent handles in the heap.
3433 */
3434class V8EXPORT PersistentHandleVisitor { // NOLINT
3435 public:
3436 virtual ~PersistentHandleVisitor() {}
3437 virtual void VisitPersistentHandle(Persistent<Value> value,
3438 uint16_t class_id) {}
3439};
3440
3441
3442/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443 * Container class for static utility functions.
3444 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003445class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003446 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003447 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003448 static void SetFatalErrorHandler(FatalErrorCallback that);
3449
v8.team.kasperl727e9952008-09-02 14:56:44 +00003450 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003451 * Set the callback to invoke to check if code generation from
3452 * strings should be allowed.
3453 */
3454 static void SetAllowCodeGenerationFromStringsCallback(
3455 AllowCodeGenerationFromStringsCallback that);
3456
3457 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003458 * Ignore out-of-memory exceptions.
3459 *
3460 * V8 running out of memory is treated as a fatal error by default.
3461 * This means that the fatal error handler is called and that V8 is
3462 * terminated.
3463 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003464 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003465 * out-of-memory situation as a fatal error. This way, the contexts
3466 * that did not cause the out of memory problem might be able to
3467 * continue execution.
3468 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003469 static void IgnoreOutOfMemoryException();
3470
v8.team.kasperl727e9952008-09-02 14:56:44 +00003471 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003472 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003473 * fatal errors such as out-of-memory situations.
3474 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003475 static bool IsDead();
3476
3477 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003478 * The following 4 functions are to be used when V8 is built with
3479 * the 'compress_startup_data' flag enabled. In this case, the
3480 * embedder must decompress startup data prior to initializing V8.
3481 *
3482 * This is how interaction with V8 should look like:
3483 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3484 * v8::StartupData* compressed_data =
3485 * new v8::StartupData[compressed_data_count];
3486 * v8::V8::GetCompressedStartupData(compressed_data);
3487 * ... decompress data (compressed_data can be updated in-place) ...
3488 * v8::V8::SetDecompressedStartupData(compressed_data);
3489 * ... now V8 can be initialized
3490 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003491 *
3492 * A helper class StartupDataDecompressor is provided. It implements
3493 * the protocol of the interaction described above, and can be used in
3494 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003495 */
3496 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3497 static int GetCompressedStartupDataCount();
3498 static void GetCompressedStartupData(StartupData* compressed_data);
3499 static void SetDecompressedStartupData(StartupData* decompressed_data);
3500
3501 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003502 * Adds a message listener.
3503 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003504 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003505 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003506 *
3507 * If data is specified, it will be passed to the callback when it is called.
3508 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003509 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003510 static bool AddMessageListener(MessageCallback that,
3511 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003512
3513 /**
3514 * Remove all message listeners from the specified callback function.
3515 */
3516 static void RemoveMessageListeners(MessageCallback that);
3517
3518 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003519 * Tells V8 to capture current stack trace when uncaught exception occurs
3520 * and report it to the message listeners. The option is off by default.
3521 */
3522 static void SetCaptureStackTraceForUncaughtExceptions(
3523 bool capture,
3524 int frame_limit = 10,
3525 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3526
3527 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003528 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003529 */
3530 static void SetFlagsFromString(const char* str, int length);
3531
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003532 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003533 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003534 */
3535 static void SetFlagsFromCommandLine(int* argc,
3536 char** argv,
3537 bool remove_flags);
3538
kasper.lund7276f142008-07-30 08:49:36 +00003539 /** Get the version string. */
3540 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003541
3542 /**
3543 * Enables the host application to provide a mechanism for recording
3544 * statistics counters.
3545 */
3546 static void SetCounterFunction(CounterLookupCallback);
3547
3548 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003549 * Enables the host application to provide a mechanism for recording
3550 * histograms. The CreateHistogram function returns a
3551 * histogram which will later be passed to the AddHistogramSample
3552 * function.
3553 */
3554 static void SetCreateHistogramFunction(CreateHistogramCallback);
3555 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3556
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003557 /** Callback function for reporting failed access checks.*/
3558 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3559
3560 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003561 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003562 * garbage collection. Allocations are not allowed in the
3563 * callback function, you therefore cannot manipulate objects (set
3564 * or delete properties for example) since it is possible such
3565 * operations will result in the allocation of objects. It is possible
3566 * to specify the GCType filter for your callback. But it is not possible to
3567 * register the same callback function two times with different
3568 * GCType filters.
3569 */
3570 static void AddGCPrologueCallback(
3571 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3572
3573 /**
3574 * This function removes callback which was installed by
3575 * AddGCPrologueCallback function.
3576 */
3577 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3578
3579 /**
3580 * The function is deprecated. Please use AddGCPrologueCallback instead.
3581 * Enables the host application to receive a notification before a
3582 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003583 * callback function, you therefore cannot manipulate objects (set
3584 * or delete properties for example) since it is possible such
3585 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003587 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003588
3589 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003590 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003591 * garbage collection. Allocations are not allowed in the
3592 * callback function, you therefore cannot manipulate objects (set
3593 * or delete properties for example) since it is possible such
3594 * operations will result in the allocation of objects. It is possible
3595 * to specify the GCType filter for your callback. But it is not possible to
3596 * register the same callback function two times with different
3597 * GCType filters.
3598 */
3599 static void AddGCEpilogueCallback(
3600 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3601
3602 /**
3603 * This function removes callback which was installed by
3604 * AddGCEpilogueCallback function.
3605 */
3606 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3607
3608 /**
3609 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3610 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003611 * major garbage collection. Allocations are not allowed in the
3612 * callback function, you therefore cannot manipulate objects (set
3613 * or delete properties for example) since it is possible such
3614 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003616 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003617
3618 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003619 * Enables the host application to provide a mechanism to be notified
3620 * and perform custom logging when V8 Allocates Executable Memory.
3621 */
3622 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3623 ObjectSpace space,
3624 AllocationAction action);
3625
3626 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003627 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003628 */
3629 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3630
3631 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003632 * Adds a callback to notify the host application when a script finished
3633 * running. If a script re-enters the runtime during executing, the
3634 * CallCompletedCallback is only invoked when the outer-most script
3635 * execution ends. Executing scripts inside the callback do not trigger
3636 * further callbacks.
3637 */
3638 static void AddCallCompletedCallback(CallCompletedCallback callback);
3639
3640 /**
3641 * Removes callback that was installed by AddCallCompletedCallback.
3642 */
3643 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3644
3645 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003646 * Allows the host application to group objects together. If one
3647 * object in the group is alive, all objects in the group are alive.
3648 * After each garbage collection, object groups are removed. It is
3649 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003650 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003651 * wrapper objects. Object groups for all dependent handles need to
3652 * be provided for kGCTypeMarkSweepCompact collections, for all other
3653 * garbage collection types it is sufficient to provide object groups
3654 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003655 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003656 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003657 // TODO(marja): deprecate AddObjectGroup. Use Isolate::SetObjectGroupId and
3658 // HeapProfiler::SetRetainedObjectInfo instead.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003659 static void AddObjectGroup(Persistent<Value>* objects,
3660 size_t length,
3661 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003662 static void AddObjectGroup(Isolate* isolate,
3663 Persistent<Value>* objects,
3664 size_t length,
3665 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003666
3667 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003668 * Allows the host application to declare implicit references between
3669 * the objects: if |parent| is alive, all |children| are alive too.
3670 * After each garbage collection, all implicit references
3671 * are removed. It is intended to be used in the before-garbage-collection
3672 * callback function.
3673 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003674 // TODO(marja): Deprecate AddImplicitReferences. Use
3675 // Isolate::SetReferenceFromGroup instead.
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003676 static void AddImplicitReferences(Persistent<Object> parent,
3677 Persistent<Value>* children,
3678 size_t length);
3679
3680 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003681 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003682 * initialize from scratch. This function is called implicitly if
3683 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003684 */
3685 static bool Initialize();
3686
kasper.lund7276f142008-07-30 08:49:36 +00003687 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003688 * Allows the host application to provide a callback which can be used
3689 * as a source of entropy for random number generators.
3690 */
3691 static void SetEntropySource(EntropySource source);
3692
3693 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003694 * Allows the host application to provide a callback that allows v8 to
3695 * cooperate with a profiler that rewrites return addresses on stack.
3696 */
3697 static void SetReturnAddressLocationResolver(
3698 ReturnAddressLocationResolver return_address_resolver);
3699
3700 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003701 * Allows the host application to provide the address of a function that's
3702 * invoked on entry to every V8-generated function.
3703 * Note that \p entry_hook is invoked at the very start of each
3704 * generated function.
3705 *
3706 * \param entry_hook a function that will be invoked on entry to every
3707 * V8-generated function.
3708 * \returns true on success on supported platforms, false on failure.
3709 * \note Setting a new entry hook function when one is already active will
3710 * fail.
3711 */
3712 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3713
3714 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003715 * Allows the host application to provide the address of a function that is
3716 * notified each time code is added, moved or removed.
3717 *
3718 * \param options options for the JIT code event handler.
3719 * \param event_handler the JIT code event handler, which will be invoked
3720 * each time code is added, moved or removed.
3721 * \note \p event_handler won't get notified of existent code.
3722 * \note since code removal notifications are not currently issued, the
3723 * \p event_handler may get notifications of code that overlaps earlier
3724 * code notifications. This happens when code areas are reused, and the
3725 * earlier overlapping code areas should therefore be discarded.
3726 * \note the events passed to \p event_handler and the strings they point to
3727 * are not guaranteed to live past each call. The \p event_handler must
3728 * copy strings and other parameters it needs to keep around.
3729 * \note the set of events declared in JitCodeEvent::EventType is expected to
3730 * grow over time, and the JitCodeEvent structure is expected to accrue
3731 * new members. The \p event_handler function must ignore event codes
3732 * it does not recognize to maintain future compatibility.
3733 */
3734 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3735 JitCodeEventHandler event_handler);
3736
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003737 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
3738 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003739 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3740 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003741
iposva@chromium.org245aa852009-02-10 00:49:54 +00003742 /**
3743 * Suspends recording of tick samples in the profiler.
3744 * When the V8 profiling mode is enabled (usually via command line
3745 * switches) this function suspends recording of tick samples.
3746 * Profiling ticks are discarded until ResumeProfiler() is called.
3747 *
3748 * See also the --prof and --prof_auto command line switches to
3749 * enable V8 profiling.
3750 */
3751 static void PauseProfiler();
3752
3753 /**
3754 * Resumes recording of tick samples in the profiler.
3755 * See also PauseProfiler().
3756 */
3757 static void ResumeProfiler();
3758
ager@chromium.org41826e72009-03-30 13:30:57 +00003759 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003760 * Return whether profiler is currently paused.
3761 */
3762 static bool IsProfilerPaused();
3763
3764 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003765 * Retrieve the V8 thread id of the calling thread.
3766 *
3767 * The thread id for a thread should only be retrieved after the V8
3768 * lock has been acquired with a Locker object with that thread.
3769 */
3770 static int GetCurrentThreadId();
3771
3772 /**
3773 * Forcefully terminate execution of a JavaScript thread. This can
3774 * be used to terminate long-running scripts.
3775 *
3776 * TerminateExecution should only be called when then V8 lock has
3777 * been acquired with a Locker object. Therefore, in order to be
3778 * able to terminate long-running threads, preemption must be
3779 * enabled to allow the user of TerminateExecution to acquire the
3780 * lock.
3781 *
3782 * The termination is achieved by throwing an exception that is
3783 * uncatchable by JavaScript exception handlers. Termination
3784 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003785 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003786 * exception handler that catches an exception should check if that
3787 * exception is a termination exception and immediately return if
3788 * that is the case. Returning immediately in that case will
3789 * continue the propagation of the termination exception if needed.
3790 *
3791 * The thread id passed to TerminateExecution must have been
3792 * obtained by calling GetCurrentThreadId on the thread in question.
3793 *
3794 * \param thread_id The thread id of the thread to terminate.
3795 */
3796 static void TerminateExecution(int thread_id);
3797
3798 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003799 * Forcefully terminate the current thread of JavaScript execution
3800 * in the given isolate. If no isolate is provided, the default
3801 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003802 *
3803 * This method can be used by any thread even if that thread has not
3804 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003805 *
3806 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003807 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003808 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003809
3810 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003811 * Is V8 terminating JavaScript execution.
3812 *
3813 * Returns true if JavaScript execution is currently terminating
3814 * because of a call to TerminateExecution. In that case there are
3815 * still JavaScript frames on the stack and the termination
3816 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003817 *
3818 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003819 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003820 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003821
3822 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003823 * Resume execution capability in the given isolate, whose execution
3824 * was previously forcefully terminated using TerminateExecution().
3825 *
3826 * When execution is forcefully terminated using TerminateExecution(),
3827 * the isolate can not resume execution until all JavaScript frames
3828 * have propagated the uncatchable exception which is generated. This
3829 * method allows the program embedding the engine to handle the
3830 * termination event and resume execution capability, even if
3831 * JavaScript frames remain on the stack.
3832 *
3833 * This method can be used by any thread even if that thread has not
3834 * acquired the V8 lock with a Locker object.
3835 *
3836 * \param isolate The isolate in which to resume execution capability.
3837 */
3838 static void CancelTerminateExecution(Isolate* isolate);
3839
3840 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003841 * Releases any resources used by v8 and stops any utility threads
3842 * that may be running. Note that disposing v8 is permanent, it
3843 * cannot be reinitialized.
3844 *
3845 * It should generally not be necessary to dispose v8 before exiting
3846 * a process, this should happen automatically. It is only necessary
3847 * to use if the process needs the resources taken up by v8.
3848 */
3849 static bool Dispose();
3850
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003851 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
3852 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003853
3854 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003855 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003856 * heap. GC is not invoked prior to iterating, therefore there is no
3857 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003858 */
3859 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3860
3861 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003862 * Iterates through all the persistent handles in the current isolate's heap
3863 * that have class_ids.
3864 */
3865 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3866
3867 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003868 * Iterates through all the persistent handles in the current isolate's heap
3869 * that have class_ids and are candidates to be marked as partially dependent
3870 * handles. This will visit handles to young objects created since the last
3871 * garbage collection but is free to visit an arbitrary superset of these
3872 * objects.
3873 */
3874 static void VisitHandlesForPartialDependence(
3875 Isolate* isolate, PersistentHandleVisitor* visitor);
3876
3877 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003878 * Optional notification that the embedder is idle.
3879 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003880 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003881 * Returns true if the embedder should stop calling IdleNotification
3882 * until real work has been done. This indicates that V8 has done
3883 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003884 *
3885 * The hint argument specifies the amount of work to be done in the function
3886 * on scale from 1 to 1000. There is no guarantee that the actual work will
3887 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003888 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003889 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003890
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003891 /**
3892 * Optional notification that the system is running low on memory.
3893 * V8 uses these notifications to attempt to free memory.
3894 */
3895 static void LowMemoryNotification();
3896
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003897 /**
3898 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003899 * these notifications to guide the GC heuristic. Returns the number
3900 * of context disposals - including this one - since the last time
3901 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003902 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003903 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003904
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003905 private:
3906 V8();
3907
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003908 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
3909 internal::Object** handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003910 static void DisposeGlobal(internal::Isolate* isolate,
3911 internal::Object** global_handle);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003912 static void MakeWeak(internal::Isolate* isolate,
3913 internal::Object** global_handle,
3914 void* data,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003915 WeakReferenceCallback weak_reference_callback,
3916 NearDeathCallback near_death_callback);
3917 static void ClearWeak(internal::Isolate* isolate,
3918 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919
3920 template <class T> friend class Handle;
3921 template <class T> friend class Local;
3922 template <class T> friend class Persistent;
3923 friend class Context;
3924};
3925
3926
3927/**
3928 * An external exception handler.
3929 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003930class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003931 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003932 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003933 * Creates a new try/catch block and registers it with v8. Note that
3934 * all TryCatch blocks should be stack allocated because the memory
3935 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936 */
3937 TryCatch();
3938
3939 /**
3940 * Unregisters and deletes this try/catch block.
3941 */
3942 ~TryCatch();
3943
3944 /**
3945 * Returns true if an exception has been caught by this try/catch block.
3946 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003947 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003948
3949 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003950 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003951 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003952 * If CanContinue returns false, the correct action is to perform any C++
3953 * cleanup needed and then return. If CanContinue returns false and
3954 * HasTerminated returns true, it is possible to call
3955 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003956 */
3957 bool CanContinue() const;
3958
3959 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003960 * Returns true if an exception has been caught due to script execution
3961 * being terminated.
3962 *
3963 * There is no JavaScript representation of an execution termination
3964 * exception. Such exceptions are thrown when the TerminateExecution
3965 * methods are called to terminate a long-running script.
3966 *
3967 * If such an exception has been thrown, HasTerminated will return true,
3968 * indicating that it is possible to call CancelTerminateExecution in order
3969 * to continue calling into the engine.
3970 */
3971 bool HasTerminated() const;
3972
3973 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003974 * Throws the exception caught by this TryCatch in a way that avoids
3975 * it being caught again by this same TryCatch. As with ThrowException
3976 * it is illegal to execute any JavaScript operations after calling
3977 * ReThrow; the caller must return immediately to where the exception
3978 * is caught.
3979 */
3980 Handle<Value> ReThrow();
3981
3982 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003983 * Returns the exception caught by this try/catch block. If no exception has
3984 * been caught an empty handle is returned.
3985 *
3986 * The returned handle is valid until this TryCatch block has been destroyed.
3987 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003988 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003989
3990 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003991 * Returns the .stack property of the thrown object. If no .stack
3992 * property is present an empty handle is returned.
3993 */
3994 Local<Value> StackTrace() const;
3995
3996 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003997 * Returns the message associated with this exception. If there is
3998 * no message associated an empty handle is returned.
3999 *
4000 * The returned handle is valid until this TryCatch block has been
4001 * destroyed.
4002 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004003 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004004
4005 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004006 * Clears any exceptions that may have been caught by this try/catch block.
4007 * After this method has been called, HasCaught() will return false.
4008 *
4009 * It is not necessary to clear a try/catch block before using it again; if
4010 * another exception is thrown the previously caught exception will just be
4011 * overwritten. However, it is often a good idea since it makes it easier
4012 * to determine which operation threw a given exception.
4013 */
4014 void Reset();
4015
v8.team.kasperl727e9952008-09-02 14:56:44 +00004016 /**
4017 * Set verbosity of the external exception handler.
4018 *
4019 * By default, exceptions that are caught by an external exception
4020 * handler are not reported. Call SetVerbose with true on an
4021 * external exception handler to have exceptions caught by the
4022 * handler reported as if they were not caught.
4023 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 void SetVerbose(bool value);
4025
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004026 /**
4027 * Set whether or not this TryCatch should capture a Message object
4028 * which holds source information about where the exception
4029 * occurred. True by default.
4030 */
4031 void SetCaptureMessage(bool value);
4032
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004033 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004034 // Make it hard to create heap-allocated TryCatch blocks.
4035 TryCatch(const TryCatch&);
4036 void operator=(const TryCatch&);
4037 void* operator new(size_t size);
4038 void operator delete(void*, size_t);
4039
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004040 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004041 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004042 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004043 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004044 bool is_verbose_ : 1;
4045 bool can_continue_ : 1;
4046 bool capture_message_ : 1;
4047 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004048 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004049
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004050 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004051};
4052
4053
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004054// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004055
4056
4057/**
4058 * Ignore
4059 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004060class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004061 public:
4062 ExtensionConfiguration(int name_count, const char* names[])
4063 : name_count_(name_count), names_(names) { }
4064 private:
4065 friend class ImplementationUtilities;
4066 int name_count_;
4067 const char** names_;
4068};
4069
4070
4071/**
4072 * A sandboxed execution context with its own set of built-in objects
4073 * and functions.
4074 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004075class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004076 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004077 /**
4078 * Returns the global proxy object or global object itself for
4079 * detached contexts.
4080 *
4081 * Global proxy object is a thin wrapper whose prototype points to
4082 * actual context's global object with the properties like Object, etc.
4083 * This is done that way for security reasons (for more details see
4084 * https://wiki.mozilla.org/Gecko:SplitWindow).
4085 *
4086 * Please note that changes to global proxy object prototype most probably
4087 * would break VM---v8 expects only global object as a prototype of
4088 * global proxy object.
4089 *
4090 * If DetachGlobal() has been invoked, Global() would return actual global
4091 * object until global is reattached with ReattachGlobal().
4092 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004093 Local<Object> Global();
4094
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004095 /**
4096 * Detaches the global object from its context before
4097 * the global object can be reused to create a new context.
4098 */
4099 void DetachGlobal();
4100
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00004101 /**
4102 * Reattaches a global object to a context. This can be used to
4103 * restore the connection between a global object and a context
4104 * after DetachGlobal has been called.
4105 *
4106 * \param global_object The global object to reattach to the
4107 * context. For this to work, the global object must be the global
4108 * object that was associated with this context before a call to
4109 * DetachGlobal.
4110 */
4111 void ReattachGlobal(Handle<Object> global_object);
4112
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004113 /**
4114 * Creates a new context and returns a handle to the newly allocated
4115 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004116 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004117 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004118 *
4119 * \param extensions An optional extension configuration containing
4120 * the extensions to be installed in the newly created context.
4121 *
4122 * \param global_template An optional object template from which the
4123 * global object for the newly created context will be created.
4124 *
4125 * \param global_object An optional global object to be reused for
4126 * the newly created context. This global object must have been
4127 * created by a previous call to Context::New with the same global
4128 * template. The state of the global object will be completely reset
4129 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004130 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004131 static Local<Context> New(
4132 Isolate* isolate,
4133 ExtensionConfiguration* extensions = NULL,
4134 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4135 Handle<Value> global_object = Handle<Value>());
4136
4137 /** Deprecated. Use Isolate version instead. */
4138 // TODO(mstarzinger): Put this behind the V8_DEPRECATED guard.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004139 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004140 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004141 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4142 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004143
kasper.lund44510672008-07-25 07:37:58 +00004144 /** Returns the last entered context. */
4145 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004147 // TODO(svenpanne) Actually deprecate this.
4148 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004149 static Local<Context> GetCurrent();
4150
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004151 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004152 * Returns the context of the calling JavaScript code. That is the
4153 * context of the top-most JavaScript frame. If there are no
4154 * JavaScript frames an empty handle is returned.
4155 */
4156 static Local<Context> GetCalling();
4157
4158 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004159 * Sets the security token for the context. To access an object in
4160 * another context, the security tokens must match.
4161 */
4162 void SetSecurityToken(Handle<Value> token);
4163
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004164 /** Restores the security token to the default value. */
4165 void UseDefaultSecurityToken();
4166
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004167 /** Returns the security token of this context.*/
4168 Handle<Value> GetSecurityToken();
4169
v8.team.kasperl727e9952008-09-02 14:56:44 +00004170 /**
4171 * Enter this context. After entering a context, all code compiled
4172 * and run is compiled and run in this context. If another context
4173 * is already entered, this old context is saved so it can be
4174 * restored when the new context is exited.
4175 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004176 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004177
4178 /**
4179 * Exit this context. Exiting the current context restores the
4180 * context that was in place when entering the current context.
4181 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004182 void Exit();
4183
v8.team.kasperl727e9952008-09-02 14:56:44 +00004184 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004185 bool HasOutOfMemoryException();
4186
v8.team.kasperl727e9952008-09-02 14:56:44 +00004187 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004188 static bool InContext();
4189
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004190 /** Returns an isolate associated with a current context. */
4191 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004192
4193 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004194 * Gets the embedder data with the given index, which must have been set by a
4195 * previous call to SetEmbedderData with the same index. Note that index 0
4196 * currently has a special meaning for Chrome's debugger.
4197 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004198 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004199
4200 /**
4201 * Sets the embedder data with the given index, growing the data as
4202 * needed. Note that index 0 currently has a special meaning for Chrome's
4203 * debugger.
4204 */
4205 void SetEmbedderData(int index, Handle<Value> value);
4206
4207 /**
4208 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4209 * index, which must have bees set by a previous call to
4210 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4211 * currently has a special meaning for Chrome's debugger.
4212 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004213 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004214
4215 /**
4216 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4217 * index, growing the data as needed. Note that index 0 currently has a
4218 * special meaning for Chrome's debugger.
4219 */
4220 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004221
4222 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004223 * Control whether code generation from strings is allowed. Calling
4224 * this method with false will disable 'eval' and the 'Function'
4225 * constructor for code running in this context. If 'eval' or the
4226 * 'Function' constructor are used an exception will be thrown.
4227 *
4228 * If code generation from strings is not allowed the
4229 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4230 * set before blocking the call to 'eval' or the 'Function'
4231 * constructor. If that callback returns true, the call will be
4232 * allowed, otherwise an exception will be thrown. If no callback is
4233 * set an exception will be thrown.
4234 */
4235 void AllowCodeGenerationFromStrings(bool allow);
4236
4237 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004238 * Returns true if code generation from strings is allowed for the context.
4239 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4240 */
4241 bool IsCodeGenerationFromStringsAllowed();
4242
4243 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004244 * Sets the error description for the exception that is thrown when
4245 * code generation from strings is not allowed and 'eval' or the 'Function'
4246 * constructor are called.
4247 */
4248 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4249
4250 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004251 * Stack-allocated class which sets the execution context for all
4252 * operations executed within a local scope.
4253 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004254 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004255 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004256 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004257 context_->Enter();
4258 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004259 V8_INLINE(~Scope()) { context_->Exit(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004260 private:
4261 Handle<Context> context_;
4262 };
4263
4264 private:
4265 friend class Value;
4266 friend class Script;
4267 friend class Object;
4268 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004269
4270 Local<Value> SlowGetEmbedderData(int index);
4271 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004272};
4273
4274
4275/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004276 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4277 * to use any given V8 isolate, see the comments in the Isolate class. The
4278 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4279 * object pointers obtained from V8 handles while in the particular V8 isolate.
4280 * It is up to the user of V8 to ensure, perhaps with locking, that this
4281 * constraint is not violated. In addition to any other synchronization
4282 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4283 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004284 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004285 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4286 * construction and destruction, the current thread is allowed to use the locked
4287 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4288 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004289 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004290 * Sample usage:
4291* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004292 * ...
4293 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004294 * v8::Locker locker(isolate);
4295 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004296 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004297 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004298 * ...
4299 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00004300 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004301 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004302 * If you wish to stop using V8 in a thread A you can do this either by
4303 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
4304 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004305 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004306 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004307 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004308 * isolate->Exit();
4309 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004310 * ...
4311 * // Code not using V8 goes here while V8 can run in another thread.
4312 * ...
4313 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004314 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004315 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004316 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004317 * The Unlocker object is intended for use in a long-running callback from V8,
4318 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004319 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004320 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
4321 * given thread. This can be useful if you have code that can be called either
4322 * from code that holds the lock or from code that does not. The Unlocker is
4323 * not recursive so you can not have several Unlockers on the stack at once, and
4324 * you can not use an Unlocker in a thread that is not inside a Locker's scope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004325 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004326 * An unlocker will unlock several lockers if it has to and reinstate the
4327 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004328 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004329 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004330 * // V8 not locked.
4331 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004332 * v8::Locker locker(isolate);
4333 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004334 * // V8 locked.
4335 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004336 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004337 * // V8 still locked (2 levels).
4338 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004339 * isolate->Exit();
4340 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004341 * // V8 not locked.
4342 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004343 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004344 * // V8 locked again (2 levels).
4345 * }
4346 * // V8 still locked (1 level).
4347 * }
4348 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004349 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004350 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004351class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004352 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004353 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004354 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004355 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004356 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
4357
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004358 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004359 V8_DEPRECATED(Unlocker());
4360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004361 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00004362 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004363 void Initialize(Isolate* isolate);
4364
lrn@chromium.org1c092762011-05-09 09:42:16 +00004365 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004366};
4367
4368
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004369class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004370 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004371 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004372 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004373 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004374 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
4375
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004376 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004377 V8_DEPRECATED(Locker());
4378
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004379 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004380
4381 /**
4382 * Start preemption.
4383 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004384 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004385 * that will switch between multiple threads that are in contention
4386 * for the V8 lock.
4387 */
4388 static void StartPreemption(int every_n_ms);
4389
4390 /**
4391 * Stop preemption.
4392 */
4393 static void StopPreemption();
4394
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004395 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004396 * Returns whether or not the locker for a given isolate, is locked by the
4397 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004398 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004399 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004400
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004401 /**
4402 * Returns whether v8::Locker is being used by this V8 instance.
4403 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004404 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004405
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004406 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004407 void Initialize(Isolate* isolate);
4408
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004409 bool has_lock_;
4410 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004411 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004412
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004413 static bool active_;
4414
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004415 // Disallow copying and assigning.
4416 Locker(const Locker&);
4417 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004418};
4419
4420
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004421/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004422 * A struct for exporting HeapStats data from V8, using "push" model.
4423 */
4424struct HeapStatsUpdate;
4425
4426
4427/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004428 * An interface for exporting data from V8, using "push" model.
4429 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004430class V8EXPORT OutputStream { // NOLINT
4431 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004432 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004433 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004434 };
4435 enum WriteResult {
4436 kContinue = 0,
4437 kAbort = 1
4438 };
4439 virtual ~OutputStream() {}
4440 /** Notify about the end of stream. */
4441 virtual void EndOfStream() = 0;
4442 /** Get preferred output chunk size. Called only once. */
4443 virtual int GetChunkSize() { return 1024; }
4444 /** Get preferred output encoding. Called only once. */
4445 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4446 /**
4447 * Writes the next chunk of snapshot data into the stream. Writing
4448 * can be stopped by returning kAbort as function result. EndOfStream
4449 * will not be called in case writing was aborted.
4450 */
4451 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004452 /**
4453 * Writes the next chunk of heap stats data into the stream. Writing
4454 * can be stopped by returning kAbort as function result. EndOfStream
4455 * will not be called in case writing was aborted.
4456 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004457 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004458 return kAbort;
4459 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004460};
4461
4462
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004463/**
4464 * An interface for reporting progress and controlling long-running
4465 * activities.
4466 */
4467class V8EXPORT ActivityControl { // NOLINT
4468 public:
4469 enum ControlOption {
4470 kContinue = 0,
4471 kAbort = 1
4472 };
4473 virtual ~ActivityControl() {}
4474 /**
4475 * Notify about current progress. The activity can be stopped by
4476 * returning kAbort as the callback result.
4477 */
4478 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4479};
4480
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004481
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004482// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004483
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004484
4485namespace internal {
4486
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004487const int kApiPointerSize = sizeof(void*); // NOLINT
4488const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004489
4490// Tag information for HeapObject.
4491const int kHeapObjectTag = 1;
4492const int kHeapObjectTagSize = 2;
4493const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4494
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004495// Tag information for Smi.
4496const int kSmiTag = 0;
4497const int kSmiTagSize = 1;
4498const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4499
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004500template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004501
4502// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004503template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004504 static const int kSmiShiftSize = 0;
4505 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004506 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004507 int shift_bits = kSmiTagSize + kSmiShiftSize;
4508 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4509 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4510 }
4511};
4512
4513// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004514template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004515 static const int kSmiShiftSize = 31;
4516 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004517 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004518 int shift_bits = kSmiTagSize + kSmiShiftSize;
4519 // Shift down and throw away top 32 bits.
4520 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4521 }
4522};
4523
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004524typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4525const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4526const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004527
4528/**
4529 * This class exports constants and functionality from within v8 that
4530 * is necessary to implement inline functions in the v8 api. Don't
4531 * depend on functions and constants defined here.
4532 */
4533class Internals {
4534 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004535 // These values match non-compiler-dependent values defined within
4536 // the implementation of v8.
4537 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004538 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004539 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004540
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004541 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004542 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004543 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004544 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4545 static const int kContextHeaderSize = 2 * kApiPointerSize;
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004546 static const int kContextEmbedderDataIndex = 56;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004547 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004548 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004549 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004550 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004551
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004552 static const int kIsolateStateOffset = 0;
4553 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4554 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4555 static const int kUndefinedValueRootIndex = 5;
4556 static const int kNullValueRootIndex = 7;
4557 static const int kTrueValueRootIndex = 8;
4558 static const int kFalseValueRootIndex = 9;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004559 static const int kEmptyStringRootIndex = 118;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004560
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004561 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
4562 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
4563 static const int kNodeStateMask = 0xf;
4564 static const int kNodeStateIsWeakValue = 2;
4565 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004566 static const int kNodeIsIndependentShift = 4;
4567 static const int kNodeIsPartiallyDependentShift = 5;
4568
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004569 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004570 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004571 static const int kOddballType = 0x83;
4572 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004573
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004574 static const int kUndefinedOddballKind = 5;
4575 static const int kNullOddballKind = 3;
4576
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004577 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004578 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4579 kHeapObjectTag);
4580 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004581
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004582 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004583 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004584 }
4585
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004586 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004587 typedef internal::Object O;
4588 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4589 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4590 }
4591
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004592 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004593 typedef internal::Object O;
4594 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4595 }
4596
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004597 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004598 int representation = (instance_type & kFullStringRepresentationMask);
4599 return representation == kExternalTwoByteRepresentationTag;
4600 }
4601
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004602 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004603 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4604 return *reinterpret_cast<int*>(addr) == 1;
4605 }
4606
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004607 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
4608 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4609 return *addr & (1 << shift);
4610 }
4611
4612 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
4613 bool value, int shift)) {
4614 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4615 uint8_t mask = 1 << shift;
4616 *addr = (*addr & ~mask) | (value << shift);
4617 }
4618
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004619 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
4620 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4621 return *addr & kNodeStateMask;
4622 }
4623
4624 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
4625 uint8_t value)) {
4626 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4627 *addr = (*addr & ~kNodeStateMask) | value;
4628 }
4629
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004630 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004631 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4632 kIsolateEmbedderDataOffset;
4633 *reinterpret_cast<void**>(addr) = data;
4634 }
4635
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004636 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004637 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4638 kIsolateEmbedderDataOffset;
4639 return *reinterpret_cast<void**>(addr);
4640 }
4641
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004642 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
4643 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004644 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4645 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4646 }
4647
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004648 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004649 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004650 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4651 return *reinterpret_cast<T*>(addr);
4652 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004653
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004654 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004655 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004656 typedef internal::Object O;
4657 typedef internal::Internals I;
4658 O* ctx = *reinterpret_cast<O**>(context);
4659 int embedder_data_offset = I::kContextHeaderSize +
4660 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4661 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4662 int value_offset =
4663 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4664 return I::ReadField<T>(embedder_data, value_offset);
4665 }
4666
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004667 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
4668 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
4669 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
4670 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
4671 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
4672 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
4673 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004674};
4675
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004676} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004677
4678
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004679template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004680Local<T>::Local() : Handle<T>() { }
4681
4682
4683template <class T>
4684Local<T> Local<T>::New(Handle<T> that) {
4685 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004686 T* that_ptr = *that;
4687 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4688 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4689 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4690 reinterpret_cast<internal::HeapObject*>(*p))));
4691 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004692 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4693}
4694
4695
4696template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004697Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004698 if (that.IsEmpty()) return Local<T>();
4699 T* that_ptr = *that;
4700 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4701 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4702 reinterpret_cast<internal::Isolate*>(isolate), *p)));
4703}
4704
4705
4706template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004707Persistent<T> Persistent<T>::New(Handle<T> that) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004708 return New(Isolate::GetCurrent(), that);
4709}
4710
4711
4712template <class T>
4713Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004714 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004715 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004716 return Persistent<T>(reinterpret_cast<T*>(
4717 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
4718 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004719}
4720
4721
4722template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004723bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004724 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004725}
4726
4727
4728template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004729bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004730 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004731 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004732 if (!I::IsInitialized(isolate)) return false;
4733 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(**this),
4734 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004735}
4736
4737
4738template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004739bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004740 return IsNearDeath(Isolate::GetCurrent());
4741}
4742
4743
4744template <class T>
4745bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
4746 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004747 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004748 if (!I::IsInitialized(isolate)) return false;
4749 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4750 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004751}
4752
4753
4754template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004755bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004756 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004757}
4758
4759
4760template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004761bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004762 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004763 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004764 if (!I::IsInitialized(isolate)) return false;
4765 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4766 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004767}
4768
4769
4770template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004771void Persistent<T>::Dispose() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004772 Dispose(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004773}
4774
4775
4776template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004777void Persistent<T>::Dispose(Isolate* isolate) {
4778 if (this->IsEmpty()) return;
4779 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4780 reinterpret_cast<internal::Object**>(**this));
4781}
4782
4783
4784template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004785Persistent<T>::Persistent() : Handle<T>() { }
4786
4787template <class T>
4788void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004789 Isolate* isolate = Isolate::GetCurrent();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004790 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4791 reinterpret_cast<internal::Object**>(**this),
4792 parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004793 callback,
4794 NULL);
4795}
4796
4797template <class T>
4798void Persistent<T>::MakeWeak(Isolate* isolate,
4799 void* parameters,
4800 NearDeathCallback callback) {
4801 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4802 reinterpret_cast<internal::Object**>(**this),
4803 parameters,
4804 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004805 callback);
4806}
4807
4808template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004809void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004810 ClearWeak(Isolate::GetCurrent());
4811}
4812
4813template <class T>
4814void Persistent<T>::ClearWeak(Isolate* isolate) {
4815 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
4816 reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004817}
4818
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004819template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004820void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004821 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004822}
4823
4824template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004825void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004826 typedef internal::Internals I;
4827 if (this->IsEmpty()) return;
4828 if (!I::IsInitialized(isolate)) return;
4829 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004830 true,
4831 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004832}
4833
4834template <class T>
4835void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004836 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004837}
4838
4839template <class T>
4840void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004841 typedef internal::Internals I;
4842 if (this->IsEmpty()) return;
4843 if (!I::IsInitialized(isolate)) return;
4844 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004845 true,
4846 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004847}
4848
4849template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004850void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004851 SetWrapperClassId(Isolate::GetCurrent(), class_id);
4852}
4853
4854template <class T>
4855void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004856 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004857 if (this->IsEmpty()) return;
4858 if (!I::IsInitialized(isolate)) return;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004859 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4860 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4861 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004862}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004863
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004864template <class T>
4865uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004866 return WrapperClassId(Isolate::GetCurrent());
4867}
4868
4869template <class T>
4870uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004871 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004872 if (this->IsEmpty()) return 0;
4873 if (!I::IsInitialized(isolate)) return 0;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004874 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4875 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4876 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004877}
4878
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004879Arguments::Arguments(internal::Object** implicit_args,
4880 internal::Object** values, int length,
4881 bool is_construct_call)
4882 : implicit_args_(implicit_args),
4883 values_(values),
4884 length_(length),
4885 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004886
4887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004888Local<Value> Arguments::operator[](int i) const {
4889 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4890 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4891}
4892
4893
4894Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004895 return Local<Function>(reinterpret_cast<Function*>(
4896 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004897}
4898
4899
4900Local<Object> Arguments::This() const {
4901 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4902}
4903
4904
4905Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004906 return Local<Object>(reinterpret_cast<Object*>(
4907 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004908}
4909
4910
4911Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004912 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004913}
4914
4915
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004916Isolate* Arguments::GetIsolate() const {
4917 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4918}
4919
4920
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004921bool Arguments::IsConstructCall() const {
4922 return is_construct_call_;
4923}
4924
4925
4926int Arguments::Length() const {
4927 return length_;
4928}
4929
4930
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004931template <class T>
4932Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004933 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4934 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004935 return Local<T>(reinterpret_cast<T*>(after));
4936}
4937
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004938Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004939 return resource_name_;
4940}
4941
4942
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004943Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004944 return resource_line_offset_;
4945}
4946
4947
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004948Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004949 return resource_column_offset_;
4950}
4951
4952
4953Handle<Boolean> Boolean::New(bool value) {
4954 return value ? True() : False();
4955}
4956
4957
4958void Template::Set(const char* name, v8::Handle<Data> value) {
4959 Set(v8::String::New(name), value);
4960}
4961
4962
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004963Local<Value> Object::GetInternalField(int index) {
4964#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004965 typedef internal::Object O;
4966 typedef internal::Internals I;
4967 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004968 // Fast path: If the object is a plain JSObject, which is the common case, we
4969 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004970 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004971 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004972 O* value = I::ReadField<O*>(obj, offset);
4973 O** result = HandleScope::CreateHandle(value);
4974 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004975 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004976#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004977 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004978}
4979
4980
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004981void* Object::GetAlignedPointerFromInternalField(int index) {
4982#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004983 typedef internal::Object O;
4984 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004985 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004986 // Fast path: If the object is a plain JSObject, which is the common case, we
4987 // know where to find the internal fields and can return the value directly.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004988 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004989 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004990 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004991 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004992#endif
4993 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004994}
4995
4996
4997String* String::Cast(v8::Value* value) {
4998#ifdef V8_ENABLE_CHECKS
4999 CheckCast(value);
5000#endif
5001 return static_cast<String*>(value);
5002}
5003
5004
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005005Local<String> String::Empty(Isolate* isolate) {
5006 typedef internal::Object* S;
5007 typedef internal::Internals I;
5008 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005009 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005010 return Local<String>(reinterpret_cast<String*>(slot));
5011}
5012
5013
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00005014Local<String> String::New(const char* data, int length) {
5015 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
5016}
5017
5018
5019Local<String> String::New(const uint16_t* data, int length) {
5020 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
5021}
5022
5023
5024Local<String> String::NewSymbol(const char* data, int length) {
5025 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
5026}
5027
5028
5029Local<String> String::NewUndetectable(const char* data, int length) {
5030 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
5031}
5032
5033
5034Local<String> String::NewUndetectable(const uint16_t* data, int length) {
5035 return NewFromTwoByte(
5036 Isolate::GetCurrent(), data, kUndetectableString, length);
5037}
5038
5039
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005040String::ExternalStringResource* String::GetExternalStringResource() const {
5041 typedef internal::Object O;
5042 typedef internal::Internals I;
5043 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005044 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005045 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005046 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5047 result = reinterpret_cast<String::ExternalStringResource*>(value);
5048 } else {
5049 result = NULL;
5050 }
5051#ifdef V8_ENABLE_CHECKS
5052 VerifyExternalStringResource(result);
5053#endif
5054 return result;
5055}
5056
5057
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005058String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
5059 String::Encoding* encoding_out) const {
5060 typedef internal::Object O;
5061 typedef internal::Internals I;
5062 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
5063 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
5064 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5065 ExternalStringResourceBase* resource = NULL;
5066 if (type == I::kExternalAsciiRepresentationTag ||
5067 type == I::kExternalTwoByteRepresentationTag) {
5068 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5069 resource = static_cast<ExternalStringResourceBase*>(value);
5070 }
5071#ifdef V8_ENABLE_CHECKS
5072 VerifyExternalStringResourceBase(resource, *encoding_out);
5073#endif
5074 return resource;
5075}
5076
5077
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005078bool Value::IsUndefined() const {
5079#ifdef V8_ENABLE_CHECKS
5080 return FullIsUndefined();
5081#else
5082 return QuickIsUndefined();
5083#endif
5084}
5085
5086bool Value::QuickIsUndefined() const {
5087 typedef internal::Object O;
5088 typedef internal::Internals I;
5089 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5090 if (!I::HasHeapObjectTag(obj)) return false;
5091 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5092 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
5093}
5094
5095
5096bool Value::IsNull() const {
5097#ifdef V8_ENABLE_CHECKS
5098 return FullIsNull();
5099#else
5100 return QuickIsNull();
5101#endif
5102}
5103
5104bool Value::QuickIsNull() const {
5105 typedef internal::Object O;
5106 typedef internal::Internals I;
5107 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5108 if (!I::HasHeapObjectTag(obj)) return false;
5109 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5110 return (I::GetOddballKind(obj) == I::kNullOddballKind);
5111}
5112
5113
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005114bool Value::IsString() const {
5115#ifdef V8_ENABLE_CHECKS
5116 return FullIsString();
5117#else
5118 return QuickIsString();
5119#endif
5120}
5121
5122bool Value::QuickIsString() const {
5123 typedef internal::Object O;
5124 typedef internal::Internals I;
5125 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5126 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005127 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005128}
5129
5130
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005131Symbol* Symbol::Cast(v8::Value* value) {
5132#ifdef V8_ENABLE_CHECKS
5133 CheckCast(value);
5134#endif
5135 return static_cast<Symbol*>(value);
5136}
5137
5138
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005139Number* Number::Cast(v8::Value* value) {
5140#ifdef V8_ENABLE_CHECKS
5141 CheckCast(value);
5142#endif
5143 return static_cast<Number*>(value);
5144}
5145
5146
5147Integer* Integer::Cast(v8::Value* value) {
5148#ifdef V8_ENABLE_CHECKS
5149 CheckCast(value);
5150#endif
5151 return static_cast<Integer*>(value);
5152}
5153
5154
5155Date* Date::Cast(v8::Value* value) {
5156#ifdef V8_ENABLE_CHECKS
5157 CheckCast(value);
5158#endif
5159 return static_cast<Date*>(value);
5160}
5161
5162
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00005163StringObject* StringObject::Cast(v8::Value* value) {
5164#ifdef V8_ENABLE_CHECKS
5165 CheckCast(value);
5166#endif
5167 return static_cast<StringObject*>(value);
5168}
5169
5170
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005171SymbolObject* SymbolObject::Cast(v8::Value* value) {
5172#ifdef V8_ENABLE_CHECKS
5173 CheckCast(value);
5174#endif
5175 return static_cast<SymbolObject*>(value);
5176}
5177
5178
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00005179NumberObject* NumberObject::Cast(v8::Value* value) {
5180#ifdef V8_ENABLE_CHECKS
5181 CheckCast(value);
5182#endif
5183 return static_cast<NumberObject*>(value);
5184}
5185
5186
5187BooleanObject* BooleanObject::Cast(v8::Value* value) {
5188#ifdef V8_ENABLE_CHECKS
5189 CheckCast(value);
5190#endif
5191 return static_cast<BooleanObject*>(value);
5192}
5193
5194
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00005195RegExp* RegExp::Cast(v8::Value* value) {
5196#ifdef V8_ENABLE_CHECKS
5197 CheckCast(value);
5198#endif
5199 return static_cast<RegExp*>(value);
5200}
5201
5202
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005203Object* Object::Cast(v8::Value* value) {
5204#ifdef V8_ENABLE_CHECKS
5205 CheckCast(value);
5206#endif
5207 return static_cast<Object*>(value);
5208}
5209
5210
5211Array* Array::Cast(v8::Value* value) {
5212#ifdef V8_ENABLE_CHECKS
5213 CheckCast(value);
5214#endif
5215 return static_cast<Array*>(value);
5216}
5217
5218
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005219ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
5220#ifdef V8_ENABLE_CHECKS
5221 CheckCast(value);
5222#endif
5223 return static_cast<ArrayBuffer*>(value);
5224}
5225
5226
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005227Function* Function::Cast(v8::Value* value) {
5228#ifdef V8_ENABLE_CHECKS
5229 CheckCast(value);
5230#endif
5231 return static_cast<Function*>(value);
5232}
5233
5234
5235External* External::Cast(v8::Value* value) {
5236#ifdef V8_ENABLE_CHECKS
5237 CheckCast(value);
5238#endif
5239 return static_cast<External*>(value);
5240}
5241
5242
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005243Isolate* AccessorInfo::GetIsolate() const {
5244 return *reinterpret_cast<Isolate**>(&args_[-3]);
5245}
5246
5247
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005248Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005249 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005250}
5251
5252
5253Local<Object> AccessorInfo::This() const {
5254 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
5255}
5256
5257
5258Local<Object> AccessorInfo::Holder() const {
5259 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
5260}
5261
5262
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005263Handle<Primitive> Undefined(Isolate* isolate) {
5264 typedef internal::Object* S;
5265 typedef internal::Internals I;
5266 if (!I::IsInitialized(isolate)) return Undefined();
5267 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
5268 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
5269}
5270
5271
5272Handle<Primitive> Null(Isolate* isolate) {
5273 typedef internal::Object* S;
5274 typedef internal::Internals I;
5275 if (!I::IsInitialized(isolate)) return Null();
5276 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
5277 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
5278}
5279
5280
5281Handle<Boolean> True(Isolate* isolate) {
5282 typedef internal::Object* S;
5283 typedef internal::Internals I;
5284 if (!I::IsInitialized(isolate)) return True();
5285 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
5286 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
5287}
5288
5289
5290Handle<Boolean> False(Isolate* isolate) {
5291 typedef internal::Object* S;
5292 typedef internal::Internals I;
5293 if (!I::IsInitialized(isolate)) return False();
5294 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
5295 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
5296}
5297
5298
5299void Isolate::SetData(void* data) {
5300 typedef internal::Internals I;
5301 I::SetEmbedderData(this, data);
5302}
5303
5304
5305void* Isolate::GetData() {
5306 typedef internal::Internals I;
5307 return I::GetEmbedderData(this);
5308}
5309
5310
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005311Local<Value> Context::GetEmbedderData(int index) {
5312#ifndef V8_ENABLE_CHECKS
5313 typedef internal::Object O;
5314 typedef internal::Internals I;
5315 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
5316 return Local<Value>(reinterpret_cast<Value*>(result));
5317#else
5318 return SlowGetEmbedderData(index);
5319#endif
5320}
5321
5322
5323void* Context::GetAlignedPointerFromEmbedderData(int index) {
5324#ifndef V8_ENABLE_CHECKS
5325 typedef internal::Internals I;
5326 return I::ReadEmbedderData<void*>(this, index);
5327#else
5328 return SlowGetAlignedPointerFromEmbedderData(index);
5329#endif
5330}
5331
5332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005333/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00005334 * \example shell.cc
5335 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005336 * command-line and executes them.
5337 */
5338
5339
5340/**
5341 * \example process.cc
5342 */
5343
5344
5345} // namespace v8
5346
5347
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005348#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005349#undef TYPE_CHECK
5350
5351
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005352#endif // V8_H_