blob: 3a86e86e020cbc9448c50f2ccd21476035a66c2e [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000065// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000066#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
67 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070#else
71#define V8EXPORT
72#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000075#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076
77#endif // _WIN32
78
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000079#if defined(__GNUC__) && !defined(DEBUG)
80#define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
81#elif defined(_MSC_VER) && !defined(DEBUG)
82#define V8_INLINE(declarator) __forceinline declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000083#else
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000084#define V8_INLINE(declarator) inline declarator
85#endif
86
87#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
88#define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
89#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
90#define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
91#else
92#define V8_DEPRECATED(declarator) declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000093#endif
94
danno@chromium.orgca29dd82013-04-26 11:59:48 +000095#if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))
96 #define V8_UNLIKELY(condition) __builtin_expect((condition), 0)
97 #define V8_LIKELY(condition) __builtin_expect((condition), 1)
98#else
99 #define V8_UNLIKELY(condition) (condition)
100 #define V8_LIKELY(condition) (condition)
101#endif
102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000104 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 */
106namespace v8 {
107
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000108class AccessorInfo;
109class AccessorSignature;
110class Array;
111class Boolean;
112class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113class Context;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000114class CpuProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000115class Data;
116class Date;
117class DeclaredAccessorDescriptor;
118class External;
119class Function;
120class FunctionTemplate;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000121class HeapProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000122class ImplementationUtilities;
123class Int32;
124class Integer;
125class Isolate;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000126class LocalContext;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000128class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129class Object;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000130class ObjectOperationDescriptor;
131class ObjectTemplate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132class Primitive;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000133class RawOperationDescriptor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134class Signature;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000135class StackFrame;
136class StackTrace;
137class String;
138class StringObject;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000139class Symbol;
140class SymbolObject;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000141class Uint32;
142class Utils;
143class Value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144template <class T> class Handle;
145template <class T> class Local;
146template <class T> class Persistent;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000148namespace internal {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000149class Arguments;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000150class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000151class HeapObject;
152class Isolate;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000153class Object;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000154}
155
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000157/**
158 * General purpose unique identifier.
159 */
160class UniqueId {
161 public:
162 explicit UniqueId(intptr_t data)
163 : data_(data) {}
164
165 bool operator==(const UniqueId& other) const {
166 return data_ == other.data_;
167 }
168
169 bool operator!=(const UniqueId& other) const {
170 return data_ != other.data_;
171 }
172
173 bool operator<(const UniqueId& other) const {
174 return data_ < other.data_;
175 }
176
177 private:
178 intptr_t data_;
179};
180
181
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000182// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183
184
185/**
186 * A weak reference callback function.
187 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000188 * This callback should either explicitly invoke Dispose on |object| if
189 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
190 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 * \param object the weak global object to be reclaimed by the garbage collector
192 * \param parameter the value passed in when making the weak global object
193 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000194template<typename T, typename P>
195class WeakReferenceCallbacks {
196 public:
197 typedef void (*Revivable)(Isolate* isolate,
198 Persistent<T>* object,
199 P* parameter);
200};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000202// TODO(svenpanne) Temporary definition until Chrome is in sync.
203typedef void (*NearDeathCallback)(Isolate* isolate,
204 Persistent<Value> object,
205 void* parameter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000207// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000209#define TYPE_CHECK(T, S) \
210 while (false) { \
211 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212 }
213
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000214
215#define V8_USE_UNSAFE_HANDLES
216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217/**
218 * An object reference managed by the v8 garbage collector.
219 *
220 * All objects returned from v8 have to be tracked by the garbage
221 * collector so that it knows that the objects are still alive. Also,
222 * because the garbage collector may move objects, it is unsafe to
223 * point directly to an object. Instead, all objects are stored in
224 * handles which are known by the garbage collector and updated
225 * whenever an object moves. Handles should always be passed by value
226 * (except in cases like out-parameters) and they should never be
227 * allocated on the heap.
228 *
229 * There are two types of handles: local and persistent handles.
230 * Local handles are light-weight and transient and typically used in
231 * local operations. They are managed by HandleScopes. Persistent
232 * handles can be used when storing objects across several independent
233 * operations and have to be explicitly deallocated when they're no
234 * longer used.
235 *
236 * It is safe to extract the object stored in the handle by
237 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000238 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 * behind the scenes and the same rules apply to these values as to
240 * their handles.
241 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000242template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 /**
245 * Creates an empty handle.
246 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000247 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000249#ifdef V8_USE_UNSAFE_HANDLES
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 /**
251 * Creates a new handle for the specified value.
252 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000253 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000254#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255
256 /**
257 * Creates a handle for the contents of the specified handle. This
258 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000259 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000261 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 * between compatible handles, for instance assigning a
263 * Handle<String> to a variable declared as Handle<Value>, is legal
264 * because String is a subclass of Value.
265 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000266 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 : val_(reinterpret_cast<T*>(*that)) {
268 /**
269 * This check fails when trying to convert between incompatible
270 * handles. For example, converting from a Handle<String> to a
271 * Handle<Number>.
272 */
273 TYPE_CHECK(T, S);
274 }
275
276 /**
277 * Returns true if the handle is empty.
278 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000279 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 /**
282 * Sets the handle to be empty. IsEmpty() will then return true.
283 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000284 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000285
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000286 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000287
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000288 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289
290 /**
291 * Checks whether two handles are the same.
292 * Returns true if both are empty, or if the objects
293 * to which they refer are identical.
294 * The handles' references are not checked.
295 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000296 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000297 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
298 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 if (a == 0) return b == 0;
300 if (b == 0) return false;
301 return *a == *b;
302 }
303
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000304#ifndef V8_USE_UNSAFE_HANDLES
305 template <class S> V8_INLINE(
306 bool operator==(const Persistent<S>& that) const) {
307 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
308 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
309 if (a == 0) return b == 0;
310 if (b == 0) return false;
311 return *a == *b;
312 }
313#endif
314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 /**
316 * Checks whether two handles are different.
317 * Returns true if only one of the handles is empty, or if
318 * the objects to which they refer are different.
319 * The handles' references are not checked.
320 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000321 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 return !operator==(that);
323 }
324
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000325 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000326#ifdef V8_ENABLE_CHECKS
327 // If we're going to perform the type check then we have to check
328 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000330#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 return Handle<T>(T::Cast(*that));
332 }
333
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000334 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000335 return Handle<S>::Cast(*this);
336 }
337
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000338#ifndef V8_USE_UNSAFE_HANDLES
339 V8_INLINE(static Handle<T> New(Isolate* isolate, Handle<T> that)) {
340 return New(isolate, that.val_);
341 }
342 // TODO(dcarney): remove before cutover
343 V8_INLINE(static Handle<T> New(Isolate* isolate, const Persistent<T>& that)) {
344 return New(isolate, that.val_);
345 }
346
347#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
348
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000350#endif
351 /**
352 * Creates a new handle for the specified value.
353 */
354 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
355#endif
356
357 private:
358 template<class F>
359 friend class Persistent;
360 template<class F>
361 friend class Local;
362 friend class Arguments;
363 friend class String;
364 friend class Object;
365 friend class AccessorInfo;
366 friend Handle<Primitive> Undefined(Isolate* isolate);
367 friend Handle<Primitive> Null(Isolate* isolate);
368 friend Handle<Boolean> True(Isolate* isolate);
369 friend Handle<Boolean> False(Isolate* isolate);
370 friend class Context;
371 friend class InternalHandleHelper;
372 friend class LocalContext;
373
374#ifndef V8_USE_UNSAFE_HANDLES
375 V8_INLINE(static Handle<T> New(Isolate* isolate, T* that));
376#endif
377
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 T* val_;
379};
380
381
382/**
383 * A light-weight stack-allocated object handle. All operations
384 * that return objects from within v8 return them in local handles. They
385 * are created within HandleScopes, and all local handles allocated within a
386 * handle scope are destroyed when the handle scope is destroyed. Hence it
387 * is not necessary to explicitly deallocate local handles.
388 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000389// TODO(dcarney): deprecate entire class
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000390template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000392 V8_INLINE(Local());
393 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 : Handle<T>(reinterpret_cast<T*>(*that)) {
395 /**
396 * This check fails when trying to convert between incompatible
397 * handles. For example, converting from a Handle<String> to a
398 * Handle<Number>.
399 */
400 TYPE_CHECK(T, S);
401 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000402
403
404#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000405 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000406#endif
407
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000408 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000409#ifdef V8_ENABLE_CHECKS
410 // If we're going to perform the type check then we have to check
411 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000413#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414 return Local<T>(T::Cast(*that));
415 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000416#ifndef V8_USE_UNSAFE_HANDLES
417 template <class S> V8_INLINE(Local(Handle<S> that))
418 : Handle<T>(reinterpret_cast<T*>(*that)) {
419 TYPE_CHECK(T, S);
420 }
421#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000423 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000424 return Local<S>::Cast(*this);
425 }
426
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000427 /**
428 * Create a local handle for the content of another handle.
429 * The referee is kept alive by the local handle even when
430 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000432 V8_INLINE(static Local<T> New(Handle<T> that));
433 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000434#ifndef V8_USE_UNSAFE_HANDLES
435 // TODO(dcarney): remove before cutover
436 V8_INLINE(static Local<T> New(Isolate* isolate, const Persistent<T>& that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000438#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
439
440 private:
441#endif
442 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
443#endif
444
445 private:
446 template<class F>
447 friend class Persistent;
448 template<class F>
449 friend class Handle;
450 friend class Arguments;
451 friend class String;
452 friend class Object;
453 friend class AccessorInfo;
454 friend class Context;
455 friend class InternalHandleHelper;
456 friend class LocalContext;
457
458 V8_INLINE(static Local<T> New(Isolate* isolate, T* that));
459};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460
461/**
462 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000463 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 * allocated, a Persistent handle remains valid until it is explicitly
465 * disposed.
466 *
467 * A persistent handle contains a reference to a storage cell within
468 * the v8 engine which holds an object value and which is updated by
469 * the garbage collector whenever the object is moved. A new storage
470 * cell can be created using Persistent::New and existing handles can
471 * be disposed using Persistent::Dispose. Since persistent handles
472 * are passed by value you may have many persistent handle objects
473 * that point to the same storage cell. For instance, if you pass a
474 * persistent handle as an argument to a function you will not get two
475 * different storage cells but rather two references to the same
476 * storage cell.
477 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000478template <class T> class Persistent // NOLINT
479#ifdef V8_USE_UNSAFE_HANDLES
480 : public Handle<T> {
481#else
482 { // NOLINT
483#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 public:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000485#ifndef V8_USE_UNSAFE_HANDLES
486 V8_INLINE(Persistent()) : val_(0) { }
487 V8_INLINE(~Persistent()) {
488 // TODO(dcarney): add this back before cutover.
489 // Dispose();
490 }
491 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
492 // TODO(dcarney): remove somehow before cutover
493 // The handle should either be 0, or a pointer to a live cell.
494 V8_INLINE(void Clear()) { val_ = 0; }
495
496 /**
497 * A constructor that creates a new global cell pointing to that. In contrast
498 * to the copy constructor, this creates a new persistent handle which needs
499 * to be separately disposed.
500 */
501 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
502 : val_(*New(isolate, that)) { }
503
504#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 /**
506 * Creates an empty persistent handle that doesn't point to any
507 * storage cell.
508 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000509 V8_INLINE(Persistent()) : Handle<T>() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510
511 /**
512 * Creates a persistent handle for the same storage cell as the
513 * specified handle. This constructor allows you to pass persistent
514 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000515 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000517 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 * between compatible persistent handles, for instance assigning a
519 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000520 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000522 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 : Handle<T>(reinterpret_cast<T*>(*that)) {
524 /**
525 * This check fails when trying to convert between incompatible
526 * handles. For example, converting from a Handle<String> to a
527 * Handle<Number>.
528 */
529 TYPE_CHECK(T, S);
530 }
531
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000532 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000534 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000535 * A constructor that creates a new global cell pointing to that. In contrast
536 * to the copy constructor, this creates a new persistent handle which needs
537 * to be separately disposed.
538 */
539 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
540 : Handle<T>(New(isolate, that)) { }
541
542 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000543 * "Casts" a plain handle which is known to be a persistent handle
544 * to a persistent handle.
545 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000546 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 : Handle<T>(*that) { }
548
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000549#endif
550
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000551 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000552#ifdef V8_ENABLE_CHECKS
553 // If we're going to perform the type check then we have to check
554 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000556#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 return Persistent<T>(T::Cast(*that));
558 }
559
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000560 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000561 return Persistent<S>::Cast(*this);
562 }
563
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000564 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
565
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000567 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000569 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000570 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000571#ifndef V8_USE_UNSAFE_HANDLES
572 // TODO(dcarney): remove before cutover
573 V8_INLINE(static Persistent<T> New(Isolate* isolate, Persistent<T> that));
574#endif
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000575
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000576#ifndef V8_USE_UNSAFE_HANDLES
577 template <class S> V8_INLINE(
578 bool operator==(const Persistent<S>& that) const) {
579 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
580 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
581 if (a == 0) return b == 0;
582 if (b == 0) return false;
583 return *a == *b;
584 }
585
586 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
587 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
588 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
589 if (a == 0) return b == 0;
590 if (b == 0) return false;
591 return *a == *b;
592 }
593#endif
594
595 V8_INLINE(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596
597 /**
598 * Releases the storage cell referenced by this persistent handle.
599 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000600 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 * cell remain and IsEmpty will still return false.
602 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000603 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000604 V8_INLINE(void Dispose(Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000606 template<typename S, typename P>
607 V8_INLINE(void MakeWeak(
608 Isolate* isolate,
609 P* parameters,
610 typename WeakReferenceCallbacks<S, P>::Revivable callback));
611
612 template<typename P>
613 V8_INLINE(void MakeWeak(
614 Isolate* isolate,
615 P* parameters,
616 typename WeakReferenceCallbacks<T, P>::Revivable callback));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000617
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618 /**
619 * Make the reference to this object weak. When only weak handles
620 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000621 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 * it the object reference and the given parameters.
623 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000624 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000625 V8_INLINE(void MakeWeak(Isolate* isolate,
626 void* parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000627 NearDeathCallback callback));
628
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000629 V8_INLINE(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000631 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000632 V8_INLINE(void ClearWeak(Isolate* isolate));
633
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000634 V8_INLINE(void MarkIndependent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635
636 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000637 * Marks the reference to this object independent. Garbage collector is free
638 * to ignore any object groups containing this object. Weak callback for an
639 * independent handle should not assume that it will be preceded by a global
640 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000641 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000642 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000643 V8_INLINE(void MarkIndependent(Isolate* isolate));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000644
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000645 V8_INLINE(void MarkPartiallyDependent());
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000646
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000647 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000648 * Marks the reference to this object partially dependent. Partially dependent
649 * handles only depend on other partially dependent handles and these
650 * dependencies are provided through object groups. It provides a way to build
651 * smaller object groups for young objects that represent only a subset of all
652 * external dependencies. This mark is automatically cleared after each
653 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000654 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000655 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000656 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000657
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000658 V8_INLINE(bool IsIndependent() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000659
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000660 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000661 V8_INLINE(bool IsIndependent(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000662
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000663 V8_INLINE(bool IsNearDeath() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000664
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000665 /** Checks if the handle holds the only reference to an object. */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000666 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000667 V8_INLINE(bool IsNearDeath(Isolate* isolate) const);
668
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000669 V8_INLINE(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000671 /** Returns true if the handle's reference is weak. */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000672 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000673 V8_INLINE(bool IsWeak(Isolate* isolate) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000675 V8_INLINE(void SetWrapperClassId(uint16_t class_id));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000676
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000677 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000678 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
679 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000680 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000681 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000682 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id));
683
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000684 V8_INLINE(uint16_t WrapperClassId() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000685
686 /**
687 * Returns the class ID previously assigned to this handle or 0 if no class ID
688 * was previously assigned.
689 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000690 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000691 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000692
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000693 /**
694 * Disposes the current contents of the handle and replaces it.
695 */
696 V8_INLINE(void Reset(Isolate* isolate, const Handle<T>& other));
697
698#ifndef V8_USE_UNSAFE_HANDLES
699
700#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
701
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000703#endif
704 // TODO(dcarney): make unlinkable before cutover
705 V8_INLINE(Persistent(const Persistent& that)) : val_(that.val_) {}
706 // TODO(dcarney): make unlinkable before cutover
707 V8_INLINE(Persistent& operator=(const Persistent& that)) { // NOLINT
708 this->val_ = that.val_;
709 return *this;
710 }
711
712 public:
713#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
714
715 private:
716#endif
717 // TODO(dcarney): remove before cutover
718 template <class S> V8_INLINE(Persistent(S* that)) : val_(that) { }
719 // TODO(dcarney): remove before cutover
720 template <class S> V8_INLINE(Persistent(Persistent<S> that))
721 : val_(*that) {
722 TYPE_CHECK(T, S);
723 }
724 // TODO(dcarney): remove before cutover
725 V8_INLINE(T* operator*() const) { return val_; }
726 public:
727#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW
728
729 private:
730#endif
731 // TODO(dcarney): remove before cutover
732 V8_INLINE(T* operator->() const) { return val_; }
733 public:
734#endif
735
736 private:
737 template<class F>
738 friend class Handle;
739 template<class F>
740 friend class Local;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 friend class ImplementationUtilities;
742 friend class ObjectTemplate;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000743 friend class Context;
744 friend class InternalHandleHelper;
745 friend class LocalContext;
746
747 V8_INLINE(static Persistent<T> New(Isolate* isolate, T* that));
748
749#ifndef V8_USE_UNSAFE_HANDLES
750 T* val_;
751#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752};
753
754
v8.team.kasperl727e9952008-09-02 14:56:44 +0000755 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756 * A stack-allocated class that governs a number of local handles.
757 * After a handle scope has been created, all local handles will be
758 * allocated within that handle scope until either the handle scope is
759 * deleted or another handle scope is created. If there is already a
760 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000761 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762 * new handles will again be allocated in the original handle scope.
763 *
764 * After the handle scope of a local handle has been deleted the
765 * garbage collector will no longer track the object stored in the
766 * handle and may deallocate it. The behavior of accessing a handle
767 * for which the handle scope has been deleted is undefined.
768 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000769class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000771 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000772 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000774 HandleScope(Isolate* isolate);
775
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000776 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777
778 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 * Closes the handle scope and returns the value as a handle in the
780 * previous scope, which is the new current scope after the call.
781 */
782 template <class T> Local<T> Close(Handle<T> value);
783
784 /**
785 * Counts the number of allocated handles.
786 */
787 static int NumberOfHandles();
788
789 /**
790 * Creates a new handle with the given value.
791 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000792 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000793 static internal::Object** CreateHandle(internal::Isolate* isolate,
794 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000795 // Faster version, uses HeapObject to obtain the current Isolate.
796 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797
798 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000799 // Make it hard to create heap-allocated or illegal handle scopes by
800 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801 HandleScope(const HandleScope&);
802 void operator=(const HandleScope&);
803 void* operator new(size_t size);
804 void operator delete(void*, size_t);
805
ager@chromium.org3811b432009-10-28 14:53:37 +0000806 // This Data class is accessible internally as HandleScopeData through a
807 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000808 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000809 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000810 internal::Object** next;
811 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000812 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000813 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000815 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816 }
817 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000818
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000819 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000820 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000822 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000823 internal::Object** prev_next_;
824 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000826 // Allow for the active closing of HandleScopes which allows to pass a handle
827 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000829 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831 friend class ImplementationUtilities;
832};
833
834
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000835// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836
837
838/**
839 * The superclass of values and API object templates.
840 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000841class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842 private:
843 Data();
844};
845
846
847/**
848 * Pre-compilation data that can be associated with a script. This
849 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000850 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000851 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000853class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854 public:
855 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000856
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000857 /**
858 * Pre-compiles the specified script (context-independent).
859 *
860 * \param input Pointer to UTF-8 script source code.
861 * \param length Length of UTF-8 script source code.
862 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000865 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000866 * Pre-compiles the specified script (context-independent).
867 *
868 * NOTE: Pre-compilation using this method cannot happen on another thread
869 * without using Lockers.
870 *
871 * \param source Script source code.
872 */
873 static ScriptData* PreCompile(Handle<String> source);
874
875 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000876 * Load previous pre-compilation data.
877 *
878 * \param data Pointer to data returned by a call to Data() of a previous
879 * ScriptData. Ownership is not transferred.
880 * \param length Length of data.
881 */
882 static ScriptData* New(const char* data, int length);
883
884 /**
885 * Returns the length of Data().
886 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000888
889 /**
890 * Returns a serialized representation of this ScriptData that can later be
891 * passed to New(). NOTE: Serialized data is platform-dependent.
892 */
893 virtual const char* Data() = 0;
894
895 /**
896 * Returns true if the source code could not be parsed.
897 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000898 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899};
900
901
902/**
903 * The origin, within a file, of a script.
904 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000905class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000907 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000908 Handle<Value> resource_name,
909 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000910 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 : resource_name_(resource_name),
912 resource_line_offset_(resource_line_offset),
913 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000914 V8_INLINE(Handle<Value> ResourceName() const);
915 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
916 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000918 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 Handle<Integer> resource_line_offset_;
920 Handle<Integer> resource_column_offset_;
921};
922
923
924/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000925 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000927class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000930 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000931 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000932 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000933 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000934 * when New() returns
935 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
936 * using pre_data speeds compilation if it's done multiple times.
937 * Owned by caller, no references are kept when New() returns.
938 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000939 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000940 * available to compile event handlers.
941 * \return Compiled script object (context independent; when run it
942 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000944 static Local<Script> New(Handle<String> source,
945 ScriptOrigin* origin = NULL,
946 ScriptData* pre_data = NULL,
947 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948
mads.s.agercbaa0602008-08-14 13:41:48 +0000949 /**
950 * Compiles the specified script using the specified file name
951 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000952 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000953 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000954 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000955 * as the script's origin.
956 * \return Compiled script object (context independent; when run it
957 * will use the currently entered context).
958 */
959 static Local<Script> New(Handle<String> source,
960 Handle<Value> file_name);
961
962 /**
963 * Compiles the specified script (bound to current context).
964 *
965 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000966 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000967 * when Compile() returns
968 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
969 * using pre_data speeds compilation if it's done multiple times.
970 * Owned by caller, no references are kept when Compile() returns.
971 * \param script_data Arbitrary data associated with script. Using
972 * this has same effect as calling SetData(), but makes data available
973 * earlier (i.e. to compile event handlers).
974 * \return Compiled script object, bound to the context that was active
975 * when this function was called. When run it will always use this
976 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000977 */
978 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000979 ScriptOrigin* origin = NULL,
980 ScriptData* pre_data = NULL,
981 Handle<String> script_data = Handle<String>());
982
983 /**
984 * Compiles the specified script using the specified file name
985 * object (typically a string) as the script's origin.
986 *
987 * \param source Script source code.
988 * \param file_name File name to use as script's origin
989 * \param script_data Arbitrary data associated with script. Using
990 * this has same effect as calling SetData(), but makes data available
991 * earlier (i.e. to compile event handlers).
992 * \return Compiled script object, bound to the context that was active
993 * when this function was called. When run it will always use this
994 * context.
995 */
996 static Local<Script> Compile(Handle<String> source,
997 Handle<Value> file_name,
998 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000999
v8.team.kasperl727e9952008-09-02 14:56:44 +00001000 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001001 * Runs the script returning the resulting value. If the script is
1002 * context independent (created using ::New) it will be run in the
1003 * currently entered context. If it is context specific (created
1004 * using ::Compile) it will be run in the context in which it was
1005 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001006 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001008
1009 /**
1010 * Returns the script id value.
1011 */
1012 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001013
1014 /**
1015 * Associate an additional data object with the script. This is mainly used
1016 * with the debugger as this data object is only available through the
1017 * debugger API.
1018 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00001019 void SetData(Handle<String> data);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001020
1021 /**
1022 * Returns the name value of one Script.
1023 */
1024 Handle<Value> GetScriptName();
1025
1026 /**
1027 * Returns zero based line number of the code_pos location in the script.
1028 * -1 will be returned if no information available.
1029 */
1030 int GetLineNumber(int code_pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031};
1032
1033
1034/**
1035 * An error message.
1036 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001037class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001039 Local<String> Get() const;
1040 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001042 /**
1043 * Returns the resource name for the script from where the function causing
1044 * the error originates.
1045 */
ager@chromium.org32912102009-01-16 10:38:43 +00001046 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001048 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001049 * Returns the resource data for the script from where the function causing
1050 * the error originates.
1051 */
1052 Handle<Value> GetScriptData() const;
1053
1054 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001055 * Exception stack trace. By default stack traces are not captured for
1056 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1057 * to change this option.
1058 */
1059 Handle<StackTrace> GetStackTrace() const;
1060
1061 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001062 * Returns the number, 1-based, of the line where the error occurred.
1063 */
ager@chromium.org32912102009-01-16 10:38:43 +00001064 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001066 /**
1067 * Returns the index within the script of the first character where
1068 * the error occurred.
1069 */
ager@chromium.org32912102009-01-16 10:38:43 +00001070 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001071
1072 /**
1073 * Returns the index within the script of the last character where
1074 * the error occurred.
1075 */
ager@chromium.org32912102009-01-16 10:38:43 +00001076 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001077
1078 /**
1079 * Returns the index within the line of the first character where
1080 * the error occurred.
1081 */
ager@chromium.org32912102009-01-16 10:38:43 +00001082 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001083
1084 /**
1085 * Returns the index within the line of the last character where
1086 * the error occurred.
1087 */
ager@chromium.org32912102009-01-16 10:38:43 +00001088 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001089
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 // TODO(1245381): Print to a string instead of on a FILE.
1091 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001092
1093 static const int kNoLineNumberInfo = 0;
1094 static const int kNoColumnInfo = 0;
1095};
1096
1097
1098/**
1099 * Representation of a JavaScript stack trace. The information collected is a
1100 * snapshot of the execution stack and the information remains valid after
1101 * execution continues.
1102 */
1103class V8EXPORT StackTrace {
1104 public:
1105 /**
1106 * Flags that determine what information is placed captured for each
1107 * StackFrame when grabbing the current stack trace.
1108 */
1109 enum StackTraceOptions {
1110 kLineNumber = 1,
1111 kColumnOffset = 1 << 1 | kLineNumber,
1112 kScriptName = 1 << 2,
1113 kFunctionName = 1 << 3,
1114 kIsEval = 1 << 4,
1115 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001116 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001117 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001118 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001119 };
1120
1121 /**
1122 * Returns a StackFrame at a particular index.
1123 */
1124 Local<StackFrame> GetFrame(uint32_t index) const;
1125
1126 /**
1127 * Returns the number of StackFrames.
1128 */
1129 int GetFrameCount() const;
1130
1131 /**
1132 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1133 */
1134 Local<Array> AsArray();
1135
1136 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001137 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001138 *
1139 * \param frame_limit The maximum number of stack frames we want to capture.
1140 * \param options Enumerates the set of things we will capture for each
1141 * StackFrame.
1142 */
1143 static Local<StackTrace> CurrentStackTrace(
1144 int frame_limit,
1145 StackTraceOptions options = kOverview);
1146};
1147
1148
1149/**
1150 * A single JavaScript stack frame.
1151 */
1152class V8EXPORT StackFrame {
1153 public:
1154 /**
1155 * Returns the number, 1-based, of the line for the associate function call.
1156 * This method will return Message::kNoLineNumberInfo if it is unable to
1157 * retrieve the line number, or if kLineNumber was not passed as an option
1158 * when capturing the StackTrace.
1159 */
1160 int GetLineNumber() const;
1161
1162 /**
1163 * Returns the 1-based column offset on the line for the associated function
1164 * call.
1165 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1166 * the column number, or if kColumnOffset was not passed as an option when
1167 * capturing the StackTrace.
1168 */
1169 int GetColumn() const;
1170
1171 /**
1172 * Returns the name of the resource that contains the script for the
1173 * function for this StackFrame.
1174 */
1175 Local<String> GetScriptName() const;
1176
1177 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001178 * Returns the name of the resource that contains the script for the
1179 * function for this StackFrame or sourceURL value if the script name
1180 * is undefined and its source ends with //@ sourceURL=... string.
1181 */
1182 Local<String> GetScriptNameOrSourceURL() const;
1183
1184 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001185 * Returns the name of the function associated with this stack frame.
1186 */
1187 Local<String> GetFunctionName() const;
1188
1189 /**
1190 * Returns whether or not the associated function is compiled via a call to
1191 * eval().
1192 */
1193 bool IsEval() const;
1194
1195 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001196 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001197 * constructor via "new".
1198 */
1199 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200};
1201
1202
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001203// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204
1205
1206/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001207 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001209class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211 /**
1212 * Returns true if this value is the undefined value. See ECMA-262
1213 * 4.3.10.
1214 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001215 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216
1217 /**
1218 * Returns true if this value is the null value. See ECMA-262
1219 * 4.3.11.
1220 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001221 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222
1223 /**
1224 * Returns true if this value is true.
1225 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001226 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227
1228 /**
1229 * Returns true if this value is false.
1230 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001231 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232
1233 /**
1234 * Returns true if this value is an instance of the String type.
1235 * See ECMA-262 8.4.
1236 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001237 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238
1239 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001240 * Returns true if this value is a symbol.
1241 * This is an experimental feature.
1242 */
1243 bool IsSymbol() const;
1244
1245 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 * Returns true if this value is a function.
1247 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001248 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249
1250 /**
1251 * Returns true if this value is an array.
1252 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001253 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254
v8.team.kasperl727e9952008-09-02 14:56:44 +00001255 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256 * Returns true if this value is an object.
1257 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001258 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259
v8.team.kasperl727e9952008-09-02 14:56:44 +00001260 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 * Returns true if this value is boolean.
1262 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001263 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001264
v8.team.kasperl727e9952008-09-02 14:56:44 +00001265 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 * Returns true if this value is a number.
1267 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001268 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269
v8.team.kasperl727e9952008-09-02 14:56:44 +00001270 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 * Returns true if this value is external.
1272 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001273 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274
v8.team.kasperl727e9952008-09-02 14:56:44 +00001275 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 * Returns true if this value is a 32-bit signed integer.
1277 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001278 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001280 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001281 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001282 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001283 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001284
1285 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001286 * Returns true if this value is a Date.
1287 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001288 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001289
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001290 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001291 * Returns true if this value is a Boolean object.
1292 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001293 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001294
1295 /**
1296 * Returns true if this value is a Number object.
1297 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001298 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001299
1300 /**
1301 * Returns true if this value is a String object.
1302 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001303 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001304
1305 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001306 * Returns true if this value is a Symbol object.
1307 * This is an experimental feature.
1308 */
1309 bool IsSymbolObject() const;
1310
1311 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001312 * Returns true if this value is a NativeError.
1313 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001314 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001315
1316 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001317 * Returns true if this value is a RegExp.
1318 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001319 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001320
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001321
1322 /**
1323 * Returns true if this value is an ArrayBuffer.
1324 * This is an experimental feature.
1325 */
1326 bool IsArrayBuffer() const;
1327
1328 /**
1329 * Returns true if this value is one of TypedArrays.
1330 * This is an experimental feature.
1331 */
1332 bool IsTypedArray() const;
1333
1334 /**
1335 * Returns true if this value is an Uint8Array.
1336 * This is an experimental feature.
1337 */
1338 bool IsUint8Array() const;
1339
1340 /**
1341 * Returns true if this value is an Uint8ClampedArray.
1342 * This is an experimental feature.
1343 */
1344 bool IsUint8ClampedArray() const;
1345
1346 /**
1347 * Returns true if this value is an Int8Array.
1348 * This is an experimental feature.
1349 */
1350 bool IsInt8Array() const;
1351
1352 /**
1353 * Returns true if this value is an Uint16Array.
1354 * This is an experimental feature.
1355 */
1356 bool IsUint16Array() const;
1357
1358 /**
1359 * Returns true if this value is an Int16Array.
1360 * This is an experimental feature.
1361 */
1362 bool IsInt16Array() const;
1363
1364 /**
1365 * Returns true if this value is an Uint32Array.
1366 * This is an experimental feature.
1367 */
1368 bool IsUint32Array() const;
1369
1370 /**
1371 * Returns true if this value is an Int32Array.
1372 * This is an experimental feature.
1373 */
1374 bool IsInt32Array() const;
1375
1376 /**
1377 * Returns true if this value is a Float32Array.
1378 * This is an experimental feature.
1379 */
1380 bool IsFloat32Array() const;
1381
1382 /**
1383 * Returns true if this value is a Float64Array.
1384 * This is an experimental feature.
1385 */
1386 bool IsFloat64Array() const;
1387
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001388 Local<Boolean> ToBoolean() const;
1389 Local<Number> ToNumber() const;
1390 Local<String> ToString() const;
1391 Local<String> ToDetailString() const;
1392 Local<Object> ToObject() const;
1393 Local<Integer> ToInteger() const;
1394 Local<Uint32> ToUint32() const;
1395 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396
1397 /**
1398 * Attempts to convert a string to an array index.
1399 * Returns an empty handle if the conversion fails.
1400 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001401 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001402
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001403 bool BooleanValue() const;
1404 double NumberValue() const;
1405 int64_t IntegerValue() const;
1406 uint32_t Uint32Value() const;
1407 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408
1409 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001410 bool Equals(Handle<Value> that) const;
1411 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001412
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001413 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001414 V8_INLINE(bool QuickIsUndefined() const);
1415 V8_INLINE(bool QuickIsNull() const);
1416 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001417 bool FullIsUndefined() const;
1418 bool FullIsNull() const;
1419 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420};
1421
1422
1423/**
1424 * The superclass of primitive values. See ECMA-262 4.3.2.
1425 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001426class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427
1428
1429/**
1430 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1431 * or false value.
1432 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001433class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001435 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001436 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437};
1438
1439
1440/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001441 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001443class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001445 enum Encoding {
1446 UNKNOWN_ENCODING = 0x1,
1447 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001448 ASCII_ENCODING = 0x4,
1449 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001450 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001451 /**
1452 * Returns the number of characters in this string.
1453 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001454 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455
v8.team.kasperl727e9952008-09-02 14:56:44 +00001456 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001457 * Returns the number of bytes in the UTF-8 encoded
1458 * representation of this string.
1459 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001460 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001461
1462 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001463 * This function is no longer useful.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001464 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001465 V8_DEPRECATED(V8_INLINE(bool MayContainNonAscii()) const) { return true; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001466
1467 /**
1468 * Returns whether this string contains only one byte data.
1469 */
1470 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001471
1472 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001473 * Write the contents of the string to an external buffer.
1474 * If no arguments are given, expects the buffer to be large
1475 * enough to hold the entire string and NULL terminator. Copies
1476 * the contents of the string and the NULL terminator into the
1477 * buffer.
1478 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001479 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1480 * before the end of the buffer.
1481 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001482 * Copies up to length characters into the output buffer.
1483 * Only null-terminates if there is enough space in the buffer.
1484 *
1485 * \param buffer The buffer into which the string will be copied.
1486 * \param start The starting position within the string at which
1487 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001488 * \param length The number of characters to copy from the string. For
1489 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001490 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001491 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001492 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001493 * \return The number of characters copied to the buffer excluding the null
1494 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001495 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001496 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001497 enum WriteOptions {
1498 NO_OPTIONS = 0,
1499 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001500 NO_NULL_TERMINATION = 2,
1501 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001502 };
1503
lrn@chromium.org34e60782011-09-15 07:25:40 +00001504 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001505 int Write(uint16_t* buffer,
1506 int start = 0,
1507 int length = -1,
1508 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001509 // ASCII characters.
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001510 V8_DEPRECATED(int WriteAscii(char* buffer,
1511 int start = 0,
1512 int length = -1,
1513 int options = NO_OPTIONS) const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001514 // One byte characters.
1515 int WriteOneByte(uint8_t* buffer,
1516 int start = 0,
1517 int length = -1,
1518 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001519 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001520 int WriteUtf8(char* buffer,
1521 int length = -1,
1522 int* nchars_ref = NULL,
1523 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001524
v8.team.kasperl727e9952008-09-02 14:56:44 +00001525 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001526 * A zero length string.
1527 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001528 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001529 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001530
1531 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001532 * Returns true if the string is external
1533 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001534 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535
v8.team.kasperl727e9952008-09-02 14:56:44 +00001536 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001537 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001538 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001539 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001540
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001541 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001542 public:
1543 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001544
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001545 protected:
1546 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001547
1548 /**
1549 * Internally V8 will call this Dispose method when the external string
1550 * resource is no longer needed. The default implementation will use the
1551 * delete operator. This method can be overridden in subclasses to
1552 * control how allocated external string resources are disposed.
1553 */
1554 virtual void Dispose() { delete this; }
1555
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001556 private:
1557 // Disallow copying and assigning.
1558 ExternalStringResourceBase(const ExternalStringResourceBase&);
1559 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001560
1561 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001562 };
1563
v8.team.kasperl727e9952008-09-02 14:56:44 +00001564 /**
1565 * An ExternalStringResource is a wrapper around a two-byte string
1566 * buffer that resides outside V8's heap. Implement an
1567 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001568 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001569 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001570 class V8EXPORT ExternalStringResource
1571 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572 public:
1573 /**
1574 * Override the destructor to manage the life cycle of the underlying
1575 * buffer.
1576 */
1577 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001578
1579 /**
1580 * The string data from the underlying buffer.
1581 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001583
1584 /**
1585 * The length of the string. That is, the number of two-byte characters.
1586 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001588
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 protected:
1590 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 };
1592
1593 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001594 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001595 * string buffer that resides outside V8's heap. Implement an
1596 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001597 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001598 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001599 * UTF-8, which would require special treatment internally in the
1600 * engine and, in the case of UTF-8, do not allow efficient indexing.
1601 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001602 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001604 class V8EXPORT ExternalAsciiStringResource
1605 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606 public:
1607 /**
1608 * Override the destructor to manage the life cycle of the underlying
1609 * buffer.
1610 */
1611 virtual ~ExternalAsciiStringResource() {}
1612 /** The string data from the underlying buffer.*/
1613 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001614 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615 virtual size_t length() const = 0;
1616 protected:
1617 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 };
1619
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001620 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1621
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001623 * If the string is an external string, return the ExternalStringResourceBase
1624 * regardless of the encoding, otherwise return NULL. The encoding of the
1625 * string is returned in encoding_out.
1626 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001627 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1628 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001629
1630 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001631 * Get the ExternalStringResource for an external string. Returns
1632 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001634 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635
1636 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001637 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001638 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001640 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001642 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001644 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001646 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001647 * The second parameter 'length' gives the buffer length. If omitted,
1648 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001650 V8_INLINE(static Local<String> New(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001652 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001653 /** Allocates a new string from 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001654 V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001656 // TODO(dcarney): deprecate
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001657 /**
1658 * Creates an internalized string (historically called a "symbol",
1659 * not to be confused with ES6 symbols). Returns one if it exists already.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001660 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001661 V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
1662
1663 enum NewStringType {
1664 kNormalString, kInternalizedString, kUndetectableString
1665 };
1666
1667 /** Allocates a new string from UTF-8 data.*/
1668 static Local<String> NewFromUtf8(Isolate* isolate,
1669 const char* data,
1670 NewStringType type = kNormalString,
1671 int length = -1);
1672
1673 /** Allocates a new string from Latin-1 data.*/
1674 static Local<String> NewFromOneByte(
1675 Isolate* isolate,
1676 const uint8_t* data,
1677 NewStringType type = kNormalString,
1678 int length = -1);
1679
1680 /** Allocates a new string from UTF-16 data.*/
1681 static Local<String> NewFromTwoByte(
1682 Isolate* isolate,
1683 const uint16_t* data,
1684 NewStringType type = kNormalString,
1685 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686
v8.team.kasperl727e9952008-09-02 14:56:44 +00001687 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001688 * Creates a new string by concatenating the left and the right strings
1689 * passed in as parameters.
1690 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001691 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001692
1693 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001694 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001695 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001696 * resource will be disposed by calling its Dispose method. The caller of
1697 * this function should not otherwise delete or modify the resource. Neither
1698 * should the underlying buffer be deallocated or modified except through the
1699 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001700 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001701 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001702
ager@chromium.org6f10e412009-02-13 10:11:16 +00001703 /**
1704 * Associate an external string resource with this string by transforming it
1705 * in place so that existing references to this string in the JavaScript heap
1706 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001707 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001708 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001709 * The string is not modified if the operation fails. See NewExternal for
1710 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001711 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001712 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713
v8.team.kasperl727e9952008-09-02 14:56:44 +00001714 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001715 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001716 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001717 * resource will be disposed by calling its Dispose method. The caller of
1718 * this function should not otherwise delete or modify the resource. Neither
1719 * should the underlying buffer be deallocated or modified except through the
1720 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001721 */
1722 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001723
ager@chromium.org6f10e412009-02-13 10:11:16 +00001724 /**
1725 * Associate an external string resource with this string by transforming it
1726 * in place so that existing references to this string in the JavaScript heap
1727 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001728 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001729 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001730 * The string is not modified if the operation fails. See NewExternal for
1731 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001732 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001733 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001735 /**
1736 * Returns true if this string can be made external.
1737 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001738 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001739
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001740 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001741 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001742 V8_INLINE(
1743 static Local<String> NewUndetectable(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001745 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001746 /** Creates an undetectable string from the supplied 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001747 V8_INLINE(static Local<String> NewUndetectable(
1748 const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749
1750 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001751 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001752 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001753 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001754 * then the length() method returns 0 and the * operator returns
1755 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001756 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001757 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001758 public:
1759 explicit Utf8Value(Handle<v8::Value> obj);
1760 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001761 char* operator*() { return str_; }
1762 const char* operator*() const { return str_; }
1763 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001764 private:
1765 char* str_;
1766 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001767
1768 // Disallow copying and assigning.
1769 Utf8Value(const Utf8Value&);
1770 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001771 };
1772
1773 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001774 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001776 * If conversion to a string fails (eg. due to an exception in the toString()
1777 * method of the object) then the length() method returns 0 and the * operator
1778 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001780 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 public:
1782 explicit AsciiValue(Handle<v8::Value> obj);
1783 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001784 char* operator*() { return str_; }
1785 const char* operator*() const { return str_; }
1786 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 private:
1788 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001789 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001790
1791 // Disallow copying and assigning.
1792 AsciiValue(const AsciiValue&);
1793 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 };
1795
1796 /**
1797 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001798 * If conversion to a string fails (eg. due to an exception in the toString()
1799 * method of the object) then the length() method returns 0 and the * operator
1800 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001802 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 public:
1804 explicit Value(Handle<v8::Value> obj);
1805 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001806 uint16_t* operator*() { return str_; }
1807 const uint16_t* operator*() const { return str_; }
1808 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809 private:
1810 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001811 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001812
1813 // Disallow copying and assigning.
1814 Value(const Value&);
1815 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001817
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001818 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001819 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1820 Encoding encoding) const;
1821 void VerifyExternalStringResource(ExternalStringResource* val) const;
1822 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823};
1824
1825
1826/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001827 * A JavaScript symbol (ECMA-262 edition 6)
1828 *
1829 * This is an experimental feature. Use at your own risk.
1830 */
1831class V8EXPORT Symbol : public Primitive {
1832 public:
1833 // Returns the print name string of the symbol, or undefined if none.
1834 Local<Value> Name() const;
1835
1836 // Create a symbol without a print name.
1837 static Local<Symbol> New(Isolate* isolate);
1838
1839 // Create a symbol with a print name.
1840 static Local<Symbol> New(Isolate *isolate, const char* data, int length = -1);
1841
1842 V8_INLINE(static Symbol* Cast(v8::Value* obj));
1843 private:
1844 Symbol();
1845 static void CheckCast(v8::Value* obj);
1846};
1847
1848
1849/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001850 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001852class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001854 double Value() const;
1855 static Local<Number> New(double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001856 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001858 Number();
1859 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860};
1861
1862
1863/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001864 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001866class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001868 static Local<Integer> New(int32_t value);
1869 static Local<Integer> NewFromUnsigned(uint32_t value);
1870 static Local<Integer> New(int32_t value, Isolate*);
1871 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1872 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001873 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001875 Integer();
1876 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877};
1878
1879
1880/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001881 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001883class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001885 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001887 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888};
1889
1890
1891/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001892 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001894class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001896 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001898 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899};
1900
1901
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902enum PropertyAttribute {
1903 None = 0,
1904 ReadOnly = 1 << 0,
1905 DontEnum = 1 << 1,
1906 DontDelete = 1 << 2
1907};
1908
ager@chromium.org3811b432009-10-28 14:53:37 +00001909enum ExternalArrayType {
1910 kExternalByteArray = 1,
1911 kExternalUnsignedByteArray,
1912 kExternalShortArray,
1913 kExternalUnsignedShortArray,
1914 kExternalIntArray,
1915 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001916 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001917 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001918 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001919};
1920
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001922 * Accessor[Getter|Setter] are used as callback functions when
1923 * setting|getting a particular property. See Object and ObjectTemplate's
1924 * method SetAccessor.
1925 */
1926typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1927 const AccessorInfo& info);
1928
1929
1930typedef void (*AccessorSetter)(Local<String> property,
1931 Local<Value> value,
1932 const AccessorInfo& info);
1933
1934
1935/**
1936 * Access control specifications.
1937 *
1938 * Some accessors should be accessible across contexts. These
1939 * accessors have an explicit access control parameter which specifies
1940 * the kind of cross-context access that should be allowed.
1941 *
1942 * Additionally, for security, accessors can prohibit overwriting by
1943 * accessors defined in JavaScript. For objects that have such
1944 * accessors either locally or in their prototype chain it is not
1945 * possible to overwrite the accessor by using __defineGetter__ or
1946 * __defineSetter__ from JavaScript code.
1947 */
1948enum AccessControl {
1949 DEFAULT = 0,
1950 ALL_CAN_READ = 1,
1951 ALL_CAN_WRITE = 1 << 1,
1952 PROHIBITS_OVERWRITING = 1 << 2
1953};
1954
1955
1956/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001957 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001959class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001961 bool Set(Handle<Value> key,
1962 Handle<Value> value,
1963 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001964
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001965 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001966
ager@chromium.orge2902be2009-06-08 12:21:35 +00001967 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001968 // overriding accessors or read-only properties.
1969 //
1970 // Note that if the object has an interceptor the property will be set
1971 // locally, but since the interceptor takes precedence the local property
1972 // will only be returned if the interceptor doesn't return a value.
1973 //
1974 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001975 bool ForceSet(Handle<Value> key,
1976 Handle<Value> value,
1977 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001978
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001979 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001981 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001982
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001983 /**
1984 * Gets the property attributes of a property which can be None or
1985 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1986 * None when the property doesn't exist.
1987 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001988 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001989
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001990 bool Has(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001991
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001992 bool Delete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001993
1994 // Delete a property on this object bypassing interceptors and
1995 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001996 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001997
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001998 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001999
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002000 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002002 bool SetAccessor(Handle<String> name,
2003 AccessorGetter getter,
2004 AccessorSetter setter = 0,
2005 Handle<Value> data = Handle<Value>(),
2006 AccessControl settings = DEFAULT,
2007 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002008
ulan@chromium.org750145a2013-03-07 15:14:13 +00002009 // This function is not yet stable and should not be used at this time.
2010 bool SetAccessor(Handle<String> name,
2011 Handle<DeclaredAccessorDescriptor> descriptor,
2012 AccessControl settings = DEFAULT,
2013 PropertyAttribute attribute = None);
2014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002016 * Returns an array containing the names of the enumerable properties
2017 * of this object, including properties from prototype objects. The
2018 * array returned by this method contains the same values as would
2019 * be enumerated by a for-in statement over this object.
2020 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002021 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002022
2023 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002024 * This function has the same functionality as GetPropertyNames but
2025 * the returned array doesn't contain the names of properties from
2026 * prototype objects.
2027 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002028 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002029
2030 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 * Get the prototype object. This does not skip objects marked to
2032 * be skipped by __proto__ and it does not consult the security
2033 * handler.
2034 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002035 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036
2037 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002038 * Set the prototype object. This does not skip objects marked to
2039 * be skipped by __proto__ and it does not consult the security
2040 * handler.
2041 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002042 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00002043
2044 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002045 * Finds an instance of the given function template in the prototype
2046 * chain.
2047 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002048 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002049
2050 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 * Call builtin Object.prototype.toString on this object.
2052 * This is different from Value::ToString() that may call
2053 * user-defined toString function. This one does not.
2054 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002055 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002057 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002058 * Returns the function invoked as a constructor for this object.
2059 * May be the null value.
2060 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002061 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002062
2063 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002064 * Returns the name of the function invoked as a constructor for this object.
2065 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002066 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002067
kasper.lund212ac232008-07-16 07:07:30 +00002068 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002069 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002070
2071 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002072 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002073
kasper.lund212ac232008-07-16 07:07:30 +00002074 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002075 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002076
2077 /**
2078 * Gets a 2-byte-aligned native pointer from an internal field. This field
2079 * must have been set by SetAlignedPointerInInternalField, everything else
2080 * leads to undefined behavior.
2081 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002082 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002083
2084 /**
2085 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2086 * a field, GetAlignedPointerFromInternalField must be used, everything else
2087 * leads to undefined behavior.
2088 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002089 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002090
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002092 bool HasOwnProperty(Handle<String> key);
2093 bool HasRealNamedProperty(Handle<String> key);
2094 bool HasRealIndexedProperty(uint32_t index);
2095 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096
2097 /**
2098 * If result.IsEmpty() no real property was located in the prototype chain.
2099 * This means interceptors in the prototype chain are not called.
2100 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002101 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00002102
2103 /**
2104 * If result.IsEmpty() no real property was located on the object or
2105 * in the prototype chain.
2106 * This means interceptors in the prototype chain are not called.
2107 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002108 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109
2110 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002111 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112
kasper.lund212ac232008-07-16 07:07:30 +00002113 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002114 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002116 /**
2117 * Turns on access check on the object if the object is an instance of
2118 * a template that has access check callbacks. If an object has no
2119 * access check info, the object cannot be accessed by anyone.
2120 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002121 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00002122
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002123 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002124 * Returns the identity hash for this object. The current implementation
2125 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002126 *
ager@chromium.org9085a012009-05-11 19:22:57 +00002127 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00002128 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002129 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002130 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00002131
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002132 /**
2133 * Access hidden properties on JavaScript objects. These properties are
2134 * hidden from the executing JavaScript and only accessible through the V8
2135 * C++ API. Hidden properties introduced by V8 internally (for example the
2136 * identity hash) are prefixed with "v8::".
2137 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002138 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
2139 Local<Value> GetHiddenValue(Handle<String> key);
2140 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002141
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002142 /**
2143 * Returns true if this is an instance of an api function (one
2144 * created from a function created from a function template) and has
2145 * been modified since it was created. Note that this method is
2146 * conservative and may return true for objects that haven't actually
2147 * been modified.
2148 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002149 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002150
2151 /**
2152 * Clone this object with a fast but shallow copy. Values will point
2153 * to the same values as the original object.
2154 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002155 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002157 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002158 * Returns the context in which the object was created.
2159 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002160 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002161
2162 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002163 * Set the backing store of the indexed properties to be managed by the
2164 * embedding layer. Access to the indexed properties will follow the rules
2165 * spelled out in CanvasPixelArray.
2166 * Note: The embedding program still owns the data and needs to ensure that
2167 * the backing store is preserved while V8 has a reference.
2168 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002169 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
2170 bool HasIndexedPropertiesInPixelData();
2171 uint8_t* GetIndexedPropertiesPixelData();
2172 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002173
ager@chromium.org3811b432009-10-28 14:53:37 +00002174 /**
2175 * Set the backing store of the indexed properties to be managed by the
2176 * embedding layer. Access to the indexed properties will follow the rules
2177 * spelled out for the CanvasArray subtypes in the WebGL specification.
2178 * Note: The embedding program still owns the data and needs to ensure that
2179 * the backing store is preserved while V8 has a reference.
2180 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002181 void SetIndexedPropertiesToExternalArrayData(void* data,
2182 ExternalArrayType array_type,
2183 int number_of_elements);
2184 bool HasIndexedPropertiesInExternalArrayData();
2185 void* GetIndexedPropertiesExternalArrayData();
2186 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
2187 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00002188
lrn@chromium.org1c092762011-05-09 09:42:16 +00002189 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002190 * Checks whether a callback is set by the
2191 * ObjectTemplate::SetCallAsFunctionHandler method.
2192 * When an Object is callable this method returns true.
2193 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002194 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002195
2196 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002197 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002198 * ObjectTemplate::SetCallAsFunctionHandler method.
2199 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002200 Local<Value> CallAsFunction(Handle<Object> recv,
2201 int argc,
2202 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002203
2204 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002205 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002206 * ObjectTemplate::SetCallAsFunctionHandler method.
2207 * Note: This method behaves like the Function::NewInstance method.
2208 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002209 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002210
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002211 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002212 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002213
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002215 Object();
2216 static void CheckCast(Value* obj);
2217 Local<Value> SlowGetInternalField(int index);
2218 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219};
2220
2221
2222/**
2223 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
2224 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002225class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002227 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228
ager@chromium.org3e875802009-06-29 08:26:34 +00002229 /**
2230 * Clones an element at index |index|. Returns an empty
2231 * handle if cloning fails (for any reason).
2232 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002233 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00002234
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002235 /**
2236 * Creates a JavaScript array with the given length. If the length
2237 * is negative the returned array will have length 0.
2238 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002239 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002240
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002241 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002243 Array();
2244 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245};
2246
2247
2248/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002249 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002251class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002253 Local<Object> NewInstance() const;
2254 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
2255 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
2256 void SetName(Handle<String> name);
2257 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00002258
2259 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002260 * Name inferred from variable or property assignment of this function.
2261 * Used to facilitate debugging and profiling of JavaScript code written
2262 * in an OO style, where many functions are anonymous but are assigned
2263 * to object properties.
2264 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002265 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002266
2267 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002268 * Returns zero based line number of function body and
2269 * kLineOffsetNotFound if no information available.
2270 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002271 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002272 /**
2273 * Returns zero based column number of function body and
2274 * kLineOffsetNotFound if no information available.
2275 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002276 int GetScriptColumnNumber() const;
2277 Handle<Value> GetScriptId() const;
2278 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002279 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002280 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002283 Function();
2284 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285};
2286
2287
2288/**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002289 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
2290 * This API is experimental and may change significantly.
2291 */
2292class V8EXPORT ArrayBuffer : public Object {
2293 public:
2294 /**
2295 * Data length in bytes.
2296 */
2297 size_t ByteLength() const;
2298 /**
2299 * Raw pointer to the array buffer data
2300 */
2301 void* Data() const;
2302
2303 /**
2304 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
2305 * Allocated memory will be owned by a created ArrayBuffer and
2306 * will be deallocated when it is garbage-collected.
2307 */
2308 static Local<ArrayBuffer> New(size_t byte_length);
2309
2310 /**
2311 * Create a new ArrayBuffer over an existing memory block.
2312 * The memory block will not be reclaimed when a created ArrayBuffer
2313 * is garbage-collected.
2314 */
2315 static Local<ArrayBuffer> New(void* data, size_t byte_length);
2316
2317 V8_INLINE(static ArrayBuffer* Cast(Value* obj));
2318
2319 private:
2320 ArrayBuffer();
2321 static void CheckCast(Value* obj);
2322};
2323
2324
2325/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002326 * A base class for an instance of TypedArray series of constructors
2327 * (ES6 draft 15.13.6).
2328 * This API is experimental and may change significantly.
2329 */
2330class V8EXPORT TypedArray : public Object {
2331 public:
2332 /**
2333 * Returns underlying ArrayBuffer.
2334 */
2335 Local<ArrayBuffer> Buffer();
2336 /**
2337 * Byte offset in |Buffer|
2338 */
2339 size_t ByteOffset();
2340 /**
2341 * Numbe of elements in this typed array.
2342 */
2343 size_t Length();
2344 /**
2345 * Size of typed array in bytes (e.g. for Int16Array, 2*|Length|).
2346 */
2347 size_t ByteLength();
2348 /**
2349 * Base address of typed array.
2350 */
2351 void* BaseAddress();
2352
2353 V8_INLINE(static TypedArray* Cast(Value* obj));
2354
2355 private:
2356 TypedArray();
2357 static void CheckCast(Value* obj);
2358};
2359
2360
2361/**
2362 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
2363 * This API is experimental and may change significantly.
2364 */
2365class V8EXPORT Uint8Array : public TypedArray {
2366 public:
2367 static Local<Uint8Array> New(Handle<ArrayBuffer> array_buffer,
2368 size_t byte_offset, size_t length);
2369 V8_INLINE(static Uint8Array* Cast(Value* obj));
2370
2371 private:
2372 Uint8Array();
2373 static void CheckCast(Value* obj);
2374};
2375
2376
2377/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002378 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
2379 * This API is experimental and may change significantly.
2380 */
2381class V8EXPORT Uint8ClampedArray : public TypedArray {
2382 public:
2383 static Local<Uint8ClampedArray> New(Handle<ArrayBuffer> array_buffer,
2384 size_t byte_offset, size_t length);
2385 V8_INLINE(static Uint8ClampedArray* Cast(Value* obj));
2386
2387 private:
2388 Uint8ClampedArray();
2389 static void CheckCast(Value* obj);
2390};
2391
2392/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002393 * An instance of Int8Array constructor (ES6 draft 15.13.6).
2394 * This API is experimental and may change significantly.
2395 */
2396class V8EXPORT Int8Array : public TypedArray {
2397 public:
2398 static Local<Int8Array> New(Handle<ArrayBuffer> array_buffer,
2399 size_t byte_offset, size_t length);
2400 V8_INLINE(static Int8Array* Cast(Value* obj));
2401
2402 private:
2403 Int8Array();
2404 static void CheckCast(Value* obj);
2405};
2406
2407
2408/**
2409 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
2410 * This API is experimental and may change significantly.
2411 */
2412class V8EXPORT Uint16Array : public TypedArray {
2413 public:
2414 static Local<Uint16Array> New(Handle<ArrayBuffer> array_buffer,
2415 size_t byte_offset, size_t length);
2416 V8_INLINE(static Uint16Array* Cast(Value* obj));
2417
2418 private:
2419 Uint16Array();
2420 static void CheckCast(Value* obj);
2421};
2422
2423
2424/**
2425 * An instance of Int16Array constructor (ES6 draft 15.13.6).
2426 * This API is experimental and may change significantly.
2427 */
2428class V8EXPORT Int16Array : public TypedArray {
2429 public:
2430 static Local<Int16Array> New(Handle<ArrayBuffer> array_buffer,
2431 size_t byte_offset, size_t length);
2432 V8_INLINE(static Int16Array* Cast(Value* obj));
2433
2434 private:
2435 Int16Array();
2436 static void CheckCast(Value* obj);
2437};
2438
2439
2440/**
2441 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
2442 * This API is experimental and may change significantly.
2443 */
2444class V8EXPORT Uint32Array : public TypedArray {
2445 public:
2446 static Local<Uint32Array> New(Handle<ArrayBuffer> array_buffer,
2447 size_t byte_offset, size_t length);
2448 V8_INLINE(static Uint32Array* Cast(Value* obj));
2449
2450 private:
2451 Uint32Array();
2452 static void CheckCast(Value* obj);
2453};
2454
2455
2456/**
2457 * An instance of Int32Array constructor (ES6 draft 15.13.6).
2458 * This API is experimental and may change significantly.
2459 */
2460class V8EXPORT Int32Array : public TypedArray {
2461 public:
2462 static Local<Int32Array> New(Handle<ArrayBuffer> array_buffer,
2463 size_t byte_offset, size_t length);
2464 V8_INLINE(static Int32Array* Cast(Value* obj));
2465
2466 private:
2467 Int32Array();
2468 static void CheckCast(Value* obj);
2469};
2470
2471
2472/**
2473 * An instance of Float32Array constructor (ES6 draft 15.13.6).
2474 * This API is experimental and may change significantly.
2475 */
2476class V8EXPORT Float32Array : public TypedArray {
2477 public:
2478 static Local<Float32Array> New(Handle<ArrayBuffer> array_buffer,
2479 size_t byte_offset, size_t length);
2480 V8_INLINE(static Float32Array* Cast(Value* obj));
2481
2482 private:
2483 Float32Array();
2484 static void CheckCast(Value* obj);
2485};
2486
2487
2488/**
2489 * An instance of Float64Array constructor (ES6 draft 15.13.6).
2490 * This API is experimental and may change significantly.
2491 */
2492class V8EXPORT Float64Array : public TypedArray {
2493 public:
2494 static Local<Float64Array> New(Handle<ArrayBuffer> array_buffer,
2495 size_t byte_offset, size_t length);
2496 V8_INLINE(static Float64Array* Cast(Value* obj));
2497
2498 private:
2499 Float64Array();
2500 static void CheckCast(Value* obj);
2501};
2502
2503
2504/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002505 * An instance of the built-in Date constructor (ECMA-262, 15.9).
2506 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002507class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002508 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002509 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002510
2511 /**
2512 * A specialization of Value::NumberValue that is more efficient
2513 * because we know the structure of this object.
2514 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002515 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002516
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002517 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002518
2519 /**
2520 * Notification that the embedder has changed the time zone,
2521 * daylight savings time, or other date / time configuration
2522 * parameters. V8 keeps a cache of various values used for
2523 * date / time computation. This notification will reset
2524 * those cached values for the current context so that date /
2525 * time configuration changes would be reflected in the Date
2526 * object.
2527 *
2528 * This API should not be called more than needed as it will
2529 * negatively impact the performance of date operations.
2530 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002531 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002532
2533 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002534 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002535};
2536
2537
2538/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002539 * A Number object (ECMA-262, 4.3.21).
2540 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002541class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002542 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002543 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002544
2545 /**
2546 * Returns the Number held by the object.
2547 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002548 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002549
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002550 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002551
2552 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002553 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002554};
2555
2556
2557/**
2558 * A Boolean object (ECMA-262, 4.3.15).
2559 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002560class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002561 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002562 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002563
2564 /**
2565 * Returns the Boolean held by the object.
2566 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002567 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002568
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002569 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002570
2571 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002572 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002573};
2574
2575
2576/**
2577 * A String object (ECMA-262, 4.3.18).
2578 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002579class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002580 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002581 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002582
2583 /**
2584 * Returns the String held by the object.
2585 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002586 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002587
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002588 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002589
2590 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002591 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002592};
2593
2594
2595/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002596 * A Symbol object (ECMA-262 edition 6).
2597 *
2598 * This is an experimental feature. Use at your own risk.
2599 */
2600class V8EXPORT SymbolObject : public Object {
2601 public:
2602 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2603
2604 /**
2605 * Returns the Symbol held by the object.
2606 */
2607 Local<Symbol> SymbolValue() const;
2608
2609 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2610
2611 private:
2612 static void CheckCast(v8::Value* obj);
2613};
2614
2615
2616/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002617 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2618 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002619class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002620 public:
2621 /**
2622 * Regular expression flag bits. They can be or'ed to enable a set
2623 * of flags.
2624 */
2625 enum Flags {
2626 kNone = 0,
2627 kGlobal = 1,
2628 kIgnoreCase = 2,
2629 kMultiline = 4
2630 };
2631
2632 /**
2633 * Creates a regular expression from the given pattern string and
2634 * the flags bit field. May throw a JavaScript exception as
2635 * described in ECMA-262, 15.10.4.1.
2636 *
2637 * For example,
2638 * RegExp::New(v8::String::New("foo"),
2639 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2640 * is equivalent to evaluating "/foo/gm".
2641 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002642 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002643
2644 /**
2645 * Returns the value of the source property: a string representing
2646 * the regular expression.
2647 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002648 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002649
2650 /**
2651 * Returns the flags bit field.
2652 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002653 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002654
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002655 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002656
2657 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002658 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002659};
2660
2661
2662/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002663 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2664 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002666class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002668 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002669 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002670 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002672 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673};
2674
2675
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002676// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677
2678
2679/**
2680 * The superclass of object and function templates.
2681 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002682class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683 public:
2684 /** Adds a property to each instance created by this template.*/
2685 void Set(Handle<String> name, Handle<Data> value,
2686 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002687 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 private:
2689 Template();
2690
2691 friend class ObjectTemplate;
2692 friend class FunctionTemplate;
2693};
2694
2695
2696/**
2697 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002698 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 * including the receiver, the number and values of arguments, and
2700 * the holder of the function.
2701 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002702class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002704 V8_INLINE(int Length() const);
2705 V8_INLINE(Local<Value> operator[](int i) const);
2706 V8_INLINE(Local<Function> Callee() const);
2707 V8_INLINE(Local<Object> This() const);
2708 V8_INLINE(Local<Object> Holder() const);
2709 V8_INLINE(bool IsConstructCall() const);
2710 V8_INLINE(Local<Value> Data() const);
2711 V8_INLINE(Isolate* GetIsolate() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002712
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002714 static const int kIsolateIndex = 0;
2715 static const int kDataIndex = -1;
2716 static const int kCalleeIndex = -2;
2717 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002718
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002719 friend class ImplementationUtilities;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002720 V8_INLINE(Arguments(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002721 internal::Object** values,
2722 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002723 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002724 internal::Object** implicit_args_;
2725 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002727 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728};
2729
2730
2731/**
2732 * The information passed to an accessor callback about the context
2733 * of the property access.
2734 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002735class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002736 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002737 V8_INLINE(AccessorInfo(internal::Object** args))
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002738 : args_(args) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002739 V8_INLINE(Isolate* GetIsolate() const);
2740 V8_INLINE(Local<Value> Data() const);
2741 V8_INLINE(Local<Object> This() const);
2742 V8_INLINE(Local<Object> Holder() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002745 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746};
2747
2748
2749typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2750
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752 * NamedProperty[Getter|Setter] are used as interceptors on object.
2753 * See ObjectTemplate::SetNamedPropertyHandler.
2754 */
2755typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2756 const AccessorInfo& info);
2757
2758
2759/**
2760 * Returns the value if the setter intercepts the request.
2761 * Otherwise, returns an empty handle.
2762 */
2763typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2764 Local<Value> value,
2765 const AccessorInfo& info);
2766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767/**
2768 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002769 * The result is an integer encoding property attributes (like v8::None,
2770 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002772typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2773 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774
2775
2776/**
2777 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002778 * The return value is true if the property could be deleted and false
2779 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780 */
2781typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2782 const AccessorInfo& info);
2783
2784/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002785 * Returns an array containing the names of the properties the named
2786 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787 */
2788typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2789
v8.team.kasperl727e9952008-09-02 14:56:44 +00002790
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002792 * Returns the value of the property if the getter intercepts the
2793 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002794 */
2795typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2796 const AccessorInfo& info);
2797
2798
2799/**
2800 * Returns the value if the setter intercepts the request.
2801 * Otherwise, returns an empty handle.
2802 */
2803typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2804 Local<Value> value,
2805 const AccessorInfo& info);
2806
2807
2808/**
2809 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002810 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002812typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2813 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002814
2815/**
2816 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002817 * The return value is true if the property could be deleted and false
2818 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819 */
2820typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2821 const AccessorInfo& info);
2822
v8.team.kasperl727e9952008-09-02 14:56:44 +00002823/**
2824 * Returns an array containing the indices of the properties the
2825 * indexed property getter intercepts.
2826 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002827typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2828
2829
2830/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002831 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832 */
2833enum AccessType {
2834 ACCESS_GET,
2835 ACCESS_SET,
2836 ACCESS_HAS,
2837 ACCESS_DELETE,
2838 ACCESS_KEYS
2839};
2840
v8.team.kasperl727e9952008-09-02 14:56:44 +00002841
2842/**
2843 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002844 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002845 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002846typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847 Local<Value> key,
2848 AccessType type,
2849 Local<Value> data);
2850
v8.team.kasperl727e9952008-09-02 14:56:44 +00002851
2852/**
2853 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002854 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002855 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002856typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002857 uint32_t index,
2858 AccessType type,
2859 Local<Value> data);
2860
2861
2862/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002863 * A FunctionTemplate is used to create functions at runtime. There
2864 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002865 * context. The lifetime of the created function is equal to the
2866 * lifetime of the context. So in case the embedder needs to create
2867 * temporary functions that can be collected using Scripts is
2868 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002869 *
2870 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002871 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002872 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002873 * A FunctionTemplate has a corresponding instance template which is
2874 * used to create object instances when the function is used as a
2875 * constructor. Properties added to the instance template are added to
2876 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877 *
2878 * A FunctionTemplate can have a prototype template. The prototype template
2879 * is used to create the prototype object of the function.
2880 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002881 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002882 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002883 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002884 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2885 * t->Set("func_property", v8::Number::New(1));
2886 *
2887 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2888 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2889 * proto_t->Set("proto_const", v8::Number::New(2));
2890 *
2891 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2892 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2893 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2894 * instance_t->Set("instance_property", Number::New(3));
2895 *
2896 * v8::Local<v8::Function> function = t->GetFunction();
2897 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002898 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002899 *
2900 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002901 * and "instance" for the instance object created above. The function
2902 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002904 * \code
2905 * func_property in function == true;
2906 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002907 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002908 * function.prototype.proto_method() invokes 'InvokeCallback'
2909 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002911 * instance instanceof function == true;
2912 * instance.instance_accessor calls 'InstanceAccessorCallback'
2913 * instance.instance_property == 3;
2914 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002916 * A FunctionTemplate can inherit from another one by calling the
2917 * FunctionTemplate::Inherit method. The following graph illustrates
2918 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002919 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002920 * \code
2921 * FunctionTemplate Parent -> Parent() . prototype -> { }
2922 * ^ ^
2923 * | Inherit(Parent) | .__proto__
2924 * | |
2925 * FunctionTemplate Child -> Child() . prototype -> { }
2926 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002927 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002928 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2929 * object of the Child() function has __proto__ pointing to the
2930 * Parent() function's prototype object. An instance of the Child
2931 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002932 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002933 * Let Parent be the FunctionTemplate initialized in the previous
2934 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002936 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937 * Local<FunctionTemplate> parent = t;
2938 * Local<FunctionTemplate> child = FunctionTemplate::New();
2939 * child->Inherit(parent);
2940 *
2941 * Local<Function> child_function = child->GetFunction();
2942 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002943 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002945 * The Child function and Child instance will have the following
2946 * properties:
2947 *
2948 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002949 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002950 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002952 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002953 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002954class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955 public:
2956 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002957 static Local<FunctionTemplate> New(
2958 InvocationCallback callback = 0,
2959 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002960 Handle<Signature> signature = Handle<Signature>(),
2961 int length = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002962 /** Returns the unique function instance in the current execution context.*/
2963 Local<Function> GetFunction();
2964
v8.team.kasperl727e9952008-09-02 14:56:44 +00002965 /**
2966 * Set the call-handler callback for a FunctionTemplate. This
2967 * callback is called whenever the function created from this
2968 * FunctionTemplate is called.
2969 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970 void SetCallHandler(InvocationCallback callback,
2971 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002972
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002973 /** Set the predefined length property for the FunctionTemplate. */
2974 void SetLength(int length);
2975
v8.team.kasperl727e9952008-09-02 14:56:44 +00002976 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002977 Local<ObjectTemplate> InstanceTemplate();
2978
2979 /** Causes the function template to inherit from a parent function template.*/
2980 void Inherit(Handle<FunctionTemplate> parent);
2981
2982 /**
2983 * A PrototypeTemplate is the template used to create the prototype object
2984 * of the function created by this template.
2985 */
2986 Local<ObjectTemplate> PrototypeTemplate();
2987
v8.team.kasperl727e9952008-09-02 14:56:44 +00002988 /**
2989 * Set the class name of the FunctionTemplate. This is used for
2990 * printing objects created with the function created from the
2991 * FunctionTemplate as its constructor.
2992 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993 void SetClassName(Handle<String> name);
2994
2995 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002996 * Determines whether the __proto__ accessor ignores instances of
2997 * the function template. If instances of the function template are
2998 * ignored, __proto__ skips all instances and instead returns the
2999 * next object in the prototype chain.
3000 *
3001 * Call with a value of true to make the __proto__ accessor ignore
3002 * instances of the function template. Call with a value of false
3003 * to make the __proto__ accessor not ignore instances of the
3004 * function template. By default, instances of a function template
3005 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003006 */
3007 void SetHiddenPrototype(bool value);
3008
3009 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003010 * Sets the ReadOnly flag in the attributes of the 'prototype' property
3011 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00003012 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003013 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00003014
3015 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003016 * Returns true if the given object is an instance of this function
3017 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003018 */
3019 bool HasInstance(Handle<Value> object);
3020
3021 private:
3022 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
3024 NamedPropertySetter setter,
3025 NamedPropertyQuery query,
3026 NamedPropertyDeleter remover,
3027 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003028 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003029 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
3030 IndexedPropertySetter setter,
3031 IndexedPropertyQuery query,
3032 IndexedPropertyDeleter remover,
3033 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003034 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003035 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
3036 Handle<Value> data);
3037
3038 friend class Context;
3039 friend class ObjectTemplate;
3040};
3041
3042
3043/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003044 * An ObjectTemplate is used to create objects at runtime.
3045 *
3046 * Properties added to an ObjectTemplate are added to each object
3047 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003048 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003049class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003051 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003053
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003054 /** Creates a new instance of this template.*/
3055 Local<Object> NewInstance();
3056
3057 /**
3058 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003059 *
3060 * Whenever the property with the given name is accessed on objects
3061 * created from this ObjectTemplate the getter and setter callbacks
3062 * are called instead of getting and setting the property directly
3063 * on the JavaScript object.
3064 *
3065 * \param name The name of the property for which an accessor is added.
3066 * \param getter The callback to invoke when getting the property.
3067 * \param setter The callback to invoke when setting the property.
3068 * \param data A piece of data that will be passed to the getter and setter
3069 * callbacks whenever they are invoked.
3070 * \param settings Access control settings for the accessor. This is a bit
3071 * field consisting of one of more of
3072 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
3073 * The default is to not allow cross-context access.
3074 * ALL_CAN_READ means that all cross-context reads are allowed.
3075 * ALL_CAN_WRITE means that all cross-context writes are allowed.
3076 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
3077 * cross-context access.
3078 * \param attribute The attributes of the property for which an accessor
3079 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003080 * \param signature The signature describes valid receivers for the accessor
3081 * and is used to perform implicit instance checks against them. If the
3082 * receiver is incompatible (i.e. is not an instance of the constructor as
3083 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
3084 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085 */
3086 void SetAccessor(Handle<String> name,
3087 AccessorGetter getter,
3088 AccessorSetter setter = 0,
3089 Handle<Value> data = Handle<Value>(),
3090 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003091 PropertyAttribute attribute = None,
3092 Handle<AccessorSignature> signature =
3093 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003094
ulan@chromium.org750145a2013-03-07 15:14:13 +00003095 // This function is not yet stable and should not be used at this time.
3096 bool SetAccessor(Handle<String> name,
3097 Handle<DeclaredAccessorDescriptor> descriptor,
3098 AccessControl settings = DEFAULT,
3099 PropertyAttribute attribute = None,
3100 Handle<AccessorSignature> signature =
3101 Handle<AccessorSignature>());
3102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003103 /**
3104 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003105 *
3106 * Whenever a named property is accessed on objects created from
3107 * this object template, the provided callback is invoked instead of
3108 * accessing the property directly on the JavaScript object.
3109 *
3110 * \param getter The callback to invoke when getting a property.
3111 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003112 * \param query The callback to invoke to check if a property is present,
3113 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003114 * \param deleter The callback to invoke when deleting a property.
3115 * \param enumerator The callback to invoke to enumerate all the named
3116 * properties of an object.
3117 * \param data A piece of data that will be passed to the callbacks
3118 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003119 */
3120 void SetNamedPropertyHandler(NamedPropertyGetter getter,
3121 NamedPropertySetter setter = 0,
3122 NamedPropertyQuery query = 0,
3123 NamedPropertyDeleter deleter = 0,
3124 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003125 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003126
3127 /**
3128 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003129 *
3130 * Whenever an indexed property is accessed on objects created from
3131 * this object template, the provided callback is invoked instead of
3132 * accessing the property directly on the JavaScript object.
3133 *
3134 * \param getter The callback to invoke when getting a property.
3135 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003136 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003137 * \param deleter The callback to invoke when deleting a property.
3138 * \param enumerator The callback to invoke to enumerate all the indexed
3139 * properties of an object.
3140 * \param data A piece of data that will be passed to the callbacks
3141 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142 */
3143 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
3144 IndexedPropertySetter setter = 0,
3145 IndexedPropertyQuery query = 0,
3146 IndexedPropertyDeleter deleter = 0,
3147 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003148 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003149
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003150 /**
3151 * Sets the callback to be used when calling instances created from
3152 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00003153 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003154 * function.
3155 */
3156 void SetCallAsFunctionHandler(InvocationCallback callback,
3157 Handle<Value> data = Handle<Value>());
3158
v8.team.kasperl727e9952008-09-02 14:56:44 +00003159 /**
3160 * Mark object instances of the template as undetectable.
3161 *
3162 * In many ways, undetectable objects behave as though they are not
3163 * there. They behave like 'undefined' in conditionals and when
3164 * printed. However, properties can be accessed and called as on
3165 * normal objects.
3166 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003167 void MarkAsUndetectable();
3168
v8.team.kasperl727e9952008-09-02 14:56:44 +00003169 /**
3170 * Sets access check callbacks on the object template.
3171 *
3172 * When accessing properties on instances of this object template,
3173 * the access check callback will be called to determine whether or
3174 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003175 * The last parameter specifies whether access checks are turned
3176 * on by default on instances. If access checks are off by default,
3177 * they can be turned on on individual instances by calling
3178 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00003179 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003180 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
3181 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003182 Handle<Value> data = Handle<Value>(),
3183 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003184
kasper.lund212ac232008-07-16 07:07:30 +00003185 /**
3186 * Gets the number of internal fields for objects generated from
3187 * this template.
3188 */
3189 int InternalFieldCount();
3190
3191 /**
3192 * Sets the number of internal fields for objects generated from
3193 * this template.
3194 */
3195 void SetInternalFieldCount(int value);
3196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003197 private:
3198 ObjectTemplate();
3199 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
3200 friend class FunctionTemplate;
3201};
3202
3203
3204/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003205 * A Signature specifies which receivers and arguments are valid
3206 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003208class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003209 public:
3210 static Local<Signature> New(Handle<FunctionTemplate> receiver =
3211 Handle<FunctionTemplate>(),
3212 int argc = 0,
3213 Handle<FunctionTemplate> argv[] = 0);
3214 private:
3215 Signature();
3216};
3217
3218
3219/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003220 * An AccessorSignature specifies which receivers are valid parameters
3221 * to an accessor callback.
3222 */
3223class V8EXPORT AccessorSignature : public Data {
3224 public:
3225 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
3226 Handle<FunctionTemplate>());
3227 private:
3228 AccessorSignature();
3229};
3230
3231
ulan@chromium.org750145a2013-03-07 15:14:13 +00003232class V8EXPORT DeclaredAccessorDescriptor : public Data {
3233 private:
3234 DeclaredAccessorDescriptor();
3235};
3236
3237
3238class V8EXPORT ObjectOperationDescriptor : public Data {
3239 public:
3240 // This function is not yet stable and should not be used at this time.
3241 static Local<RawOperationDescriptor> NewInternalFieldDereference(
3242 Isolate* isolate,
3243 int internal_field);
3244 private:
3245 ObjectOperationDescriptor();
3246};
3247
3248
3249enum DeclaredAccessorDescriptorDataType {
3250 kDescriptorBoolType,
3251 kDescriptorInt8Type, kDescriptorUint8Type,
3252 kDescriptorInt16Type, kDescriptorUint16Type,
3253 kDescriptorInt32Type, kDescriptorUint32Type,
3254 kDescriptorFloatType, kDescriptorDoubleType
3255};
3256
3257
3258class V8EXPORT RawOperationDescriptor : public Data {
3259 public:
3260 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
3261 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
3262 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
3263 int16_t byte_offset);
3264 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
3265 void* compare_value);
3266 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
3267 Isolate* isolate,
3268 DeclaredAccessorDescriptorDataType data_type,
3269 uint8_t bool_offset = 0);
3270 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
3271 uint8_t bitmask,
3272 uint8_t compare_value);
3273 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
3274 Isolate* isolate,
3275 uint16_t bitmask,
3276 uint16_t compare_value);
3277 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
3278 Isolate* isolate,
3279 uint32_t bitmask,
3280 uint32_t compare_value);
3281
3282 private:
3283 RawOperationDescriptor();
3284};
3285
3286
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003287/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003288 * A utility for determining the type of objects based on the template
3289 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003290 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003291class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003292 public:
3293 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
3294 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
3295 int match(Handle<Value> value);
3296 private:
3297 TypeSwitch();
3298};
3299
3300
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003301// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003302
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003303class V8EXPORT ExternalAsciiStringResourceImpl
3304 : public String::ExternalAsciiStringResource {
3305 public:
3306 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
3307 ExternalAsciiStringResourceImpl(const char* data, size_t length)
3308 : data_(data), length_(length) {}
3309 const char* data() const { return data_; }
3310 size_t length() const { return length_; }
3311
3312 private:
3313 const char* data_;
3314 size_t length_;
3315};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003316
3317/**
3318 * Ignore
3319 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003320class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003321 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003322 // Note that the strings passed into this constructor must live as long
3323 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003324 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003325 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003326 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003327 const char** deps = 0,
3328 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329 virtual ~Extension() { }
3330 virtual v8::Handle<v8::FunctionTemplate>
3331 GetNativeFunction(v8::Handle<v8::String> name) {
3332 return v8::Handle<v8::FunctionTemplate>();
3333 }
3334
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003335 const char* name() const { return name_; }
3336 size_t source_length() const { return source_length_; }
3337 const String::ExternalAsciiStringResource* source() const {
3338 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003339 int dependency_count() { return dep_count_; }
3340 const char** dependencies() { return deps_; }
3341 void set_auto_enable(bool value) { auto_enable_ = value; }
3342 bool auto_enable() { return auto_enable_; }
3343
3344 private:
3345 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003346 size_t source_length_; // expected to initialize before source_
3347 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003348 int dep_count_;
3349 const char** deps_;
3350 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003351
3352 // Disallow copying and assigning.
3353 Extension(const Extension&);
3354 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003355};
3356
3357
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003358void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359
3360
3361/**
3362 * Ignore
3363 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003364class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003366 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003367 RegisterExtension(extension);
3368 }
3369};
3370
3371
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003372// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003373
3374
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003375Handle<Primitive> V8EXPORT Undefined();
3376Handle<Primitive> V8EXPORT Null();
3377Handle<Boolean> V8EXPORT True();
3378Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003379
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003380V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
3381V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
3382V8_INLINE(Handle<Boolean> True(Isolate* isolate));
3383V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003384
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003385
3386/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003387 * A set of constraints that specifies the limits of the runtime's memory use.
3388 * You must set the heap size before initializing the VM - the size cannot be
3389 * adjusted after the VM is initialized.
3390 *
3391 * If you are using threads then you should hold the V8::Locker lock while
3392 * setting the stack limit and you must set a non-default stack limit separately
3393 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003395class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003396 public:
3397 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003398 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003399 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003400 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003401 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003402 int max_executable_size() { return max_executable_size_; }
3403 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003404 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003405 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
3407 private:
3408 int max_young_space_size_;
3409 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003410 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003411 uint32_t* stack_limit_;
3412};
3413
3414
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003415bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003416
3417
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003418// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419
3420
3421typedef void (*FatalErrorCallback)(const char* location, const char* message);
3422
3423
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003424typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003425
3426
3427/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003428 * Schedules an exception to be thrown when returning to JavaScript. When an
3429 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003430 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00003431 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003432 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003433Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003434
3435/**
3436 * Create new error objects by calling the corresponding error object
3437 * constructor with the message.
3438 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003439class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003440 public:
3441 static Local<Value> RangeError(Handle<String> message);
3442 static Local<Value> ReferenceError(Handle<String> message);
3443 static Local<Value> SyntaxError(Handle<String> message);
3444 static Local<Value> TypeError(Handle<String> message);
3445 static Local<Value> Error(Handle<String> message);
3446};
3447
3448
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003449// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003450
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003451typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003452
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003453typedef void* (*CreateHistogramCallback)(const char* name,
3454 int min,
3455 int max,
3456 size_t buckets);
3457
3458typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
3459
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003460// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003461 enum ObjectSpace {
3462 kObjectSpaceNewSpace = 1 << 0,
3463 kObjectSpaceOldPointerSpace = 1 << 1,
3464 kObjectSpaceOldDataSpace = 1 << 2,
3465 kObjectSpaceCodeSpace = 1 << 3,
3466 kObjectSpaceMapSpace = 1 << 4,
3467 kObjectSpaceLoSpace = 1 << 5,
3468
3469 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
3470 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
3471 kObjectSpaceLoSpace
3472 };
3473
3474 enum AllocationAction {
3475 kAllocationActionAllocate = 1 << 0,
3476 kAllocationActionFree = 1 << 1,
3477 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
3478 };
3479
3480typedef void (*MemoryAllocationCallback)(ObjectSpace space,
3481 AllocationAction action,
3482 int size);
3483
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003484// --- Leave Script Callback ---
3485typedef void (*CallCompletedCallback)();
3486
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003487// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488typedef void (*FailedAccessCheckCallback)(Local<Object> target,
3489 AccessType type,
3490 Local<Value> data);
3491
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003492// --- AllowCodeGenerationFromStrings callbacks ---
3493
3494/**
3495 * Callback to check if code generation from strings is allowed. See
3496 * Context::AllowCodeGenerationFromStrings.
3497 */
3498typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
3499
3500// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003501
3502/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003503 * Applications can register callback functions which will be called
3504 * before and after a garbage collection. Allocations are not
3505 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003506 * objects (set or delete properties for example) since it is possible
3507 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003508 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003509enum GCType {
3510 kGCTypeScavenge = 1 << 0,
3511 kGCTypeMarkSweepCompact = 1 << 1,
3512 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
3513};
3514
3515enum GCCallbackFlags {
3516 kNoGCCallbackFlags = 0,
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003517 kGCCallbackFlagCompacted = 1 << 0,
3518 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003519};
3520
3521typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
3522typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
3523
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003524typedef void (*GCCallback)();
3525
3526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003527/**
ager@chromium.org3811b432009-10-28 14:53:37 +00003528 * Collection of V8 heap information.
3529 *
3530 * Instances of this class can be passed to v8::V8::HeapStatistics to
3531 * get heap statistics from V8.
3532 */
3533class V8EXPORT HeapStatistics {
3534 public:
3535 HeapStatistics();
3536 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003537 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003538 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003539 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003540 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003541
3542 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003543 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003544 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003545 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003546 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003547 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003548
3549 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003550 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003551};
3552
3553
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003554class RetainedObjectInfo;
3555
ager@chromium.org3811b432009-10-28 14:53:37 +00003556/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003557 * Isolate represents an isolated instance of the V8 engine. V8
3558 * isolates have completely separate states. Objects from one isolate
3559 * must not be used in other isolates. When V8 is initialized a
3560 * default isolate is implicitly created and entered. The embedder
3561 * can create additional isolates and use them in parallel in multiple
3562 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003563 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003564 */
3565class V8EXPORT Isolate {
3566 public:
3567 /**
3568 * Stack-allocated class which sets the isolate for all operations
3569 * executed within a local scope.
3570 */
3571 class V8EXPORT Scope {
3572 public:
3573 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3574 isolate->Enter();
3575 }
3576
3577 ~Scope() { isolate_->Exit(); }
3578
3579 private:
3580 Isolate* const isolate_;
3581
3582 // Prevent copying of Scope objects.
3583 Scope(const Scope&);
3584 Scope& operator=(const Scope&);
3585 };
3586
3587 /**
3588 * Creates a new isolate. Does not change the currently entered
3589 * isolate.
3590 *
3591 * When an isolate is no longer used its resources should be freed
3592 * by calling Dispose(). Using the delete operator is not allowed.
3593 */
3594 static Isolate* New();
3595
3596 /**
3597 * Returns the entered isolate for the current thread or NULL in
3598 * case there is no current isolate.
3599 */
3600 static Isolate* GetCurrent();
3601
3602 /**
3603 * Methods below this point require holding a lock (using Locker) in
3604 * a multi-threaded environment.
3605 */
3606
3607 /**
3608 * Sets this isolate as the entered one for the current thread.
3609 * Saves the previously entered one (if any), so that it can be
3610 * restored when exiting. Re-entering an isolate is allowed.
3611 */
3612 void Enter();
3613
3614 /**
3615 * Exits this isolate by restoring the previously entered one in the
3616 * current thread. The isolate may still stay the same, if it was
3617 * entered more than once.
3618 *
3619 * Requires: this == Isolate::GetCurrent().
3620 */
3621 void Exit();
3622
3623 /**
3624 * Disposes the isolate. The isolate must not be entered by any
3625 * thread to be disposable.
3626 */
3627 void Dispose();
3628
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003629 /**
3630 * Associate embedder-specific data with the isolate
3631 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003632 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003633
3634 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003635 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003636 * Returns NULL if SetData has never been called.
3637 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003638 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003639
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003640 /**
3641 * Get statistics about the heap memory usage.
3642 */
3643 void GetHeapStatistics(HeapStatistics* heap_statistics);
3644
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003645 /**
3646 * Adjusts the amount of registered external memory. Used to give V8 an
3647 * indication of the amount of externally allocated memory that is kept alive
3648 * by JavaScript objects. V8 uses this to decide when to perform global
3649 * garbage collections. Registering externally allocated memory will trigger
3650 * global garbage collections more often than it would otherwise in an attempt
3651 * to garbage collect the JavaScript objects that keep the externally
3652 * allocated memory alive.
3653 *
3654 * \param change_in_bytes the change in externally allocated memory that is
3655 * kept alive by JavaScript objects.
3656 * \returns the adjusted value.
3657 */
3658 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3659
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003660 /**
3661 * Returns heap profiler for this isolate. Will return NULL until the isolate
3662 * is initialized.
3663 */
3664 HeapProfiler* GetHeapProfiler();
3665
3666 /**
3667 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3668 * is initialized.
3669 */
3670 CpuProfiler* GetCpuProfiler();
3671
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003672 /** Returns the context that is on the top of the stack. */
3673 Local<Context> GetCurrentContext();
3674
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003675 /**
3676 * Allows the host application to group objects together. If one
3677 * object in the group is alive, all objects in the group are alive.
3678 * After each garbage collection, object groups are removed. It is
3679 * intended to be used in the before-garbage-collection callback
3680 * function, for instance to simulate DOM tree connections among JS
3681 * wrapper objects. Object groups for all dependent handles need to
3682 * be provided for kGCTypeMarkSweepCompact collections, for all other
3683 * garbage collection types it is sufficient to provide object groups
3684 * for partially dependent handles only.
3685 */
3686 void SetObjectGroupId(const Persistent<Value>& object,
3687 UniqueId id);
3688
3689 /**
3690 * Allows the host application to declare implicit references from an object
3691 * group to an object. If the objects of the object group are alive, the child
3692 * object is alive too. After each garbage collection, all implicit references
3693 * are removed. It is intended to be used in the before-garbage-collection
3694 * callback function.
3695 */
3696 void SetReferenceFromGroup(UniqueId id,
3697 const Persistent<Value>& child);
3698
3699 /**
3700 * Allows the host application to declare implicit references from an object
3701 * to another object. If the parent object is alive, the child object is alive
3702 * too. After each garbage collection, all implicit references are removed. It
3703 * is intended to be used in the before-garbage-collection callback function.
3704 */
3705 void SetReference(const Persistent<Object>& parent,
3706 const Persistent<Value>& child);
3707
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003708 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003709 Isolate();
3710 Isolate(const Isolate&);
3711 ~Isolate();
3712 Isolate& operator=(const Isolate&);
3713 void* operator new(size_t size);
3714 void operator delete(void*, size_t);
3715};
3716
3717
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003718class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003719 public:
3720 enum CompressionAlgorithm {
3721 kUncompressed,
3722 kBZip2
3723 };
3724
3725 const char* data;
3726 int compressed_size;
3727 int raw_size;
3728};
3729
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003730
3731/**
3732 * A helper class for driving V8 startup data decompression. It is based on
3733 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3734 * for an embedder to use this class, instead, API functions can be used
3735 * directly.
3736 *
3737 * For an example of the class usage, see the "shell.cc" sample application.
3738 */
3739class V8EXPORT StartupDataDecompressor { // NOLINT
3740 public:
3741 StartupDataDecompressor();
3742 virtual ~StartupDataDecompressor();
3743 int Decompress();
3744
3745 protected:
3746 virtual int DecompressData(char* raw_data,
3747 int* raw_data_size,
3748 const char* compressed_data,
3749 int compressed_data_size) = 0;
3750
3751 private:
3752 char** raw_data;
3753};
3754
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003755
3756/**
3757 * EntropySource is used as a callback function when v8 needs a source
3758 * of entropy.
3759 */
3760typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3761
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003762
3763/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003764 * ReturnAddressLocationResolver is used as a callback function when v8 is
3765 * resolving the location of a return address on the stack. Profilers that
3766 * change the return address on the stack can use this to resolve the stack
3767 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003768 *
3769 * \param return_addr_location points to a location on stack where a machine
3770 * return address resides.
3771 * \returns either return_addr_location, or else a pointer to the profiler's
3772 * copy of the original return address.
3773 *
3774 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003775 */
3776typedef uintptr_t (*ReturnAddressLocationResolver)(
3777 uintptr_t return_addr_location);
3778
3779
3780/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003781 * FunctionEntryHook is the type of the profile entry hook called at entry to
3782 * any generated function when function-level profiling is enabled.
3783 *
3784 * \param function the address of the function that's being entered.
3785 * \param return_addr_location points to a location on stack where the machine
3786 * return address resides. This can be used to identify the caller of
3787 * \p function, and/or modified to divert execution when \p function exits.
3788 *
3789 * \note the entry hook must not cause garbage collection.
3790 */
3791typedef void (*FunctionEntryHook)(uintptr_t function,
3792 uintptr_t return_addr_location);
3793
3794
3795/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003796 * A JIT code event is issued each time code is added, moved or removed.
3797 *
3798 * \note removal events are not currently issued.
3799 */
3800struct JitCodeEvent {
3801 enum EventType {
3802 CODE_ADDED,
3803 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003804 CODE_REMOVED,
3805 CODE_ADD_LINE_POS_INFO,
3806 CODE_START_LINE_INFO_RECORDING,
3807 CODE_END_LINE_INFO_RECORDING
3808 };
3809 // Definition of the code position type. The "POSITION" type means the place
3810 // in the source code which are of interest when making stack traces to
3811 // pin-point the source location of a stack frame as close as possible.
3812 // The "STATEMENT_POSITION" means the place at the beginning of each
3813 // statement, and is used to indicate possible break locations.
3814 enum PositionType {
3815 POSITION,
3816 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003817 };
3818
3819 // Type of event.
3820 EventType type;
3821 // Start of the instructions.
3822 void* code_start;
3823 // Size of the instructions.
3824 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003825 // Script info for CODE_ADDED event.
3826 Handle<Script> script;
3827 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
3828 // code line information which is returned from the
3829 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
3830 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
3831 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003832
3833 union {
3834 // Only valid for CODE_ADDED.
3835 struct {
3836 // Name of the object associated with the code, note that the string is
3837 // not zero-terminated.
3838 const char* str;
3839 // Number of chars in str.
3840 size_t len;
3841 } name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003842
3843 // Only valid for CODE_ADD_LINE_POS_INFO
3844 struct {
3845 // PC offset
3846 size_t offset;
3847 // Code postion
3848 size_t pos;
3849 // The position type.
3850 PositionType position_type;
3851 } line_info;
3852
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003853 // New location of instructions. Only valid for CODE_MOVED.
3854 void* new_code_start;
3855 };
3856};
3857
3858/**
3859 * Option flags passed to the SetJitCodeEventHandler function.
3860 */
3861enum JitCodeEventOptions {
3862 kJitCodeEventDefault = 0,
3863 // Generate callbacks for already existent code.
3864 kJitCodeEventEnumExisting = 1
3865};
3866
3867
3868/**
3869 * Callback function passed to SetJitCodeEventHandler.
3870 *
3871 * \param event code add, move or removal event.
3872 */
3873typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3874
3875
3876/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003877 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003878 */
3879class V8EXPORT ExternalResourceVisitor { // NOLINT
3880 public:
3881 virtual ~ExternalResourceVisitor() {}
3882 virtual void VisitExternalString(Handle<String> string) {}
3883};
3884
3885
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003886/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003887 * Interface for iterating through all the persistent handles in the heap.
3888 */
3889class V8EXPORT PersistentHandleVisitor { // NOLINT
3890 public:
3891 virtual ~PersistentHandleVisitor() {}
3892 virtual void VisitPersistentHandle(Persistent<Value> value,
3893 uint16_t class_id) {}
3894};
3895
3896
3897/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00003898 * Asserts that no action is performed that could cause a handle's value
3899 * to be modified. Useful when otherwise unsafe handle operations need to
3900 * be performed.
3901 */
3902class V8EXPORT AssertNoGCScope {
3903#ifndef DEBUG
3904 V8_INLINE(AssertNoGCScope(Isolate* isolate)) {}
3905#else
3906 AssertNoGCScope(Isolate* isolate);
3907 ~AssertNoGCScope();
3908 private:
3909 Isolate* isolate_;
3910 bool last_state_;
3911#endif
3912};
3913
3914
3915/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003916 * Container class for static utility functions.
3917 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003918class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003920 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003921 static void SetFatalErrorHandler(FatalErrorCallback that);
3922
v8.team.kasperl727e9952008-09-02 14:56:44 +00003923 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003924 * Set the callback to invoke to check if code generation from
3925 * strings should be allowed.
3926 */
3927 static void SetAllowCodeGenerationFromStringsCallback(
3928 AllowCodeGenerationFromStringsCallback that);
3929
3930 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003931 * Ignore out-of-memory exceptions.
3932 *
3933 * V8 running out of memory is treated as a fatal error by default.
3934 * This means that the fatal error handler is called and that V8 is
3935 * terminated.
3936 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003937 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003938 * out-of-memory situation as a fatal error. This way, the contexts
3939 * that did not cause the out of memory problem might be able to
3940 * continue execution.
3941 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003942 static void IgnoreOutOfMemoryException();
3943
v8.team.kasperl727e9952008-09-02 14:56:44 +00003944 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003945 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003946 * fatal errors such as out-of-memory situations.
3947 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003948 static bool IsDead();
3949
3950 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003951 * The following 4 functions are to be used when V8 is built with
3952 * the 'compress_startup_data' flag enabled. In this case, the
3953 * embedder must decompress startup data prior to initializing V8.
3954 *
3955 * This is how interaction with V8 should look like:
3956 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3957 * v8::StartupData* compressed_data =
3958 * new v8::StartupData[compressed_data_count];
3959 * v8::V8::GetCompressedStartupData(compressed_data);
3960 * ... decompress data (compressed_data can be updated in-place) ...
3961 * v8::V8::SetDecompressedStartupData(compressed_data);
3962 * ... now V8 can be initialized
3963 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003964 *
3965 * A helper class StartupDataDecompressor is provided. It implements
3966 * the protocol of the interaction described above, and can be used in
3967 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003968 */
3969 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3970 static int GetCompressedStartupDataCount();
3971 static void GetCompressedStartupData(StartupData* compressed_data);
3972 static void SetDecompressedStartupData(StartupData* decompressed_data);
3973
3974 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003975 * Adds a message listener.
3976 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003977 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003978 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003979 *
3980 * If data is specified, it will be passed to the callback when it is called.
3981 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003983 static bool AddMessageListener(MessageCallback that,
3984 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003985
3986 /**
3987 * Remove all message listeners from the specified callback function.
3988 */
3989 static void RemoveMessageListeners(MessageCallback that);
3990
3991 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003992 * Tells V8 to capture current stack trace when uncaught exception occurs
3993 * and report it to the message listeners. The option is off by default.
3994 */
3995 static void SetCaptureStackTraceForUncaughtExceptions(
3996 bool capture,
3997 int frame_limit = 10,
3998 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3999
4000 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004001 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004002 */
4003 static void SetFlagsFromString(const char* str, int length);
4004
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004005 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004006 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004007 */
4008 static void SetFlagsFromCommandLine(int* argc,
4009 char** argv,
4010 bool remove_flags);
4011
kasper.lund7276f142008-07-30 08:49:36 +00004012 /** Get the version string. */
4013 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004014
4015 /**
4016 * Enables the host application to provide a mechanism for recording
4017 * statistics counters.
4018 */
4019 static void SetCounterFunction(CounterLookupCallback);
4020
4021 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004022 * Enables the host application to provide a mechanism for recording
4023 * histograms. The CreateHistogram function returns a
4024 * histogram which will later be passed to the AddHistogramSample
4025 * function.
4026 */
4027 static void SetCreateHistogramFunction(CreateHistogramCallback);
4028 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
4029
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004030 /** Callback function for reporting failed access checks.*/
4031 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
4032
4033 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004034 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004035 * garbage collection. Allocations are not allowed in the
4036 * callback function, you therefore cannot manipulate objects (set
4037 * or delete properties for example) since it is possible such
4038 * operations will result in the allocation of objects. It is possible
4039 * to specify the GCType filter for your callback. But it is not possible to
4040 * register the same callback function two times with different
4041 * GCType filters.
4042 */
4043 static void AddGCPrologueCallback(
4044 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
4045
4046 /**
4047 * This function removes callback which was installed by
4048 * AddGCPrologueCallback function.
4049 */
4050 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
4051
4052 /**
4053 * The function is deprecated. Please use AddGCPrologueCallback instead.
4054 * Enables the host application to receive a notification before a
4055 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00004056 * callback function, you therefore cannot manipulate objects (set
4057 * or delete properties for example) since it is possible such
4058 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004059 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004060 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004061
4062 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004063 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004064 * garbage collection. Allocations are not allowed in the
4065 * callback function, you therefore cannot manipulate objects (set
4066 * or delete properties for example) since it is possible such
4067 * operations will result in the allocation of objects. It is possible
4068 * to specify the GCType filter for your callback. But it is not possible to
4069 * register the same callback function two times with different
4070 * GCType filters.
4071 */
4072 static void AddGCEpilogueCallback(
4073 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
4074
4075 /**
4076 * This function removes callback which was installed by
4077 * AddGCEpilogueCallback function.
4078 */
4079 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
4080
4081 /**
4082 * The function is deprecated. Please use AddGCEpilogueCallback instead.
4083 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00004084 * major garbage collection. Allocations are not allowed in the
4085 * callback function, you therefore cannot manipulate objects (set
4086 * or delete properties for example) since it is possible such
4087 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004088 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004089 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090
4091 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004092 * Enables the host application to provide a mechanism to be notified
4093 * and perform custom logging when V8 Allocates Executable Memory.
4094 */
4095 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
4096 ObjectSpace space,
4097 AllocationAction action);
4098
4099 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004100 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004101 */
4102 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
4103
4104 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004105 * Adds a callback to notify the host application when a script finished
4106 * running. If a script re-enters the runtime during executing, the
4107 * CallCompletedCallback is only invoked when the outer-most script
4108 * execution ends. Executing scripts inside the callback do not trigger
4109 * further callbacks.
4110 */
4111 static void AddCallCompletedCallback(CallCompletedCallback callback);
4112
4113 /**
4114 * Removes callback that was installed by AddCallCompletedCallback.
4115 */
4116 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
4117
4118 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004119 * Allows the host application to group objects together. If one
4120 * object in the group is alive, all objects in the group are alive.
4121 * After each garbage collection, object groups are removed. It is
4122 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00004123 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004124 * wrapper objects. Object groups for all dependent handles need to
4125 * be provided for kGCTypeMarkSweepCompact collections, for all other
4126 * garbage collection types it is sufficient to provide object groups
4127 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004128 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004129 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004130 // TODO(marja): deprecate AddObjectGroup. Use Isolate::SetObjectGroupId and
4131 // HeapProfiler::SetRetainedObjectInfo instead.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004132 static void AddObjectGroup(Persistent<Value>* objects,
4133 size_t length,
4134 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004135 static void AddObjectGroup(Isolate* isolate,
4136 Persistent<Value>* objects,
4137 size_t length,
4138 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004139
4140 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004141 * Allows the host application to declare implicit references between
4142 * the objects: if |parent| is alive, all |children| are alive too.
4143 * After each garbage collection, all implicit references
4144 * are removed. It is intended to be used in the before-garbage-collection
4145 * callback function.
4146 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004147 // TODO(marja): Deprecate AddImplicitReferences. Use
4148 // Isolate::SetReferenceFromGroup instead.
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004149 static void AddImplicitReferences(Persistent<Object> parent,
4150 Persistent<Value>* children,
4151 size_t length);
4152
4153 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004154 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004155 * initialize from scratch. This function is called implicitly if
4156 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004157 */
4158 static bool Initialize();
4159
kasper.lund7276f142008-07-30 08:49:36 +00004160 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004161 * Allows the host application to provide a callback which can be used
4162 * as a source of entropy for random number generators.
4163 */
4164 static void SetEntropySource(EntropySource source);
4165
4166 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004167 * Allows the host application to provide a callback that allows v8 to
4168 * cooperate with a profiler that rewrites return addresses on stack.
4169 */
4170 static void SetReturnAddressLocationResolver(
4171 ReturnAddressLocationResolver return_address_resolver);
4172
4173 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004174 * Allows the host application to provide the address of a function that's
4175 * invoked on entry to every V8-generated function.
4176 * Note that \p entry_hook is invoked at the very start of each
4177 * generated function.
4178 *
4179 * \param entry_hook a function that will be invoked on entry to every
4180 * V8-generated function.
4181 * \returns true on success on supported platforms, false on failure.
4182 * \note Setting a new entry hook function when one is already active will
4183 * fail.
4184 */
4185 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
4186
4187 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004188 * Allows the host application to provide the address of a function that is
4189 * notified each time code is added, moved or removed.
4190 *
4191 * \param options options for the JIT code event handler.
4192 * \param event_handler the JIT code event handler, which will be invoked
4193 * each time code is added, moved or removed.
4194 * \note \p event_handler won't get notified of existent code.
4195 * \note since code removal notifications are not currently issued, the
4196 * \p event_handler may get notifications of code that overlaps earlier
4197 * code notifications. This happens when code areas are reused, and the
4198 * earlier overlapping code areas should therefore be discarded.
4199 * \note the events passed to \p event_handler and the strings they point to
4200 * are not guaranteed to live past each call. The \p event_handler must
4201 * copy strings and other parameters it needs to keep around.
4202 * \note the set of events declared in JitCodeEvent::EventType is expected to
4203 * grow over time, and the JitCodeEvent structure is expected to accrue
4204 * new members. The \p event_handler function must ignore event codes
4205 * it does not recognize to maintain future compatibility.
4206 */
4207 static void SetJitCodeEventHandler(JitCodeEventOptions options,
4208 JitCodeEventHandler event_handler);
4209
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004210 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
4211 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004212 static intptr_t AdjustAmountOfExternalAllocatedMemory(
4213 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00004214
iposva@chromium.org245aa852009-02-10 00:49:54 +00004215 /**
4216 * Suspends recording of tick samples in the profiler.
4217 * When the V8 profiling mode is enabled (usually via command line
4218 * switches) this function suspends recording of tick samples.
4219 * Profiling ticks are discarded until ResumeProfiler() is called.
4220 *
4221 * See also the --prof and --prof_auto command line switches to
4222 * enable V8 profiling.
4223 */
4224 static void PauseProfiler();
4225
4226 /**
4227 * Resumes recording of tick samples in the profiler.
4228 * See also PauseProfiler().
4229 */
4230 static void ResumeProfiler();
4231
ager@chromium.org41826e72009-03-30 13:30:57 +00004232 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004233 * Return whether profiler is currently paused.
4234 */
4235 static bool IsProfilerPaused();
4236
4237 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004238 * Retrieve the V8 thread id of the calling thread.
4239 *
4240 * The thread id for a thread should only be retrieved after the V8
4241 * lock has been acquired with a Locker object with that thread.
4242 */
4243 static int GetCurrentThreadId();
4244
4245 /**
4246 * Forcefully terminate execution of a JavaScript thread. This can
4247 * be used to terminate long-running scripts.
4248 *
4249 * TerminateExecution should only be called when then V8 lock has
4250 * been acquired with a Locker object. Therefore, in order to be
4251 * able to terminate long-running threads, preemption must be
4252 * enabled to allow the user of TerminateExecution to acquire the
4253 * lock.
4254 *
4255 * The termination is achieved by throwing an exception that is
4256 * uncatchable by JavaScript exception handlers. Termination
4257 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004258 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004259 * exception handler that catches an exception should check if that
4260 * exception is a termination exception and immediately return if
4261 * that is the case. Returning immediately in that case will
4262 * continue the propagation of the termination exception if needed.
4263 *
4264 * The thread id passed to TerminateExecution must have been
4265 * obtained by calling GetCurrentThreadId on the thread in question.
4266 *
4267 * \param thread_id The thread id of the thread to terminate.
4268 */
4269 static void TerminateExecution(int thread_id);
4270
4271 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004272 * Forcefully terminate the current thread of JavaScript execution
4273 * in the given isolate. If no isolate is provided, the default
4274 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004275 *
4276 * This method can be used by any thread even if that thread has not
4277 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004278 *
4279 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004280 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004281 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00004282
4283 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004284 * Is V8 terminating JavaScript execution.
4285 *
4286 * Returns true if JavaScript execution is currently terminating
4287 * because of a call to TerminateExecution. In that case there are
4288 * still JavaScript frames on the stack and the termination
4289 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004290 *
4291 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004292 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004293 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004294
4295 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004296 * Resume execution capability in the given isolate, whose execution
4297 * was previously forcefully terminated using TerminateExecution().
4298 *
4299 * When execution is forcefully terminated using TerminateExecution(),
4300 * the isolate can not resume execution until all JavaScript frames
4301 * have propagated the uncatchable exception which is generated. This
4302 * method allows the program embedding the engine to handle the
4303 * termination event and resume execution capability, even if
4304 * JavaScript frames remain on the stack.
4305 *
4306 * This method can be used by any thread even if that thread has not
4307 * acquired the V8 lock with a Locker object.
4308 *
4309 * \param isolate The isolate in which to resume execution capability.
4310 */
4311 static void CancelTerminateExecution(Isolate* isolate);
4312
4313 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00004314 * Releases any resources used by v8 and stops any utility threads
4315 * that may be running. Note that disposing v8 is permanent, it
4316 * cannot be reinitialized.
4317 *
4318 * It should generally not be necessary to dispose v8 before exiting
4319 * a process, this should happen automatically. It is only necessary
4320 * to use if the process needs the resources taken up by v8.
4321 */
4322 static bool Dispose();
4323
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00004324 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
4325 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004326
4327 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004328 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004329 * heap. GC is not invoked prior to iterating, therefore there is no
4330 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004331 */
4332 static void VisitExternalResources(ExternalResourceVisitor* visitor);
4333
4334 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004335 * Iterates through all the persistent handles in the current isolate's heap
4336 * that have class_ids.
4337 */
4338 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
4339
4340 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004341 * Iterates through all the persistent handles in the current isolate's heap
4342 * that have class_ids and are candidates to be marked as partially dependent
4343 * handles. This will visit handles to young objects created since the last
4344 * garbage collection but is free to visit an arbitrary superset of these
4345 * objects.
4346 */
4347 static void VisitHandlesForPartialDependence(
4348 Isolate* isolate, PersistentHandleVisitor* visitor);
4349
4350 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004351 * Optional notification that the embedder is idle.
4352 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004353 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004354 * Returns true if the embedder should stop calling IdleNotification
4355 * until real work has been done. This indicates that V8 has done
4356 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004357 *
4358 * The hint argument specifies the amount of work to be done in the function
4359 * on scale from 1 to 1000. There is no guarantee that the actual work will
4360 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004361 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004362 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004363
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004364 /**
4365 * Optional notification that the system is running low on memory.
4366 * V8 uses these notifications to attempt to free memory.
4367 */
4368 static void LowMemoryNotification();
4369
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004370 /**
4371 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004372 * these notifications to guide the GC heuristic. Returns the number
4373 * of context disposals - including this one - since the last time
4374 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004375 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004376 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004377
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004378 private:
4379 V8();
4380
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004381 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
4382 internal::Object** handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004383 static void DisposeGlobal(internal::Isolate* isolate,
4384 internal::Object** global_handle);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004385 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004386 static void MakeWeak(internal::Isolate* isolate,
4387 internal::Object** global_handle,
4388 void* data,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004389 RevivableCallback weak_reference_callback,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004390 NearDeathCallback near_death_callback);
4391 static void ClearWeak(internal::Isolate* isolate,
4392 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004393
4394 template <class T> friend class Handle;
4395 template <class T> friend class Local;
4396 template <class T> friend class Persistent;
4397 friend class Context;
4398};
4399
4400
4401/**
4402 * An external exception handler.
4403 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004404class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004405 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004406 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004407 * Creates a new try/catch block and registers it with v8. Note that
4408 * all TryCatch blocks should be stack allocated because the memory
4409 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004410 */
4411 TryCatch();
4412
4413 /**
4414 * Unregisters and deletes this try/catch block.
4415 */
4416 ~TryCatch();
4417
4418 /**
4419 * Returns true if an exception has been caught by this try/catch block.
4420 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004421 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004422
4423 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004424 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004425 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004426 * If CanContinue returns false, the correct action is to perform any C++
4427 * cleanup needed and then return. If CanContinue returns false and
4428 * HasTerminated returns true, it is possible to call
4429 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004430 */
4431 bool CanContinue() const;
4432
4433 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004434 * Returns true if an exception has been caught due to script execution
4435 * being terminated.
4436 *
4437 * There is no JavaScript representation of an execution termination
4438 * exception. Such exceptions are thrown when the TerminateExecution
4439 * methods are called to terminate a long-running script.
4440 *
4441 * If such an exception has been thrown, HasTerminated will return true,
4442 * indicating that it is possible to call CancelTerminateExecution in order
4443 * to continue calling into the engine.
4444 */
4445 bool HasTerminated() const;
4446
4447 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004448 * Throws the exception caught by this TryCatch in a way that avoids
4449 * it being caught again by this same TryCatch. As with ThrowException
4450 * it is illegal to execute any JavaScript operations after calling
4451 * ReThrow; the caller must return immediately to where the exception
4452 * is caught.
4453 */
4454 Handle<Value> ReThrow();
4455
4456 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004457 * Returns the exception caught by this try/catch block. If no exception has
4458 * been caught an empty handle is returned.
4459 *
4460 * The returned handle is valid until this TryCatch block has been destroyed.
4461 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004462 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004463
4464 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004465 * Returns the .stack property of the thrown object. If no .stack
4466 * property is present an empty handle is returned.
4467 */
4468 Local<Value> StackTrace() const;
4469
4470 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004471 * Returns the message associated with this exception. If there is
4472 * no message associated an empty handle is returned.
4473 *
4474 * The returned handle is valid until this TryCatch block has been
4475 * destroyed.
4476 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004477 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004478
4479 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004480 * Clears any exceptions that may have been caught by this try/catch block.
4481 * After this method has been called, HasCaught() will return false.
4482 *
4483 * It is not necessary to clear a try/catch block before using it again; if
4484 * another exception is thrown the previously caught exception will just be
4485 * overwritten. However, it is often a good idea since it makes it easier
4486 * to determine which operation threw a given exception.
4487 */
4488 void Reset();
4489
v8.team.kasperl727e9952008-09-02 14:56:44 +00004490 /**
4491 * Set verbosity of the external exception handler.
4492 *
4493 * By default, exceptions that are caught by an external exception
4494 * handler are not reported. Call SetVerbose with true on an
4495 * external exception handler to have exceptions caught by the
4496 * handler reported as if they were not caught.
4497 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004498 void SetVerbose(bool value);
4499
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004500 /**
4501 * Set whether or not this TryCatch should capture a Message object
4502 * which holds source information about where the exception
4503 * occurred. True by default.
4504 */
4505 void SetCaptureMessage(bool value);
4506
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004507 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004508 // Make it hard to create heap-allocated TryCatch blocks.
4509 TryCatch(const TryCatch&);
4510 void operator=(const TryCatch&);
4511 void* operator new(size_t size);
4512 void operator delete(void*, size_t);
4513
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004514 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004515 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004516 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004517 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004518 bool is_verbose_ : 1;
4519 bool can_continue_ : 1;
4520 bool capture_message_ : 1;
4521 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004522 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004523
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004524 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004525};
4526
4527
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004528// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004529
4530
4531/**
4532 * Ignore
4533 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004534class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004535 public:
4536 ExtensionConfiguration(int name_count, const char* names[])
4537 : name_count_(name_count), names_(names) { }
4538 private:
4539 friend class ImplementationUtilities;
4540 int name_count_;
4541 const char** names_;
4542};
4543
4544
4545/**
4546 * A sandboxed execution context with its own set of built-in objects
4547 * and functions.
4548 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004549class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004550 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004551 /**
4552 * Returns the global proxy object or global object itself for
4553 * detached contexts.
4554 *
4555 * Global proxy object is a thin wrapper whose prototype points to
4556 * actual context's global object with the properties like Object, etc.
4557 * This is done that way for security reasons (for more details see
4558 * https://wiki.mozilla.org/Gecko:SplitWindow).
4559 *
4560 * Please note that changes to global proxy object prototype most probably
4561 * would break VM---v8 expects only global object as a prototype of
4562 * global proxy object.
4563 *
4564 * If DetachGlobal() has been invoked, Global() would return actual global
4565 * object until global is reattached with ReattachGlobal().
4566 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004567 Local<Object> Global();
4568
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004569 /**
4570 * Detaches the global object from its context before
4571 * the global object can be reused to create a new context.
4572 */
4573 void DetachGlobal();
4574
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00004575 /**
4576 * Reattaches a global object to a context. This can be used to
4577 * restore the connection between a global object and a context
4578 * after DetachGlobal has been called.
4579 *
4580 * \param global_object The global object to reattach to the
4581 * context. For this to work, the global object must be the global
4582 * object that was associated with this context before a call to
4583 * DetachGlobal.
4584 */
4585 void ReattachGlobal(Handle<Object> global_object);
4586
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004587 /**
4588 * Creates a new context and returns a handle to the newly allocated
4589 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004590 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004591 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004592 *
4593 * \param extensions An optional extension configuration containing
4594 * the extensions to be installed in the newly created context.
4595 *
4596 * \param global_template An optional object template from which the
4597 * global object for the newly created context will be created.
4598 *
4599 * \param global_object An optional global object to be reused for
4600 * the newly created context. This global object must have been
4601 * created by a previous call to Context::New with the same global
4602 * template. The state of the global object will be completely reset
4603 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004604 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004605 static Local<Context> New(
4606 Isolate* isolate,
4607 ExtensionConfiguration* extensions = NULL,
4608 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4609 Handle<Value> global_object = Handle<Value>());
4610
4611 /** Deprecated. Use Isolate version instead. */
4612 // TODO(mstarzinger): Put this behind the V8_DEPRECATED guard.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004613 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004614 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004615 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4616 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004617
kasper.lund44510672008-07-25 07:37:58 +00004618 /** Returns the last entered context. */
4619 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004620
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004621 // TODO(svenpanne) Actually deprecate this.
4622 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004623 static Local<Context> GetCurrent();
4624
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004625 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004626 * Returns the context of the calling JavaScript code. That is the
4627 * context of the top-most JavaScript frame. If there are no
4628 * JavaScript frames an empty handle is returned.
4629 */
4630 static Local<Context> GetCalling();
4631
4632 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004633 * Sets the security token for the context. To access an object in
4634 * another context, the security tokens must match.
4635 */
4636 void SetSecurityToken(Handle<Value> token);
4637
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004638 /** Restores the security token to the default value. */
4639 void UseDefaultSecurityToken();
4640
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004641 /** Returns the security token of this context.*/
4642 Handle<Value> GetSecurityToken();
4643
v8.team.kasperl727e9952008-09-02 14:56:44 +00004644 /**
4645 * Enter this context. After entering a context, all code compiled
4646 * and run is compiled and run in this context. If another context
4647 * is already entered, this old context is saved so it can be
4648 * restored when the new context is exited.
4649 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004650 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004651
4652 /**
4653 * Exit this context. Exiting the current context restores the
4654 * context that was in place when entering the current context.
4655 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004656 void Exit();
4657
v8.team.kasperl727e9952008-09-02 14:56:44 +00004658 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004659 bool HasOutOfMemoryException();
4660
v8.team.kasperl727e9952008-09-02 14:56:44 +00004661 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004662 static bool InContext();
4663
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004664 /** Returns an isolate associated with a current context. */
4665 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004666
4667 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004668 * Gets the embedder data with the given index, which must have been set by a
4669 * previous call to SetEmbedderData with the same index. Note that index 0
4670 * currently has a special meaning for Chrome's debugger.
4671 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004672 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004673
4674 /**
4675 * Sets the embedder data with the given index, growing the data as
4676 * needed. Note that index 0 currently has a special meaning for Chrome's
4677 * debugger.
4678 */
4679 void SetEmbedderData(int index, Handle<Value> value);
4680
4681 /**
4682 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4683 * index, which must have bees set by a previous call to
4684 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4685 * currently has a special meaning for Chrome's debugger.
4686 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004687 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004688
4689 /**
4690 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4691 * index, growing the data as needed. Note that index 0 currently has a
4692 * special meaning for Chrome's debugger.
4693 */
4694 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004695
4696 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004697 * Control whether code generation from strings is allowed. Calling
4698 * this method with false will disable 'eval' and the 'Function'
4699 * constructor for code running in this context. If 'eval' or the
4700 * 'Function' constructor are used an exception will be thrown.
4701 *
4702 * If code generation from strings is not allowed the
4703 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4704 * set before blocking the call to 'eval' or the 'Function'
4705 * constructor. If that callback returns true, the call will be
4706 * allowed, otherwise an exception will be thrown. If no callback is
4707 * set an exception will be thrown.
4708 */
4709 void AllowCodeGenerationFromStrings(bool allow);
4710
4711 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004712 * Returns true if code generation from strings is allowed for the context.
4713 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4714 */
4715 bool IsCodeGenerationFromStringsAllowed();
4716
4717 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004718 * Sets the error description for the exception that is thrown when
4719 * code generation from strings is not allowed and 'eval' or the 'Function'
4720 * constructor are called.
4721 */
4722 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4723
4724 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004725 * Stack-allocated class which sets the execution context for all
4726 * operations executed within a local scope.
4727 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004728 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004729 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004730 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004731 context_->Enter();
4732 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004733 V8_INLINE(Scope(Isolate* isolate, Persistent<Context>& context)) // NOLINT
4734#ifndef V8_USE_UNSAFE_HANDLES
4735 : context_(Handle<Context>::New(isolate, context)) {
4736#else
4737 : context_(Local<Context>::New(isolate, context)) {
4738#endif
4739 context_->Enter();
4740 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004741 V8_INLINE(~Scope()) { context_->Exit(); }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004742
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004743 private:
4744 Handle<Context> context_;
4745 };
4746
4747 private:
4748 friend class Value;
4749 friend class Script;
4750 friend class Object;
4751 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004752
4753 Local<Value> SlowGetEmbedderData(int index);
4754 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004755};
4756
4757
4758/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004759 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4760 * to use any given V8 isolate, see the comments in the Isolate class. The
4761 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4762 * object pointers obtained from V8 handles while in the particular V8 isolate.
4763 * It is up to the user of V8 to ensure, perhaps with locking, that this
4764 * constraint is not violated. In addition to any other synchronization
4765 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4766 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004767 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004768 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4769 * construction and destruction, the current thread is allowed to use the locked
4770 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4771 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004772 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004773 * Sample usage:
4774* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004775 * ...
4776 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004777 * v8::Locker locker(isolate);
4778 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004779 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004780 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004781 * ...
4782 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00004783 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004784 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004785 * If you wish to stop using V8 in a thread A you can do this either by
4786 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
4787 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004788 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004789 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004790 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004791 * isolate->Exit();
4792 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004793 * ...
4794 * // Code not using V8 goes here while V8 can run in another thread.
4795 * ...
4796 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004797 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004798 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004799 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004800 * The Unlocker object is intended for use in a long-running callback from V8,
4801 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004802 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004803 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
4804 * given thread. This can be useful if you have code that can be called either
4805 * from code that holds the lock or from code that does not. The Unlocker is
4806 * not recursive so you can not have several Unlockers on the stack at once, and
4807 * 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 +00004808 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004809 * An unlocker will unlock several lockers if it has to and reinstate the
4810 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004811 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004812 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004813 * // V8 not locked.
4814 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004815 * v8::Locker locker(isolate);
4816 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004817 * // V8 locked.
4818 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004819 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004820 * // V8 still locked (2 levels).
4821 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004822 * isolate->Exit();
4823 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004824 * // V8 not locked.
4825 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004826 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004827 * // V8 locked again (2 levels).
4828 * }
4829 * // V8 still locked (1 level).
4830 * }
4831 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004832 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004833 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004834class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004835 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004836 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004837 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004838 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004839 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
4840
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004841 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004842 V8_DEPRECATED(Unlocker());
4843
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004844 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00004845 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004846 void Initialize(Isolate* isolate);
4847
lrn@chromium.org1c092762011-05-09 09:42:16 +00004848 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004849};
4850
4851
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004852class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004853 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004854 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004855 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004856 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004857 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
4858
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004859 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004860 V8_DEPRECATED(Locker());
4861
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004862 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004863
4864 /**
4865 * Start preemption.
4866 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004867 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004868 * that will switch between multiple threads that are in contention
4869 * for the V8 lock.
4870 */
4871 static void StartPreemption(int every_n_ms);
4872
4873 /**
4874 * Stop preemption.
4875 */
4876 static void StopPreemption();
4877
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004878 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004879 * Returns whether or not the locker for a given isolate, is locked by the
4880 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004881 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004882 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004883
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004884 /**
4885 * Returns whether v8::Locker is being used by this V8 instance.
4886 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004887 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004889 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004890 void Initialize(Isolate* isolate);
4891
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004892 bool has_lock_;
4893 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004894 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004895
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004896 static bool active_;
4897
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004898 // Disallow copying and assigning.
4899 Locker(const Locker&);
4900 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004901};
4902
4903
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004904/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004905 * A struct for exporting HeapStats data from V8, using "push" model.
4906 */
4907struct HeapStatsUpdate;
4908
4909
4910/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004911 * An interface for exporting data from V8, using "push" model.
4912 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004913class V8EXPORT OutputStream { // NOLINT
4914 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004915 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004916 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004917 };
4918 enum WriteResult {
4919 kContinue = 0,
4920 kAbort = 1
4921 };
4922 virtual ~OutputStream() {}
4923 /** Notify about the end of stream. */
4924 virtual void EndOfStream() = 0;
4925 /** Get preferred output chunk size. Called only once. */
4926 virtual int GetChunkSize() { return 1024; }
4927 /** Get preferred output encoding. Called only once. */
4928 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4929 /**
4930 * Writes the next chunk of snapshot data into the stream. Writing
4931 * can be stopped by returning kAbort as function result. EndOfStream
4932 * will not be called in case writing was aborted.
4933 */
4934 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004935 /**
4936 * Writes the next chunk of heap stats data into the stream. Writing
4937 * can be stopped by returning kAbort as function result. EndOfStream
4938 * will not be called in case writing was aborted.
4939 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004940 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004941 return kAbort;
4942 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004943};
4944
4945
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004946/**
4947 * An interface for reporting progress and controlling long-running
4948 * activities.
4949 */
4950class V8EXPORT ActivityControl { // NOLINT
4951 public:
4952 enum ControlOption {
4953 kContinue = 0,
4954 kAbort = 1
4955 };
4956 virtual ~ActivityControl() {}
4957 /**
4958 * Notify about current progress. The activity can be stopped by
4959 * returning kAbort as the callback result.
4960 */
4961 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4962};
4963
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004964
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004965// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004966
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004967
4968namespace internal {
4969
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004970const int kApiPointerSize = sizeof(void*); // NOLINT
4971const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004972
4973// Tag information for HeapObject.
4974const int kHeapObjectTag = 1;
4975const int kHeapObjectTagSize = 2;
4976const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4977
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004978// Tag information for Smi.
4979const int kSmiTag = 0;
4980const int kSmiTagSize = 1;
4981const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4982
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004983template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004984
4985// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004986template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004987 static const int kSmiShiftSize = 0;
4988 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004989 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004990 int shift_bits = kSmiTagSize + kSmiShiftSize;
4991 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4992 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4993 }
4994};
4995
4996// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004997template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004998 static const int kSmiShiftSize = 31;
4999 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005000 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005001 int shift_bits = kSmiTagSize + kSmiShiftSize;
5002 // Shift down and throw away top 32 bits.
5003 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
5004 }
5005};
5006
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005007typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
5008const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
5009const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005010
5011/**
5012 * This class exports constants and functionality from within v8 that
5013 * is necessary to implement inline functions in the v8 api. Don't
5014 * depend on functions and constants defined here.
5015 */
5016class Internals {
5017 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005018 // These values match non-compiler-dependent values defined within
5019 // the implementation of v8.
5020 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005021 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005022 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005023
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005024 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00005025 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005026 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005027 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
5028 static const int kContextHeaderSize = 2 * kApiPointerSize;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005029 static const int kContextEmbedderDataIndex = 65;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005030 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005031 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00005032 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005033 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005034
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005035 static const int kIsolateStateOffset = 0;
5036 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
5037 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
5038 static const int kUndefinedValueRootIndex = 5;
5039 static const int kNullValueRootIndex = 7;
5040 static const int kTrueValueRootIndex = 8;
5041 static const int kFalseValueRootIndex = 9;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005042 static const int kEmptyStringRootIndex = 118;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005043
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005044 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
5045 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
5046 static const int kNodeStateMask = 0xf;
5047 static const int kNodeStateIsWeakValue = 2;
5048 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005049 static const int kNodeIsIndependentShift = 4;
5050 static const int kNodeIsPartiallyDependentShift = 5;
5051
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005052 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005053 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005054 static const int kOddballType = 0x83;
5055 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005056
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005057 static const int kUndefinedOddballKind = 5;
5058 static const int kNullOddballKind = 3;
5059
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005060 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005061 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
5062 kHeapObjectTag);
5063 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005064
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005065 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005066 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005067 }
5068
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005069 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005070 typedef internal::Object O;
5071 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
5072 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
5073 }
5074
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005075 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005076 typedef internal::Object O;
5077 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
5078 }
5079
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005080 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005081 int representation = (instance_type & kFullStringRepresentationMask);
5082 return representation == kExternalTwoByteRepresentationTag;
5083 }
5084
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005085 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005086 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
5087 return *reinterpret_cast<int*>(addr) == 1;
5088 }
5089
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005090 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
5091 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5092 return *addr & (1 << shift);
5093 }
5094
5095 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
5096 bool value, int shift)) {
5097 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5098 uint8_t mask = 1 << shift;
5099 *addr = (*addr & ~mask) | (value << shift);
5100 }
5101
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005102 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
5103 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5104 return *addr & kNodeStateMask;
5105 }
5106
5107 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
5108 uint8_t value)) {
5109 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5110 *addr = (*addr & ~kNodeStateMask) | value;
5111 }
5112
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005113 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005114 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5115 kIsolateEmbedderDataOffset;
5116 *reinterpret_cast<void**>(addr) = data;
5117 }
5118
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005119 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005120 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5121 kIsolateEmbedderDataOffset;
5122 return *reinterpret_cast<void**>(addr);
5123 }
5124
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005125 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
5126 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005127 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
5128 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
5129 }
5130
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005131 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005132 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005133 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
5134 return *reinterpret_cast<T*>(addr);
5135 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005136
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005137 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005138 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005139 typedef internal::Object O;
5140 typedef internal::Internals I;
5141 O* ctx = *reinterpret_cast<O**>(context);
5142 int embedder_data_offset = I::kContextHeaderSize +
5143 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
5144 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
5145 int value_offset =
5146 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
5147 return I::ReadField<T>(embedder_data, value_offset);
5148 }
5149
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005150 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
5151 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
5152 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
5153 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
5154 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
5155 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
5156 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005157};
5158
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005159} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005160
5161
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005162template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005163Local<T>::Local() : Handle<T>() { }
5164
5165
5166template <class T>
5167Local<T> Local<T>::New(Handle<T> that) {
5168 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005169 T* that_ptr = *that;
5170 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5171 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
5172 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5173 reinterpret_cast<internal::HeapObject*>(*p))));
5174 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005175 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
5176}
5177
5178
5179template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005180Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005181 return New(isolate, that.val_);
5182}
5183
5184#ifndef V8_USE_UNSAFE_HANDLES
5185template <class T>
5186Local<T> Local<T>::New(Isolate* isolate, const Persistent<T>& that) {
5187 return New(isolate, that.val_);
5188}
5189
5190template <class T>
5191Handle<T> Handle<T>::New(Isolate* isolate, T* that) {
5192 if (that == NULL) return Handle<T>();
5193 T* that_ptr = that;
5194 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5195 return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5196 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5197}
5198#endif
5199
5200
5201template <class T>
5202Local<T> Local<T>::New(Isolate* isolate, T* that) {
5203 if (that == NULL) return Local<T>();
5204 T* that_ptr = that;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005205 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5206 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5207 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5208}
5209
5210
5211template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005212Persistent<T> Persistent<T>::New(Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005213 return New(Isolate::GetCurrent(), that.val_);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005214}
5215
5216
5217template <class T>
5218Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005219 return New(Isolate::GetCurrent(), that.val_);
5220}
5221
5222#ifndef V8_USE_UNSAFE_HANDLES
5223template <class T>
5224Persistent<T> Persistent<T>::New(Isolate* isolate, Persistent<T> that) {
5225 return New(Isolate::GetCurrent(), that.val_);
5226}
5227#endif
5228
5229template <class T>
5230Persistent<T> Persistent<T>::New(Isolate* isolate, T* that) {
5231 if (that == NULL) return Persistent<T>();
5232 internal::Object** p = reinterpret_cast<internal::Object**>(that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005233 return Persistent<T>(reinterpret_cast<T*>(
5234 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
5235 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005236}
5237
5238
5239template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005240bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005241 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005242}
5243
5244
5245template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005246bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005247 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005248 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005249 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005250 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005251 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005252}
5253
5254
5255template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005256bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005257 return IsNearDeath(Isolate::GetCurrent());
5258}
5259
5260
5261template <class T>
5262bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
5263 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005264 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005265 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005266 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005267 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005268}
5269
5270
5271template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005272bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005273 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005274}
5275
5276
5277template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005278bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005279 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005280 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005281 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005282 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005283 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005284}
5285
5286
5287template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005288void Persistent<T>::Dispose() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005289 Dispose(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005290}
5291
5292
5293template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005294void Persistent<T>::Dispose(Isolate* isolate) {
5295 if (this->IsEmpty()) return;
5296 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005297 reinterpret_cast<internal::Object**>(this->val_));
5298#ifndef V8_USE_UNSAFE_HANDLES
5299 val_ = 0;
5300#endif
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005301}
5302
5303
5304template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005305template <typename S, typename P>
5306void Persistent<T>::MakeWeak(
5307 Isolate* isolate,
5308 P* parameters,
5309 typename WeakReferenceCallbacks<S, P>::Revivable callback) {
5310 TYPE_CHECK(S, T);
5311 typedef typename WeakReferenceCallbacks<Value, void>::Revivable Revivable;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005312 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005313 reinterpret_cast<internal::Object**>(this->val_),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005314 parameters,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005315 reinterpret_cast<Revivable>(callback),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005316 NULL);
5317}
5318
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005319
5320template <class T>
5321template <typename P>
5322void Persistent<T>::MakeWeak(
5323 Isolate* isolate,
5324 P* parameters,
5325 typename WeakReferenceCallbacks<T, P>::Revivable callback) {
5326 MakeWeak<T, P>(isolate, parameters, callback);
5327}
5328
5329
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005330template <class T>
5331void Persistent<T>::MakeWeak(Isolate* isolate,
5332 void* parameters,
5333 NearDeathCallback callback) {
5334 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005335 reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005336 parameters,
5337 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005338 callback);
5339}
5340
5341template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005342void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005343 ClearWeak(Isolate::GetCurrent());
5344}
5345
5346template <class T>
5347void Persistent<T>::ClearWeak(Isolate* isolate) {
5348 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005349 reinterpret_cast<internal::Object**>(this->val_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005350}
5351
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005352template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005353void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005354 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005355}
5356
5357template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005358void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005359 typedef internal::Internals I;
5360 if (this->IsEmpty()) return;
5361 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005362 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005363 true,
5364 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005365}
5366
5367template <class T>
5368void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005369 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005370}
5371
5372template <class T>
5373void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005374 typedef internal::Internals I;
5375 if (this->IsEmpty()) return;
5376 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005377 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005378 true,
5379 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005380}
5381
5382template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005383void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005384 SetWrapperClassId(Isolate::GetCurrent(), class_id);
5385}
5386
5387template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005388void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
5389 Dispose(isolate);
5390#ifdef V8_USE_UNSAFE_HANDLES
5391 *this = *New(isolate, other);
5392#else
5393 if (other.IsEmpty()) {
5394 this->val_ = NULL;
5395 return;
5396 }
5397 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5398 this->val_ = reinterpret_cast<T*>(
5399 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5400#endif
5401}
5402
5403template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005404void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005405 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005406 if (this->IsEmpty()) return;
5407 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005408 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005409 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5410 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005411}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005412
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005413template <class T>
5414uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005415 return WrapperClassId(Isolate::GetCurrent());
5416}
5417
5418template <class T>
5419uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005420 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005421 if (this->IsEmpty()) return 0;
5422 if (!I::IsInitialized(isolate)) return 0;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005423 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005424 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5425 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005426}
5427
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005428Arguments::Arguments(internal::Object** implicit_args,
5429 internal::Object** values, int length,
5430 bool is_construct_call)
5431 : implicit_args_(implicit_args),
5432 values_(values),
5433 length_(length),
5434 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005435
5436
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005437Local<Value> Arguments::operator[](int i) const {
5438 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
5439 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
5440}
5441
5442
5443Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005444 return Local<Function>(reinterpret_cast<Function*>(
5445 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005446}
5447
5448
5449Local<Object> Arguments::This() const {
5450 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
5451}
5452
5453
5454Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005455 return Local<Object>(reinterpret_cast<Object*>(
5456 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005457}
5458
5459
5460Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005461 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005462}
5463
5464
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005465Isolate* Arguments::GetIsolate() const {
5466 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
5467}
5468
5469
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005470bool Arguments::IsConstructCall() const {
5471 return is_construct_call_;
5472}
5473
5474
5475int Arguments::Length() const {
5476 return length_;
5477}
5478
5479
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005480template <class T>
5481Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005482 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
5483 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005484 return Local<T>(reinterpret_cast<T*>(after));
5485}
5486
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005487Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005488 return resource_name_;
5489}
5490
5491
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005492Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005493 return resource_line_offset_;
5494}
5495
5496
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005497Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005498 return resource_column_offset_;
5499}
5500
5501
5502Handle<Boolean> Boolean::New(bool value) {
5503 return value ? True() : False();
5504}
5505
5506
5507void Template::Set(const char* name, v8::Handle<Data> value) {
5508 Set(v8::String::New(name), value);
5509}
5510
5511
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005512Local<Value> Object::GetInternalField(int index) {
5513#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005514 typedef internal::Object O;
5515 typedef internal::Internals I;
5516 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005517 // Fast path: If the object is a plain JSObject, which is the common case, we
5518 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005519 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005520 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005521 O* value = I::ReadField<O*>(obj, offset);
5522 O** result = HandleScope::CreateHandle(value);
5523 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005524 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005525#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005526 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005527}
5528
5529
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005530void* Object::GetAlignedPointerFromInternalField(int index) {
5531#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005532 typedef internal::Object O;
5533 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005534 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005535 // Fast path: If the object is a plain JSObject, which is the common case, we
5536 // know where to find the internal fields and can return the value directly.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005537 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005538 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005539 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005540 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005541#endif
5542 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005543}
5544
5545
5546String* String::Cast(v8::Value* value) {
5547#ifdef V8_ENABLE_CHECKS
5548 CheckCast(value);
5549#endif
5550 return static_cast<String*>(value);
5551}
5552
5553
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005554Local<String> String::Empty(Isolate* isolate) {
5555 typedef internal::Object* S;
5556 typedef internal::Internals I;
5557 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005558 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005559 return Local<String>(reinterpret_cast<String*>(slot));
5560}
5561
5562
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00005563Local<String> String::New(const char* data, int length) {
5564 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
5565}
5566
5567
5568Local<String> String::New(const uint16_t* data, int length) {
5569 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
5570}
5571
5572
5573Local<String> String::NewSymbol(const char* data, int length) {
5574 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
5575}
5576
5577
5578Local<String> String::NewUndetectable(const char* data, int length) {
5579 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
5580}
5581
5582
5583Local<String> String::NewUndetectable(const uint16_t* data, int length) {
5584 return NewFromTwoByte(
5585 Isolate::GetCurrent(), data, kUndetectableString, length);
5586}
5587
5588
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005589String::ExternalStringResource* String::GetExternalStringResource() const {
5590 typedef internal::Object O;
5591 typedef internal::Internals I;
5592 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005593 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005594 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005595 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5596 result = reinterpret_cast<String::ExternalStringResource*>(value);
5597 } else {
5598 result = NULL;
5599 }
5600#ifdef V8_ENABLE_CHECKS
5601 VerifyExternalStringResource(result);
5602#endif
5603 return result;
5604}
5605
5606
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005607String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
5608 String::Encoding* encoding_out) const {
5609 typedef internal::Object O;
5610 typedef internal::Internals I;
5611 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
5612 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
5613 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5614 ExternalStringResourceBase* resource = NULL;
5615 if (type == I::kExternalAsciiRepresentationTag ||
5616 type == I::kExternalTwoByteRepresentationTag) {
5617 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5618 resource = static_cast<ExternalStringResourceBase*>(value);
5619 }
5620#ifdef V8_ENABLE_CHECKS
5621 VerifyExternalStringResourceBase(resource, *encoding_out);
5622#endif
5623 return resource;
5624}
5625
5626
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005627bool Value::IsUndefined() const {
5628#ifdef V8_ENABLE_CHECKS
5629 return FullIsUndefined();
5630#else
5631 return QuickIsUndefined();
5632#endif
5633}
5634
5635bool Value::QuickIsUndefined() const {
5636 typedef internal::Object O;
5637 typedef internal::Internals I;
5638 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5639 if (!I::HasHeapObjectTag(obj)) return false;
5640 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5641 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
5642}
5643
5644
5645bool Value::IsNull() const {
5646#ifdef V8_ENABLE_CHECKS
5647 return FullIsNull();
5648#else
5649 return QuickIsNull();
5650#endif
5651}
5652
5653bool Value::QuickIsNull() const {
5654 typedef internal::Object O;
5655 typedef internal::Internals I;
5656 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5657 if (!I::HasHeapObjectTag(obj)) return false;
5658 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5659 return (I::GetOddballKind(obj) == I::kNullOddballKind);
5660}
5661
5662
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005663bool Value::IsString() const {
5664#ifdef V8_ENABLE_CHECKS
5665 return FullIsString();
5666#else
5667 return QuickIsString();
5668#endif
5669}
5670
5671bool Value::QuickIsString() const {
5672 typedef internal::Object O;
5673 typedef internal::Internals I;
5674 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5675 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005676 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005677}
5678
5679
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005680Symbol* Symbol::Cast(v8::Value* value) {
5681#ifdef V8_ENABLE_CHECKS
5682 CheckCast(value);
5683#endif
5684 return static_cast<Symbol*>(value);
5685}
5686
5687
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005688Number* Number::Cast(v8::Value* value) {
5689#ifdef V8_ENABLE_CHECKS
5690 CheckCast(value);
5691#endif
5692 return static_cast<Number*>(value);
5693}
5694
5695
5696Integer* Integer::Cast(v8::Value* value) {
5697#ifdef V8_ENABLE_CHECKS
5698 CheckCast(value);
5699#endif
5700 return static_cast<Integer*>(value);
5701}
5702
5703
5704Date* Date::Cast(v8::Value* value) {
5705#ifdef V8_ENABLE_CHECKS
5706 CheckCast(value);
5707#endif
5708 return static_cast<Date*>(value);
5709}
5710
5711
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00005712StringObject* StringObject::Cast(v8::Value* value) {
5713#ifdef V8_ENABLE_CHECKS
5714 CheckCast(value);
5715#endif
5716 return static_cast<StringObject*>(value);
5717}
5718
5719
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005720SymbolObject* SymbolObject::Cast(v8::Value* value) {
5721#ifdef V8_ENABLE_CHECKS
5722 CheckCast(value);
5723#endif
5724 return static_cast<SymbolObject*>(value);
5725}
5726
5727
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00005728NumberObject* NumberObject::Cast(v8::Value* value) {
5729#ifdef V8_ENABLE_CHECKS
5730 CheckCast(value);
5731#endif
5732 return static_cast<NumberObject*>(value);
5733}
5734
5735
5736BooleanObject* BooleanObject::Cast(v8::Value* value) {
5737#ifdef V8_ENABLE_CHECKS
5738 CheckCast(value);
5739#endif
5740 return static_cast<BooleanObject*>(value);
5741}
5742
5743
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00005744RegExp* RegExp::Cast(v8::Value* value) {
5745#ifdef V8_ENABLE_CHECKS
5746 CheckCast(value);
5747#endif
5748 return static_cast<RegExp*>(value);
5749}
5750
5751
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005752Object* Object::Cast(v8::Value* value) {
5753#ifdef V8_ENABLE_CHECKS
5754 CheckCast(value);
5755#endif
5756 return static_cast<Object*>(value);
5757}
5758
5759
5760Array* Array::Cast(v8::Value* value) {
5761#ifdef V8_ENABLE_CHECKS
5762 CheckCast(value);
5763#endif
5764 return static_cast<Array*>(value);
5765}
5766
5767
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005768ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
5769#ifdef V8_ENABLE_CHECKS
5770 CheckCast(value);
5771#endif
5772 return static_cast<ArrayBuffer*>(value);
5773}
5774
5775
danno@chromium.orgf005df62013-04-30 16:36:45 +00005776TypedArray* TypedArray::Cast(v8::Value* value) {
5777#ifdef V8_ENABLE_CHECKS
5778 CheckCast(value);
5779#endif
5780 return static_cast<TypedArray*>(value);
5781}
5782
5783
5784Uint8Array* Uint8Array::Cast(v8::Value* value) {
5785#ifdef V8_ENABLE_CHECKS
5786 CheckCast(value);
5787#endif
5788 return static_cast<Uint8Array*>(value);
5789}
5790
5791
5792Int8Array* Int8Array::Cast(v8::Value* value) {
5793#ifdef V8_ENABLE_CHECKS
5794 CheckCast(value);
5795#endif
5796 return static_cast<Int8Array*>(value);
5797}
5798
5799
5800Uint16Array* Uint16Array::Cast(v8::Value* value) {
5801#ifdef V8_ENABLE_CHECKS
5802 CheckCast(value);
5803#endif
5804 return static_cast<Uint16Array*>(value);
5805}
5806
5807
5808Int16Array* Int16Array::Cast(v8::Value* value) {
5809#ifdef V8_ENABLE_CHECKS
5810 CheckCast(value);
5811#endif
5812 return static_cast<Int16Array*>(value);
5813}
5814
5815
5816Uint32Array* Uint32Array::Cast(v8::Value* value) {
5817#ifdef V8_ENABLE_CHECKS
5818 CheckCast(value);
5819#endif
5820 return static_cast<Uint32Array*>(value);
5821}
5822
5823
5824Int32Array* Int32Array::Cast(v8::Value* value) {
5825#ifdef V8_ENABLE_CHECKS
5826 CheckCast(value);
5827#endif
5828 return static_cast<Int32Array*>(value);
5829}
5830
5831
5832Float32Array* Float32Array::Cast(v8::Value* value) {
5833#ifdef V8_ENABLE_CHECKS
5834 CheckCast(value);
5835#endif
5836 return static_cast<Float32Array*>(value);
5837}
5838
5839
5840Float64Array* Float64Array::Cast(v8::Value* value) {
5841#ifdef V8_ENABLE_CHECKS
5842 CheckCast(value);
5843#endif
5844 return static_cast<Float64Array*>(value);
5845}
5846
5847
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005848Function* Function::Cast(v8::Value* value) {
5849#ifdef V8_ENABLE_CHECKS
5850 CheckCast(value);
5851#endif
5852 return static_cast<Function*>(value);
5853}
5854
5855
5856External* External::Cast(v8::Value* value) {
5857#ifdef V8_ENABLE_CHECKS
5858 CheckCast(value);
5859#endif
5860 return static_cast<External*>(value);
5861}
5862
5863
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005864Isolate* AccessorInfo::GetIsolate() const {
5865 return *reinterpret_cast<Isolate**>(&args_[-3]);
5866}
5867
5868
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005869Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005870 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005871}
5872
5873
5874Local<Object> AccessorInfo::This() const {
5875 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
5876}
5877
5878
5879Local<Object> AccessorInfo::Holder() const {
5880 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
5881}
5882
5883
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005884Handle<Primitive> Undefined(Isolate* isolate) {
5885 typedef internal::Object* S;
5886 typedef internal::Internals I;
5887 if (!I::IsInitialized(isolate)) return Undefined();
5888 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
5889 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
5890}
5891
5892
5893Handle<Primitive> Null(Isolate* isolate) {
5894 typedef internal::Object* S;
5895 typedef internal::Internals I;
5896 if (!I::IsInitialized(isolate)) return Null();
5897 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
5898 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
5899}
5900
5901
5902Handle<Boolean> True(Isolate* isolate) {
5903 typedef internal::Object* S;
5904 typedef internal::Internals I;
5905 if (!I::IsInitialized(isolate)) return True();
5906 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
5907 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
5908}
5909
5910
5911Handle<Boolean> False(Isolate* isolate) {
5912 typedef internal::Object* S;
5913 typedef internal::Internals I;
5914 if (!I::IsInitialized(isolate)) return False();
5915 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
5916 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
5917}
5918
5919
5920void Isolate::SetData(void* data) {
5921 typedef internal::Internals I;
5922 I::SetEmbedderData(this, data);
5923}
5924
5925
5926void* Isolate::GetData() {
5927 typedef internal::Internals I;
5928 return I::GetEmbedderData(this);
5929}
5930
5931
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005932Local<Value> Context::GetEmbedderData(int index) {
5933#ifndef V8_ENABLE_CHECKS
5934 typedef internal::Object O;
5935 typedef internal::Internals I;
5936 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
5937 return Local<Value>(reinterpret_cast<Value*>(result));
5938#else
5939 return SlowGetEmbedderData(index);
5940#endif
5941}
5942
5943
5944void* Context::GetAlignedPointerFromEmbedderData(int index) {
5945#ifndef V8_ENABLE_CHECKS
5946 typedef internal::Internals I;
5947 return I::ReadEmbedderData<void*>(this, index);
5948#else
5949 return SlowGetAlignedPointerFromEmbedderData(index);
5950#endif
5951}
5952
5953
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005954/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00005955 * \example shell.cc
5956 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005957 * command-line and executes them.
5958 */
5959
5960
5961/**
5962 * \example process.cc
5963 */
5964
5965
5966} // namespace v8
5967
5968
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005969#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005970#undef TYPE_CHECK
5971
5972
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005973#endif // V8_H_