blob: 8efb7c33c532f714277680ab503f4025fe233d80 [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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000096 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 */
98namespace v8 {
99
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000100class AccessorInfo;
101class AccessorSignature;
102class Array;
103class Boolean;
104class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105class Context;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000106class CpuProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000107class Data;
108class Date;
109class DeclaredAccessorDescriptor;
110class External;
111class Function;
112class FunctionTemplate;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000113class HeapProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000114class ImplementationUtilities;
115class Int32;
116class Integer;
117class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000119class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120class Object;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000121class ObjectOperationDescriptor;
122class ObjectTemplate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123class Primitive;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000124class RawOperationDescriptor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125class Signature;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000126class StackFrame;
127class StackTrace;
128class String;
129class StringObject;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000130class Symbol;
131class SymbolObject;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000132class Uint32;
133class Utils;
134class Value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135template <class T> class Handle;
136template <class T> class Local;
137template <class T> class Persistent;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000139namespace internal {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000140class Arguments;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000141class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000142class HeapObject;
143class Isolate;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000144class Object;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000145}
146
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000148// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149
150
151/**
152 * A weak reference callback function.
153 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000154 * This callback should either explicitly invoke Dispose on |object| if
155 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
156 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 * \param object the weak global object to be reclaimed by the garbage collector
158 * \param parameter the value passed in when making the weak global object
159 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000160typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 void* parameter);
162
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000163// TODO(svenpanne) Temporary definition until Chrome is in sync.
164typedef void (*NearDeathCallback)(Isolate* isolate,
165 Persistent<Value> object,
166 void* parameter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000168// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000170#define TYPE_CHECK(T, S) \
171 while (false) { \
172 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 }
174
175/**
176 * An object reference managed by the v8 garbage collector.
177 *
178 * All objects returned from v8 have to be tracked by the garbage
179 * collector so that it knows that the objects are still alive. Also,
180 * because the garbage collector may move objects, it is unsafe to
181 * point directly to an object. Instead, all objects are stored in
182 * handles which are known by the garbage collector and updated
183 * whenever an object moves. Handles should always be passed by value
184 * (except in cases like out-parameters) and they should never be
185 * allocated on the heap.
186 *
187 * There are two types of handles: local and persistent handles.
188 * Local handles are light-weight and transient and typically used in
189 * local operations. They are managed by HandleScopes. Persistent
190 * handles can be used when storing objects across several independent
191 * operations and have to be explicitly deallocated when they're no
192 * longer used.
193 *
194 * It is safe to extract the object stored in the handle by
195 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000196 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 * behind the scenes and the same rules apply to these values as to
198 * their handles.
199 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000200template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202 /**
203 * Creates an empty handle.
204 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000205 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
207 /**
208 * Creates a new handle for the specified value.
209 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000210 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211
212 /**
213 * Creates a handle for the contents of the specified handle. This
214 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000215 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000217 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218 * between compatible handles, for instance assigning a
219 * Handle<String> to a variable declared as Handle<Value>, is legal
220 * because String is a subclass of Value.
221 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000222 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 : val_(reinterpret_cast<T*>(*that)) {
224 /**
225 * This check fails when trying to convert between incompatible
226 * handles. For example, converting from a Handle<String> to a
227 * Handle<Number>.
228 */
229 TYPE_CHECK(T, S);
230 }
231
232 /**
233 * Returns true if the handle is empty.
234 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000235 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 /**
238 * Sets the handle to be empty. IsEmpty() will then return true.
239 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000240 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000241
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000242 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000243
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000244 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
246 /**
247 * Checks whether two handles are the same.
248 * Returns true if both are empty, or if the objects
249 * to which they refer are identical.
250 * The handles' references are not checked.
251 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000252 template <class S> V8_INLINE(bool operator==(Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000253 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
254 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 if (a == 0) return b == 0;
256 if (b == 0) return false;
257 return *a == *b;
258 }
259
260 /**
261 * Checks whether two handles are different.
262 * Returns true if only one of the handles is empty, or if
263 * the objects to which they refer are different.
264 * The handles' references are not checked.
265 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000266 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 return !operator==(that);
268 }
269
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000270 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000271#ifdef V8_ENABLE_CHECKS
272 // If we're going to perform the type check then we have to check
273 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000275#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 return Handle<T>(T::Cast(*that));
277 }
278
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000279 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000280 return Handle<S>::Cast(*this);
281 }
282
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 private:
284 T* val_;
285};
286
287
288/**
289 * A light-weight stack-allocated object handle. All operations
290 * that return objects from within v8 return them in local handles. They
291 * are created within HandleScopes, and all local handles allocated within a
292 * handle scope are destroyed when the handle scope is destroyed. Hence it
293 * is not necessary to explicitly deallocate local handles.
294 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000295template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000297 V8_INLINE(Local());
298 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 : Handle<T>(reinterpret_cast<T*>(*that)) {
300 /**
301 * This check fails when trying to convert between incompatible
302 * handles. For example, converting from a Handle<String> to a
303 * Handle<Number>.
304 */
305 TYPE_CHECK(T, S);
306 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000307 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
308 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000309#ifdef V8_ENABLE_CHECKS
310 // If we're going to perform the type check then we have to check
311 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000313#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 return Local<T>(T::Cast(*that));
315 }
316
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000317 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000318 return Local<S>::Cast(*this);
319 }
320
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000321 /**
322 * Create a local handle for the content of another handle.
323 * The referee is kept alive by the local handle even when
324 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000326 V8_INLINE(static Local<T> New(Handle<T> that));
327 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328};
329
330
331/**
332 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000333 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 * allocated, a Persistent handle remains valid until it is explicitly
335 * disposed.
336 *
337 * A persistent handle contains a reference to a storage cell within
338 * the v8 engine which holds an object value and which is updated by
339 * the garbage collector whenever the object is moved. A new storage
340 * cell can be created using Persistent::New and existing handles can
341 * be disposed using Persistent::Dispose. Since persistent handles
342 * are passed by value you may have many persistent handle objects
343 * that point to the same storage cell. For instance, if you pass a
344 * persistent handle as an argument to a function you will not get two
345 * different storage cells but rather two references to the same
346 * storage cell.
347 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000348template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 /**
351 * Creates an empty persistent handle that doesn't point to any
352 * storage cell.
353 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000354 V8_INLINE(Persistent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355
356 /**
357 * Creates a persistent handle for the same storage cell as the
358 * specified handle. This constructor allows you to pass persistent
359 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000360 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000362 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 * between compatible persistent handles, for instance assigning a
364 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000365 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000367 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 : Handle<T>(reinterpret_cast<T*>(*that)) {
369 /**
370 * This check fails when trying to convert between incompatible
371 * handles. For example, converting from a Handle<String> to a
372 * Handle<Number>.
373 */
374 TYPE_CHECK(T, S);
375 }
376
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000377 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000379 /**
380 * "Casts" a plain handle which is known to be a persistent handle
381 * to a persistent handle.
382 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000383 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 : Handle<T>(*that) { }
385
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000386 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000387#ifdef V8_ENABLE_CHECKS
388 // If we're going to perform the type check then we have to check
389 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000391#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392 return Persistent<T>(T::Cast(*that));
393 }
394
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000395 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000396 return Persistent<S>::Cast(*this);
397 }
398
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000399 /** Deprecated. Use Isolate version instead. */
400 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
401
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000403 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000405 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
406
407 /** Deprecated. Use Isolate version instead. */
408 V8_DEPRECATED(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409
410 /**
411 * Releases the storage cell referenced by this persistent handle.
412 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000413 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414 * cell remain and IsEmpty will still return false.
415 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000416 V8_INLINE(void Dispose(Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000418 /** Deprecated. Use Isolate version instead. */
419 V8_DEPRECATED(void MakeWeak(void* parameters,
420 WeakReferenceCallback callback));
421
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422 /**
423 * Make the reference to this object weak. When only weak handles
424 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000425 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 * it the object reference and the given parameters.
427 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000428 V8_INLINE(void MakeWeak(Isolate* isolate,
429 void* parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000430 NearDeathCallback callback));
431
432 /** Deprecated. Use Isolate version instead. */
433 V8_DEPRECATED(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000435 /** Clears the weak reference to this object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000436 V8_INLINE(void ClearWeak(Isolate* isolate));
437
438 /** Deprecated. Use Isolate version instead. */
439 V8_DEPRECATED(void MarkIndependent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440
441 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000442 * Marks the reference to this object independent. Garbage collector is free
443 * to ignore any object groups containing this object. Weak callback for an
444 * independent handle should not assume that it will be preceded by a global
445 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000446 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000447 V8_INLINE(void MarkIndependent(Isolate* isolate));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000448
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000449 /** Deprecated. Use Isolate version instead. */
450 V8_DEPRECATED(void MarkPartiallyDependent());
451
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000452 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000453 * Marks the reference to this object partially dependent. Partially dependent
454 * handles only depend on other partially dependent handles and these
455 * dependencies are provided through object groups. It provides a way to build
456 * smaller object groups for young objects that represent only a subset of all
457 * external dependencies. This mark is automatically cleared after each
458 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000459 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000460 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000461
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000462 /** Deprecated. Use Isolate version instead. */
463 V8_DEPRECATED(bool IsIndependent() const);
464
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000465 /** Returns true if this handle was previously marked as independent. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000466 V8_INLINE(bool IsIndependent(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000467
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000468 /** Deprecated. Use Isolate version instead. */
469 V8_DEPRECATED(bool IsNearDeath() const);
470
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000471 /** Checks if the handle holds the only reference to an object. */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000472 V8_INLINE(bool IsNearDeath(Isolate* isolate) const);
473
474 /** Deprecated. Use Isolate version instead. */
475 V8_DEPRECATED(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000477 /** Returns true if the handle's reference is weak. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000478 V8_INLINE(bool IsWeak(Isolate* isolate) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000480 /** Deprecated. Use Isolate version instead. */
481 V8_DEPRECATED(void SetWrapperClassId(uint16_t class_id));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000482
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000483 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000484 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
485 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000486 */
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000487 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id));
488
489 /** Deprecated. Use Isolate version instead. */
490 V8_DEPRECATED(uint16_t WrapperClassId() const);
491
492 /**
493 * Returns the class ID previously assigned to this handle or 0 if no class ID
494 * was previously assigned.
495 */
496 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 private:
499 friend class ImplementationUtilities;
500 friend class ObjectTemplate;
501};
502
503
v8.team.kasperl727e9952008-09-02 14:56:44 +0000504 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 * A stack-allocated class that governs a number of local handles.
506 * After a handle scope has been created, all local handles will be
507 * allocated within that handle scope until either the handle scope is
508 * deleted or another handle scope is created. If there is already a
509 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000510 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 * new handles will again be allocated in the original handle scope.
512 *
513 * After the handle scope of a local handle has been deleted the
514 * garbage collector will no longer track the object stored in the
515 * handle and may deallocate it. The behavior of accessing a handle
516 * for which the handle scope has been deleted is undefined.
517 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000518class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000520 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000521 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000523 HandleScope(Isolate* isolate);
524
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000525 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526
527 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 * Closes the handle scope and returns the value as a handle in the
529 * previous scope, which is the new current scope after the call.
530 */
531 template <class T> Local<T> Close(Handle<T> value);
532
533 /**
534 * Counts the number of allocated handles.
535 */
536 static int NumberOfHandles();
537
538 /**
539 * Creates a new handle with the given value.
540 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000541 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000542 static internal::Object** CreateHandle(internal::Isolate* isolate,
543 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000544 // Faster version, uses HeapObject to obtain the current Isolate.
545 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546
547 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000548 // Make it hard to create heap-allocated or illegal handle scopes by
549 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 HandleScope(const HandleScope&);
551 void operator=(const HandleScope&);
552 void* operator new(size_t size);
553 void operator delete(void*, size_t);
554
ager@chromium.org3811b432009-10-28 14:53:37 +0000555 // This Data class is accessible internally as HandleScopeData through a
556 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000557 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000559 internal::Object** next;
560 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000561 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000562 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000564 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 }
566 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000567
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000568 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000569 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000571 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000572 internal::Object** prev_next_;
573 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000575 // Allow for the active closing of HandleScopes which allows to pass a handle
576 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000578 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 friend class ImplementationUtilities;
581};
582
583
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000584// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585
586
587/**
588 * The superclass of values and API object templates.
589 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000590class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 private:
592 Data();
593};
594
595
596/**
597 * Pre-compilation data that can be associated with a script. This
598 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000599 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000600 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000602class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 public:
604 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000605
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000606 /**
607 * Pre-compiles the specified script (context-independent).
608 *
609 * \param input Pointer to UTF-8 script source code.
610 * \param length Length of UTF-8 script source code.
611 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000614 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000615 * Pre-compiles the specified script (context-independent).
616 *
617 * NOTE: Pre-compilation using this method cannot happen on another thread
618 * without using Lockers.
619 *
620 * \param source Script source code.
621 */
622 static ScriptData* PreCompile(Handle<String> source);
623
624 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000625 * Load previous pre-compilation data.
626 *
627 * \param data Pointer to data returned by a call to Data() of a previous
628 * ScriptData. Ownership is not transferred.
629 * \param length Length of data.
630 */
631 static ScriptData* New(const char* data, int length);
632
633 /**
634 * Returns the length of Data().
635 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000637
638 /**
639 * Returns a serialized representation of this ScriptData that can later be
640 * passed to New(). NOTE: Serialized data is platform-dependent.
641 */
642 virtual const char* Data() = 0;
643
644 /**
645 * Returns true if the source code could not be parsed.
646 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000647 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648};
649
650
651/**
652 * The origin, within a file, of a script.
653 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000654class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000656 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000657 Handle<Value> resource_name,
658 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000659 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660 : resource_name_(resource_name),
661 resource_line_offset_(resource_line_offset),
662 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000663 V8_INLINE(Handle<Value> ResourceName() const);
664 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
665 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000667 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 Handle<Integer> resource_line_offset_;
669 Handle<Integer> resource_column_offset_;
670};
671
672
673/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000674 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000676class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000679 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000680 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000681 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000682 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000683 * when New() returns
684 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
685 * using pre_data speeds compilation if it's done multiple times.
686 * Owned by caller, no references are kept when New() returns.
687 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000688 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000689 * available to compile event handlers.
690 * \return Compiled script object (context independent; when run it
691 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000693 static Local<Script> New(Handle<String> source,
694 ScriptOrigin* origin = NULL,
695 ScriptData* pre_data = NULL,
696 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697
mads.s.agercbaa0602008-08-14 13:41:48 +0000698 /**
699 * Compiles the specified script using the specified file name
700 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000701 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000702 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000703 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000704 * as the script's origin.
705 * \return Compiled script object (context independent; when run it
706 * will use the currently entered context).
707 */
708 static Local<Script> New(Handle<String> source,
709 Handle<Value> file_name);
710
711 /**
712 * Compiles the specified script (bound to current context).
713 *
714 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000715 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000716 * when Compile() returns
717 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
718 * using pre_data speeds compilation if it's done multiple times.
719 * Owned by caller, no references are kept when Compile() returns.
720 * \param script_data Arbitrary data associated with script. Using
721 * this has same effect as calling SetData(), but makes data available
722 * earlier (i.e. to compile event handlers).
723 * \return Compiled script object, bound to the context that was active
724 * when this function was called. When run it will always use this
725 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000726 */
727 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000728 ScriptOrigin* origin = NULL,
729 ScriptData* pre_data = NULL,
730 Handle<String> script_data = Handle<String>());
731
732 /**
733 * Compiles the specified script using the specified file name
734 * object (typically a string) as the script's origin.
735 *
736 * \param source Script source code.
737 * \param file_name File name to use as script's origin
738 * \param script_data Arbitrary data associated with script. Using
739 * this has same effect as calling SetData(), but makes data available
740 * earlier (i.e. to compile event handlers).
741 * \return Compiled script object, bound to the context that was active
742 * when this function was called. When run it will always use this
743 * context.
744 */
745 static Local<Script> Compile(Handle<String> source,
746 Handle<Value> file_name,
747 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000748
v8.team.kasperl727e9952008-09-02 14:56:44 +0000749 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000750 * Runs the script returning the resulting value. If the script is
751 * context independent (created using ::New) it will be run in the
752 * currently entered context. If it is context specific (created
753 * using ::Compile) it will be run in the context in which it was
754 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000755 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000757
758 /**
759 * Returns the script id value.
760 */
761 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000762
763 /**
764 * Associate an additional data object with the script. This is mainly used
765 * with the debugger as this data object is only available through the
766 * debugger API.
767 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000768 void SetData(Handle<String> data);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000769
770 /**
771 * Returns the name value of one Script.
772 */
773 Handle<Value> GetScriptName();
774
775 /**
776 * Returns zero based line number of the code_pos location in the script.
777 * -1 will be returned if no information available.
778 */
779 int GetLineNumber(int code_pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780};
781
782
783/**
784 * An error message.
785 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000786class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000788 Local<String> Get() const;
789 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000791 /**
792 * Returns the resource name for the script from where the function causing
793 * the error originates.
794 */
ager@chromium.org32912102009-01-16 10:38:43 +0000795 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000797 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000798 * Returns the resource data for the script from where the function causing
799 * the error originates.
800 */
801 Handle<Value> GetScriptData() const;
802
803 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000804 * Exception stack trace. By default stack traces are not captured for
805 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
806 * to change this option.
807 */
808 Handle<StackTrace> GetStackTrace() const;
809
810 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000811 * Returns the number, 1-based, of the line where the error occurred.
812 */
ager@chromium.org32912102009-01-16 10:38:43 +0000813 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000815 /**
816 * Returns the index within the script of the first character where
817 * the error occurred.
818 */
ager@chromium.org32912102009-01-16 10:38:43 +0000819 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000820
821 /**
822 * Returns the index within the script of the last character where
823 * the error occurred.
824 */
ager@chromium.org32912102009-01-16 10:38:43 +0000825 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000826
827 /**
828 * Returns the index within the line of the first character where
829 * the error occurred.
830 */
ager@chromium.org32912102009-01-16 10:38:43 +0000831 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000832
833 /**
834 * Returns the index within the line of the last character where
835 * the error occurred.
836 */
ager@chromium.org32912102009-01-16 10:38:43 +0000837 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000838
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000839 // TODO(1245381): Print to a string instead of on a FILE.
840 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000841
842 static const int kNoLineNumberInfo = 0;
843 static const int kNoColumnInfo = 0;
844};
845
846
847/**
848 * Representation of a JavaScript stack trace. The information collected is a
849 * snapshot of the execution stack and the information remains valid after
850 * execution continues.
851 */
852class V8EXPORT StackTrace {
853 public:
854 /**
855 * Flags that determine what information is placed captured for each
856 * StackFrame when grabbing the current stack trace.
857 */
858 enum StackTraceOptions {
859 kLineNumber = 1,
860 kColumnOffset = 1 << 1 | kLineNumber,
861 kScriptName = 1 << 2,
862 kFunctionName = 1 << 3,
863 kIsEval = 1 << 4,
864 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000865 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000866 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000867 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000868 };
869
870 /**
871 * Returns a StackFrame at a particular index.
872 */
873 Local<StackFrame> GetFrame(uint32_t index) const;
874
875 /**
876 * Returns the number of StackFrames.
877 */
878 int GetFrameCount() const;
879
880 /**
881 * Returns StackTrace as a v8::Array that contains StackFrame objects.
882 */
883 Local<Array> AsArray();
884
885 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000886 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000887 *
888 * \param frame_limit The maximum number of stack frames we want to capture.
889 * \param options Enumerates the set of things we will capture for each
890 * StackFrame.
891 */
892 static Local<StackTrace> CurrentStackTrace(
893 int frame_limit,
894 StackTraceOptions options = kOverview);
895};
896
897
898/**
899 * A single JavaScript stack frame.
900 */
901class V8EXPORT StackFrame {
902 public:
903 /**
904 * Returns the number, 1-based, of the line for the associate function call.
905 * This method will return Message::kNoLineNumberInfo if it is unable to
906 * retrieve the line number, or if kLineNumber was not passed as an option
907 * when capturing the StackTrace.
908 */
909 int GetLineNumber() const;
910
911 /**
912 * Returns the 1-based column offset on the line for the associated function
913 * call.
914 * This method will return Message::kNoColumnInfo if it is unable to retrieve
915 * the column number, or if kColumnOffset was not passed as an option when
916 * capturing the StackTrace.
917 */
918 int GetColumn() const;
919
920 /**
921 * Returns the name of the resource that contains the script for the
922 * function for this StackFrame.
923 */
924 Local<String> GetScriptName() const;
925
926 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000927 * Returns the name of the resource that contains the script for the
928 * function for this StackFrame or sourceURL value if the script name
929 * is undefined and its source ends with //@ sourceURL=... string.
930 */
931 Local<String> GetScriptNameOrSourceURL() const;
932
933 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000934 * Returns the name of the function associated with this stack frame.
935 */
936 Local<String> GetFunctionName() const;
937
938 /**
939 * Returns whether or not the associated function is compiled via a call to
940 * eval().
941 */
942 bool IsEval() const;
943
944 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000945 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000946 * constructor via "new".
947 */
948 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949};
950
951
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000952// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953
954
955/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000956 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000958class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960 /**
961 * Returns true if this value is the undefined value. See ECMA-262
962 * 4.3.10.
963 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000964 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965
966 /**
967 * Returns true if this value is the null value. See ECMA-262
968 * 4.3.11.
969 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000970 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971
972 /**
973 * Returns true if this value is true.
974 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000975 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976
977 /**
978 * Returns true if this value is false.
979 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000980 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981
982 /**
983 * Returns true if this value is an instance of the String type.
984 * See ECMA-262 8.4.
985 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000986 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987
988 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000989 * Returns true if this value is a symbol.
990 * This is an experimental feature.
991 */
992 bool IsSymbol() const;
993
994 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 * Returns true if this value is a function.
996 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000997 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998
999 /**
1000 * Returns true if this value is an array.
1001 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001002 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003
v8.team.kasperl727e9952008-09-02 14:56:44 +00001004 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 * Returns true if this value is an object.
1006 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001007 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008
v8.team.kasperl727e9952008-09-02 14:56:44 +00001009 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 * Returns true if this value is boolean.
1011 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001012 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001013
v8.team.kasperl727e9952008-09-02 14:56:44 +00001014 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 * Returns true if this value is a number.
1016 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001017 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018
v8.team.kasperl727e9952008-09-02 14:56:44 +00001019 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 * Returns true if this value is external.
1021 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001022 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023
v8.team.kasperl727e9952008-09-02 14:56:44 +00001024 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 * Returns true if this value is a 32-bit signed integer.
1026 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001027 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001029 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001030 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001031 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001032 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001033
1034 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001035 * Returns true if this value is a Date.
1036 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001037 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001038
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001039 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001040 * Returns true if this value is a Boolean object.
1041 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001042 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001043
1044 /**
1045 * Returns true if this value is a Number object.
1046 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001047 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001048
1049 /**
1050 * Returns true if this value is a String object.
1051 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001052 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001053
1054 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001055 * Returns true if this value is a Symbol object.
1056 * This is an experimental feature.
1057 */
1058 bool IsSymbolObject() const;
1059
1060 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001061 * Returns true if this value is a NativeError.
1062 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001063 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001064
1065 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001066 * Returns true if this value is a RegExp.
1067 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001068 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001069
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001070 Local<Boolean> ToBoolean() const;
1071 Local<Number> ToNumber() const;
1072 Local<String> ToString() const;
1073 Local<String> ToDetailString() const;
1074 Local<Object> ToObject() const;
1075 Local<Integer> ToInteger() const;
1076 Local<Uint32> ToUint32() const;
1077 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078
1079 /**
1080 * Attempts to convert a string to an array index.
1081 * Returns an empty handle if the conversion fails.
1082 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001083 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001085 bool BooleanValue() const;
1086 double NumberValue() const;
1087 int64_t IntegerValue() const;
1088 uint32_t Uint32Value() const;
1089 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090
1091 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001092 bool Equals(Handle<Value> that) const;
1093 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001094
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001095 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001096 V8_INLINE(bool QuickIsUndefined() const);
1097 V8_INLINE(bool QuickIsNull() const);
1098 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001099 bool FullIsUndefined() const;
1100 bool FullIsNull() const;
1101 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102};
1103
1104
1105/**
1106 * The superclass of primitive values. See ECMA-262 4.3.2.
1107 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001108class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109
1110
1111/**
1112 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1113 * or false value.
1114 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001115class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001117 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001118 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119};
1120
1121
1122/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001123 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001125class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001127 enum Encoding {
1128 UNKNOWN_ENCODING = 0x1,
1129 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001130 ASCII_ENCODING = 0x4,
1131 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001132 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001133 /**
1134 * Returns the number of characters in this string.
1135 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001136 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137
v8.team.kasperl727e9952008-09-02 14:56:44 +00001138 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001139 * Returns the number of bytes in the UTF-8 encoded
1140 * representation of this string.
1141 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001142 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001143
1144 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001145 * A fast conservative check for non-ASCII characters. May
1146 * return true even for ASCII strings, but if it returns
1147 * false you can be sure that all characters are in the range
1148 * 0-127.
1149 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001150 bool MayContainNonAscii() const;
1151
1152 /**
1153 * Returns whether this string contains only one byte data.
1154 */
1155 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001156
1157 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001158 * Write the contents of the string to an external buffer.
1159 * If no arguments are given, expects the buffer to be large
1160 * enough to hold the entire string and NULL terminator. Copies
1161 * the contents of the string and the NULL terminator into the
1162 * buffer.
1163 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001164 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1165 * before the end of the buffer.
1166 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001167 * Copies up to length characters into the output buffer.
1168 * Only null-terminates if there is enough space in the buffer.
1169 *
1170 * \param buffer The buffer into which the string will be copied.
1171 * \param start The starting position within the string at which
1172 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001173 * \param length The number of characters to copy from the string. For
1174 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001175 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001176 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001177 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001178 * \return The number of characters copied to the buffer excluding the null
1179 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001180 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001181 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001182 enum WriteOptions {
1183 NO_OPTIONS = 0,
1184 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001185 NO_NULL_TERMINATION = 2,
1186 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001187 };
1188
lrn@chromium.org34e60782011-09-15 07:25:40 +00001189 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001190 int Write(uint16_t* buffer,
1191 int start = 0,
1192 int length = -1,
1193 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001194 // ASCII characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001195 int WriteAscii(char* buffer,
1196 int start = 0,
1197 int length = -1,
1198 int options = NO_OPTIONS) const;
1199 // One byte characters.
1200 int WriteOneByte(uint8_t* buffer,
1201 int start = 0,
1202 int length = -1,
1203 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001204 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001205 int WriteUtf8(char* buffer,
1206 int length = -1,
1207 int* nchars_ref = NULL,
1208 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209
v8.team.kasperl727e9952008-09-02 14:56:44 +00001210 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001211 * A zero length string.
1212 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001213 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001214 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001215
1216 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001217 * Returns true if the string is external
1218 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001219 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220
v8.team.kasperl727e9952008-09-02 14:56:44 +00001221 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001222 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001223 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001224 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001225
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001226 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001227 public:
1228 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001229
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001230 protected:
1231 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001232
1233 /**
1234 * Internally V8 will call this Dispose method when the external string
1235 * resource is no longer needed. The default implementation will use the
1236 * delete operator. This method can be overridden in subclasses to
1237 * control how allocated external string resources are disposed.
1238 */
1239 virtual void Dispose() { delete this; }
1240
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001241 private:
1242 // Disallow copying and assigning.
1243 ExternalStringResourceBase(const ExternalStringResourceBase&);
1244 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001245
1246 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001247 };
1248
v8.team.kasperl727e9952008-09-02 14:56:44 +00001249 /**
1250 * An ExternalStringResource is a wrapper around a two-byte string
1251 * buffer that resides outside V8's heap. Implement an
1252 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001253 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001254 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001255 class V8EXPORT ExternalStringResource
1256 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 public:
1258 /**
1259 * Override the destructor to manage the life cycle of the underlying
1260 * buffer.
1261 */
1262 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001263
1264 /**
1265 * The string data from the underlying buffer.
1266 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001268
1269 /**
1270 * The length of the string. That is, the number of two-byte characters.
1271 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 protected:
1275 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 };
1277
1278 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001279 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001280 * string buffer that resides outside V8's heap. Implement an
1281 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001282 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001283 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001284 * UTF-8, which would require special treatment internally in the
1285 * engine and, in the case of UTF-8, do not allow efficient indexing.
1286 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001287 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001289 class V8EXPORT ExternalAsciiStringResource
1290 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 public:
1292 /**
1293 * Override the destructor to manage the life cycle of the underlying
1294 * buffer.
1295 */
1296 virtual ~ExternalAsciiStringResource() {}
1297 /** The string data from the underlying buffer.*/
1298 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001299 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 virtual size_t length() const = 0;
1301 protected:
1302 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 };
1304
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001305 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1306
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001308 * If the string is an external string, return the ExternalStringResourceBase
1309 * regardless of the encoding, otherwise return NULL. The encoding of the
1310 * string is returned in encoding_out.
1311 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001312 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1313 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001314
1315 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001316 * Get the ExternalStringResource for an external string. Returns
1317 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001319 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320
1321 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001322 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001323 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001325 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001327 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001329 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001331 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001332 * The second parameter 'length' gives the buffer length. If omitted,
1333 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001335 V8_INLINE(static Local<String> New(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001337 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001338 /** Allocates a new string from 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001339 V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001341 // TODO(dcarney): deprecate
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001342 /**
1343 * Creates an internalized string (historically called a "symbol",
1344 * not to be confused with ES6 symbols). Returns one if it exists already.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001345 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001346 V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
1347
1348 enum NewStringType {
1349 kNormalString, kInternalizedString, kUndetectableString
1350 };
1351
1352 /** Allocates a new string from UTF-8 data.*/
1353 static Local<String> NewFromUtf8(Isolate* isolate,
1354 const char* data,
1355 NewStringType type = kNormalString,
1356 int length = -1);
1357
1358 /** Allocates a new string from Latin-1 data.*/
1359 static Local<String> NewFromOneByte(
1360 Isolate* isolate,
1361 const uint8_t* data,
1362 NewStringType type = kNormalString,
1363 int length = -1);
1364
1365 /** Allocates a new string from UTF-16 data.*/
1366 static Local<String> NewFromTwoByte(
1367 Isolate* isolate,
1368 const uint16_t* data,
1369 NewStringType type = kNormalString,
1370 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371
v8.team.kasperl727e9952008-09-02 14:56:44 +00001372 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001373 * Creates a new string by concatenating the left and the right strings
1374 * passed in as parameters.
1375 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001376 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001377
1378 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001379 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001380 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001381 * resource will be disposed by calling its Dispose method. The caller of
1382 * this function should not otherwise delete or modify the resource. Neither
1383 * should the underlying buffer be deallocated or modified except through the
1384 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001385 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001386 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001387
ager@chromium.org6f10e412009-02-13 10:11:16 +00001388 /**
1389 * Associate an external string resource with this string by transforming it
1390 * in place so that existing references to this string in the JavaScript heap
1391 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001392 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001393 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001394 * The string is not modified if the operation fails. See NewExternal for
1395 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001396 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001397 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398
v8.team.kasperl727e9952008-09-02 14:56:44 +00001399 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001400 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001401 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001402 * resource will be disposed by calling its Dispose method. The caller of
1403 * this function should not otherwise delete or modify the resource. Neither
1404 * should the underlying buffer be deallocated or modified except through the
1405 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001406 */
1407 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001408
ager@chromium.org6f10e412009-02-13 10:11:16 +00001409 /**
1410 * Associate an external string resource with this string by transforming it
1411 * in place so that existing references to this string in the JavaScript heap
1412 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001413 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001414 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001415 * The string is not modified if the operation fails. See NewExternal for
1416 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001417 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001418 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001420 /**
1421 * Returns true if this string can be made external.
1422 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001423 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001424
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001425 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001426 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001427 V8_INLINE(
1428 static Local<String> NewUndetectable(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001429
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001430 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001431 /** Creates an undetectable string from the supplied 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001432 V8_INLINE(static Local<String> NewUndetectable(
1433 const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434
1435 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001436 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001437 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001438 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001439 * then the length() method returns 0 and the * operator returns
1440 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001441 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001442 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001443 public:
1444 explicit Utf8Value(Handle<v8::Value> obj);
1445 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001446 char* operator*() { return str_; }
1447 const char* operator*() const { return str_; }
1448 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001449 private:
1450 char* str_;
1451 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001452
1453 // Disallow copying and assigning.
1454 Utf8Value(const Utf8Value&);
1455 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001456 };
1457
1458 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001459 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001461 * If conversion to a string fails (eg. due to an exception in the toString()
1462 * method of the object) then the length() method returns 0 and the * operator
1463 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001465 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 public:
1467 explicit AsciiValue(Handle<v8::Value> obj);
1468 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001469 char* operator*() { return str_; }
1470 const char* operator*() const { return str_; }
1471 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 private:
1473 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001474 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001475
1476 // Disallow copying and assigning.
1477 AsciiValue(const AsciiValue&);
1478 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 };
1480
1481 /**
1482 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001483 * If conversion to a string fails (eg. due to an exception in the toString()
1484 * method of the object) then the length() method returns 0 and the * operator
1485 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001487 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 public:
1489 explicit Value(Handle<v8::Value> obj);
1490 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001491 uint16_t* operator*() { return str_; }
1492 const uint16_t* operator*() const { return str_; }
1493 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494 private:
1495 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001496 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001497
1498 // Disallow copying and assigning.
1499 Value(const Value&);
1500 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001502
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001503 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001504 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1505 Encoding encoding) const;
1506 void VerifyExternalStringResource(ExternalStringResource* val) const;
1507 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508};
1509
1510
1511/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001512 * A JavaScript symbol (ECMA-262 edition 6)
1513 *
1514 * This is an experimental feature. Use at your own risk.
1515 */
1516class V8EXPORT Symbol : public Primitive {
1517 public:
1518 // Returns the print name string of the symbol, or undefined if none.
1519 Local<Value> Name() const;
1520
1521 // Create a symbol without a print name.
1522 static Local<Symbol> New(Isolate* isolate);
1523
1524 // Create a symbol with a print name.
1525 static Local<Symbol> New(Isolate *isolate, const char* data, int length = -1);
1526
1527 V8_INLINE(static Symbol* Cast(v8::Value* obj));
1528 private:
1529 Symbol();
1530 static void CheckCast(v8::Value* obj);
1531};
1532
1533
1534/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001535 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001537class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001539 double Value() const;
1540 static Local<Number> New(double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001541 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001543 Number();
1544 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545};
1546
1547
1548/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001549 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001551class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001552 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001553 static Local<Integer> New(int32_t value);
1554 static Local<Integer> NewFromUnsigned(uint32_t value);
1555 static Local<Integer> New(int32_t value, Isolate*);
1556 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1557 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001558 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001560 Integer();
1561 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562};
1563
1564
1565/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001566 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001568class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001570 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001572 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573};
1574
1575
1576/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001577 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001579class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001581 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001583 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584};
1585
1586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587enum PropertyAttribute {
1588 None = 0,
1589 ReadOnly = 1 << 0,
1590 DontEnum = 1 << 1,
1591 DontDelete = 1 << 2
1592};
1593
ager@chromium.org3811b432009-10-28 14:53:37 +00001594enum ExternalArrayType {
1595 kExternalByteArray = 1,
1596 kExternalUnsignedByteArray,
1597 kExternalShortArray,
1598 kExternalUnsignedShortArray,
1599 kExternalIntArray,
1600 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001601 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001602 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001603 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001604};
1605
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001607 * Accessor[Getter|Setter] are used as callback functions when
1608 * setting|getting a particular property. See Object and ObjectTemplate's
1609 * method SetAccessor.
1610 */
1611typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1612 const AccessorInfo& info);
1613
1614
1615typedef void (*AccessorSetter)(Local<String> property,
1616 Local<Value> value,
1617 const AccessorInfo& info);
1618
1619
1620/**
1621 * Access control specifications.
1622 *
1623 * Some accessors should be accessible across contexts. These
1624 * accessors have an explicit access control parameter which specifies
1625 * the kind of cross-context access that should be allowed.
1626 *
1627 * Additionally, for security, accessors can prohibit overwriting by
1628 * accessors defined in JavaScript. For objects that have such
1629 * accessors either locally or in their prototype chain it is not
1630 * possible to overwrite the accessor by using __defineGetter__ or
1631 * __defineSetter__ from JavaScript code.
1632 */
1633enum AccessControl {
1634 DEFAULT = 0,
1635 ALL_CAN_READ = 1,
1636 ALL_CAN_WRITE = 1 << 1,
1637 PROHIBITS_OVERWRITING = 1 << 2
1638};
1639
1640
1641/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001642 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001644class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001646 bool Set(Handle<Value> key,
1647 Handle<Value> value,
1648 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001649
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001650 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001651
ager@chromium.orge2902be2009-06-08 12:21:35 +00001652 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001653 // overriding accessors or read-only properties.
1654 //
1655 // Note that if the object has an interceptor the property will be set
1656 // locally, but since the interceptor takes precedence the local property
1657 // will only be returned if the interceptor doesn't return a value.
1658 //
1659 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001660 bool ForceSet(Handle<Value> key,
1661 Handle<Value> value,
1662 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001663
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001664 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001666 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001667
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001668 /**
1669 * Gets the property attributes of a property which can be None or
1670 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1671 * None when the property doesn't exist.
1672 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001673 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001674
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001675 bool Has(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001676
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001677 bool Delete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001678
1679 // Delete a property on this object bypassing interceptors and
1680 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001681 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001682
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001683 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001684
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001685 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001687 bool SetAccessor(Handle<String> name,
1688 AccessorGetter getter,
1689 AccessorSetter setter = 0,
1690 Handle<Value> data = Handle<Value>(),
1691 AccessControl settings = DEFAULT,
1692 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001693
ulan@chromium.org750145a2013-03-07 15:14:13 +00001694 // This function is not yet stable and should not be used at this time.
1695 bool SetAccessor(Handle<String> name,
1696 Handle<DeclaredAccessorDescriptor> descriptor,
1697 AccessControl settings = DEFAULT,
1698 PropertyAttribute attribute = None);
1699
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001701 * Returns an array containing the names of the enumerable properties
1702 * of this object, including properties from prototype objects. The
1703 * array returned by this method contains the same values as would
1704 * be enumerated by a for-in statement over this object.
1705 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001706 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001707
1708 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001709 * This function has the same functionality as GetPropertyNames but
1710 * the returned array doesn't contain the names of properties from
1711 * prototype objects.
1712 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001713 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001714
1715 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 * Get the prototype object. This does not skip objects marked to
1717 * be skipped by __proto__ and it does not consult the security
1718 * handler.
1719 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001720 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721
1722 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001723 * Set the prototype object. This does not skip objects marked to
1724 * be skipped by __proto__ and it does not consult the security
1725 * handler.
1726 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001727 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001728
1729 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001730 * Finds an instance of the given function template in the prototype
1731 * chain.
1732 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001733 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001734
1735 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001736 * Call builtin Object.prototype.toString on this object.
1737 * This is different from Value::ToString() that may call
1738 * user-defined toString function. This one does not.
1739 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001740 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001742 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001743 * Returns the function invoked as a constructor for this object.
1744 * May be the null value.
1745 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001746 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001747
1748 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001749 * Returns the name of the function invoked as a constructor for this object.
1750 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001751 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001752
kasper.lund212ac232008-07-16 07:07:30 +00001753 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001754 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001755
1756 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001757 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001758
kasper.lund212ac232008-07-16 07:07:30 +00001759 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001760 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001761
1762 /**
1763 * Gets a 2-byte-aligned native pointer from an internal field. This field
1764 * must have been set by SetAlignedPointerInInternalField, everything else
1765 * leads to undefined behavior.
1766 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001767 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001768
1769 /**
1770 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1771 * a field, GetAlignedPointerFromInternalField must be used, everything else
1772 * leads to undefined behavior.
1773 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001774 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001775
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001777 bool HasOwnProperty(Handle<String> key);
1778 bool HasRealNamedProperty(Handle<String> key);
1779 bool HasRealIndexedProperty(uint32_t index);
1780 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781
1782 /**
1783 * If result.IsEmpty() no real property was located in the prototype chain.
1784 * This means interceptors in the prototype chain are not called.
1785 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001786 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001787
1788 /**
1789 * If result.IsEmpty() no real property was located on the object or
1790 * in the prototype chain.
1791 * This means interceptors in the prototype chain are not called.
1792 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001793 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794
1795 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001796 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797
kasper.lund212ac232008-07-16 07:07:30 +00001798 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001799 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001801 /**
1802 * Turns on access check on the object if the object is an instance of
1803 * a template that has access check callbacks. If an object has no
1804 * access check info, the object cannot be accessed by anyone.
1805 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001806 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001807
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001808 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001809 * Returns the identity hash for this object. The current implementation
1810 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001811 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001812 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001813 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001814 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001815 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001816
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001817 /**
1818 * Access hidden properties on JavaScript objects. These properties are
1819 * hidden from the executing JavaScript and only accessible through the V8
1820 * C++ API. Hidden properties introduced by V8 internally (for example the
1821 * identity hash) are prefixed with "v8::".
1822 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001823 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1824 Local<Value> GetHiddenValue(Handle<String> key);
1825 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001826
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001827 /**
1828 * Returns true if this is an instance of an api function (one
1829 * created from a function created from a function template) and has
1830 * been modified since it was created. Note that this method is
1831 * conservative and may return true for objects that haven't actually
1832 * been modified.
1833 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001834 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001835
1836 /**
1837 * Clone this object with a fast but shallow copy. Values will point
1838 * to the same values as the original object.
1839 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001840 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001842 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001843 * Returns the context in which the object was created.
1844 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001845 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001846
1847 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001848 * Set the backing store of the indexed properties to be managed by the
1849 * embedding layer. Access to the indexed properties will follow the rules
1850 * spelled out in CanvasPixelArray.
1851 * Note: The embedding program still owns the data and needs to ensure that
1852 * the backing store is preserved while V8 has a reference.
1853 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001854 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1855 bool HasIndexedPropertiesInPixelData();
1856 uint8_t* GetIndexedPropertiesPixelData();
1857 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001858
ager@chromium.org3811b432009-10-28 14:53:37 +00001859 /**
1860 * Set the backing store of the indexed properties to be managed by the
1861 * embedding layer. Access to the indexed properties will follow the rules
1862 * spelled out for the CanvasArray subtypes in the WebGL specification.
1863 * Note: The embedding program still owns the data and needs to ensure that
1864 * the backing store is preserved while V8 has a reference.
1865 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001866 void SetIndexedPropertiesToExternalArrayData(void* data,
1867 ExternalArrayType array_type,
1868 int number_of_elements);
1869 bool HasIndexedPropertiesInExternalArrayData();
1870 void* GetIndexedPropertiesExternalArrayData();
1871 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1872 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001873
lrn@chromium.org1c092762011-05-09 09:42:16 +00001874 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001875 * Checks whether a callback is set by the
1876 * ObjectTemplate::SetCallAsFunctionHandler method.
1877 * When an Object is callable this method returns true.
1878 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001879 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001880
1881 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001882 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001883 * ObjectTemplate::SetCallAsFunctionHandler method.
1884 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001885 Local<Value> CallAsFunction(Handle<Object> recv,
1886 int argc,
1887 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001888
1889 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001890 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001891 * ObjectTemplate::SetCallAsFunctionHandler method.
1892 * Note: This method behaves like the Function::NewInstance method.
1893 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001894 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00001895
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001896 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001897 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001898
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001900 Object();
1901 static void CheckCast(Value* obj);
1902 Local<Value> SlowGetInternalField(int index);
1903 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904};
1905
1906
1907/**
1908 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1909 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001910class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001912 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913
ager@chromium.org3e875802009-06-29 08:26:34 +00001914 /**
1915 * Clones an element at index |index|. Returns an empty
1916 * handle if cloning fails (for any reason).
1917 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001918 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001919
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001920 /**
1921 * Creates a JavaScript array with the given length. If the length
1922 * is negative the returned array will have length 0.
1923 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001924 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001925
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001926 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001928 Array();
1929 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930};
1931
1932
1933/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001934 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001936class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001938 Local<Object> NewInstance() const;
1939 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1940 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1941 void SetName(Handle<String> name);
1942 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001943
1944 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001945 * Name inferred from variable or property assignment of this function.
1946 * Used to facilitate debugging and profiling of JavaScript code written
1947 * in an OO style, where many functions are anonymous but are assigned
1948 * to object properties.
1949 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001950 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001951
1952 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001953 * Returns zero based line number of function body and
1954 * kLineOffsetNotFound if no information available.
1955 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001956 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001957 /**
1958 * Returns zero based column number of function body and
1959 * kLineOffsetNotFound if no information available.
1960 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001961 int GetScriptColumnNumber() const;
1962 Handle<Value> GetScriptId() const;
1963 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001964 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001965 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001966
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001968 Function();
1969 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970};
1971
1972
1973/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001974 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1975 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001976class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001977 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001978 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001979
1980 /**
1981 * A specialization of Value::NumberValue that is more efficient
1982 * because we know the structure of this object.
1983 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001984 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001985
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001986 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001987
1988 /**
1989 * Notification that the embedder has changed the time zone,
1990 * daylight savings time, or other date / time configuration
1991 * parameters. V8 keeps a cache of various values used for
1992 * date / time computation. This notification will reset
1993 * those cached values for the current context so that date /
1994 * time configuration changes would be reflected in the Date
1995 * object.
1996 *
1997 * This API should not be called more than needed as it will
1998 * negatively impact the performance of date operations.
1999 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002000 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002001
2002 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002003 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002004};
2005
2006
2007/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002008 * A Number object (ECMA-262, 4.3.21).
2009 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002010class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002011 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002012 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002013
2014 /**
2015 * Returns the Number held by the object.
2016 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002017 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002018
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002019 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002020
2021 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002022 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002023};
2024
2025
2026/**
2027 * A Boolean object (ECMA-262, 4.3.15).
2028 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002029class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002030 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002031 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002032
2033 /**
2034 * Returns the Boolean held by the object.
2035 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002036 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002037
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002038 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002039
2040 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002041 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002042};
2043
2044
2045/**
2046 * A String object (ECMA-262, 4.3.18).
2047 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002048class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002049 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002050 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002051
2052 /**
2053 * Returns the String held by the object.
2054 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002055 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002056
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002057 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002058
2059 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002060 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002061};
2062
2063
2064/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002065 * A Symbol object (ECMA-262 edition 6).
2066 *
2067 * This is an experimental feature. Use at your own risk.
2068 */
2069class V8EXPORT SymbolObject : public Object {
2070 public:
2071 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2072
2073 /**
2074 * Returns the Symbol held by the object.
2075 */
2076 Local<Symbol> SymbolValue() const;
2077
2078 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2079
2080 private:
2081 static void CheckCast(v8::Value* obj);
2082};
2083
2084
2085/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002086 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2087 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002088class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002089 public:
2090 /**
2091 * Regular expression flag bits. They can be or'ed to enable a set
2092 * of flags.
2093 */
2094 enum Flags {
2095 kNone = 0,
2096 kGlobal = 1,
2097 kIgnoreCase = 2,
2098 kMultiline = 4
2099 };
2100
2101 /**
2102 * Creates a regular expression from the given pattern string and
2103 * the flags bit field. May throw a JavaScript exception as
2104 * described in ECMA-262, 15.10.4.1.
2105 *
2106 * For example,
2107 * RegExp::New(v8::String::New("foo"),
2108 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2109 * is equivalent to evaluating "/foo/gm".
2110 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002111 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002112
2113 /**
2114 * Returns the value of the source property: a string representing
2115 * the regular expression.
2116 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002117 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002118
2119 /**
2120 * Returns the flags bit field.
2121 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002122 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002123
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002124 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002125
2126 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002127 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002128};
2129
2130
2131/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002132 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2133 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002135class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002137 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002138 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002139 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002141 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142};
2143
2144
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002145// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146
2147
2148/**
2149 * The superclass of object and function templates.
2150 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002151class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 public:
2153 /** Adds a property to each instance created by this template.*/
2154 void Set(Handle<String> name, Handle<Data> value,
2155 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002156 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157 private:
2158 Template();
2159
2160 friend class ObjectTemplate;
2161 friend class FunctionTemplate;
2162};
2163
2164
2165/**
2166 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002167 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168 * including the receiver, the number and values of arguments, and
2169 * the holder of the function.
2170 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002171class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002172 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002173 V8_INLINE(int Length() const);
2174 V8_INLINE(Local<Value> operator[](int i) const);
2175 V8_INLINE(Local<Function> Callee() const);
2176 V8_INLINE(Local<Object> This() const);
2177 V8_INLINE(Local<Object> Holder() const);
2178 V8_INLINE(bool IsConstructCall() const);
2179 V8_INLINE(Local<Value> Data() const);
2180 V8_INLINE(Isolate* GetIsolate() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002181
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002183 static const int kIsolateIndex = 0;
2184 static const int kDataIndex = -1;
2185 static const int kCalleeIndex = -2;
2186 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188 friend class ImplementationUtilities;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002189 V8_INLINE(Arguments(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002190 internal::Object** values,
2191 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002192 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002193 internal::Object** implicit_args_;
2194 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002196 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197};
2198
2199
2200/**
2201 * The information passed to an accessor callback about the context
2202 * of the property access.
2203 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002204class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002206 V8_INLINE(AccessorInfo(internal::Object** args))
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002207 : args_(args) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002208 V8_INLINE(Isolate* GetIsolate() const);
2209 V8_INLINE(Local<Value> Data() const);
2210 V8_INLINE(Local<Object> This() const);
2211 V8_INLINE(Local<Object> Holder() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002214 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002215};
2216
2217
2218typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2219
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002221 * NamedProperty[Getter|Setter] are used as interceptors on object.
2222 * See ObjectTemplate::SetNamedPropertyHandler.
2223 */
2224typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2225 const AccessorInfo& info);
2226
2227
2228/**
2229 * Returns the value if the setter intercepts the request.
2230 * Otherwise, returns an empty handle.
2231 */
2232typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2233 Local<Value> value,
2234 const AccessorInfo& info);
2235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236/**
2237 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002238 * The result is an integer encoding property attributes (like v8::None,
2239 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002241typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2242 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243
2244
2245/**
2246 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002247 * The return value is true if the property could be deleted and false
2248 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 */
2250typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2251 const AccessorInfo& info);
2252
2253/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002254 * Returns an array containing the names of the properties the named
2255 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 */
2257typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2258
v8.team.kasperl727e9952008-09-02 14:56:44 +00002259
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002261 * Returns the value of the property if the getter intercepts the
2262 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 */
2264typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2265 const AccessorInfo& info);
2266
2267
2268/**
2269 * Returns the value if the setter intercepts the request.
2270 * Otherwise, returns an empty handle.
2271 */
2272typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2273 Local<Value> value,
2274 const AccessorInfo& info);
2275
2276
2277/**
2278 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002279 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002280 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002281typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2282 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283
2284/**
2285 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002286 * The return value is true if the property could be deleted and false
2287 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 */
2289typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2290 const AccessorInfo& info);
2291
v8.team.kasperl727e9952008-09-02 14:56:44 +00002292/**
2293 * Returns an array containing the indices of the properties the
2294 * indexed property getter intercepts.
2295 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2297
2298
2299/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002300 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 */
2302enum AccessType {
2303 ACCESS_GET,
2304 ACCESS_SET,
2305 ACCESS_HAS,
2306 ACCESS_DELETE,
2307 ACCESS_KEYS
2308};
2309
v8.team.kasperl727e9952008-09-02 14:56:44 +00002310
2311/**
2312 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002313 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002314 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002315typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 Local<Value> key,
2317 AccessType type,
2318 Local<Value> data);
2319
v8.team.kasperl727e9952008-09-02 14:56:44 +00002320
2321/**
2322 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002323 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002324 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002325typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 uint32_t index,
2327 AccessType type,
2328 Local<Value> data);
2329
2330
2331/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002332 * A FunctionTemplate is used to create functions at runtime. There
2333 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002334 * context. The lifetime of the created function is equal to the
2335 * lifetime of the context. So in case the embedder needs to create
2336 * temporary functions that can be collected using Scripts is
2337 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338 *
2339 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002340 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002341 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002342 * A FunctionTemplate has a corresponding instance template which is
2343 * used to create object instances when the function is used as a
2344 * constructor. Properties added to the instance template are added to
2345 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346 *
2347 * A FunctionTemplate can have a prototype template. The prototype template
2348 * is used to create the prototype object of the function.
2349 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002350 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002352 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2354 * t->Set("func_property", v8::Number::New(1));
2355 *
2356 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2357 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2358 * proto_t->Set("proto_const", v8::Number::New(2));
2359 *
2360 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2361 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2362 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2363 * instance_t->Set("instance_property", Number::New(3));
2364 *
2365 * v8::Local<v8::Function> function = t->GetFunction();
2366 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002367 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 *
2369 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002370 * and "instance" for the instance object created above. The function
2371 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002373 * \code
2374 * func_property in function == true;
2375 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002376 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002377 * function.prototype.proto_method() invokes 'InvokeCallback'
2378 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002379 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002380 * instance instanceof function == true;
2381 * instance.instance_accessor calls 'InstanceAccessorCallback'
2382 * instance.instance_property == 3;
2383 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002385 * A FunctionTemplate can inherit from another one by calling the
2386 * FunctionTemplate::Inherit method. The following graph illustrates
2387 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002389 * \code
2390 * FunctionTemplate Parent -> Parent() . prototype -> { }
2391 * ^ ^
2392 * | Inherit(Parent) | .__proto__
2393 * | |
2394 * FunctionTemplate Child -> Child() . prototype -> { }
2395 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002397 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2398 * object of the Child() function has __proto__ pointing to the
2399 * Parent() function's prototype object. An instance of the Child
2400 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002402 * Let Parent be the FunctionTemplate initialized in the previous
2403 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002404 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002405 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406 * Local<FunctionTemplate> parent = t;
2407 * Local<FunctionTemplate> child = FunctionTemplate::New();
2408 * child->Inherit(parent);
2409 *
2410 * Local<Function> child_function = child->GetFunction();
2411 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002412 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002414 * The Child function and Child instance will have the following
2415 * properties:
2416 *
2417 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002419 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002421 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002423class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424 public:
2425 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002426 static Local<FunctionTemplate> New(
2427 InvocationCallback callback = 0,
2428 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002429 Handle<Signature> signature = Handle<Signature>(),
2430 int length = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431 /** Returns the unique function instance in the current execution context.*/
2432 Local<Function> GetFunction();
2433
v8.team.kasperl727e9952008-09-02 14:56:44 +00002434 /**
2435 * Set the call-handler callback for a FunctionTemplate. This
2436 * callback is called whenever the function created from this
2437 * FunctionTemplate is called.
2438 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439 void SetCallHandler(InvocationCallback callback,
2440 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002442 /** Set the predefined length property for the FunctionTemplate. */
2443 void SetLength(int length);
2444
v8.team.kasperl727e9952008-09-02 14:56:44 +00002445 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446 Local<ObjectTemplate> InstanceTemplate();
2447
2448 /** Causes the function template to inherit from a parent function template.*/
2449 void Inherit(Handle<FunctionTemplate> parent);
2450
2451 /**
2452 * A PrototypeTemplate is the template used to create the prototype object
2453 * of the function created by this template.
2454 */
2455 Local<ObjectTemplate> PrototypeTemplate();
2456
v8.team.kasperl727e9952008-09-02 14:56:44 +00002457 /**
2458 * Set the class name of the FunctionTemplate. This is used for
2459 * printing objects created with the function created from the
2460 * FunctionTemplate as its constructor.
2461 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462 void SetClassName(Handle<String> name);
2463
2464 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002465 * Determines whether the __proto__ accessor ignores instances of
2466 * the function template. If instances of the function template are
2467 * ignored, __proto__ skips all instances and instead returns the
2468 * next object in the prototype chain.
2469 *
2470 * Call with a value of true to make the __proto__ accessor ignore
2471 * instances of the function template. Call with a value of false
2472 * to make the __proto__ accessor not ignore instances of the
2473 * function template. By default, instances of a function template
2474 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475 */
2476 void SetHiddenPrototype(bool value);
2477
2478 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002479 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2480 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002481 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002482 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002483
2484 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002485 * Returns true if the given object is an instance of this function
2486 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 */
2488 bool HasInstance(Handle<Value> object);
2489
2490 private:
2491 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2493 NamedPropertySetter setter,
2494 NamedPropertyQuery query,
2495 NamedPropertyDeleter remover,
2496 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002497 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2499 IndexedPropertySetter setter,
2500 IndexedPropertyQuery query,
2501 IndexedPropertyDeleter remover,
2502 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002503 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002504 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2505 Handle<Value> data);
2506
2507 friend class Context;
2508 friend class ObjectTemplate;
2509};
2510
2511
2512/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002513 * An ObjectTemplate is used to create objects at runtime.
2514 *
2515 * Properties added to an ObjectTemplate are added to each object
2516 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002518class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002520 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002521 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523 /** Creates a new instance of this template.*/
2524 Local<Object> NewInstance();
2525
2526 /**
2527 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002528 *
2529 * Whenever the property with the given name is accessed on objects
2530 * created from this ObjectTemplate the getter and setter callbacks
2531 * are called instead of getting and setting the property directly
2532 * on the JavaScript object.
2533 *
2534 * \param name The name of the property for which an accessor is added.
2535 * \param getter The callback to invoke when getting the property.
2536 * \param setter The callback to invoke when setting the property.
2537 * \param data A piece of data that will be passed to the getter and setter
2538 * callbacks whenever they are invoked.
2539 * \param settings Access control settings for the accessor. This is a bit
2540 * field consisting of one of more of
2541 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2542 * The default is to not allow cross-context access.
2543 * ALL_CAN_READ means that all cross-context reads are allowed.
2544 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2545 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2546 * cross-context access.
2547 * \param attribute The attributes of the property for which an accessor
2548 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002549 * \param signature The signature describes valid receivers for the accessor
2550 * and is used to perform implicit instance checks against them. If the
2551 * receiver is incompatible (i.e. is not an instance of the constructor as
2552 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2553 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002554 */
2555 void SetAccessor(Handle<String> name,
2556 AccessorGetter getter,
2557 AccessorSetter setter = 0,
2558 Handle<Value> data = Handle<Value>(),
2559 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002560 PropertyAttribute attribute = None,
2561 Handle<AccessorSignature> signature =
2562 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563
ulan@chromium.org750145a2013-03-07 15:14:13 +00002564 // This function is not yet stable and should not be used at this time.
2565 bool SetAccessor(Handle<String> name,
2566 Handle<DeclaredAccessorDescriptor> descriptor,
2567 AccessControl settings = DEFAULT,
2568 PropertyAttribute attribute = None,
2569 Handle<AccessorSignature> signature =
2570 Handle<AccessorSignature>());
2571
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572 /**
2573 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002574 *
2575 * Whenever a named property is accessed on objects created from
2576 * this object template, the provided callback is invoked instead of
2577 * accessing the property directly on the JavaScript object.
2578 *
2579 * \param getter The callback to invoke when getting a property.
2580 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002581 * \param query The callback to invoke to check if a property is present,
2582 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002583 * \param deleter The callback to invoke when deleting a property.
2584 * \param enumerator The callback to invoke to enumerate all the named
2585 * properties of an object.
2586 * \param data A piece of data that will be passed to the callbacks
2587 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588 */
2589 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2590 NamedPropertySetter setter = 0,
2591 NamedPropertyQuery query = 0,
2592 NamedPropertyDeleter deleter = 0,
2593 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002594 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595
2596 /**
2597 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002598 *
2599 * Whenever an indexed property is accessed on objects created from
2600 * this object template, the provided callback is invoked instead of
2601 * accessing the property directly on the JavaScript object.
2602 *
2603 * \param getter The callback to invoke when getting a property.
2604 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002605 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002606 * \param deleter The callback to invoke when deleting a property.
2607 * \param enumerator The callback to invoke to enumerate all the indexed
2608 * properties of an object.
2609 * \param data A piece of data that will be passed to the callbacks
2610 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611 */
2612 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2613 IndexedPropertySetter setter = 0,
2614 IndexedPropertyQuery query = 0,
2615 IndexedPropertyDeleter deleter = 0,
2616 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002617 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 /**
2620 * Sets the callback to be used when calling instances created from
2621 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002622 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002623 * function.
2624 */
2625 void SetCallAsFunctionHandler(InvocationCallback callback,
2626 Handle<Value> data = Handle<Value>());
2627
v8.team.kasperl727e9952008-09-02 14:56:44 +00002628 /**
2629 * Mark object instances of the template as undetectable.
2630 *
2631 * In many ways, undetectable objects behave as though they are not
2632 * there. They behave like 'undefined' in conditionals and when
2633 * printed. However, properties can be accessed and called as on
2634 * normal objects.
2635 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 void MarkAsUndetectable();
2637
v8.team.kasperl727e9952008-09-02 14:56:44 +00002638 /**
2639 * Sets access check callbacks on the object template.
2640 *
2641 * When accessing properties on instances of this object template,
2642 * the access check callback will be called to determine whether or
2643 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002644 * The last parameter specifies whether access checks are turned
2645 * on by default on instances. If access checks are off by default,
2646 * they can be turned on on individual instances by calling
2647 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002648 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2650 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002651 Handle<Value> data = Handle<Value>(),
2652 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653
kasper.lund212ac232008-07-16 07:07:30 +00002654 /**
2655 * Gets the number of internal fields for objects generated from
2656 * this template.
2657 */
2658 int InternalFieldCount();
2659
2660 /**
2661 * Sets the number of internal fields for objects generated from
2662 * this template.
2663 */
2664 void SetInternalFieldCount(int value);
2665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 private:
2667 ObjectTemplate();
2668 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2669 friend class FunctionTemplate;
2670};
2671
2672
2673/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002674 * A Signature specifies which receivers and arguments are valid
2675 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002677class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678 public:
2679 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2680 Handle<FunctionTemplate>(),
2681 int argc = 0,
2682 Handle<FunctionTemplate> argv[] = 0);
2683 private:
2684 Signature();
2685};
2686
2687
2688/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002689 * An AccessorSignature specifies which receivers are valid parameters
2690 * to an accessor callback.
2691 */
2692class V8EXPORT AccessorSignature : public Data {
2693 public:
2694 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2695 Handle<FunctionTemplate>());
2696 private:
2697 AccessorSignature();
2698};
2699
2700
ulan@chromium.org750145a2013-03-07 15:14:13 +00002701class V8EXPORT DeclaredAccessorDescriptor : public Data {
2702 private:
2703 DeclaredAccessorDescriptor();
2704};
2705
2706
2707class V8EXPORT ObjectOperationDescriptor : public Data {
2708 public:
2709 // This function is not yet stable and should not be used at this time.
2710 static Local<RawOperationDescriptor> NewInternalFieldDereference(
2711 Isolate* isolate,
2712 int internal_field);
2713 private:
2714 ObjectOperationDescriptor();
2715};
2716
2717
2718enum DeclaredAccessorDescriptorDataType {
2719 kDescriptorBoolType,
2720 kDescriptorInt8Type, kDescriptorUint8Type,
2721 kDescriptorInt16Type, kDescriptorUint16Type,
2722 kDescriptorInt32Type, kDescriptorUint32Type,
2723 kDescriptorFloatType, kDescriptorDoubleType
2724};
2725
2726
2727class V8EXPORT RawOperationDescriptor : public Data {
2728 public:
2729 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
2730 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
2731 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
2732 int16_t byte_offset);
2733 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
2734 void* compare_value);
2735 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
2736 Isolate* isolate,
2737 DeclaredAccessorDescriptorDataType data_type,
2738 uint8_t bool_offset = 0);
2739 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
2740 uint8_t bitmask,
2741 uint8_t compare_value);
2742 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
2743 Isolate* isolate,
2744 uint16_t bitmask,
2745 uint16_t compare_value);
2746 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
2747 Isolate* isolate,
2748 uint32_t bitmask,
2749 uint32_t compare_value);
2750
2751 private:
2752 RawOperationDescriptor();
2753};
2754
2755
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002756/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002757 * A utility for determining the type of objects based on the template
2758 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002759 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002760class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 public:
2762 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2763 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2764 int match(Handle<Value> value);
2765 private:
2766 TypeSwitch();
2767};
2768
2769
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002770// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002772class V8EXPORT ExternalAsciiStringResourceImpl
2773 : public String::ExternalAsciiStringResource {
2774 public:
2775 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2776 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2777 : data_(data), length_(length) {}
2778 const char* data() const { return data_; }
2779 size_t length() const { return length_; }
2780
2781 private:
2782 const char* data_;
2783 size_t length_;
2784};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785
2786/**
2787 * Ignore
2788 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002789class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002791 // Note that the strings passed into this constructor must live as long
2792 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002794 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002795 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002796 const char** deps = 0,
2797 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798 virtual ~Extension() { }
2799 virtual v8::Handle<v8::FunctionTemplate>
2800 GetNativeFunction(v8::Handle<v8::String> name) {
2801 return v8::Handle<v8::FunctionTemplate>();
2802 }
2803
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002804 const char* name() const { return name_; }
2805 size_t source_length() const { return source_length_; }
2806 const String::ExternalAsciiStringResource* source() const {
2807 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002808 int dependency_count() { return dep_count_; }
2809 const char** dependencies() { return deps_; }
2810 void set_auto_enable(bool value) { auto_enable_ = value; }
2811 bool auto_enable() { return auto_enable_; }
2812
2813 private:
2814 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002815 size_t source_length_; // expected to initialize before source_
2816 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002817 int dep_count_;
2818 const char** deps_;
2819 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002820
2821 // Disallow copying and assigning.
2822 Extension(const Extension&);
2823 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824};
2825
2826
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002827void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002828
2829
2830/**
2831 * Ignore
2832 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002833class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002834 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002835 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002836 RegisterExtension(extension);
2837 }
2838};
2839
2840
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002841// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002842
2843
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002844Handle<Primitive> V8EXPORT Undefined();
2845Handle<Primitive> V8EXPORT Null();
2846Handle<Boolean> V8EXPORT True();
2847Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002848
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002849V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
2850V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
2851V8_INLINE(Handle<Boolean> True(Isolate* isolate));
2852V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002853
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002854
2855/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002856 * A set of constraints that specifies the limits of the runtime's memory use.
2857 * You must set the heap size before initializing the VM - the size cannot be
2858 * adjusted after the VM is initialized.
2859 *
2860 * If you are using threads then you should hold the V8::Locker lock while
2861 * setting the stack limit and you must set a non-default stack limit separately
2862 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002863 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002864class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002865 public:
2866 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002867 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002869 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002870 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002871 int max_executable_size() { return max_executable_size_; }
2872 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002873 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002874 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2876 private:
2877 int max_young_space_size_;
2878 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002879 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880 uint32_t* stack_limit_;
2881};
2882
2883
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002884bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002885
2886
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002887// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888
2889
2890typedef void (*FatalErrorCallback)(const char* location, const char* message);
2891
2892
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002893typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002894
2895
2896/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002897 * Schedules an exception to be thrown when returning to JavaScript. When an
2898 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002899 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002900 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002902Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903
2904/**
2905 * Create new error objects by calling the corresponding error object
2906 * constructor with the message.
2907 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002908class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002909 public:
2910 static Local<Value> RangeError(Handle<String> message);
2911 static Local<Value> ReferenceError(Handle<String> message);
2912 static Local<Value> SyntaxError(Handle<String> message);
2913 static Local<Value> TypeError(Handle<String> message);
2914 static Local<Value> Error(Handle<String> message);
2915};
2916
2917
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002918// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002919
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002920typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002921
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002922typedef void* (*CreateHistogramCallback)(const char* name,
2923 int min,
2924 int max,
2925 size_t buckets);
2926
2927typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2928
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002929// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002930 enum ObjectSpace {
2931 kObjectSpaceNewSpace = 1 << 0,
2932 kObjectSpaceOldPointerSpace = 1 << 1,
2933 kObjectSpaceOldDataSpace = 1 << 2,
2934 kObjectSpaceCodeSpace = 1 << 3,
2935 kObjectSpaceMapSpace = 1 << 4,
2936 kObjectSpaceLoSpace = 1 << 5,
2937
2938 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2939 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2940 kObjectSpaceLoSpace
2941 };
2942
2943 enum AllocationAction {
2944 kAllocationActionAllocate = 1 << 0,
2945 kAllocationActionFree = 1 << 1,
2946 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2947 };
2948
2949typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2950 AllocationAction action,
2951 int size);
2952
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002953// --- Leave Script Callback ---
2954typedef void (*CallCompletedCallback)();
2955
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002956// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2958 AccessType type,
2959 Local<Value> data);
2960
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002961// --- AllowCodeGenerationFromStrings callbacks ---
2962
2963/**
2964 * Callback to check if code generation from strings is allowed. See
2965 * Context::AllowCodeGenerationFromStrings.
2966 */
2967typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2968
2969// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970
2971/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002972 * Applications can register callback functions which will be called
2973 * before and after a garbage collection. Allocations are not
2974 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002975 * objects (set or delete properties for example) since it is possible
2976 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002977 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002978enum GCType {
2979 kGCTypeScavenge = 1 << 0,
2980 kGCTypeMarkSweepCompact = 1 << 1,
2981 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2982};
2983
2984enum GCCallbackFlags {
2985 kNoGCCallbackFlags = 0,
2986 kGCCallbackFlagCompacted = 1 << 0
2987};
2988
2989typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2990typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2991
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002992typedef void (*GCCallback)();
2993
2994
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002996 * Collection of V8 heap information.
2997 *
2998 * Instances of this class can be passed to v8::V8::HeapStatistics to
2999 * get heap statistics from V8.
3000 */
3001class V8EXPORT HeapStatistics {
3002 public:
3003 HeapStatistics();
3004 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003005 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003006 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003007 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003008 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003009
3010 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003011 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003012 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003013 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003014 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003015 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003016
3017 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003018 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003019};
3020
3021
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003022class RetainedObjectInfo;
3023
ager@chromium.org3811b432009-10-28 14:53:37 +00003024/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003025 * Isolate represents an isolated instance of the V8 engine. V8
3026 * isolates have completely separate states. Objects from one isolate
3027 * must not be used in other isolates. When V8 is initialized a
3028 * default isolate is implicitly created and entered. The embedder
3029 * can create additional isolates and use them in parallel in multiple
3030 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003031 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003032 */
3033class V8EXPORT Isolate {
3034 public:
3035 /**
3036 * Stack-allocated class which sets the isolate for all operations
3037 * executed within a local scope.
3038 */
3039 class V8EXPORT Scope {
3040 public:
3041 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3042 isolate->Enter();
3043 }
3044
3045 ~Scope() { isolate_->Exit(); }
3046
3047 private:
3048 Isolate* const isolate_;
3049
3050 // Prevent copying of Scope objects.
3051 Scope(const Scope&);
3052 Scope& operator=(const Scope&);
3053 };
3054
3055 /**
3056 * Creates a new isolate. Does not change the currently entered
3057 * isolate.
3058 *
3059 * When an isolate is no longer used its resources should be freed
3060 * by calling Dispose(). Using the delete operator is not allowed.
3061 */
3062 static Isolate* New();
3063
3064 /**
3065 * Returns the entered isolate for the current thread or NULL in
3066 * case there is no current isolate.
3067 */
3068 static Isolate* GetCurrent();
3069
3070 /**
3071 * Methods below this point require holding a lock (using Locker) in
3072 * a multi-threaded environment.
3073 */
3074
3075 /**
3076 * Sets this isolate as the entered one for the current thread.
3077 * Saves the previously entered one (if any), so that it can be
3078 * restored when exiting. Re-entering an isolate is allowed.
3079 */
3080 void Enter();
3081
3082 /**
3083 * Exits this isolate by restoring the previously entered one in the
3084 * current thread. The isolate may still stay the same, if it was
3085 * entered more than once.
3086 *
3087 * Requires: this == Isolate::GetCurrent().
3088 */
3089 void Exit();
3090
3091 /**
3092 * Disposes the isolate. The isolate must not be entered by any
3093 * thread to be disposable.
3094 */
3095 void Dispose();
3096
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003097 /**
3098 * Associate embedder-specific data with the isolate
3099 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003100 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003101
3102 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003103 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003104 * Returns NULL if SetData has never been called.
3105 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003106 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003107
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003108 /**
3109 * Get statistics about the heap memory usage.
3110 */
3111 void GetHeapStatistics(HeapStatistics* heap_statistics);
3112
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003113 /**
3114 * Adjusts the amount of registered external memory. Used to give V8 an
3115 * indication of the amount of externally allocated memory that is kept alive
3116 * by JavaScript objects. V8 uses this to decide when to perform global
3117 * garbage collections. Registering externally allocated memory will trigger
3118 * global garbage collections more often than it would otherwise in an attempt
3119 * to garbage collect the JavaScript objects that keep the externally
3120 * allocated memory alive.
3121 *
3122 * \param change_in_bytes the change in externally allocated memory that is
3123 * kept alive by JavaScript objects.
3124 * \returns the adjusted value.
3125 */
3126 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3127
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003128 /**
3129 * Returns heap profiler for this isolate. Will return NULL until the isolate
3130 * is initialized.
3131 */
3132 HeapProfiler* GetHeapProfiler();
3133
3134 /**
3135 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3136 * is initialized.
3137 */
3138 CpuProfiler* GetCpuProfiler();
3139
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003140 /** Returns the context that is on the top of the stack. */
3141 Local<Context> GetCurrentContext();
3142
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003143 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003144 Isolate();
3145 Isolate(const Isolate&);
3146 ~Isolate();
3147 Isolate& operator=(const Isolate&);
3148 void* operator new(size_t size);
3149 void operator delete(void*, size_t);
3150};
3151
3152
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003153class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003154 public:
3155 enum CompressionAlgorithm {
3156 kUncompressed,
3157 kBZip2
3158 };
3159
3160 const char* data;
3161 int compressed_size;
3162 int raw_size;
3163};
3164
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003165
3166/**
3167 * A helper class for driving V8 startup data decompression. It is based on
3168 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3169 * for an embedder to use this class, instead, API functions can be used
3170 * directly.
3171 *
3172 * For an example of the class usage, see the "shell.cc" sample application.
3173 */
3174class V8EXPORT StartupDataDecompressor { // NOLINT
3175 public:
3176 StartupDataDecompressor();
3177 virtual ~StartupDataDecompressor();
3178 int Decompress();
3179
3180 protected:
3181 virtual int DecompressData(char* raw_data,
3182 int* raw_data_size,
3183 const char* compressed_data,
3184 int compressed_data_size) = 0;
3185
3186 private:
3187 char** raw_data;
3188};
3189
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003190
3191/**
3192 * EntropySource is used as a callback function when v8 needs a source
3193 * of entropy.
3194 */
3195typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3196
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003197
3198/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003199 * ReturnAddressLocationResolver is used as a callback function when v8 is
3200 * resolving the location of a return address on the stack. Profilers that
3201 * change the return address on the stack can use this to resolve the stack
3202 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003203 *
3204 * \param return_addr_location points to a location on stack where a machine
3205 * return address resides.
3206 * \returns either return_addr_location, or else a pointer to the profiler's
3207 * copy of the original return address.
3208 *
3209 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003210 */
3211typedef uintptr_t (*ReturnAddressLocationResolver)(
3212 uintptr_t return_addr_location);
3213
3214
3215/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003216 * FunctionEntryHook is the type of the profile entry hook called at entry to
3217 * any generated function when function-level profiling is enabled.
3218 *
3219 * \param function the address of the function that's being entered.
3220 * \param return_addr_location points to a location on stack where the machine
3221 * return address resides. This can be used to identify the caller of
3222 * \p function, and/or modified to divert execution when \p function exits.
3223 *
3224 * \note the entry hook must not cause garbage collection.
3225 */
3226typedef void (*FunctionEntryHook)(uintptr_t function,
3227 uintptr_t return_addr_location);
3228
3229
3230/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003231 * A JIT code event is issued each time code is added, moved or removed.
3232 *
3233 * \note removal events are not currently issued.
3234 */
3235struct JitCodeEvent {
3236 enum EventType {
3237 CODE_ADDED,
3238 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003239 CODE_REMOVED,
3240 CODE_ADD_LINE_POS_INFO,
3241 CODE_START_LINE_INFO_RECORDING,
3242 CODE_END_LINE_INFO_RECORDING
3243 };
3244 // Definition of the code position type. The "POSITION" type means the place
3245 // in the source code which are of interest when making stack traces to
3246 // pin-point the source location of a stack frame as close as possible.
3247 // The "STATEMENT_POSITION" means the place at the beginning of each
3248 // statement, and is used to indicate possible break locations.
3249 enum PositionType {
3250 POSITION,
3251 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003252 };
3253
3254 // Type of event.
3255 EventType type;
3256 // Start of the instructions.
3257 void* code_start;
3258 // Size of the instructions.
3259 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003260 // Script info for CODE_ADDED event.
3261 Handle<Script> script;
3262 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
3263 // code line information which is returned from the
3264 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
3265 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
3266 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003267
3268 union {
3269 // Only valid for CODE_ADDED.
3270 struct {
3271 // Name of the object associated with the code, note that the string is
3272 // not zero-terminated.
3273 const char* str;
3274 // Number of chars in str.
3275 size_t len;
3276 } name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003277
3278 // Only valid for CODE_ADD_LINE_POS_INFO
3279 struct {
3280 // PC offset
3281 size_t offset;
3282 // Code postion
3283 size_t pos;
3284 // The position type.
3285 PositionType position_type;
3286 } line_info;
3287
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003288 // New location of instructions. Only valid for CODE_MOVED.
3289 void* new_code_start;
3290 };
3291};
3292
3293/**
3294 * Option flags passed to the SetJitCodeEventHandler function.
3295 */
3296enum JitCodeEventOptions {
3297 kJitCodeEventDefault = 0,
3298 // Generate callbacks for already existent code.
3299 kJitCodeEventEnumExisting = 1
3300};
3301
3302
3303/**
3304 * Callback function passed to SetJitCodeEventHandler.
3305 *
3306 * \param event code add, move or removal event.
3307 */
3308typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3309
3310
3311/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003312 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003313 */
3314class V8EXPORT ExternalResourceVisitor { // NOLINT
3315 public:
3316 virtual ~ExternalResourceVisitor() {}
3317 virtual void VisitExternalString(Handle<String> string) {}
3318};
3319
3320
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003321/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003322 * Interface for iterating through all the persistent handles in the heap.
3323 */
3324class V8EXPORT PersistentHandleVisitor { // NOLINT
3325 public:
3326 virtual ~PersistentHandleVisitor() {}
3327 virtual void VisitPersistentHandle(Persistent<Value> value,
3328 uint16_t class_id) {}
3329};
3330
3331
3332/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003333 * Container class for static utility functions.
3334 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003335class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003336 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003337 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003338 static void SetFatalErrorHandler(FatalErrorCallback that);
3339
v8.team.kasperl727e9952008-09-02 14:56:44 +00003340 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003341 * Set the callback to invoke to check if code generation from
3342 * strings should be allowed.
3343 */
3344 static void SetAllowCodeGenerationFromStringsCallback(
3345 AllowCodeGenerationFromStringsCallback that);
3346
3347 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003348 * Ignore out-of-memory exceptions.
3349 *
3350 * V8 running out of memory is treated as a fatal error by default.
3351 * This means that the fatal error handler is called and that V8 is
3352 * terminated.
3353 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003354 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003355 * out-of-memory situation as a fatal error. This way, the contexts
3356 * that did not cause the out of memory problem might be able to
3357 * continue execution.
3358 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359 static void IgnoreOutOfMemoryException();
3360
v8.team.kasperl727e9952008-09-02 14:56:44 +00003361 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003362 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003363 * fatal errors such as out-of-memory situations.
3364 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365 static bool IsDead();
3366
3367 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003368 * The following 4 functions are to be used when V8 is built with
3369 * the 'compress_startup_data' flag enabled. In this case, the
3370 * embedder must decompress startup data prior to initializing V8.
3371 *
3372 * This is how interaction with V8 should look like:
3373 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3374 * v8::StartupData* compressed_data =
3375 * new v8::StartupData[compressed_data_count];
3376 * v8::V8::GetCompressedStartupData(compressed_data);
3377 * ... decompress data (compressed_data can be updated in-place) ...
3378 * v8::V8::SetDecompressedStartupData(compressed_data);
3379 * ... now V8 can be initialized
3380 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003381 *
3382 * A helper class StartupDataDecompressor is provided. It implements
3383 * the protocol of the interaction described above, and can be used in
3384 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003385 */
3386 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3387 static int GetCompressedStartupDataCount();
3388 static void GetCompressedStartupData(StartupData* compressed_data);
3389 static void SetDecompressedStartupData(StartupData* decompressed_data);
3390
3391 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003392 * Adds a message listener.
3393 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003394 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003395 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003396 *
3397 * If data is specified, it will be passed to the callback when it is called.
3398 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003399 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003400 static bool AddMessageListener(MessageCallback that,
3401 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003402
3403 /**
3404 * Remove all message listeners from the specified callback function.
3405 */
3406 static void RemoveMessageListeners(MessageCallback that);
3407
3408 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003409 * Tells V8 to capture current stack trace when uncaught exception occurs
3410 * and report it to the message listeners. The option is off by default.
3411 */
3412 static void SetCaptureStackTraceForUncaughtExceptions(
3413 bool capture,
3414 int frame_limit = 10,
3415 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3416
3417 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003418 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419 */
3420 static void SetFlagsFromString(const char* str, int length);
3421
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003422 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003423 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003424 */
3425 static void SetFlagsFromCommandLine(int* argc,
3426 char** argv,
3427 bool remove_flags);
3428
kasper.lund7276f142008-07-30 08:49:36 +00003429 /** Get the version string. */
3430 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003431
3432 /**
3433 * Enables the host application to provide a mechanism for recording
3434 * statistics counters.
3435 */
3436 static void SetCounterFunction(CounterLookupCallback);
3437
3438 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003439 * Enables the host application to provide a mechanism for recording
3440 * histograms. The CreateHistogram function returns a
3441 * histogram which will later be passed to the AddHistogramSample
3442 * function.
3443 */
3444 static void SetCreateHistogramFunction(CreateHistogramCallback);
3445 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3446
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003447 /** Callback function for reporting failed access checks.*/
3448 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3449
3450 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003451 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003452 * garbage collection. Allocations are not allowed in the
3453 * callback function, you therefore cannot manipulate objects (set
3454 * or delete properties for example) since it is possible such
3455 * operations will result in the allocation of objects. It is possible
3456 * to specify the GCType filter for your callback. But it is not possible to
3457 * register the same callback function two times with different
3458 * GCType filters.
3459 */
3460 static void AddGCPrologueCallback(
3461 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3462
3463 /**
3464 * This function removes callback which was installed by
3465 * AddGCPrologueCallback function.
3466 */
3467 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3468
3469 /**
3470 * The function is deprecated. Please use AddGCPrologueCallback instead.
3471 * Enables the host application to receive a notification before a
3472 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003473 * callback function, you therefore cannot manipulate objects (set
3474 * or delete properties for example) since it is possible such
3475 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003476 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003477 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003478
3479 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003480 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003481 * garbage collection. Allocations are not allowed in the
3482 * callback function, you therefore cannot manipulate objects (set
3483 * or delete properties for example) since it is possible such
3484 * operations will result in the allocation of objects. It is possible
3485 * to specify the GCType filter for your callback. But it is not possible to
3486 * register the same callback function two times with different
3487 * GCType filters.
3488 */
3489 static void AddGCEpilogueCallback(
3490 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3491
3492 /**
3493 * This function removes callback which was installed by
3494 * AddGCEpilogueCallback function.
3495 */
3496 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3497
3498 /**
3499 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3500 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003501 * major garbage collection. Allocations are not allowed in the
3502 * callback function, you therefore cannot manipulate objects (set
3503 * or delete properties for example) since it is possible such
3504 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003505 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003506 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003507
3508 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003509 * Enables the host application to provide a mechanism to be notified
3510 * and perform custom logging when V8 Allocates Executable Memory.
3511 */
3512 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3513 ObjectSpace space,
3514 AllocationAction action);
3515
3516 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003517 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003518 */
3519 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3520
3521 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003522 * Adds a callback to notify the host application when a script finished
3523 * running. If a script re-enters the runtime during executing, the
3524 * CallCompletedCallback is only invoked when the outer-most script
3525 * execution ends. Executing scripts inside the callback do not trigger
3526 * further callbacks.
3527 */
3528 static void AddCallCompletedCallback(CallCompletedCallback callback);
3529
3530 /**
3531 * Removes callback that was installed by AddCallCompletedCallback.
3532 */
3533 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3534
3535 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003536 * Allows the host application to group objects together. If one
3537 * object in the group is alive, all objects in the group are alive.
3538 * After each garbage collection, object groups are removed. It is
3539 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003540 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003541 * wrapper objects. Object groups for all dependent handles need to
3542 * be provided for kGCTypeMarkSweepCompact collections, for all other
3543 * garbage collection types it is sufficient to provide object groups
3544 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003545 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003546 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003547 static void AddObjectGroup(Persistent<Value>* objects,
3548 size_t length,
3549 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003550 static void AddObjectGroup(Isolate* isolate,
3551 Persistent<Value>* objects,
3552 size_t length,
3553 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003554
3555 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003556 * Allows the host application to declare implicit references between
3557 * the objects: if |parent| is alive, all |children| are alive too.
3558 * After each garbage collection, all implicit references
3559 * are removed. It is intended to be used in the before-garbage-collection
3560 * callback function.
3561 */
3562 static void AddImplicitReferences(Persistent<Object> parent,
3563 Persistent<Value>* children,
3564 size_t length);
3565
3566 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003567 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003568 * initialize from scratch. This function is called implicitly if
3569 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570 */
3571 static bool Initialize();
3572
kasper.lund7276f142008-07-30 08:49:36 +00003573 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003574 * Allows the host application to provide a callback which can be used
3575 * as a source of entropy for random number generators.
3576 */
3577 static void SetEntropySource(EntropySource source);
3578
3579 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003580 * Allows the host application to provide a callback that allows v8 to
3581 * cooperate with a profiler that rewrites return addresses on stack.
3582 */
3583 static void SetReturnAddressLocationResolver(
3584 ReturnAddressLocationResolver return_address_resolver);
3585
3586 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003587 * Allows the host application to provide the address of a function that's
3588 * invoked on entry to every V8-generated function.
3589 * Note that \p entry_hook is invoked at the very start of each
3590 * generated function.
3591 *
3592 * \param entry_hook a function that will be invoked on entry to every
3593 * V8-generated function.
3594 * \returns true on success on supported platforms, false on failure.
3595 * \note Setting a new entry hook function when one is already active will
3596 * fail.
3597 */
3598 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3599
3600 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003601 * Allows the host application to provide the address of a function that is
3602 * notified each time code is added, moved or removed.
3603 *
3604 * \param options options for the JIT code event handler.
3605 * \param event_handler the JIT code event handler, which will be invoked
3606 * each time code is added, moved or removed.
3607 * \note \p event_handler won't get notified of existent code.
3608 * \note since code removal notifications are not currently issued, the
3609 * \p event_handler may get notifications of code that overlaps earlier
3610 * code notifications. This happens when code areas are reused, and the
3611 * earlier overlapping code areas should therefore be discarded.
3612 * \note the events passed to \p event_handler and the strings they point to
3613 * are not guaranteed to live past each call. The \p event_handler must
3614 * copy strings and other parameters it needs to keep around.
3615 * \note the set of events declared in JitCodeEvent::EventType is expected to
3616 * grow over time, and the JitCodeEvent structure is expected to accrue
3617 * new members. The \p event_handler function must ignore event codes
3618 * it does not recognize to maintain future compatibility.
3619 */
3620 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3621 JitCodeEventHandler event_handler);
3622
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003623 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
3624 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003625 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3626 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003627
iposva@chromium.org245aa852009-02-10 00:49:54 +00003628 /**
3629 * Suspends recording of tick samples in the profiler.
3630 * When the V8 profiling mode is enabled (usually via command line
3631 * switches) this function suspends recording of tick samples.
3632 * Profiling ticks are discarded until ResumeProfiler() is called.
3633 *
3634 * See also the --prof and --prof_auto command line switches to
3635 * enable V8 profiling.
3636 */
3637 static void PauseProfiler();
3638
3639 /**
3640 * Resumes recording of tick samples in the profiler.
3641 * See also PauseProfiler().
3642 */
3643 static void ResumeProfiler();
3644
ager@chromium.org41826e72009-03-30 13:30:57 +00003645 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003646 * Return whether profiler is currently paused.
3647 */
3648 static bool IsProfilerPaused();
3649
3650 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003651 * Retrieve the V8 thread id of the calling thread.
3652 *
3653 * The thread id for a thread should only be retrieved after the V8
3654 * lock has been acquired with a Locker object with that thread.
3655 */
3656 static int GetCurrentThreadId();
3657
3658 /**
3659 * Forcefully terminate execution of a JavaScript thread. This can
3660 * be used to terminate long-running scripts.
3661 *
3662 * TerminateExecution should only be called when then V8 lock has
3663 * been acquired with a Locker object. Therefore, in order to be
3664 * able to terminate long-running threads, preemption must be
3665 * enabled to allow the user of TerminateExecution to acquire the
3666 * lock.
3667 *
3668 * The termination is achieved by throwing an exception that is
3669 * uncatchable by JavaScript exception handlers. Termination
3670 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003671 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003672 * exception handler that catches an exception should check if that
3673 * exception is a termination exception and immediately return if
3674 * that is the case. Returning immediately in that case will
3675 * continue the propagation of the termination exception if needed.
3676 *
3677 * The thread id passed to TerminateExecution must have been
3678 * obtained by calling GetCurrentThreadId on the thread in question.
3679 *
3680 * \param thread_id The thread id of the thread to terminate.
3681 */
3682 static void TerminateExecution(int thread_id);
3683
3684 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003685 * Forcefully terminate the current thread of JavaScript execution
3686 * in the given isolate. If no isolate is provided, the default
3687 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003688 *
3689 * This method can be used by any thread even if that thread has not
3690 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003691 *
3692 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003693 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003694 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003695
3696 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003697 * Is V8 terminating JavaScript execution.
3698 *
3699 * Returns true if JavaScript execution is currently terminating
3700 * because of a call to TerminateExecution. In that case there are
3701 * still JavaScript frames on the stack and the termination
3702 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003703 *
3704 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003705 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003706 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003707
3708 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003709 * Resume execution capability in the given isolate, whose execution
3710 * was previously forcefully terminated using TerminateExecution().
3711 *
3712 * When execution is forcefully terminated using TerminateExecution(),
3713 * the isolate can not resume execution until all JavaScript frames
3714 * have propagated the uncatchable exception which is generated. This
3715 * method allows the program embedding the engine to handle the
3716 * termination event and resume execution capability, even if
3717 * JavaScript frames remain on the stack.
3718 *
3719 * This method can be used by any thread even if that thread has not
3720 * acquired the V8 lock with a Locker object.
3721 *
3722 * \param isolate The isolate in which to resume execution capability.
3723 */
3724 static void CancelTerminateExecution(Isolate* isolate);
3725
3726 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003727 * Releases any resources used by v8 and stops any utility threads
3728 * that may be running. Note that disposing v8 is permanent, it
3729 * cannot be reinitialized.
3730 *
3731 * It should generally not be necessary to dispose v8 before exiting
3732 * a process, this should happen automatically. It is only necessary
3733 * to use if the process needs the resources taken up by v8.
3734 */
3735 static bool Dispose();
3736
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003737 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
3738 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003739
3740 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003741 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003742 * heap. GC is not invoked prior to iterating, therefore there is no
3743 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003744 */
3745 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3746
3747 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003748 * Iterates through all the persistent handles in the current isolate's heap
3749 * that have class_ids.
3750 */
3751 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3752
3753 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00003754 * Iterates through all the persistent handles in the current isolate's heap
3755 * that have class_ids and are candidates to be marked as partially dependent
3756 * handles. This will visit handles to young objects created since the last
3757 * garbage collection but is free to visit an arbitrary superset of these
3758 * objects.
3759 */
3760 static void VisitHandlesForPartialDependence(
3761 Isolate* isolate, PersistentHandleVisitor* visitor);
3762
3763 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003764 * Optional notification that the embedder is idle.
3765 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003766 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003767 * Returns true if the embedder should stop calling IdleNotification
3768 * until real work has been done. This indicates that V8 has done
3769 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003770 *
3771 * The hint argument specifies the amount of work to be done in the function
3772 * on scale from 1 to 1000. There is no guarantee that the actual work will
3773 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003774 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003775 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003776
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003777 /**
3778 * Optional notification that the system is running low on memory.
3779 * V8 uses these notifications to attempt to free memory.
3780 */
3781 static void LowMemoryNotification();
3782
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003783 /**
3784 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003785 * these notifications to guide the GC heuristic. Returns the number
3786 * of context disposals - including this one - since the last time
3787 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003788 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003789 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003790
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003791 private:
3792 V8();
3793
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003794 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
3795 internal::Object** handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003796 static void DisposeGlobal(internal::Isolate* isolate,
3797 internal::Object** global_handle);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003798 static void MakeWeak(internal::Isolate* isolate,
3799 internal::Object** global_handle,
3800 void* data,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00003801 WeakReferenceCallback weak_reference_callback,
3802 NearDeathCallback near_death_callback);
3803 static void ClearWeak(internal::Isolate* isolate,
3804 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003805
3806 template <class T> friend class Handle;
3807 template <class T> friend class Local;
3808 template <class T> friend class Persistent;
3809 friend class Context;
3810};
3811
3812
3813/**
3814 * An external exception handler.
3815 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003816class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003817 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003818 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003819 * Creates a new try/catch block and registers it with v8. Note that
3820 * all TryCatch blocks should be stack allocated because the memory
3821 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003822 */
3823 TryCatch();
3824
3825 /**
3826 * Unregisters and deletes this try/catch block.
3827 */
3828 ~TryCatch();
3829
3830 /**
3831 * Returns true if an exception has been caught by this try/catch block.
3832 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003833 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834
3835 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003836 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003837 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003838 * If CanContinue returns false, the correct action is to perform any C++
3839 * cleanup needed and then return. If CanContinue returns false and
3840 * HasTerminated returns true, it is possible to call
3841 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003842 */
3843 bool CanContinue() const;
3844
3845 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003846 * Returns true if an exception has been caught due to script execution
3847 * being terminated.
3848 *
3849 * There is no JavaScript representation of an execution termination
3850 * exception. Such exceptions are thrown when the TerminateExecution
3851 * methods are called to terminate a long-running script.
3852 *
3853 * If such an exception has been thrown, HasTerminated will return true,
3854 * indicating that it is possible to call CancelTerminateExecution in order
3855 * to continue calling into the engine.
3856 */
3857 bool HasTerminated() const;
3858
3859 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003860 * Throws the exception caught by this TryCatch in a way that avoids
3861 * it being caught again by this same TryCatch. As with ThrowException
3862 * it is illegal to execute any JavaScript operations after calling
3863 * ReThrow; the caller must return immediately to where the exception
3864 * is caught.
3865 */
3866 Handle<Value> ReThrow();
3867
3868 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869 * Returns the exception caught by this try/catch block. If no exception has
3870 * been caught an empty handle is returned.
3871 *
3872 * The returned handle is valid until this TryCatch block has been destroyed.
3873 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003874 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875
3876 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003877 * Returns the .stack property of the thrown object. If no .stack
3878 * property is present an empty handle is returned.
3879 */
3880 Local<Value> StackTrace() const;
3881
3882 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003883 * Returns the message associated with this exception. If there is
3884 * no message associated an empty handle is returned.
3885 *
3886 * The returned handle is valid until this TryCatch block has been
3887 * destroyed.
3888 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003889 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003890
3891 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003892 * Clears any exceptions that may have been caught by this try/catch block.
3893 * After this method has been called, HasCaught() will return false.
3894 *
3895 * It is not necessary to clear a try/catch block before using it again; if
3896 * another exception is thrown the previously caught exception will just be
3897 * overwritten. However, it is often a good idea since it makes it easier
3898 * to determine which operation threw a given exception.
3899 */
3900 void Reset();
3901
v8.team.kasperl727e9952008-09-02 14:56:44 +00003902 /**
3903 * Set verbosity of the external exception handler.
3904 *
3905 * By default, exceptions that are caught by an external exception
3906 * handler are not reported. Call SetVerbose with true on an
3907 * external exception handler to have exceptions caught by the
3908 * handler reported as if they were not caught.
3909 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003910 void SetVerbose(bool value);
3911
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003912 /**
3913 * Set whether or not this TryCatch should capture a Message object
3914 * which holds source information about where the exception
3915 * occurred. True by default.
3916 */
3917 void SetCaptureMessage(bool value);
3918
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003919 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003920 // Make it hard to create heap-allocated TryCatch blocks.
3921 TryCatch(const TryCatch&);
3922 void operator=(const TryCatch&);
3923 void* operator new(size_t size);
3924 void operator delete(void*, size_t);
3925
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003926 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003927 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003929 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003930 bool is_verbose_ : 1;
3931 bool can_continue_ : 1;
3932 bool capture_message_ : 1;
3933 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003934 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003935
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003936 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003937};
3938
3939
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003940// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003941
3942
3943/**
3944 * Ignore
3945 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003946class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003947 public:
3948 ExtensionConfiguration(int name_count, const char* names[])
3949 : name_count_(name_count), names_(names) { }
3950 private:
3951 friend class ImplementationUtilities;
3952 int name_count_;
3953 const char** names_;
3954};
3955
3956
3957/**
3958 * A sandboxed execution context with its own set of built-in objects
3959 * and functions.
3960 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003961class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003962 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003963 /**
3964 * Returns the global proxy object or global object itself for
3965 * detached contexts.
3966 *
3967 * Global proxy object is a thin wrapper whose prototype points to
3968 * actual context's global object with the properties like Object, etc.
3969 * This is done that way for security reasons (for more details see
3970 * https://wiki.mozilla.org/Gecko:SplitWindow).
3971 *
3972 * Please note that changes to global proxy object prototype most probably
3973 * would break VM---v8 expects only global object as a prototype of
3974 * global proxy object.
3975 *
3976 * If DetachGlobal() has been invoked, Global() would return actual global
3977 * object until global is reattached with ReattachGlobal().
3978 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003979 Local<Object> Global();
3980
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003981 /**
3982 * Detaches the global object from its context before
3983 * the global object can be reused to create a new context.
3984 */
3985 void DetachGlobal();
3986
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003987 /**
3988 * Reattaches a global object to a context. This can be used to
3989 * restore the connection between a global object and a context
3990 * after DetachGlobal has been called.
3991 *
3992 * \param global_object The global object to reattach to the
3993 * context. For this to work, the global object must be the global
3994 * object that was associated with this context before a call to
3995 * DetachGlobal.
3996 */
3997 void ReattachGlobal(Handle<Object> global_object);
3998
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00003999 /**
4000 * Creates a new context and returns a handle to the newly allocated
4001 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004002 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004003 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004004 *
4005 * \param extensions An optional extension configuration containing
4006 * the extensions to be installed in the newly created context.
4007 *
4008 * \param global_template An optional object template from which the
4009 * global object for the newly created context will be created.
4010 *
4011 * \param global_object An optional global object to be reused for
4012 * the newly created context. This global object must have been
4013 * created by a previous call to Context::New with the same global
4014 * template. The state of the global object will be completely reset
4015 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004016 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004017 static Local<Context> New(
4018 Isolate* isolate,
4019 ExtensionConfiguration* extensions = NULL,
4020 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4021 Handle<Value> global_object = Handle<Value>());
4022
4023 /** Deprecated. Use Isolate version instead. */
4024 // TODO(mstarzinger): Put this behind the V8_DEPRECATED guard.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004025 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004026 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004027 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4028 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004029
kasper.lund44510672008-07-25 07:37:58 +00004030 /** Returns the last entered context. */
4031 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004033 // TODO(svenpanne) Actually deprecate this.
4034 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004035 static Local<Context> GetCurrent();
4036
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004037 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004038 * Returns the context of the calling JavaScript code. That is the
4039 * context of the top-most JavaScript frame. If there are no
4040 * JavaScript frames an empty handle is returned.
4041 */
4042 static Local<Context> GetCalling();
4043
4044 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004045 * Sets the security token for the context. To access an object in
4046 * another context, the security tokens must match.
4047 */
4048 void SetSecurityToken(Handle<Value> token);
4049
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004050 /** Restores the security token to the default value. */
4051 void UseDefaultSecurityToken();
4052
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004053 /** Returns the security token of this context.*/
4054 Handle<Value> GetSecurityToken();
4055
v8.team.kasperl727e9952008-09-02 14:56:44 +00004056 /**
4057 * Enter this context. After entering a context, all code compiled
4058 * and run is compiled and run in this context. If another context
4059 * is already entered, this old context is saved so it can be
4060 * restored when the new context is exited.
4061 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004062 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004063
4064 /**
4065 * Exit this context. Exiting the current context restores the
4066 * context that was in place when entering the current context.
4067 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004068 void Exit();
4069
v8.team.kasperl727e9952008-09-02 14:56:44 +00004070 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071 bool HasOutOfMemoryException();
4072
v8.team.kasperl727e9952008-09-02 14:56:44 +00004073 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004074 static bool InContext();
4075
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004076 /** Returns an isolate associated with a current context. */
4077 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004078
4079 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004080 * Gets the embedder data with the given index, which must have been set by a
4081 * previous call to SetEmbedderData with the same index. Note that index 0
4082 * currently has a special meaning for Chrome's debugger.
4083 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004084 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004085
4086 /**
4087 * Sets the embedder data with the given index, growing the data as
4088 * needed. Note that index 0 currently has a special meaning for Chrome's
4089 * debugger.
4090 */
4091 void SetEmbedderData(int index, Handle<Value> value);
4092
4093 /**
4094 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4095 * index, which must have bees set by a previous call to
4096 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4097 * currently has a special meaning for Chrome's debugger.
4098 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004099 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004100
4101 /**
4102 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4103 * index, growing the data as needed. Note that index 0 currently has a
4104 * special meaning for Chrome's debugger.
4105 */
4106 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004107
4108 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004109 * Control whether code generation from strings is allowed. Calling
4110 * this method with false will disable 'eval' and the 'Function'
4111 * constructor for code running in this context. If 'eval' or the
4112 * 'Function' constructor are used an exception will be thrown.
4113 *
4114 * If code generation from strings is not allowed the
4115 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4116 * set before blocking the call to 'eval' or the 'Function'
4117 * constructor. If that callback returns true, the call will be
4118 * allowed, otherwise an exception will be thrown. If no callback is
4119 * set an exception will be thrown.
4120 */
4121 void AllowCodeGenerationFromStrings(bool allow);
4122
4123 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004124 * Returns true if code generation from strings is allowed for the context.
4125 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4126 */
4127 bool IsCodeGenerationFromStringsAllowed();
4128
4129 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004130 * Sets the error description for the exception that is thrown when
4131 * code generation from strings is not allowed and 'eval' or the 'Function'
4132 * constructor are called.
4133 */
4134 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4135
4136 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004137 * Stack-allocated class which sets the execution context for all
4138 * operations executed within a local scope.
4139 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004140 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004141 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004142 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004143 context_->Enter();
4144 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004145 V8_INLINE(~Scope()) { context_->Exit(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146 private:
4147 Handle<Context> context_;
4148 };
4149
4150 private:
4151 friend class Value;
4152 friend class Script;
4153 friend class Object;
4154 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004155
4156 Local<Value> SlowGetEmbedderData(int index);
4157 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004158};
4159
4160
4161/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004162 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4163 * to use any given V8 isolate, see the comments in the Isolate class. The
4164 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4165 * object pointers obtained from V8 handles while in the particular V8 isolate.
4166 * It is up to the user of V8 to ensure, perhaps with locking, that this
4167 * constraint is not violated. In addition to any other synchronization
4168 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4169 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004170 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004171 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4172 * construction and destruction, the current thread is allowed to use the locked
4173 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4174 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004175 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004176 * Sample usage:
4177* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004178 * ...
4179 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004180 * v8::Locker locker(isolate);
4181 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004182 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004183 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004184 * ...
4185 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00004186 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004187 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004188 * If you wish to stop using V8 in a thread A you can do this either by
4189 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
4190 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004191 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004192 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004193 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004194 * isolate->Exit();
4195 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004196 * ...
4197 * // Code not using V8 goes here while V8 can run in another thread.
4198 * ...
4199 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004200 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004201 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004202 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004203 * The Unlocker object is intended for use in a long-running callback from V8,
4204 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004205 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004206 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
4207 * given thread. This can be useful if you have code that can be called either
4208 * from code that holds the lock or from code that does not. The Unlocker is
4209 * not recursive so you can not have several Unlockers on the stack at once, and
4210 * 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 +00004211 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004212 * An unlocker will unlock several lockers if it has to and reinstate the
4213 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004214 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004215 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004216 * // V8 not locked.
4217 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004218 * v8::Locker locker(isolate);
4219 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004220 * // V8 locked.
4221 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004222 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004223 * // V8 still locked (2 levels).
4224 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004225 * isolate->Exit();
4226 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004227 * // V8 not locked.
4228 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004229 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004230 * // V8 locked again (2 levels).
4231 * }
4232 * // V8 still locked (1 level).
4233 * }
4234 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004235 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004236 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004237class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004238 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004239 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004240 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004241 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004242 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
4243
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004244 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004245 V8_DEPRECATED(Unlocker());
4246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004247 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00004248 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004249 void Initialize(Isolate* isolate);
4250
lrn@chromium.org1c092762011-05-09 09:42:16 +00004251 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004252};
4253
4254
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004255class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004256 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004257 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004258 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004259 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004260 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
4261
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004262 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004263 V8_DEPRECATED(Locker());
4264
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004265 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004266
4267 /**
4268 * Start preemption.
4269 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004270 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004271 * that will switch between multiple threads that are in contention
4272 * for the V8 lock.
4273 */
4274 static void StartPreemption(int every_n_ms);
4275
4276 /**
4277 * Stop preemption.
4278 */
4279 static void StopPreemption();
4280
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004281 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004282 * Returns whether or not the locker for a given isolate, is locked by the
4283 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004284 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004285 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004286
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004287 /**
4288 * Returns whether v8::Locker is being used by this V8 instance.
4289 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004290 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004291
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004292 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004293 void Initialize(Isolate* isolate);
4294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004295 bool has_lock_;
4296 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004297 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004298
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004299 static bool active_;
4300
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004301 // Disallow copying and assigning.
4302 Locker(const Locker&);
4303 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004304};
4305
4306
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004307/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004308 * A struct for exporting HeapStats data from V8, using "push" model.
4309 */
4310struct HeapStatsUpdate;
4311
4312
4313/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004314 * An interface for exporting data from V8, using "push" model.
4315 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004316class V8EXPORT OutputStream { // NOLINT
4317 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004318 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004319 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004320 };
4321 enum WriteResult {
4322 kContinue = 0,
4323 kAbort = 1
4324 };
4325 virtual ~OutputStream() {}
4326 /** Notify about the end of stream. */
4327 virtual void EndOfStream() = 0;
4328 /** Get preferred output chunk size. Called only once. */
4329 virtual int GetChunkSize() { return 1024; }
4330 /** Get preferred output encoding. Called only once. */
4331 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4332 /**
4333 * Writes the next chunk of snapshot data into the stream. Writing
4334 * can be stopped by returning kAbort as function result. EndOfStream
4335 * will not be called in case writing was aborted.
4336 */
4337 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004338 /**
4339 * Writes the next chunk of heap stats data into the stream. Writing
4340 * can be stopped by returning kAbort as function result. EndOfStream
4341 * will not be called in case writing was aborted.
4342 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004343 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004344 return kAbort;
4345 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004346};
4347
4348
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004349/**
4350 * An interface for reporting progress and controlling long-running
4351 * activities.
4352 */
4353class V8EXPORT ActivityControl { // NOLINT
4354 public:
4355 enum ControlOption {
4356 kContinue = 0,
4357 kAbort = 1
4358 };
4359 virtual ~ActivityControl() {}
4360 /**
4361 * Notify about current progress. The activity can be stopped by
4362 * returning kAbort as the callback result.
4363 */
4364 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4365};
4366
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004367
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004368// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004369
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004370
4371namespace internal {
4372
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004373const int kApiPointerSize = sizeof(void*); // NOLINT
4374const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004375
4376// Tag information for HeapObject.
4377const int kHeapObjectTag = 1;
4378const int kHeapObjectTagSize = 2;
4379const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4380
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004381// Tag information for Smi.
4382const int kSmiTag = 0;
4383const int kSmiTagSize = 1;
4384const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4385
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004386template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004387
4388// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004389template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004390 static const int kSmiShiftSize = 0;
4391 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004392 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004393 int shift_bits = kSmiTagSize + kSmiShiftSize;
4394 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4395 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4396 }
4397};
4398
4399// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004400template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004401 static const int kSmiShiftSize = 31;
4402 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004403 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004404 int shift_bits = kSmiTagSize + kSmiShiftSize;
4405 // Shift down and throw away top 32 bits.
4406 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4407 }
4408};
4409
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004410typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4411const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4412const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004413
4414/**
4415 * This class exports constants and functionality from within v8 that
4416 * is necessary to implement inline functions in the v8 api. Don't
4417 * depend on functions and constants defined here.
4418 */
4419class Internals {
4420 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004421 // These values match non-compiler-dependent values defined within
4422 // the implementation of v8.
4423 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004424 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004425 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004426
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004427 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004428 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004429 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004430 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4431 static const int kContextHeaderSize = 2 * kApiPointerSize;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00004432 static const int kContextEmbedderDataIndex = 55;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004433 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004434 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004435 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004436 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004437
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004438 static const int kIsolateStateOffset = 0;
4439 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4440 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4441 static const int kUndefinedValueRootIndex = 5;
4442 static const int kNullValueRootIndex = 7;
4443 static const int kTrueValueRootIndex = 8;
4444 static const int kFalseValueRootIndex = 9;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004445 static const int kEmptyStringRootIndex = 118;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004446
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004447 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
4448 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
4449 static const int kNodeStateMask = 0xf;
4450 static const int kNodeStateIsWeakValue = 2;
4451 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004452 static const int kNodeIsIndependentShift = 4;
4453 static const int kNodeIsPartiallyDependentShift = 5;
4454
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004455 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004456 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004457 static const int kOddballType = 0x83;
4458 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004459
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004460 static const int kUndefinedOddballKind = 5;
4461 static const int kNullOddballKind = 3;
4462
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004463 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004464 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4465 kHeapObjectTag);
4466 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004467
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004468 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004469 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004470 }
4471
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004472 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004473 typedef internal::Object O;
4474 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4475 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4476 }
4477
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004478 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004479 typedef internal::Object O;
4480 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4481 }
4482
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004483 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004484 int representation = (instance_type & kFullStringRepresentationMask);
4485 return representation == kExternalTwoByteRepresentationTag;
4486 }
4487
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004488 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004489 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4490 return *reinterpret_cast<int*>(addr) == 1;
4491 }
4492
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004493 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
4494 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4495 return *addr & (1 << shift);
4496 }
4497
4498 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
4499 bool value, int shift)) {
4500 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4501 uint8_t mask = 1 << shift;
4502 *addr = (*addr & ~mask) | (value << shift);
4503 }
4504
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004505 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
4506 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4507 return *addr & kNodeStateMask;
4508 }
4509
4510 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
4511 uint8_t value)) {
4512 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4513 *addr = (*addr & ~kNodeStateMask) | value;
4514 }
4515
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004516 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004517 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4518 kIsolateEmbedderDataOffset;
4519 *reinterpret_cast<void**>(addr) = data;
4520 }
4521
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004522 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004523 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4524 kIsolateEmbedderDataOffset;
4525 return *reinterpret_cast<void**>(addr);
4526 }
4527
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004528 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
4529 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004530 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4531 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4532 }
4533
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004534 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004535 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004536 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4537 return *reinterpret_cast<T*>(addr);
4538 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004539
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004540 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004541 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004542 typedef internal::Object O;
4543 typedef internal::Internals I;
4544 O* ctx = *reinterpret_cast<O**>(context);
4545 int embedder_data_offset = I::kContextHeaderSize +
4546 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4547 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4548 int value_offset =
4549 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4550 return I::ReadField<T>(embedder_data, value_offset);
4551 }
4552
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004553 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
4554 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
4555 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
4556 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
4557 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
4558 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
4559 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004560};
4561
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004562} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004563
4564
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004565template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004566Local<T>::Local() : Handle<T>() { }
4567
4568
4569template <class T>
4570Local<T> Local<T>::New(Handle<T> that) {
4571 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004572 T* that_ptr = *that;
4573 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4574 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4575 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4576 reinterpret_cast<internal::HeapObject*>(*p))));
4577 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004578 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4579}
4580
4581
4582template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004583Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004584 if (that.IsEmpty()) return Local<T>();
4585 T* that_ptr = *that;
4586 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4587 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4588 reinterpret_cast<internal::Isolate*>(isolate), *p)));
4589}
4590
4591
4592template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004593Persistent<T> Persistent<T>::New(Handle<T> that) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004594 return New(Isolate::GetCurrent(), that);
4595}
4596
4597
4598template <class T>
4599Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004600 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004601 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004602 return Persistent<T>(reinterpret_cast<T*>(
4603 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
4604 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004605}
4606
4607
4608template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004609bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004610 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004611}
4612
4613
4614template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004615bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004616 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004617 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004618 if (!I::IsInitialized(isolate)) return false;
4619 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(**this),
4620 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004621}
4622
4623
4624template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004625bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004626 return IsNearDeath(Isolate::GetCurrent());
4627}
4628
4629
4630template <class T>
4631bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
4632 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004633 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004634 if (!I::IsInitialized(isolate)) return false;
4635 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4636 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004637}
4638
4639
4640template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004641bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004642 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004643}
4644
4645
4646template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004647bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004648 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004649 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004650 if (!I::IsInitialized(isolate)) return false;
4651 return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4652 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004653}
4654
4655
4656template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004657void Persistent<T>::Dispose() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004658 Dispose(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004659}
4660
4661
4662template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004663void Persistent<T>::Dispose(Isolate* isolate) {
4664 if (this->IsEmpty()) return;
4665 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4666 reinterpret_cast<internal::Object**>(**this));
4667}
4668
4669
4670template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004671Persistent<T>::Persistent() : Handle<T>() { }
4672
4673template <class T>
4674void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004675 Isolate* isolate = Isolate::GetCurrent();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004676 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4677 reinterpret_cast<internal::Object**>(**this),
4678 parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004679 callback,
4680 NULL);
4681}
4682
4683template <class T>
4684void Persistent<T>::MakeWeak(Isolate* isolate,
4685 void* parameters,
4686 NearDeathCallback callback) {
4687 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4688 reinterpret_cast<internal::Object**>(**this),
4689 parameters,
4690 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004691 callback);
4692}
4693
4694template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004695void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004696 ClearWeak(Isolate::GetCurrent());
4697}
4698
4699template <class T>
4700void Persistent<T>::ClearWeak(Isolate* isolate) {
4701 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
4702 reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004703}
4704
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004705template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004706void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004707 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004708}
4709
4710template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004711void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004712 typedef internal::Internals I;
4713 if (this->IsEmpty()) return;
4714 if (!I::IsInitialized(isolate)) return;
4715 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004716 true,
4717 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004718}
4719
4720template <class T>
4721void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004722 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004723}
4724
4725template <class T>
4726void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004727 typedef internal::Internals I;
4728 if (this->IsEmpty()) return;
4729 if (!I::IsInitialized(isolate)) return;
4730 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004731 true,
4732 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004733}
4734
4735template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004736void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004737 SetWrapperClassId(Isolate::GetCurrent(), class_id);
4738}
4739
4740template <class T>
4741void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004742 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004743 if (this->IsEmpty()) return;
4744 if (!I::IsInitialized(isolate)) return;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004745 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4746 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4747 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004748}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004749
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004750template <class T>
4751uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004752 return WrapperClassId(Isolate::GetCurrent());
4753}
4754
4755template <class T>
4756uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004757 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004758 if (this->IsEmpty()) return 0;
4759 if (!I::IsInitialized(isolate)) return 0;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004760 internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4761 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4762 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004763}
4764
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004765Arguments::Arguments(internal::Object** implicit_args,
4766 internal::Object** values, int length,
4767 bool is_construct_call)
4768 : implicit_args_(implicit_args),
4769 values_(values),
4770 length_(length),
4771 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004772
4773
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004774Local<Value> Arguments::operator[](int i) const {
4775 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4776 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4777}
4778
4779
4780Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004781 return Local<Function>(reinterpret_cast<Function*>(
4782 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004783}
4784
4785
4786Local<Object> Arguments::This() const {
4787 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4788}
4789
4790
4791Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004792 return Local<Object>(reinterpret_cast<Object*>(
4793 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004794}
4795
4796
4797Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004798 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004799}
4800
4801
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004802Isolate* Arguments::GetIsolate() const {
4803 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4804}
4805
4806
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004807bool Arguments::IsConstructCall() const {
4808 return is_construct_call_;
4809}
4810
4811
4812int Arguments::Length() const {
4813 return length_;
4814}
4815
4816
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004817template <class T>
4818Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004819 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4820 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004821 return Local<T>(reinterpret_cast<T*>(after));
4822}
4823
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004824Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004825 return resource_name_;
4826}
4827
4828
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004829Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004830 return resource_line_offset_;
4831}
4832
4833
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004834Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004835 return resource_column_offset_;
4836}
4837
4838
4839Handle<Boolean> Boolean::New(bool value) {
4840 return value ? True() : False();
4841}
4842
4843
4844void Template::Set(const char* name, v8::Handle<Data> value) {
4845 Set(v8::String::New(name), value);
4846}
4847
4848
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004849Local<Value> Object::GetInternalField(int index) {
4850#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004851 typedef internal::Object O;
4852 typedef internal::Internals I;
4853 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004854 // Fast path: If the object is a plain JSObject, which is the common case, we
4855 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004856 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004857 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004858 O* value = I::ReadField<O*>(obj, offset);
4859 O** result = HandleScope::CreateHandle(value);
4860 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004861 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004862#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004863 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004864}
4865
4866
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004867void* Object::GetAlignedPointerFromInternalField(int index) {
4868#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004869 typedef internal::Object O;
4870 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004871 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004872 // Fast path: If the object is a plain JSObject, which is the common case, we
4873 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004874 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004875 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004876 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004877 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004878#endif
4879 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004880}
4881
4882
4883String* String::Cast(v8::Value* value) {
4884#ifdef V8_ENABLE_CHECKS
4885 CheckCast(value);
4886#endif
4887 return static_cast<String*>(value);
4888}
4889
4890
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004891Local<String> String::Empty(Isolate* isolate) {
4892 typedef internal::Object* S;
4893 typedef internal::Internals I;
4894 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004895 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004896 return Local<String>(reinterpret_cast<String*>(slot));
4897}
4898
4899
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004900Local<String> String::New(const char* data, int length) {
4901 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
4902}
4903
4904
4905Local<String> String::New(const uint16_t* data, int length) {
4906 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
4907}
4908
4909
4910Local<String> String::NewSymbol(const char* data, int length) {
4911 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
4912}
4913
4914
4915Local<String> String::NewUndetectable(const char* data, int length) {
4916 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
4917}
4918
4919
4920Local<String> String::NewUndetectable(const uint16_t* data, int length) {
4921 return NewFromTwoByte(
4922 Isolate::GetCurrent(), data, kUndetectableString, length);
4923}
4924
4925
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004926String::ExternalStringResource* String::GetExternalStringResource() const {
4927 typedef internal::Object O;
4928 typedef internal::Internals I;
4929 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004930 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004931 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004932 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4933 result = reinterpret_cast<String::ExternalStringResource*>(value);
4934 } else {
4935 result = NULL;
4936 }
4937#ifdef V8_ENABLE_CHECKS
4938 VerifyExternalStringResource(result);
4939#endif
4940 return result;
4941}
4942
4943
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004944String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4945 String::Encoding* encoding_out) const {
4946 typedef internal::Object O;
4947 typedef internal::Internals I;
4948 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4949 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4950 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4951 ExternalStringResourceBase* resource = NULL;
4952 if (type == I::kExternalAsciiRepresentationTag ||
4953 type == I::kExternalTwoByteRepresentationTag) {
4954 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4955 resource = static_cast<ExternalStringResourceBase*>(value);
4956 }
4957#ifdef V8_ENABLE_CHECKS
4958 VerifyExternalStringResourceBase(resource, *encoding_out);
4959#endif
4960 return resource;
4961}
4962
4963
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004964bool Value::IsUndefined() const {
4965#ifdef V8_ENABLE_CHECKS
4966 return FullIsUndefined();
4967#else
4968 return QuickIsUndefined();
4969#endif
4970}
4971
4972bool Value::QuickIsUndefined() const {
4973 typedef internal::Object O;
4974 typedef internal::Internals I;
4975 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4976 if (!I::HasHeapObjectTag(obj)) return false;
4977 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4978 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4979}
4980
4981
4982bool Value::IsNull() const {
4983#ifdef V8_ENABLE_CHECKS
4984 return FullIsNull();
4985#else
4986 return QuickIsNull();
4987#endif
4988}
4989
4990bool Value::QuickIsNull() const {
4991 typedef internal::Object O;
4992 typedef internal::Internals I;
4993 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4994 if (!I::HasHeapObjectTag(obj)) return false;
4995 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4996 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4997}
4998
4999
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005000bool Value::IsString() const {
5001#ifdef V8_ENABLE_CHECKS
5002 return FullIsString();
5003#else
5004 return QuickIsString();
5005#endif
5006}
5007
5008bool Value::QuickIsString() const {
5009 typedef internal::Object O;
5010 typedef internal::Internals I;
5011 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5012 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005013 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005014}
5015
5016
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005017Symbol* Symbol::Cast(v8::Value* value) {
5018#ifdef V8_ENABLE_CHECKS
5019 CheckCast(value);
5020#endif
5021 return static_cast<Symbol*>(value);
5022}
5023
5024
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005025Number* Number::Cast(v8::Value* value) {
5026#ifdef V8_ENABLE_CHECKS
5027 CheckCast(value);
5028#endif
5029 return static_cast<Number*>(value);
5030}
5031
5032
5033Integer* Integer::Cast(v8::Value* value) {
5034#ifdef V8_ENABLE_CHECKS
5035 CheckCast(value);
5036#endif
5037 return static_cast<Integer*>(value);
5038}
5039
5040
5041Date* Date::Cast(v8::Value* value) {
5042#ifdef V8_ENABLE_CHECKS
5043 CheckCast(value);
5044#endif
5045 return static_cast<Date*>(value);
5046}
5047
5048
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00005049StringObject* StringObject::Cast(v8::Value* value) {
5050#ifdef V8_ENABLE_CHECKS
5051 CheckCast(value);
5052#endif
5053 return static_cast<StringObject*>(value);
5054}
5055
5056
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005057SymbolObject* SymbolObject::Cast(v8::Value* value) {
5058#ifdef V8_ENABLE_CHECKS
5059 CheckCast(value);
5060#endif
5061 return static_cast<SymbolObject*>(value);
5062}
5063
5064
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00005065NumberObject* NumberObject::Cast(v8::Value* value) {
5066#ifdef V8_ENABLE_CHECKS
5067 CheckCast(value);
5068#endif
5069 return static_cast<NumberObject*>(value);
5070}
5071
5072
5073BooleanObject* BooleanObject::Cast(v8::Value* value) {
5074#ifdef V8_ENABLE_CHECKS
5075 CheckCast(value);
5076#endif
5077 return static_cast<BooleanObject*>(value);
5078}
5079
5080
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00005081RegExp* RegExp::Cast(v8::Value* value) {
5082#ifdef V8_ENABLE_CHECKS
5083 CheckCast(value);
5084#endif
5085 return static_cast<RegExp*>(value);
5086}
5087
5088
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005089Object* Object::Cast(v8::Value* value) {
5090#ifdef V8_ENABLE_CHECKS
5091 CheckCast(value);
5092#endif
5093 return static_cast<Object*>(value);
5094}
5095
5096
5097Array* Array::Cast(v8::Value* value) {
5098#ifdef V8_ENABLE_CHECKS
5099 CheckCast(value);
5100#endif
5101 return static_cast<Array*>(value);
5102}
5103
5104
5105Function* Function::Cast(v8::Value* value) {
5106#ifdef V8_ENABLE_CHECKS
5107 CheckCast(value);
5108#endif
5109 return static_cast<Function*>(value);
5110}
5111
5112
5113External* External::Cast(v8::Value* value) {
5114#ifdef V8_ENABLE_CHECKS
5115 CheckCast(value);
5116#endif
5117 return static_cast<External*>(value);
5118}
5119
5120
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005121Isolate* AccessorInfo::GetIsolate() const {
5122 return *reinterpret_cast<Isolate**>(&args_[-3]);
5123}
5124
5125
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005126Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005127 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005128}
5129
5130
5131Local<Object> AccessorInfo::This() const {
5132 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
5133}
5134
5135
5136Local<Object> AccessorInfo::Holder() const {
5137 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
5138}
5139
5140
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005141Handle<Primitive> Undefined(Isolate* isolate) {
5142 typedef internal::Object* S;
5143 typedef internal::Internals I;
5144 if (!I::IsInitialized(isolate)) return Undefined();
5145 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
5146 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
5147}
5148
5149
5150Handle<Primitive> Null(Isolate* isolate) {
5151 typedef internal::Object* S;
5152 typedef internal::Internals I;
5153 if (!I::IsInitialized(isolate)) return Null();
5154 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
5155 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
5156}
5157
5158
5159Handle<Boolean> True(Isolate* isolate) {
5160 typedef internal::Object* S;
5161 typedef internal::Internals I;
5162 if (!I::IsInitialized(isolate)) return True();
5163 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
5164 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
5165}
5166
5167
5168Handle<Boolean> False(Isolate* isolate) {
5169 typedef internal::Object* S;
5170 typedef internal::Internals I;
5171 if (!I::IsInitialized(isolate)) return False();
5172 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
5173 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
5174}
5175
5176
5177void Isolate::SetData(void* data) {
5178 typedef internal::Internals I;
5179 I::SetEmbedderData(this, data);
5180}
5181
5182
5183void* Isolate::GetData() {
5184 typedef internal::Internals I;
5185 return I::GetEmbedderData(this);
5186}
5187
5188
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005189Local<Value> Context::GetEmbedderData(int index) {
5190#ifndef V8_ENABLE_CHECKS
5191 typedef internal::Object O;
5192 typedef internal::Internals I;
5193 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
5194 return Local<Value>(reinterpret_cast<Value*>(result));
5195#else
5196 return SlowGetEmbedderData(index);
5197#endif
5198}
5199
5200
5201void* Context::GetAlignedPointerFromEmbedderData(int index) {
5202#ifndef V8_ENABLE_CHECKS
5203 typedef internal::Internals I;
5204 return I::ReadEmbedderData<void*>(this, index);
5205#else
5206 return SlowGetAlignedPointerFromEmbedderData(index);
5207#endif
5208}
5209
5210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005211/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00005212 * \example shell.cc
5213 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005214 * command-line and executes them.
5215 */
5216
5217
5218/**
5219 * \example process.cc
5220 */
5221
5222
5223} // namespace v8
5224
5225
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005226#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005227#undef TYPE_CHECK
5228
5229
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005230#endif // V8_H_