blob: 0cb9df7896ade609b06553738e5b981f378521b3 [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
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000698 /**
699 * Returns the underlying raw pointer and clears the handle. The caller is
700 * responsible of eventually destroying the underlying object (by creating a
701 * Persistent handle which points to it and Disposing it). In the future,
702 * destructing a Persistent will also Dispose it. With this function, the
703 * embedder can let the Persistent go out of scope without it getting
704 * disposed.
705 */
706 V8_INLINE(T* ClearAndLeak());
707
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000708#ifndef V8_USE_UNSAFE_HANDLES
709
710#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
711
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000713#endif
714 // TODO(dcarney): make unlinkable before cutover
715 V8_INLINE(Persistent(const Persistent& that)) : val_(that.val_) {}
716 // TODO(dcarney): make unlinkable before cutover
717 V8_INLINE(Persistent& operator=(const Persistent& that)) { // NOLINT
718 this->val_ = that.val_;
719 return *this;
720 }
721
722 public:
723#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
724
725 private:
726#endif
727 // TODO(dcarney): remove before cutover
728 template <class S> V8_INLINE(Persistent(S* that)) : val_(that) { }
729 // TODO(dcarney): remove before cutover
730 template <class S> V8_INLINE(Persistent(Persistent<S> that))
731 : val_(*that) {
732 TYPE_CHECK(T, S);
733 }
734 // TODO(dcarney): remove before cutover
735 V8_INLINE(T* operator*() const) { return val_; }
736 public:
737#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW
738
739 private:
740#endif
741 // TODO(dcarney): remove before cutover
742 V8_INLINE(T* operator->() const) { return val_; }
743 public:
744#endif
745
746 private:
747 template<class F>
748 friend class Handle;
749 template<class F>
750 friend class Local;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 friend class ImplementationUtilities;
752 friend class ObjectTemplate;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000753 friend class Context;
754 friend class InternalHandleHelper;
755 friend class LocalContext;
756
757 V8_INLINE(static Persistent<T> New(Isolate* isolate, T* that));
758
759#ifndef V8_USE_UNSAFE_HANDLES
760 T* val_;
761#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762};
763
764
v8.team.kasperl727e9952008-09-02 14:56:44 +0000765 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766 * A stack-allocated class that governs a number of local handles.
767 * After a handle scope has been created, all local handles will be
768 * allocated within that handle scope until either the handle scope is
769 * deleted or another handle scope is created. If there is already a
770 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000771 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 * new handles will again be allocated in the original handle scope.
773 *
774 * After the handle scope of a local handle has been deleted the
775 * garbage collector will no longer track the object stored in the
776 * handle and may deallocate it. The behavior of accessing a handle
777 * for which the handle scope has been deleted is undefined.
778 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000779class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000781 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000782 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000784 HandleScope(Isolate* isolate);
785
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000786 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787
788 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789 * Closes the handle scope and returns the value as a handle in the
790 * previous scope, which is the new current scope after the call.
791 */
792 template <class T> Local<T> Close(Handle<T> value);
793
794 /**
795 * Counts the number of allocated handles.
796 */
797 static int NumberOfHandles();
798
799 /**
800 * Creates a new handle with the given value.
801 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000802 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000803 static internal::Object** CreateHandle(internal::Isolate* isolate,
804 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000805 // Faster version, uses HeapObject to obtain the current Isolate.
806 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807
808 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000809 // Make it hard to create heap-allocated or illegal handle scopes by
810 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811 HandleScope(const HandleScope&);
812 void operator=(const HandleScope&);
813 void* operator new(size_t size);
814 void operator delete(void*, size_t);
815
ager@chromium.org3811b432009-10-28 14:53:37 +0000816 // This Data class is accessible internally as HandleScopeData through a
817 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000818 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000820 internal::Object** next;
821 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000822 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000823 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000824 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000825 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 }
827 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000828
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000829 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000830 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000832 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000833 internal::Object** prev_next_;
834 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000836 // Allow for the active closing of HandleScopes which allows to pass a handle
837 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000839 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841 friend class ImplementationUtilities;
842};
843
844
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000845// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846
847
848/**
849 * The superclass of values and API object templates.
850 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000851class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 private:
853 Data();
854};
855
856
857/**
858 * Pre-compilation data that can be associated with a script. This
859 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000860 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000861 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000863class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864 public:
865 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000866
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000867 /**
868 * Pre-compiles the specified script (context-independent).
869 *
870 * \param input Pointer to UTF-8 script source code.
871 * \param length Length of UTF-8 script source code.
872 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000875 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000876 * Pre-compiles the specified script (context-independent).
877 *
878 * NOTE: Pre-compilation using this method cannot happen on another thread
879 * without using Lockers.
880 *
881 * \param source Script source code.
882 */
883 static ScriptData* PreCompile(Handle<String> source);
884
885 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000886 * Load previous pre-compilation data.
887 *
888 * \param data Pointer to data returned by a call to Data() of a previous
889 * ScriptData. Ownership is not transferred.
890 * \param length Length of data.
891 */
892 static ScriptData* New(const char* data, int length);
893
894 /**
895 * Returns the length of Data().
896 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000898
899 /**
900 * Returns a serialized representation of this ScriptData that can later be
901 * passed to New(). NOTE: Serialized data is platform-dependent.
902 */
903 virtual const char* Data() = 0;
904
905 /**
906 * Returns true if the source code could not be parsed.
907 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000908 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909};
910
911
912/**
913 * The origin, within a file, of a script.
914 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000915class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000917 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000918 Handle<Value> resource_name,
919 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000920 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 : resource_name_(resource_name),
922 resource_line_offset_(resource_line_offset),
923 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000924 V8_INLINE(Handle<Value> ResourceName() const);
925 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
926 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000928 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 Handle<Integer> resource_line_offset_;
930 Handle<Integer> resource_column_offset_;
931};
932
933
934/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000935 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000937class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000940 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000941 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000942 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000943 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000944 * when New() returns
945 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
946 * using pre_data speeds compilation if it's done multiple times.
947 * Owned by caller, no references are kept when New() returns.
948 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000949 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000950 * available to compile event handlers.
951 * \return Compiled script object (context independent; when run it
952 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000954 static Local<Script> New(Handle<String> source,
955 ScriptOrigin* origin = NULL,
956 ScriptData* pre_data = NULL,
957 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958
mads.s.agercbaa0602008-08-14 13:41:48 +0000959 /**
960 * Compiles the specified script using the specified file name
961 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000962 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000963 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000964 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000965 * as the script's origin.
966 * \return Compiled script object (context independent; when run it
967 * will use the currently entered context).
968 */
969 static Local<Script> New(Handle<String> source,
970 Handle<Value> file_name);
971
972 /**
973 * Compiles the specified script (bound to current context).
974 *
975 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000976 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000977 * when Compile() returns
978 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
979 * using pre_data speeds compilation if it's done multiple times.
980 * Owned by caller, no references are kept when Compile() returns.
981 * \param script_data Arbitrary data associated with script. Using
982 * this has same effect as calling SetData(), but makes data available
983 * earlier (i.e. to compile event handlers).
984 * \return Compiled script object, bound to the context that was active
985 * when this function was called. When run it will always use this
986 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000987 */
988 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000989 ScriptOrigin* origin = NULL,
990 ScriptData* pre_data = NULL,
991 Handle<String> script_data = Handle<String>());
992
993 /**
994 * Compiles the specified script using the specified file name
995 * object (typically a string) as the script's origin.
996 *
997 * \param source Script source code.
998 * \param file_name File name to use as script's origin
999 * \param script_data Arbitrary data associated with script. Using
1000 * this has same effect as calling SetData(), but makes data available
1001 * earlier (i.e. to compile event handlers).
1002 * \return Compiled script object, bound to the context that was active
1003 * when this function was called. When run it will always use this
1004 * context.
1005 */
1006 static Local<Script> Compile(Handle<String> source,
1007 Handle<Value> file_name,
1008 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +00001009
v8.team.kasperl727e9952008-09-02 14:56:44 +00001010 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001011 * Runs the script returning the resulting value. If the script is
1012 * context independent (created using ::New) it will be run in the
1013 * currently entered context. If it is context specific (created
1014 * using ::Compile) it will be run in the context in which it was
1015 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001016 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001018
1019 /**
1020 * Returns the script id value.
1021 */
1022 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001023
1024 /**
1025 * Associate an additional data object with the script. This is mainly used
1026 * with the debugger as this data object is only available through the
1027 * debugger API.
1028 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00001029 void SetData(Handle<String> data);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001030
1031 /**
1032 * Returns the name value of one Script.
1033 */
1034 Handle<Value> GetScriptName();
1035
1036 /**
1037 * Returns zero based line number of the code_pos location in the script.
1038 * -1 will be returned if no information available.
1039 */
1040 int GetLineNumber(int code_pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041};
1042
1043
1044/**
1045 * An error message.
1046 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001047class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001049 Local<String> Get() const;
1050 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001052 /**
1053 * Returns the resource name for the script from where the function causing
1054 * the error originates.
1055 */
ager@chromium.org32912102009-01-16 10:38:43 +00001056 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001058 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001059 * Returns the resource data for the script from where the function causing
1060 * the error originates.
1061 */
1062 Handle<Value> GetScriptData() const;
1063
1064 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001065 * Exception stack trace. By default stack traces are not captured for
1066 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1067 * to change this option.
1068 */
1069 Handle<StackTrace> GetStackTrace() const;
1070
1071 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001072 * Returns the number, 1-based, of the line where the error occurred.
1073 */
ager@chromium.org32912102009-01-16 10:38:43 +00001074 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001076 /**
1077 * Returns the index within the script of the first character where
1078 * the error occurred.
1079 */
ager@chromium.org32912102009-01-16 10:38:43 +00001080 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001081
1082 /**
1083 * Returns the index within the script of the last character where
1084 * the error occurred.
1085 */
ager@chromium.org32912102009-01-16 10:38:43 +00001086 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001087
1088 /**
1089 * Returns the index within the line of the first character where
1090 * the error occurred.
1091 */
ager@chromium.org32912102009-01-16 10:38:43 +00001092 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001093
1094 /**
1095 * Returns the index within the line of the last character where
1096 * the error occurred.
1097 */
ager@chromium.org32912102009-01-16 10:38:43 +00001098 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001099
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100 // TODO(1245381): Print to a string instead of on a FILE.
1101 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001102
1103 static const int kNoLineNumberInfo = 0;
1104 static const int kNoColumnInfo = 0;
1105};
1106
1107
1108/**
1109 * Representation of a JavaScript stack trace. The information collected is a
1110 * snapshot of the execution stack and the information remains valid after
1111 * execution continues.
1112 */
1113class V8EXPORT StackTrace {
1114 public:
1115 /**
1116 * Flags that determine what information is placed captured for each
1117 * StackFrame when grabbing the current stack trace.
1118 */
1119 enum StackTraceOptions {
1120 kLineNumber = 1,
1121 kColumnOffset = 1 << 1 | kLineNumber,
1122 kScriptName = 1 << 2,
1123 kFunctionName = 1 << 3,
1124 kIsEval = 1 << 4,
1125 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001126 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001127 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001128 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001129 };
1130
1131 /**
1132 * Returns a StackFrame at a particular index.
1133 */
1134 Local<StackFrame> GetFrame(uint32_t index) const;
1135
1136 /**
1137 * Returns the number of StackFrames.
1138 */
1139 int GetFrameCount() const;
1140
1141 /**
1142 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1143 */
1144 Local<Array> AsArray();
1145
1146 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001147 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001148 *
1149 * \param frame_limit The maximum number of stack frames we want to capture.
1150 * \param options Enumerates the set of things we will capture for each
1151 * StackFrame.
1152 */
1153 static Local<StackTrace> CurrentStackTrace(
1154 int frame_limit,
1155 StackTraceOptions options = kOverview);
1156};
1157
1158
1159/**
1160 * A single JavaScript stack frame.
1161 */
1162class V8EXPORT StackFrame {
1163 public:
1164 /**
1165 * Returns the number, 1-based, of the line for the associate function call.
1166 * This method will return Message::kNoLineNumberInfo if it is unable to
1167 * retrieve the line number, or if kLineNumber was not passed as an option
1168 * when capturing the StackTrace.
1169 */
1170 int GetLineNumber() const;
1171
1172 /**
1173 * Returns the 1-based column offset on the line for the associated function
1174 * call.
1175 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1176 * the column number, or if kColumnOffset was not passed as an option when
1177 * capturing the StackTrace.
1178 */
1179 int GetColumn() const;
1180
1181 /**
1182 * Returns the name of the resource that contains the script for the
1183 * function for this StackFrame.
1184 */
1185 Local<String> GetScriptName() const;
1186
1187 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001188 * Returns the name of the resource that contains the script for the
1189 * function for this StackFrame or sourceURL value if the script name
1190 * is undefined and its source ends with //@ sourceURL=... string.
1191 */
1192 Local<String> GetScriptNameOrSourceURL() const;
1193
1194 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001195 * Returns the name of the function associated with this stack frame.
1196 */
1197 Local<String> GetFunctionName() const;
1198
1199 /**
1200 * Returns whether or not the associated function is compiled via a call to
1201 * eval().
1202 */
1203 bool IsEval() const;
1204
1205 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001206 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001207 * constructor via "new".
1208 */
1209 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210};
1211
1212
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001213// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214
1215
1216/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001217 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001219class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221 /**
1222 * Returns true if this value is the undefined value. See ECMA-262
1223 * 4.3.10.
1224 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001225 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226
1227 /**
1228 * Returns true if this value is the null value. See ECMA-262
1229 * 4.3.11.
1230 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001231 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232
1233 /**
1234 * Returns true if this value is true.
1235 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001236 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237
1238 /**
1239 * Returns true if this value is false.
1240 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001241 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242
1243 /**
1244 * Returns true if this value is an instance of the String type.
1245 * See ECMA-262 8.4.
1246 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001247 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248
1249 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001250 * Returns true if this value is a symbol.
1251 * This is an experimental feature.
1252 */
1253 bool IsSymbol() const;
1254
1255 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256 * Returns true if this value is a function.
1257 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001258 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259
1260 /**
1261 * Returns true if this value is an array.
1262 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001263 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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 an object.
1267 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001268 bool IsObject() 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 boolean.
1272 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001273 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +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 number.
1277 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001278 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279
v8.team.kasperl727e9952008-09-02 14:56:44 +00001280 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 * Returns true if this value is external.
1282 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001283 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284
v8.team.kasperl727e9952008-09-02 14:56:44 +00001285 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 * Returns true if this value is a 32-bit signed integer.
1287 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001288 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001290 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001291 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001292 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001293 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001294
1295 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001296 * Returns true if this value is a Date.
1297 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001298 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001299
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001300 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001301 * Returns true if this value is a Boolean object.
1302 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001303 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001304
1305 /**
1306 * Returns true if this value is a Number object.
1307 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001308 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001309
1310 /**
1311 * Returns true if this value is a String object.
1312 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001313 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001314
1315 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001316 * Returns true if this value is a Symbol object.
1317 * This is an experimental feature.
1318 */
1319 bool IsSymbolObject() const;
1320
1321 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001322 * Returns true if this value is a NativeError.
1323 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001324 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001325
1326 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001327 * Returns true if this value is a RegExp.
1328 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001329 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001330
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001331
1332 /**
1333 * Returns true if this value is an ArrayBuffer.
1334 * This is an experimental feature.
1335 */
1336 bool IsArrayBuffer() const;
1337
1338 /**
1339 * Returns true if this value is one of TypedArrays.
1340 * This is an experimental feature.
1341 */
1342 bool IsTypedArray() const;
1343
1344 /**
1345 * Returns true if this value is an Uint8Array.
1346 * This is an experimental feature.
1347 */
1348 bool IsUint8Array() const;
1349
1350 /**
1351 * Returns true if this value is an Uint8ClampedArray.
1352 * This is an experimental feature.
1353 */
1354 bool IsUint8ClampedArray() const;
1355
1356 /**
1357 * Returns true if this value is an Int8Array.
1358 * This is an experimental feature.
1359 */
1360 bool IsInt8Array() const;
1361
1362 /**
1363 * Returns true if this value is an Uint16Array.
1364 * This is an experimental feature.
1365 */
1366 bool IsUint16Array() const;
1367
1368 /**
1369 * Returns true if this value is an Int16Array.
1370 * This is an experimental feature.
1371 */
1372 bool IsInt16Array() const;
1373
1374 /**
1375 * Returns true if this value is an Uint32Array.
1376 * This is an experimental feature.
1377 */
1378 bool IsUint32Array() const;
1379
1380 /**
1381 * Returns true if this value is an Int32Array.
1382 * This is an experimental feature.
1383 */
1384 bool IsInt32Array() const;
1385
1386 /**
1387 * Returns true if this value is a Float32Array.
1388 * This is an experimental feature.
1389 */
1390 bool IsFloat32Array() const;
1391
1392 /**
1393 * Returns true if this value is a Float64Array.
1394 * This is an experimental feature.
1395 */
1396 bool IsFloat64Array() const;
1397
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001398 Local<Boolean> ToBoolean() const;
1399 Local<Number> ToNumber() const;
1400 Local<String> ToString() const;
1401 Local<String> ToDetailString() const;
1402 Local<Object> ToObject() const;
1403 Local<Integer> ToInteger() const;
1404 Local<Uint32> ToUint32() const;
1405 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406
1407 /**
1408 * Attempts to convert a string to an array index.
1409 * Returns an empty handle if the conversion fails.
1410 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001411 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001413 bool BooleanValue() const;
1414 double NumberValue() const;
1415 int64_t IntegerValue() const;
1416 uint32_t Uint32Value() const;
1417 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418
1419 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001420 bool Equals(Handle<Value> that) const;
1421 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001422
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001423 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001424 V8_INLINE(bool QuickIsUndefined() const);
1425 V8_INLINE(bool QuickIsNull() const);
1426 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001427 bool FullIsUndefined() const;
1428 bool FullIsNull() const;
1429 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430};
1431
1432
1433/**
1434 * The superclass of primitive values. See ECMA-262 4.3.2.
1435 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001436class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437
1438
1439/**
1440 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1441 * or false value.
1442 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001443class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001445 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001446 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447};
1448
1449
1450/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001451 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001453class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001455 enum Encoding {
1456 UNKNOWN_ENCODING = 0x1,
1457 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001458 ASCII_ENCODING = 0x4,
1459 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001460 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001461 /**
1462 * Returns the number of characters in this string.
1463 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001464 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465
v8.team.kasperl727e9952008-09-02 14:56:44 +00001466 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001467 * Returns the number of bytes in the UTF-8 encoded
1468 * representation of this string.
1469 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001470 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001471
1472 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001473 * This function is no longer useful.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001474 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001475 V8_DEPRECATED(V8_INLINE(bool MayContainNonAscii()) const) { return true; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001476
1477 /**
1478 * Returns whether this string contains only one byte data.
1479 */
1480 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001481
1482 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001483 * Write the contents of the string to an external buffer.
1484 * If no arguments are given, expects the buffer to be large
1485 * enough to hold the entire string and NULL terminator. Copies
1486 * the contents of the string and the NULL terminator into the
1487 * buffer.
1488 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001489 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1490 * before the end of the buffer.
1491 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001492 * Copies up to length characters into the output buffer.
1493 * Only null-terminates if there is enough space in the buffer.
1494 *
1495 * \param buffer The buffer into which the string will be copied.
1496 * \param start The starting position within the string at which
1497 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001498 * \param length The number of characters to copy from the string. For
1499 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001500 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001501 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001502 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001503 * \return The number of characters copied to the buffer excluding the null
1504 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001505 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001506 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001507 enum WriteOptions {
1508 NO_OPTIONS = 0,
1509 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001510 NO_NULL_TERMINATION = 2,
1511 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001512 };
1513
lrn@chromium.org34e60782011-09-15 07:25:40 +00001514 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001515 int Write(uint16_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 // ASCII characters.
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001520 V8_DEPRECATED(int WriteAscii(char* buffer,
1521 int start = 0,
1522 int length = -1,
1523 int options = NO_OPTIONS) const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001524 // One byte characters.
1525 int WriteOneByte(uint8_t* buffer,
1526 int start = 0,
1527 int length = -1,
1528 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001529 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001530 int WriteUtf8(char* buffer,
1531 int length = -1,
1532 int* nchars_ref = NULL,
1533 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534
v8.team.kasperl727e9952008-09-02 14:56:44 +00001535 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001536 * A zero length string.
1537 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001538 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001539 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001540
1541 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001542 * Returns true if the string is external
1543 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001544 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545
v8.team.kasperl727e9952008-09-02 14:56:44 +00001546 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001547 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001548 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001549 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001550
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001551 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001552 public:
1553 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001554
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001555 protected:
1556 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001557
1558 /**
1559 * Internally V8 will call this Dispose method when the external string
1560 * resource is no longer needed. The default implementation will use the
1561 * delete operator. This method can be overridden in subclasses to
1562 * control how allocated external string resources are disposed.
1563 */
1564 virtual void Dispose() { delete this; }
1565
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001566 private:
1567 // Disallow copying and assigning.
1568 ExternalStringResourceBase(const ExternalStringResourceBase&);
1569 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001570
1571 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001572 };
1573
v8.team.kasperl727e9952008-09-02 14:56:44 +00001574 /**
1575 * An ExternalStringResource is a wrapper around a two-byte string
1576 * buffer that resides outside V8's heap. Implement an
1577 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001578 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001579 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001580 class V8EXPORT ExternalStringResource
1581 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 public:
1583 /**
1584 * Override the destructor to manage the life cycle of the underlying
1585 * buffer.
1586 */
1587 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001588
1589 /**
1590 * The string data from the underlying buffer.
1591 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001593
1594 /**
1595 * The length of the string. That is, the number of two-byte characters.
1596 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001598
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599 protected:
1600 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 };
1602
1603 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001604 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001605 * string buffer that resides outside V8's heap. Implement an
1606 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001607 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001608 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001609 * UTF-8, which would require special treatment internally in the
1610 * engine and, in the case of UTF-8, do not allow efficient indexing.
1611 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001612 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001614 class V8EXPORT ExternalAsciiStringResource
1615 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 public:
1617 /**
1618 * Override the destructor to manage the life cycle of the underlying
1619 * buffer.
1620 */
1621 virtual ~ExternalAsciiStringResource() {}
1622 /** The string data from the underlying buffer.*/
1623 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001624 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001625 virtual size_t length() const = 0;
1626 protected:
1627 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628 };
1629
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001630 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1631
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001633 * If the string is an external string, return the ExternalStringResourceBase
1634 * regardless of the encoding, otherwise return NULL. The encoding of the
1635 * string is returned in encoding_out.
1636 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001637 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1638 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001639
1640 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001641 * Get the ExternalStringResource for an external string. Returns
1642 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001644 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645
1646 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001647 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001648 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001650 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001652 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001654 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001656 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001657 * The second parameter 'length' gives the buffer length. If omitted,
1658 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001660 V8_INLINE(static Local<String> New(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001662 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001663 /** Allocates a new string from 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001664 V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001666 // TODO(dcarney): deprecate
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001667 /**
1668 * Creates an internalized string (historically called a "symbol",
1669 * not to be confused with ES6 symbols). Returns one if it exists already.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001670 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001671 V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
1672
1673 enum NewStringType {
1674 kNormalString, kInternalizedString, kUndetectableString
1675 };
1676
1677 /** Allocates a new string from UTF-8 data.*/
1678 static Local<String> NewFromUtf8(Isolate* isolate,
1679 const char* data,
1680 NewStringType type = kNormalString,
1681 int length = -1);
1682
1683 /** Allocates a new string from Latin-1 data.*/
1684 static Local<String> NewFromOneByte(
1685 Isolate* isolate,
1686 const uint8_t* data,
1687 NewStringType type = kNormalString,
1688 int length = -1);
1689
1690 /** Allocates a new string from UTF-16 data.*/
1691 static Local<String> NewFromTwoByte(
1692 Isolate* isolate,
1693 const uint16_t* data,
1694 NewStringType type = kNormalString,
1695 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696
v8.team.kasperl727e9952008-09-02 14:56:44 +00001697 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001698 * Creates a new string by concatenating the left and the right strings
1699 * passed in as parameters.
1700 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001701 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001702
1703 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001704 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001705 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001706 * resource will be disposed by calling its Dispose method. The caller of
1707 * this function should not otherwise delete or modify the resource. Neither
1708 * should the underlying buffer be deallocated or modified except through the
1709 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001710 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001711 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001712
ager@chromium.org6f10e412009-02-13 10:11:16 +00001713 /**
1714 * Associate an external string resource with this string by transforming it
1715 * in place so that existing references to this string in the JavaScript heap
1716 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001717 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001718 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001719 * The string is not modified if the operation fails. See NewExternal for
1720 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001721 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001722 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723
v8.team.kasperl727e9952008-09-02 14:56:44 +00001724 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001725 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001726 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001727 * resource will be disposed by calling its Dispose method. The caller of
1728 * this function should not otherwise delete or modify the resource. Neither
1729 * should the underlying buffer be deallocated or modified except through the
1730 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001731 */
1732 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001733
ager@chromium.org6f10e412009-02-13 10:11:16 +00001734 /**
1735 * Associate an external string resource with this string by transforming it
1736 * in place so that existing references to this string in the JavaScript heap
1737 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001738 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001739 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001740 * The string is not modified if the operation fails. See NewExternal for
1741 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001742 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001743 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001745 /**
1746 * Returns true if this string can be made external.
1747 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001748 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001749
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001750 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001751 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001752 V8_INLINE(
1753 static Local<String> NewUndetectable(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001755 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001756 /** Creates an undetectable string from the supplied 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001757 V8_INLINE(static Local<String> NewUndetectable(
1758 const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759
1760 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001761 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001762 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001763 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001764 * then the length() method returns 0 and the * operator returns
1765 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001766 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001767 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001768 public:
1769 explicit Utf8Value(Handle<v8::Value> obj);
1770 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001771 char* operator*() { return str_; }
1772 const char* operator*() const { return str_; }
1773 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001774 private:
1775 char* str_;
1776 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001777
1778 // Disallow copying and assigning.
1779 Utf8Value(const Utf8Value&);
1780 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001781 };
1782
1783 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001784 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001786 * If conversion to a string fails (eg. due to an exception in the toString()
1787 * method of the object) then the length() method returns 0 and the * operator
1788 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001790 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 public:
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001792 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 explicit AsciiValue(Handle<v8::Value> obj);
1794 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001795 char* operator*() { return str_; }
1796 const char* operator*() const { return str_; }
1797 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 private:
1799 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001800 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001801
1802 // Disallow copying and assigning.
1803 AsciiValue(const AsciiValue&);
1804 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805 };
1806
1807 /**
1808 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001809 * If conversion to a string fails (eg. due to an exception in the toString()
1810 * method of the object) then the length() method returns 0 and the * operator
1811 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001813 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 public:
1815 explicit Value(Handle<v8::Value> obj);
1816 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001817 uint16_t* operator*() { return str_; }
1818 const uint16_t* operator*() const { return str_; }
1819 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 private:
1821 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001822 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001823
1824 // Disallow copying and assigning.
1825 Value(const Value&);
1826 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001828
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001829 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001830 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1831 Encoding encoding) const;
1832 void VerifyExternalStringResource(ExternalStringResource* val) const;
1833 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834};
1835
1836
1837/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001838 * A JavaScript symbol (ECMA-262 edition 6)
1839 *
1840 * This is an experimental feature. Use at your own risk.
1841 */
1842class V8EXPORT Symbol : public Primitive {
1843 public:
1844 // Returns the print name string of the symbol, or undefined if none.
1845 Local<Value> Name() const;
1846
1847 // Create a symbol without a print name.
1848 static Local<Symbol> New(Isolate* isolate);
1849
1850 // Create a symbol with a print name.
1851 static Local<Symbol> New(Isolate *isolate, const char* data, int length = -1);
1852
1853 V8_INLINE(static Symbol* Cast(v8::Value* obj));
1854 private:
1855 Symbol();
1856 static void CheckCast(v8::Value* obj);
1857};
1858
1859
1860/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001861 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001863class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001865 double Value() const;
1866 static Local<Number> New(double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001867 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001869 Number();
1870 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871};
1872
1873
1874/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001875 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001877class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001879 static Local<Integer> New(int32_t value);
1880 static Local<Integer> NewFromUnsigned(uint32_t value);
1881 static Local<Integer> New(int32_t value, Isolate*);
1882 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1883 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001884 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001886 Integer();
1887 static void CheckCast(v8::Value* obj);
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 signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001894class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001896 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001898 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899};
1900
1901
1902/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001903 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001905class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001907 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001909 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910};
1911
1912
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913enum PropertyAttribute {
1914 None = 0,
1915 ReadOnly = 1 << 0,
1916 DontEnum = 1 << 1,
1917 DontDelete = 1 << 2
1918};
1919
ager@chromium.org3811b432009-10-28 14:53:37 +00001920enum ExternalArrayType {
1921 kExternalByteArray = 1,
1922 kExternalUnsignedByteArray,
1923 kExternalShortArray,
1924 kExternalUnsignedShortArray,
1925 kExternalIntArray,
1926 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001927 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001928 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001929 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001930};
1931
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001933 * Accessor[Getter|Setter] are used as callback functions when
1934 * setting|getting a particular property. See Object and ObjectTemplate's
1935 * method SetAccessor.
1936 */
1937typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1938 const AccessorInfo& info);
1939
1940
1941typedef void (*AccessorSetter)(Local<String> property,
1942 Local<Value> value,
1943 const AccessorInfo& info);
1944
1945
1946/**
1947 * Access control specifications.
1948 *
1949 * Some accessors should be accessible across contexts. These
1950 * accessors have an explicit access control parameter which specifies
1951 * the kind of cross-context access that should be allowed.
1952 *
1953 * Additionally, for security, accessors can prohibit overwriting by
1954 * accessors defined in JavaScript. For objects that have such
1955 * accessors either locally or in their prototype chain it is not
1956 * possible to overwrite the accessor by using __defineGetter__ or
1957 * __defineSetter__ from JavaScript code.
1958 */
1959enum AccessControl {
1960 DEFAULT = 0,
1961 ALL_CAN_READ = 1,
1962 ALL_CAN_WRITE = 1 << 1,
1963 PROHIBITS_OVERWRITING = 1 << 2
1964};
1965
1966
1967/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001968 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001970class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001972 bool Set(Handle<Value> key,
1973 Handle<Value> value,
1974 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001975
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001976 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001977
ager@chromium.orge2902be2009-06-08 12:21:35 +00001978 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001979 // overriding accessors or read-only properties.
1980 //
1981 // Note that if the object has an interceptor the property will be set
1982 // locally, but since the interceptor takes precedence the local property
1983 // will only be returned if the interceptor doesn't return a value.
1984 //
1985 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001986 bool ForceSet(Handle<Value> key,
1987 Handle<Value> value,
1988 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001989
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001990 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001992 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001993
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001994 /**
1995 * Gets the property attributes of a property which can be None or
1996 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1997 * None when the property doesn't exist.
1998 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001999 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002000
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002001 bool Has(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002002
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002003 bool Delete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002004
2005 // Delete a property on this object bypassing interceptors and
2006 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002007 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002008
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002009 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002010
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002011 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002013 bool SetAccessor(Handle<String> name,
2014 AccessorGetter getter,
2015 AccessorSetter setter = 0,
2016 Handle<Value> data = Handle<Value>(),
2017 AccessControl settings = DEFAULT,
2018 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002019
ulan@chromium.org750145a2013-03-07 15:14:13 +00002020 // This function is not yet stable and should not be used at this time.
2021 bool SetAccessor(Handle<String> name,
2022 Handle<DeclaredAccessorDescriptor> descriptor,
2023 AccessControl settings = DEFAULT,
2024 PropertyAttribute attribute = None);
2025
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002027 * Returns an array containing the names of the enumerable properties
2028 * of this object, including properties from prototype objects. The
2029 * array returned by this method contains the same values as would
2030 * be enumerated by a for-in statement over this object.
2031 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002032 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002033
2034 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002035 * This function has the same functionality as GetPropertyNames but
2036 * the returned array doesn't contain the names of properties from
2037 * prototype objects.
2038 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002039 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002040
2041 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 * Get the prototype object. This does not skip objects marked to
2043 * be skipped by __proto__ and it does not consult the security
2044 * handler.
2045 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002046 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047
2048 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002049 * Set the prototype object. This does not skip objects marked to
2050 * be skipped by __proto__ and it does not consult the security
2051 * handler.
2052 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002053 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00002054
2055 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002056 * Finds an instance of the given function template in the prototype
2057 * chain.
2058 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002059 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002060
2061 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 * Call builtin Object.prototype.toString on this object.
2063 * This is different from Value::ToString() that may call
2064 * user-defined toString function. This one does not.
2065 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002066 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002068 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002069 * Returns the function invoked as a constructor for this object.
2070 * May be the null value.
2071 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002072 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002073
2074 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002075 * Returns the name of the function invoked as a constructor for this object.
2076 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002077 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002078
kasper.lund212ac232008-07-16 07:07:30 +00002079 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002080 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002081
2082 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002083 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002084
kasper.lund212ac232008-07-16 07:07:30 +00002085 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002086 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002087
2088 /**
2089 * Gets a 2-byte-aligned native pointer from an internal field. This field
2090 * must have been set by SetAlignedPointerInInternalField, everything else
2091 * leads to undefined behavior.
2092 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002093 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002094
2095 /**
2096 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2097 * a field, GetAlignedPointerFromInternalField must be used, everything else
2098 * leads to undefined behavior.
2099 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002100 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002103 bool HasOwnProperty(Handle<String> key);
2104 bool HasRealNamedProperty(Handle<String> key);
2105 bool HasRealIndexedProperty(uint32_t index);
2106 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107
2108 /**
2109 * If result.IsEmpty() no real property was located in the prototype chain.
2110 * This means interceptors in the prototype chain are not called.
2111 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002112 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00002113
2114 /**
2115 * If result.IsEmpty() no real property was located on the object or
2116 * in the prototype chain.
2117 * This means interceptors in the prototype chain are not called.
2118 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002119 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002120
2121 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002122 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123
kasper.lund212ac232008-07-16 07:07:30 +00002124 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002125 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002127 /**
2128 * Turns on access check on the object if the object is an instance of
2129 * a template that has access check callbacks. If an object has no
2130 * access check info, the object cannot be accessed by anyone.
2131 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002132 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00002133
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002134 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002135 * Returns the identity hash for this object. The current implementation
2136 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002137 *
ager@chromium.org9085a012009-05-11 19:22:57 +00002138 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00002139 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002140 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002141 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00002142
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002143 /**
2144 * Access hidden properties on JavaScript objects. These properties are
2145 * hidden from the executing JavaScript and only accessible through the V8
2146 * C++ API. Hidden properties introduced by V8 internally (for example the
2147 * identity hash) are prefixed with "v8::".
2148 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002149 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
2150 Local<Value> GetHiddenValue(Handle<String> key);
2151 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002152
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002153 /**
2154 * Returns true if this is an instance of an api function (one
2155 * created from a function created from a function template) and has
2156 * been modified since it was created. Note that this method is
2157 * conservative and may return true for objects that haven't actually
2158 * been modified.
2159 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002160 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002161
2162 /**
2163 * Clone this object with a fast but shallow copy. Values will point
2164 * to the same values as the original object.
2165 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002166 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002168 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002169 * Returns the context in which the object was created.
2170 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002171 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002172
2173 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002174 * Set the backing store of the indexed properties to be managed by the
2175 * embedding layer. Access to the indexed properties will follow the rules
2176 * spelled out in CanvasPixelArray.
2177 * Note: The embedding program still owns the data and needs to ensure that
2178 * the backing store is preserved while V8 has a reference.
2179 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002180 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
2181 bool HasIndexedPropertiesInPixelData();
2182 uint8_t* GetIndexedPropertiesPixelData();
2183 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002184
ager@chromium.org3811b432009-10-28 14:53:37 +00002185 /**
2186 * Set the backing store of the indexed properties to be managed by the
2187 * embedding layer. Access to the indexed properties will follow the rules
2188 * spelled out for the CanvasArray subtypes in the WebGL specification.
2189 * Note: The embedding program still owns the data and needs to ensure that
2190 * the backing store is preserved while V8 has a reference.
2191 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002192 void SetIndexedPropertiesToExternalArrayData(void* data,
2193 ExternalArrayType array_type,
2194 int number_of_elements);
2195 bool HasIndexedPropertiesInExternalArrayData();
2196 void* GetIndexedPropertiesExternalArrayData();
2197 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
2198 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00002199
lrn@chromium.org1c092762011-05-09 09:42:16 +00002200 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002201 * Checks whether a callback is set by the
2202 * ObjectTemplate::SetCallAsFunctionHandler method.
2203 * When an Object is callable this method returns true.
2204 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002205 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002206
2207 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002208 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002209 * ObjectTemplate::SetCallAsFunctionHandler method.
2210 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002211 Local<Value> CallAsFunction(Handle<Object> recv,
2212 int argc,
2213 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002214
2215 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002216 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002217 * ObjectTemplate::SetCallAsFunctionHandler method.
2218 * Note: This method behaves like the Function::NewInstance method.
2219 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002220 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002221
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002222 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002223 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002224
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002226 Object();
2227 static void CheckCast(Value* obj);
2228 Local<Value> SlowGetInternalField(int index);
2229 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230};
2231
2232
2233/**
2234 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
2235 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002236class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002238 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239
ager@chromium.org3e875802009-06-29 08:26:34 +00002240 /**
2241 * Clones an element at index |index|. Returns an empty
2242 * handle if cloning fails (for any reason).
2243 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002244 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00002245
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002246 /**
2247 * Creates a JavaScript array with the given length. If the length
2248 * is negative the returned array will have length 0.
2249 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002250 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002251
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002252 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002254 Array();
2255 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256};
2257
2258
2259/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002260 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002262class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002264 Local<Object> NewInstance() const;
2265 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
2266 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
2267 void SetName(Handle<String> name);
2268 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00002269
2270 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002271 * Name inferred from variable or property assignment of this function.
2272 * Used to facilitate debugging and profiling of JavaScript code written
2273 * in an OO style, where many functions are anonymous but are assigned
2274 * to object properties.
2275 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002276 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002277
2278 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002279 * Returns zero based line number of function body and
2280 * kLineOffsetNotFound if no information available.
2281 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002282 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002283 /**
2284 * Returns zero based column number of function body and
2285 * kLineOffsetNotFound if no information available.
2286 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002287 int GetScriptColumnNumber() const;
2288 Handle<Value> GetScriptId() const;
2289 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002290 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002291 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002292
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002294 Function();
2295 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296};
2297
2298
2299/**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002300 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
2301 * This API is experimental and may change significantly.
2302 */
2303class V8EXPORT ArrayBuffer : public Object {
2304 public:
2305 /**
2306 * Data length in bytes.
2307 */
2308 size_t ByteLength() const;
2309 /**
2310 * Raw pointer to the array buffer data
2311 */
2312 void* Data() const;
2313
2314 /**
2315 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
2316 * Allocated memory will be owned by a created ArrayBuffer and
2317 * will be deallocated when it is garbage-collected.
2318 */
2319 static Local<ArrayBuffer> New(size_t byte_length);
2320
2321 /**
2322 * Create a new ArrayBuffer over an existing memory block.
2323 * The memory block will not be reclaimed when a created ArrayBuffer
2324 * is garbage-collected.
2325 */
2326 static Local<ArrayBuffer> New(void* data, size_t byte_length);
2327
2328 V8_INLINE(static ArrayBuffer* Cast(Value* obj));
2329
2330 private:
2331 ArrayBuffer();
2332 static void CheckCast(Value* obj);
2333};
2334
2335
2336/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002337 * A base class for an instance of TypedArray series of constructors
2338 * (ES6 draft 15.13.6).
2339 * This API is experimental and may change significantly.
2340 */
2341class V8EXPORT TypedArray : public Object {
2342 public:
2343 /**
2344 * Returns underlying ArrayBuffer.
2345 */
2346 Local<ArrayBuffer> Buffer();
2347 /**
2348 * Byte offset in |Buffer|
2349 */
2350 size_t ByteOffset();
2351 /**
2352 * Numbe of elements in this typed array.
2353 */
2354 size_t Length();
2355 /**
2356 * Size of typed array in bytes (e.g. for Int16Array, 2*|Length|).
2357 */
2358 size_t ByteLength();
2359 /**
2360 * Base address of typed array.
2361 */
2362 void* BaseAddress();
2363
2364 V8_INLINE(static TypedArray* Cast(Value* obj));
2365
2366 private:
2367 TypedArray();
2368 static void CheckCast(Value* obj);
2369};
2370
2371
2372/**
2373 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
2374 * This API is experimental and may change significantly.
2375 */
2376class V8EXPORT Uint8Array : public TypedArray {
2377 public:
2378 static Local<Uint8Array> New(Handle<ArrayBuffer> array_buffer,
2379 size_t byte_offset, size_t length);
2380 V8_INLINE(static Uint8Array* Cast(Value* obj));
2381
2382 private:
2383 Uint8Array();
2384 static void CheckCast(Value* obj);
2385};
2386
2387
2388/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002389 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
2390 * This API is experimental and may change significantly.
2391 */
2392class V8EXPORT Uint8ClampedArray : public TypedArray {
2393 public:
2394 static Local<Uint8ClampedArray> New(Handle<ArrayBuffer> array_buffer,
2395 size_t byte_offset, size_t length);
2396 V8_INLINE(static Uint8ClampedArray* Cast(Value* obj));
2397
2398 private:
2399 Uint8ClampedArray();
2400 static void CheckCast(Value* obj);
2401};
2402
2403/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002404 * An instance of Int8Array constructor (ES6 draft 15.13.6).
2405 * This API is experimental and may change significantly.
2406 */
2407class V8EXPORT Int8Array : public TypedArray {
2408 public:
2409 static Local<Int8Array> New(Handle<ArrayBuffer> array_buffer,
2410 size_t byte_offset, size_t length);
2411 V8_INLINE(static Int8Array* Cast(Value* obj));
2412
2413 private:
2414 Int8Array();
2415 static void CheckCast(Value* obj);
2416};
2417
2418
2419/**
2420 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
2421 * This API is experimental and may change significantly.
2422 */
2423class V8EXPORT Uint16Array : public TypedArray {
2424 public:
2425 static Local<Uint16Array> New(Handle<ArrayBuffer> array_buffer,
2426 size_t byte_offset, size_t length);
2427 V8_INLINE(static Uint16Array* Cast(Value* obj));
2428
2429 private:
2430 Uint16Array();
2431 static void CheckCast(Value* obj);
2432};
2433
2434
2435/**
2436 * An instance of Int16Array constructor (ES6 draft 15.13.6).
2437 * This API is experimental and may change significantly.
2438 */
2439class V8EXPORT Int16Array : public TypedArray {
2440 public:
2441 static Local<Int16Array> New(Handle<ArrayBuffer> array_buffer,
2442 size_t byte_offset, size_t length);
2443 V8_INLINE(static Int16Array* Cast(Value* obj));
2444
2445 private:
2446 Int16Array();
2447 static void CheckCast(Value* obj);
2448};
2449
2450
2451/**
2452 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
2453 * This API is experimental and may change significantly.
2454 */
2455class V8EXPORT Uint32Array : public TypedArray {
2456 public:
2457 static Local<Uint32Array> New(Handle<ArrayBuffer> array_buffer,
2458 size_t byte_offset, size_t length);
2459 V8_INLINE(static Uint32Array* Cast(Value* obj));
2460
2461 private:
2462 Uint32Array();
2463 static void CheckCast(Value* obj);
2464};
2465
2466
2467/**
2468 * An instance of Int32Array constructor (ES6 draft 15.13.6).
2469 * This API is experimental and may change significantly.
2470 */
2471class V8EXPORT Int32Array : public TypedArray {
2472 public:
2473 static Local<Int32Array> New(Handle<ArrayBuffer> array_buffer,
2474 size_t byte_offset, size_t length);
2475 V8_INLINE(static Int32Array* Cast(Value* obj));
2476
2477 private:
2478 Int32Array();
2479 static void CheckCast(Value* obj);
2480};
2481
2482
2483/**
2484 * An instance of Float32Array constructor (ES6 draft 15.13.6).
2485 * This API is experimental and may change significantly.
2486 */
2487class V8EXPORT Float32Array : public TypedArray {
2488 public:
2489 static Local<Float32Array> New(Handle<ArrayBuffer> array_buffer,
2490 size_t byte_offset, size_t length);
2491 V8_INLINE(static Float32Array* Cast(Value* obj));
2492
2493 private:
2494 Float32Array();
2495 static void CheckCast(Value* obj);
2496};
2497
2498
2499/**
2500 * An instance of Float64Array constructor (ES6 draft 15.13.6).
2501 * This API is experimental and may change significantly.
2502 */
2503class V8EXPORT Float64Array : public TypedArray {
2504 public:
2505 static Local<Float64Array> New(Handle<ArrayBuffer> array_buffer,
2506 size_t byte_offset, size_t length);
2507 V8_INLINE(static Float64Array* Cast(Value* obj));
2508
2509 private:
2510 Float64Array();
2511 static void CheckCast(Value* obj);
2512};
2513
2514
2515/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002516 * An instance of the built-in Date constructor (ECMA-262, 15.9).
2517 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002518class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002519 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002520 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002521
2522 /**
2523 * A specialization of Value::NumberValue that is more efficient
2524 * because we know the structure of this object.
2525 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002526 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002527
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002528 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002529
2530 /**
2531 * Notification that the embedder has changed the time zone,
2532 * daylight savings time, or other date / time configuration
2533 * parameters. V8 keeps a cache of various values used for
2534 * date / time computation. This notification will reset
2535 * those cached values for the current context so that date /
2536 * time configuration changes would be reflected in the Date
2537 * object.
2538 *
2539 * This API should not be called more than needed as it will
2540 * negatively impact the performance of date operations.
2541 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002542 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002543
2544 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002545 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002546};
2547
2548
2549/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002550 * A Number object (ECMA-262, 4.3.21).
2551 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002552class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002553 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002554 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002555
2556 /**
2557 * Returns the Number held by the object.
2558 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002559 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002560
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002561 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002562
2563 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002564 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002565};
2566
2567
2568/**
2569 * A Boolean object (ECMA-262, 4.3.15).
2570 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002571class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002572 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002573 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002574
2575 /**
2576 * Returns the Boolean held by the object.
2577 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002578 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002579
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002580 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002581
2582 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002583 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002584};
2585
2586
2587/**
2588 * A String object (ECMA-262, 4.3.18).
2589 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002590class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002591 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002592 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002593
2594 /**
2595 * Returns the String held by the object.
2596 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002597 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002598
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002599 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002600
2601 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002602 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002603};
2604
2605
2606/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002607 * A Symbol object (ECMA-262 edition 6).
2608 *
2609 * This is an experimental feature. Use at your own risk.
2610 */
2611class V8EXPORT SymbolObject : public Object {
2612 public:
2613 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2614
2615 /**
2616 * Returns the Symbol held by the object.
2617 */
2618 Local<Symbol> SymbolValue() const;
2619
2620 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2621
2622 private:
2623 static void CheckCast(v8::Value* obj);
2624};
2625
2626
2627/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002628 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2629 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002630class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002631 public:
2632 /**
2633 * Regular expression flag bits. They can be or'ed to enable a set
2634 * of flags.
2635 */
2636 enum Flags {
2637 kNone = 0,
2638 kGlobal = 1,
2639 kIgnoreCase = 2,
2640 kMultiline = 4
2641 };
2642
2643 /**
2644 * Creates a regular expression from the given pattern string and
2645 * the flags bit field. May throw a JavaScript exception as
2646 * described in ECMA-262, 15.10.4.1.
2647 *
2648 * For example,
2649 * RegExp::New(v8::String::New("foo"),
2650 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2651 * is equivalent to evaluating "/foo/gm".
2652 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002653 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002654
2655 /**
2656 * Returns the value of the source property: a string representing
2657 * the regular expression.
2658 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002659 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002660
2661 /**
2662 * Returns the flags bit field.
2663 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002664 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002665
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002666 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002667
2668 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002669 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002670};
2671
2672
2673/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002674 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2675 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002677class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002679 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002680 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002681 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002683 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684};
2685
2686
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002687// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688
2689
2690/**
2691 * The superclass of object and function templates.
2692 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002693class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 public:
2695 /** Adds a property to each instance created by this template.*/
2696 void Set(Handle<String> name, Handle<Data> value,
2697 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002698 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 private:
2700 Template();
2701
2702 friend class ObjectTemplate;
2703 friend class FunctionTemplate;
2704};
2705
2706
2707/**
2708 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002709 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 * including the receiver, the number and values of arguments, and
2711 * the holder of the function.
2712 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002713class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002715 V8_INLINE(int Length() const);
2716 V8_INLINE(Local<Value> operator[](int i) const);
2717 V8_INLINE(Local<Function> Callee() const);
2718 V8_INLINE(Local<Object> This() const);
2719 V8_INLINE(Local<Object> Holder() const);
2720 V8_INLINE(bool IsConstructCall() const);
2721 V8_INLINE(Local<Value> Data() const);
2722 V8_INLINE(Isolate* GetIsolate() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002723
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002725 static const int kIsolateIndex = 0;
2726 static const int kDataIndex = -1;
2727 static const int kCalleeIndex = -2;
2728 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002729
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 friend class ImplementationUtilities;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002731 V8_INLINE(Arguments(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002732 internal::Object** values,
2733 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002734 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002735 internal::Object** implicit_args_;
2736 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002738 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002739};
2740
2741
2742/**
2743 * The information passed to an accessor callback about the context
2744 * of the property access.
2745 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002746class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002748 V8_INLINE(AccessorInfo(internal::Object** args))
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002749 : args_(args) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002750 V8_INLINE(Isolate* GetIsolate() const);
2751 V8_INLINE(Local<Value> Data() const);
2752 V8_INLINE(Local<Object> This() const);
2753 V8_INLINE(Local<Object> Holder() const);
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002754
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002755 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002756 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757};
2758
2759
2760typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2761
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763 * NamedProperty[Getter|Setter] are used as interceptors on object.
2764 * See ObjectTemplate::SetNamedPropertyHandler.
2765 */
2766typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2767 const AccessorInfo& info);
2768
2769
2770/**
2771 * Returns the value if the setter intercepts the request.
2772 * Otherwise, returns an empty handle.
2773 */
2774typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2775 Local<Value> value,
2776 const AccessorInfo& info);
2777
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778/**
2779 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002780 * The result is an integer encoding property attributes (like v8::None,
2781 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002782 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002783typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2784 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785
2786
2787/**
2788 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002789 * The return value is true if the property could be deleted and false
2790 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791 */
2792typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2793 const AccessorInfo& info);
2794
2795/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002796 * Returns an array containing the names of the properties the named
2797 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798 */
2799typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2800
v8.team.kasperl727e9952008-09-02 14:56:44 +00002801
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002803 * Returns the value of the property if the getter intercepts the
2804 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805 */
2806typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2807 const AccessorInfo& info);
2808
2809
2810/**
2811 * Returns the value if the setter intercepts the request.
2812 * Otherwise, returns an empty handle.
2813 */
2814typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2815 Local<Value> value,
2816 const AccessorInfo& info);
2817
2818
2819/**
2820 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002821 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002822 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002823typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2824 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
2826/**
2827 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002828 * The return value is true if the property could be deleted and false
2829 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830 */
2831typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2832 const AccessorInfo& info);
2833
v8.team.kasperl727e9952008-09-02 14:56:44 +00002834/**
2835 * Returns an array containing the indices of the properties the
2836 * indexed property getter intercepts.
2837 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2839
2840
2841/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002842 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843 */
2844enum AccessType {
2845 ACCESS_GET,
2846 ACCESS_SET,
2847 ACCESS_HAS,
2848 ACCESS_DELETE,
2849 ACCESS_KEYS
2850};
2851
v8.team.kasperl727e9952008-09-02 14:56:44 +00002852
2853/**
2854 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002855 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002856 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002857typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858 Local<Value> key,
2859 AccessType type,
2860 Local<Value> data);
2861
v8.team.kasperl727e9952008-09-02 14:56:44 +00002862
2863/**
2864 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002865 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002866 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002867typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868 uint32_t index,
2869 AccessType type,
2870 Local<Value> data);
2871
2872
2873/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002874 * A FunctionTemplate is used to create functions at runtime. There
2875 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002876 * context. The lifetime of the created function is equal to the
2877 * lifetime of the context. So in case the embedder needs to create
2878 * temporary functions that can be collected using Scripts is
2879 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880 *
2881 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002882 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002884 * A FunctionTemplate has a corresponding instance template which is
2885 * used to create object instances when the function is used as a
2886 * constructor. Properties added to the instance template are added to
2887 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888 *
2889 * A FunctionTemplate can have a prototype template. The prototype template
2890 * is used to create the prototype object of the function.
2891 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002892 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002893 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002894 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002895 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2896 * t->Set("func_property", v8::Number::New(1));
2897 *
2898 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2899 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2900 * proto_t->Set("proto_const", v8::Number::New(2));
2901 *
2902 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2903 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2904 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2905 * instance_t->Set("instance_property", Number::New(3));
2906 *
2907 * v8::Local<v8::Function> function = t->GetFunction();
2908 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002909 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 *
2911 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002912 * and "instance" for the instance object created above. The function
2913 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002914 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002915 * \code
2916 * func_property in function == true;
2917 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002919 * function.prototype.proto_method() invokes 'InvokeCallback'
2920 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002921 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002922 * instance instanceof function == true;
2923 * instance.instance_accessor calls 'InstanceAccessorCallback'
2924 * instance.instance_property == 3;
2925 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002926 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002927 * A FunctionTemplate can inherit from another one by calling the
2928 * FunctionTemplate::Inherit method. The following graph illustrates
2929 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002930 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002931 * \code
2932 * FunctionTemplate Parent -> Parent() . prototype -> { }
2933 * ^ ^
2934 * | Inherit(Parent) | .__proto__
2935 * | |
2936 * FunctionTemplate Child -> Child() . prototype -> { }
2937 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002938 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002939 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2940 * object of the Child() function has __proto__ pointing to the
2941 * Parent() function's prototype object. An instance of the Child
2942 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002943 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002944 * Let Parent be the FunctionTemplate initialized in the previous
2945 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002946 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002947 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002948 * Local<FunctionTemplate> parent = t;
2949 * Local<FunctionTemplate> child = FunctionTemplate::New();
2950 * child->Inherit(parent);
2951 *
2952 * Local<Function> child_function = child->GetFunction();
2953 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002954 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002956 * The Child function and Child instance will have the following
2957 * properties:
2958 *
2959 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002960 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002961 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002962 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002963 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002965class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002966 public:
2967 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002968 static Local<FunctionTemplate> New(
2969 InvocationCallback callback = 0,
2970 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002971 Handle<Signature> signature = Handle<Signature>(),
2972 int length = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 /** Returns the unique function instance in the current execution context.*/
2974 Local<Function> GetFunction();
2975
v8.team.kasperl727e9952008-09-02 14:56:44 +00002976 /**
2977 * Set the call-handler callback for a FunctionTemplate. This
2978 * callback is called whenever the function created from this
2979 * FunctionTemplate is called.
2980 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981 void SetCallHandler(InvocationCallback callback,
2982 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002983
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002984 /** Set the predefined length property for the FunctionTemplate. */
2985 void SetLength(int length);
2986
v8.team.kasperl727e9952008-09-02 14:56:44 +00002987 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988 Local<ObjectTemplate> InstanceTemplate();
2989
2990 /** Causes the function template to inherit from a parent function template.*/
2991 void Inherit(Handle<FunctionTemplate> parent);
2992
2993 /**
2994 * A PrototypeTemplate is the template used to create the prototype object
2995 * of the function created by this template.
2996 */
2997 Local<ObjectTemplate> PrototypeTemplate();
2998
v8.team.kasperl727e9952008-09-02 14:56:44 +00002999 /**
3000 * Set the class name of the FunctionTemplate. This is used for
3001 * printing objects created with the function created from the
3002 * FunctionTemplate as its constructor.
3003 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003004 void SetClassName(Handle<String> name);
3005
3006 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003007 * Determines whether the __proto__ accessor ignores instances of
3008 * the function template. If instances of the function template are
3009 * ignored, __proto__ skips all instances and instead returns the
3010 * next object in the prototype chain.
3011 *
3012 * Call with a value of true to make the __proto__ accessor ignore
3013 * instances of the function template. Call with a value of false
3014 * to make the __proto__ accessor not ignore instances of the
3015 * function template. By default, instances of a function template
3016 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003017 */
3018 void SetHiddenPrototype(bool value);
3019
3020 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003021 * Sets the ReadOnly flag in the attributes of the 'prototype' property
3022 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00003023 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003024 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00003025
3026 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003027 * Returns true if the given object is an instance of this function
3028 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003029 */
3030 bool HasInstance(Handle<Value> object);
3031
3032 private:
3033 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003034 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
3035 NamedPropertySetter setter,
3036 NamedPropertyQuery query,
3037 NamedPropertyDeleter remover,
3038 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003039 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003040 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
3041 IndexedPropertySetter setter,
3042 IndexedPropertyQuery query,
3043 IndexedPropertyDeleter remover,
3044 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003045 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
3047 Handle<Value> data);
3048
3049 friend class Context;
3050 friend class ObjectTemplate;
3051};
3052
3053
3054/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003055 * An ObjectTemplate is used to create objects at runtime.
3056 *
3057 * Properties added to an ObjectTemplate are added to each object
3058 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003059 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003060class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003062 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003063 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003064
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003065 /** Creates a new instance of this template.*/
3066 Local<Object> NewInstance();
3067
3068 /**
3069 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003070 *
3071 * Whenever the property with the given name is accessed on objects
3072 * created from this ObjectTemplate the getter and setter callbacks
3073 * are called instead of getting and setting the property directly
3074 * on the JavaScript object.
3075 *
3076 * \param name The name of the property for which an accessor is added.
3077 * \param getter The callback to invoke when getting the property.
3078 * \param setter The callback to invoke when setting the property.
3079 * \param data A piece of data that will be passed to the getter and setter
3080 * callbacks whenever they are invoked.
3081 * \param settings Access control settings for the accessor. This is a bit
3082 * field consisting of one of more of
3083 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
3084 * The default is to not allow cross-context access.
3085 * ALL_CAN_READ means that all cross-context reads are allowed.
3086 * ALL_CAN_WRITE means that all cross-context writes are allowed.
3087 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
3088 * cross-context access.
3089 * \param attribute The attributes of the property for which an accessor
3090 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003091 * \param signature The signature describes valid receivers for the accessor
3092 * and is used to perform implicit instance checks against them. If the
3093 * receiver is incompatible (i.e. is not an instance of the constructor as
3094 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
3095 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003096 */
3097 void SetAccessor(Handle<String> name,
3098 AccessorGetter getter,
3099 AccessorSetter setter = 0,
3100 Handle<Value> data = Handle<Value>(),
3101 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003102 PropertyAttribute attribute = None,
3103 Handle<AccessorSignature> signature =
3104 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003105
ulan@chromium.org750145a2013-03-07 15:14:13 +00003106 // This function is not yet stable and should not be used at this time.
3107 bool SetAccessor(Handle<String> name,
3108 Handle<DeclaredAccessorDescriptor> descriptor,
3109 AccessControl settings = DEFAULT,
3110 PropertyAttribute attribute = None,
3111 Handle<AccessorSignature> signature =
3112 Handle<AccessorSignature>());
3113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003114 /**
3115 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003116 *
3117 * Whenever a named property is accessed on objects created from
3118 * this object template, the provided callback is invoked instead of
3119 * accessing the property directly on the JavaScript object.
3120 *
3121 * \param getter The callback to invoke when getting a property.
3122 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003123 * \param query The callback to invoke to check if a property is present,
3124 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003125 * \param deleter The callback to invoke when deleting a property.
3126 * \param enumerator The callback to invoke to enumerate all the named
3127 * properties of an object.
3128 * \param data A piece of data that will be passed to the callbacks
3129 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003130 */
3131 void SetNamedPropertyHandler(NamedPropertyGetter getter,
3132 NamedPropertySetter setter = 0,
3133 NamedPropertyQuery query = 0,
3134 NamedPropertyDeleter deleter = 0,
3135 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003136 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003137
3138 /**
3139 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003140 *
3141 * Whenever an indexed property is accessed on objects created from
3142 * this object template, the provided callback is invoked instead of
3143 * accessing the property directly on the JavaScript object.
3144 *
3145 * \param getter The callback to invoke when getting a property.
3146 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003147 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003148 * \param deleter The callback to invoke when deleting a property.
3149 * \param enumerator The callback to invoke to enumerate all the indexed
3150 * properties of an object.
3151 * \param data A piece of data that will be passed to the callbacks
3152 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003153 */
3154 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
3155 IndexedPropertySetter setter = 0,
3156 IndexedPropertyQuery query = 0,
3157 IndexedPropertyDeleter deleter = 0,
3158 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003159 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003160
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003161 /**
3162 * Sets the callback to be used when calling instances created from
3163 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00003164 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003165 * function.
3166 */
3167 void SetCallAsFunctionHandler(InvocationCallback callback,
3168 Handle<Value> data = Handle<Value>());
3169
v8.team.kasperl727e9952008-09-02 14:56:44 +00003170 /**
3171 * Mark object instances of the template as undetectable.
3172 *
3173 * In many ways, undetectable objects behave as though they are not
3174 * there. They behave like 'undefined' in conditionals and when
3175 * printed. However, properties can be accessed and called as on
3176 * normal objects.
3177 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178 void MarkAsUndetectable();
3179
v8.team.kasperl727e9952008-09-02 14:56:44 +00003180 /**
3181 * Sets access check callbacks on the object template.
3182 *
3183 * When accessing properties on instances of this object template,
3184 * the access check callback will be called to determine whether or
3185 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003186 * The last parameter specifies whether access checks are turned
3187 * on by default on instances. If access checks are off by default,
3188 * they can be turned on on individual instances by calling
3189 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00003190 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003191 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
3192 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003193 Handle<Value> data = Handle<Value>(),
3194 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003195
kasper.lund212ac232008-07-16 07:07:30 +00003196 /**
3197 * Gets the number of internal fields for objects generated from
3198 * this template.
3199 */
3200 int InternalFieldCount();
3201
3202 /**
3203 * Sets the number of internal fields for objects generated from
3204 * this template.
3205 */
3206 void SetInternalFieldCount(int value);
3207
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003208 private:
3209 ObjectTemplate();
3210 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
3211 friend class FunctionTemplate;
3212};
3213
3214
3215/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003216 * A Signature specifies which receivers and arguments are valid
3217 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003219class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220 public:
3221 static Local<Signature> New(Handle<FunctionTemplate> receiver =
3222 Handle<FunctionTemplate>(),
3223 int argc = 0,
3224 Handle<FunctionTemplate> argv[] = 0);
3225 private:
3226 Signature();
3227};
3228
3229
3230/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003231 * An AccessorSignature specifies which receivers are valid parameters
3232 * to an accessor callback.
3233 */
3234class V8EXPORT AccessorSignature : public Data {
3235 public:
3236 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
3237 Handle<FunctionTemplate>());
3238 private:
3239 AccessorSignature();
3240};
3241
3242
ulan@chromium.org750145a2013-03-07 15:14:13 +00003243class V8EXPORT DeclaredAccessorDescriptor : public Data {
3244 private:
3245 DeclaredAccessorDescriptor();
3246};
3247
3248
3249class V8EXPORT ObjectOperationDescriptor : public Data {
3250 public:
3251 // This function is not yet stable and should not be used at this time.
3252 static Local<RawOperationDescriptor> NewInternalFieldDereference(
3253 Isolate* isolate,
3254 int internal_field);
3255 private:
3256 ObjectOperationDescriptor();
3257};
3258
3259
3260enum DeclaredAccessorDescriptorDataType {
3261 kDescriptorBoolType,
3262 kDescriptorInt8Type, kDescriptorUint8Type,
3263 kDescriptorInt16Type, kDescriptorUint16Type,
3264 kDescriptorInt32Type, kDescriptorUint32Type,
3265 kDescriptorFloatType, kDescriptorDoubleType
3266};
3267
3268
3269class V8EXPORT RawOperationDescriptor : public Data {
3270 public:
3271 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
3272 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
3273 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
3274 int16_t byte_offset);
3275 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
3276 void* compare_value);
3277 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
3278 Isolate* isolate,
3279 DeclaredAccessorDescriptorDataType data_type,
3280 uint8_t bool_offset = 0);
3281 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
3282 uint8_t bitmask,
3283 uint8_t compare_value);
3284 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
3285 Isolate* isolate,
3286 uint16_t bitmask,
3287 uint16_t compare_value);
3288 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
3289 Isolate* isolate,
3290 uint32_t bitmask,
3291 uint32_t compare_value);
3292
3293 private:
3294 RawOperationDescriptor();
3295};
3296
3297
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003298/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003299 * A utility for determining the type of objects based on the template
3300 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003301 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003302class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003303 public:
3304 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
3305 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
3306 int match(Handle<Value> value);
3307 private:
3308 TypeSwitch();
3309};
3310
3311
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003312// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003313
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003314class V8EXPORT ExternalAsciiStringResourceImpl
3315 : public String::ExternalAsciiStringResource {
3316 public:
3317 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
3318 ExternalAsciiStringResourceImpl(const char* data, size_t length)
3319 : data_(data), length_(length) {}
3320 const char* data() const { return data_; }
3321 size_t length() const { return length_; }
3322
3323 private:
3324 const char* data_;
3325 size_t length_;
3326};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003327
3328/**
3329 * Ignore
3330 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003331class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003332 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003333 // Note that the strings passed into this constructor must live as long
3334 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003335 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003336 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003337 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003338 const char** deps = 0,
3339 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003340 virtual ~Extension() { }
3341 virtual v8::Handle<v8::FunctionTemplate>
3342 GetNativeFunction(v8::Handle<v8::String> name) {
3343 return v8::Handle<v8::FunctionTemplate>();
3344 }
3345
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003346 const char* name() const { return name_; }
3347 size_t source_length() const { return source_length_; }
3348 const String::ExternalAsciiStringResource* source() const {
3349 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003350 int dependency_count() { return dep_count_; }
3351 const char** dependencies() { return deps_; }
3352 void set_auto_enable(bool value) { auto_enable_ = value; }
3353 bool auto_enable() { return auto_enable_; }
3354
3355 private:
3356 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003357 size_t source_length_; // expected to initialize before source_
3358 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359 int dep_count_;
3360 const char** deps_;
3361 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003362
3363 // Disallow copying and assigning.
3364 Extension(const Extension&);
3365 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003366};
3367
3368
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003369void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003370
3371
3372/**
3373 * Ignore
3374 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003375class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003376 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003377 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003378 RegisterExtension(extension);
3379 }
3380};
3381
3382
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003383// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003384
3385
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003386Handle<Primitive> V8EXPORT Undefined();
3387Handle<Primitive> V8EXPORT Null();
3388Handle<Boolean> V8EXPORT True();
3389Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003390
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003391V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
3392V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
3393V8_INLINE(Handle<Boolean> True(Isolate* isolate));
3394V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003395
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003396
3397/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003398 * A set of constraints that specifies the limits of the runtime's memory use.
3399 * You must set the heap size before initializing the VM - the size cannot be
3400 * adjusted after the VM is initialized.
3401 *
3402 * If you are using threads then you should hold the V8::Locker lock while
3403 * setting the stack limit and you must set a non-default stack limit separately
3404 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003406class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003407 public:
3408 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003409 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003410 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003411 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003412 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003413 int max_executable_size() { return max_executable_size_; }
3414 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003415 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003416 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003417 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
3418 private:
3419 int max_young_space_size_;
3420 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003421 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003422 uint32_t* stack_limit_;
3423};
3424
3425
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003426bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003427
3428
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003429// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003430
3431
3432typedef void (*FatalErrorCallback)(const char* location, const char* message);
3433
3434
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003435typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003436
3437
3438/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003439 * Schedules an exception to be thrown when returning to JavaScript. When an
3440 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00003442 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003444Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003445
3446/**
3447 * Create new error objects by calling the corresponding error object
3448 * constructor with the message.
3449 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003450class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003451 public:
3452 static Local<Value> RangeError(Handle<String> message);
3453 static Local<Value> ReferenceError(Handle<String> message);
3454 static Local<Value> SyntaxError(Handle<String> message);
3455 static Local<Value> TypeError(Handle<String> message);
3456 static Local<Value> Error(Handle<String> message);
3457};
3458
3459
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003460// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003461
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003462typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003463
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003464typedef void* (*CreateHistogramCallback)(const char* name,
3465 int min,
3466 int max,
3467 size_t buckets);
3468
3469typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
3470
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003471// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003472 enum ObjectSpace {
3473 kObjectSpaceNewSpace = 1 << 0,
3474 kObjectSpaceOldPointerSpace = 1 << 1,
3475 kObjectSpaceOldDataSpace = 1 << 2,
3476 kObjectSpaceCodeSpace = 1 << 3,
3477 kObjectSpaceMapSpace = 1 << 4,
3478 kObjectSpaceLoSpace = 1 << 5,
3479
3480 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
3481 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
3482 kObjectSpaceLoSpace
3483 };
3484
3485 enum AllocationAction {
3486 kAllocationActionAllocate = 1 << 0,
3487 kAllocationActionFree = 1 << 1,
3488 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
3489 };
3490
3491typedef void (*MemoryAllocationCallback)(ObjectSpace space,
3492 AllocationAction action,
3493 int size);
3494
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003495// --- Leave Script Callback ---
3496typedef void (*CallCompletedCallback)();
3497
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003498// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499typedef void (*FailedAccessCheckCallback)(Local<Object> target,
3500 AccessType type,
3501 Local<Value> data);
3502
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003503// --- AllowCodeGenerationFromStrings callbacks ---
3504
3505/**
3506 * Callback to check if code generation from strings is allowed. See
3507 * Context::AllowCodeGenerationFromStrings.
3508 */
3509typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
3510
3511// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003512
3513/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003514 * Applications can register callback functions which will be called
3515 * before and after a garbage collection. Allocations are not
3516 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003517 * objects (set or delete properties for example) since it is possible
3518 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003519 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003520enum GCType {
3521 kGCTypeScavenge = 1 << 0,
3522 kGCTypeMarkSweepCompact = 1 << 1,
3523 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
3524};
3525
3526enum GCCallbackFlags {
3527 kNoGCCallbackFlags = 0,
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003528 kGCCallbackFlagCompacted = 1 << 0,
3529 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003530};
3531
3532typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
3533typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
3534
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003535typedef void (*GCCallback)();
3536
3537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003538/**
ager@chromium.org3811b432009-10-28 14:53:37 +00003539 * Collection of V8 heap information.
3540 *
3541 * Instances of this class can be passed to v8::V8::HeapStatistics to
3542 * get heap statistics from V8.
3543 */
3544class V8EXPORT HeapStatistics {
3545 public:
3546 HeapStatistics();
3547 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003548 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003549 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003550 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003551 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003552
3553 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003554 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003555 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003556 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003557 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003558 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003559
3560 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003561 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003562};
3563
3564
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003565class RetainedObjectInfo;
3566
ager@chromium.org3811b432009-10-28 14:53:37 +00003567/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003568 * Isolate represents an isolated instance of the V8 engine. V8
3569 * isolates have completely separate states. Objects from one isolate
3570 * must not be used in other isolates. When V8 is initialized a
3571 * default isolate is implicitly created and entered. The embedder
3572 * can create additional isolates and use them in parallel in multiple
3573 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003574 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003575 */
3576class V8EXPORT Isolate {
3577 public:
3578 /**
3579 * Stack-allocated class which sets the isolate for all operations
3580 * executed within a local scope.
3581 */
3582 class V8EXPORT Scope {
3583 public:
3584 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3585 isolate->Enter();
3586 }
3587
3588 ~Scope() { isolate_->Exit(); }
3589
3590 private:
3591 Isolate* const isolate_;
3592
3593 // Prevent copying of Scope objects.
3594 Scope(const Scope&);
3595 Scope& operator=(const Scope&);
3596 };
3597
3598 /**
3599 * Creates a new isolate. Does not change the currently entered
3600 * isolate.
3601 *
3602 * When an isolate is no longer used its resources should be freed
3603 * by calling Dispose(). Using the delete operator is not allowed.
3604 */
3605 static Isolate* New();
3606
3607 /**
3608 * Returns the entered isolate for the current thread or NULL in
3609 * case there is no current isolate.
3610 */
3611 static Isolate* GetCurrent();
3612
3613 /**
3614 * Methods below this point require holding a lock (using Locker) in
3615 * a multi-threaded environment.
3616 */
3617
3618 /**
3619 * Sets this isolate as the entered one for the current thread.
3620 * Saves the previously entered one (if any), so that it can be
3621 * restored when exiting. Re-entering an isolate is allowed.
3622 */
3623 void Enter();
3624
3625 /**
3626 * Exits this isolate by restoring the previously entered one in the
3627 * current thread. The isolate may still stay the same, if it was
3628 * entered more than once.
3629 *
3630 * Requires: this == Isolate::GetCurrent().
3631 */
3632 void Exit();
3633
3634 /**
3635 * Disposes the isolate. The isolate must not be entered by any
3636 * thread to be disposable.
3637 */
3638 void Dispose();
3639
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003640 /**
3641 * Associate embedder-specific data with the isolate
3642 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003643 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003644
3645 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003646 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003647 * Returns NULL if SetData has never been called.
3648 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003649 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003650
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003651 /**
3652 * Get statistics about the heap memory usage.
3653 */
3654 void GetHeapStatistics(HeapStatistics* heap_statistics);
3655
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003656 /**
3657 * Adjusts the amount of registered external memory. Used to give V8 an
3658 * indication of the amount of externally allocated memory that is kept alive
3659 * by JavaScript objects. V8 uses this to decide when to perform global
3660 * garbage collections. Registering externally allocated memory will trigger
3661 * global garbage collections more often than it would otherwise in an attempt
3662 * to garbage collect the JavaScript objects that keep the externally
3663 * allocated memory alive.
3664 *
3665 * \param change_in_bytes the change in externally allocated memory that is
3666 * kept alive by JavaScript objects.
3667 * \returns the adjusted value.
3668 */
3669 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3670
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003671 /**
3672 * Returns heap profiler for this isolate. Will return NULL until the isolate
3673 * is initialized.
3674 */
3675 HeapProfiler* GetHeapProfiler();
3676
3677 /**
3678 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3679 * is initialized.
3680 */
3681 CpuProfiler* GetCpuProfiler();
3682
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003683 /** Returns the context that is on the top of the stack. */
3684 Local<Context> GetCurrentContext();
3685
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003686 /**
3687 * Allows the host application to group objects together. If one
3688 * object in the group is alive, all objects in the group are alive.
3689 * After each garbage collection, object groups are removed. It is
3690 * intended to be used in the before-garbage-collection callback
3691 * function, for instance to simulate DOM tree connections among JS
3692 * wrapper objects. Object groups for all dependent handles need to
3693 * be provided for kGCTypeMarkSweepCompact collections, for all other
3694 * garbage collection types it is sufficient to provide object groups
3695 * for partially dependent handles only.
3696 */
3697 void SetObjectGroupId(const Persistent<Value>& object,
3698 UniqueId id);
3699
3700 /**
3701 * Allows the host application to declare implicit references from an object
3702 * group to an object. If the objects of the object group are alive, the child
3703 * object is alive too. After each garbage collection, all implicit references
3704 * are removed. It is intended to be used in the before-garbage-collection
3705 * callback function.
3706 */
3707 void SetReferenceFromGroup(UniqueId id,
3708 const Persistent<Value>& child);
3709
3710 /**
3711 * Allows the host application to declare implicit references from an object
3712 * to another object. If the parent object is alive, the child object is alive
3713 * too. After each garbage collection, all implicit references are removed. It
3714 * is intended to be used in the before-garbage-collection callback function.
3715 */
3716 void SetReference(const Persistent<Object>& parent,
3717 const Persistent<Value>& child);
3718
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003719 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003720 Isolate();
3721 Isolate(const Isolate&);
3722 ~Isolate();
3723 Isolate& operator=(const Isolate&);
3724 void* operator new(size_t size);
3725 void operator delete(void*, size_t);
3726};
3727
3728
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003729class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003730 public:
3731 enum CompressionAlgorithm {
3732 kUncompressed,
3733 kBZip2
3734 };
3735
3736 const char* data;
3737 int compressed_size;
3738 int raw_size;
3739};
3740
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003741
3742/**
3743 * A helper class for driving V8 startup data decompression. It is based on
3744 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3745 * for an embedder to use this class, instead, API functions can be used
3746 * directly.
3747 *
3748 * For an example of the class usage, see the "shell.cc" sample application.
3749 */
3750class V8EXPORT StartupDataDecompressor { // NOLINT
3751 public:
3752 StartupDataDecompressor();
3753 virtual ~StartupDataDecompressor();
3754 int Decompress();
3755
3756 protected:
3757 virtual int DecompressData(char* raw_data,
3758 int* raw_data_size,
3759 const char* compressed_data,
3760 int compressed_data_size) = 0;
3761
3762 private:
3763 char** raw_data;
3764};
3765
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003766
3767/**
3768 * EntropySource is used as a callback function when v8 needs a source
3769 * of entropy.
3770 */
3771typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3772
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003773
3774/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003775 * ReturnAddressLocationResolver is used as a callback function when v8 is
3776 * resolving the location of a return address on the stack. Profilers that
3777 * change the return address on the stack can use this to resolve the stack
3778 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003779 *
3780 * \param return_addr_location points to a location on stack where a machine
3781 * return address resides.
3782 * \returns either return_addr_location, or else a pointer to the profiler's
3783 * copy of the original return address.
3784 *
3785 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003786 */
3787typedef uintptr_t (*ReturnAddressLocationResolver)(
3788 uintptr_t return_addr_location);
3789
3790
3791/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003792 * FunctionEntryHook is the type of the profile entry hook called at entry to
3793 * any generated function when function-level profiling is enabled.
3794 *
3795 * \param function the address of the function that's being entered.
3796 * \param return_addr_location points to a location on stack where the machine
3797 * return address resides. This can be used to identify the caller of
3798 * \p function, and/or modified to divert execution when \p function exits.
3799 *
3800 * \note the entry hook must not cause garbage collection.
3801 */
3802typedef void (*FunctionEntryHook)(uintptr_t function,
3803 uintptr_t return_addr_location);
3804
3805
3806/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003807 * A JIT code event is issued each time code is added, moved or removed.
3808 *
3809 * \note removal events are not currently issued.
3810 */
3811struct JitCodeEvent {
3812 enum EventType {
3813 CODE_ADDED,
3814 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003815 CODE_REMOVED,
3816 CODE_ADD_LINE_POS_INFO,
3817 CODE_START_LINE_INFO_RECORDING,
3818 CODE_END_LINE_INFO_RECORDING
3819 };
3820 // Definition of the code position type. The "POSITION" type means the place
3821 // in the source code which are of interest when making stack traces to
3822 // pin-point the source location of a stack frame as close as possible.
3823 // The "STATEMENT_POSITION" means the place at the beginning of each
3824 // statement, and is used to indicate possible break locations.
3825 enum PositionType {
3826 POSITION,
3827 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003828 };
3829
3830 // Type of event.
3831 EventType type;
3832 // Start of the instructions.
3833 void* code_start;
3834 // Size of the instructions.
3835 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003836 // Script info for CODE_ADDED event.
3837 Handle<Script> script;
3838 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
3839 // code line information which is returned from the
3840 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
3841 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
3842 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003843
3844 union {
3845 // Only valid for CODE_ADDED.
3846 struct {
3847 // Name of the object associated with the code, note that the string is
3848 // not zero-terminated.
3849 const char* str;
3850 // Number of chars in str.
3851 size_t len;
3852 } name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003853
3854 // Only valid for CODE_ADD_LINE_POS_INFO
3855 struct {
3856 // PC offset
3857 size_t offset;
3858 // Code postion
3859 size_t pos;
3860 // The position type.
3861 PositionType position_type;
3862 } line_info;
3863
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003864 // New location of instructions. Only valid for CODE_MOVED.
3865 void* new_code_start;
3866 };
3867};
3868
3869/**
3870 * Option flags passed to the SetJitCodeEventHandler function.
3871 */
3872enum JitCodeEventOptions {
3873 kJitCodeEventDefault = 0,
3874 // Generate callbacks for already existent code.
3875 kJitCodeEventEnumExisting = 1
3876};
3877
3878
3879/**
3880 * Callback function passed to SetJitCodeEventHandler.
3881 *
3882 * \param event code add, move or removal event.
3883 */
3884typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3885
3886
3887/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003888 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003889 */
3890class V8EXPORT ExternalResourceVisitor { // NOLINT
3891 public:
3892 virtual ~ExternalResourceVisitor() {}
3893 virtual void VisitExternalString(Handle<String> string) {}
3894};
3895
3896
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003897/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003898 * Interface for iterating through all the persistent handles in the heap.
3899 */
3900class V8EXPORT PersistentHandleVisitor { // NOLINT
3901 public:
3902 virtual ~PersistentHandleVisitor() {}
3903 virtual void VisitPersistentHandle(Persistent<Value> value,
3904 uint16_t class_id) {}
3905};
3906
3907
3908/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00003909 * Asserts that no action is performed that could cause a handle's value
3910 * to be modified. Useful when otherwise unsafe handle operations need to
3911 * be performed.
3912 */
3913class V8EXPORT AssertNoGCScope {
3914#ifndef DEBUG
3915 V8_INLINE(AssertNoGCScope(Isolate* isolate)) {}
3916#else
3917 AssertNoGCScope(Isolate* isolate);
3918 ~AssertNoGCScope();
3919 private:
3920 Isolate* isolate_;
3921 bool last_state_;
3922#endif
3923};
3924
3925
3926/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003927 * Container class for static utility functions.
3928 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003929class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003931 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003932 static void SetFatalErrorHandler(FatalErrorCallback that);
3933
v8.team.kasperl727e9952008-09-02 14:56:44 +00003934 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003935 * Set the callback to invoke to check if code generation from
3936 * strings should be allowed.
3937 */
3938 static void SetAllowCodeGenerationFromStringsCallback(
3939 AllowCodeGenerationFromStringsCallback that);
3940
3941 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003942 * Ignore out-of-memory exceptions.
3943 *
3944 * V8 running out of memory is treated as a fatal error by default.
3945 * This means that the fatal error handler is called and that V8 is
3946 * terminated.
3947 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003948 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003949 * out-of-memory situation as a fatal error. This way, the contexts
3950 * that did not cause the out of memory problem might be able to
3951 * continue execution.
3952 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953 static void IgnoreOutOfMemoryException();
3954
v8.team.kasperl727e9952008-09-02 14:56:44 +00003955 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003956 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003957 * fatal errors such as out-of-memory situations.
3958 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003959 static bool IsDead();
3960
3961 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003962 * The following 4 functions are to be used when V8 is built with
3963 * the 'compress_startup_data' flag enabled. In this case, the
3964 * embedder must decompress startup data prior to initializing V8.
3965 *
3966 * This is how interaction with V8 should look like:
3967 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3968 * v8::StartupData* compressed_data =
3969 * new v8::StartupData[compressed_data_count];
3970 * v8::V8::GetCompressedStartupData(compressed_data);
3971 * ... decompress data (compressed_data can be updated in-place) ...
3972 * v8::V8::SetDecompressedStartupData(compressed_data);
3973 * ... now V8 can be initialized
3974 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003975 *
3976 * A helper class StartupDataDecompressor is provided. It implements
3977 * the protocol of the interaction described above, and can be used in
3978 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003979 */
3980 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3981 static int GetCompressedStartupDataCount();
3982 static void GetCompressedStartupData(StartupData* compressed_data);
3983 static void SetDecompressedStartupData(StartupData* decompressed_data);
3984
3985 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003986 * Adds a message listener.
3987 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003988 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003989 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003990 *
3991 * If data is specified, it will be passed to the callback when it is called.
3992 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003993 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00003994 static bool AddMessageListener(MessageCallback that,
3995 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003996
3997 /**
3998 * Remove all message listeners from the specified callback function.
3999 */
4000 static void RemoveMessageListeners(MessageCallback that);
4001
4002 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004003 * Tells V8 to capture current stack trace when uncaught exception occurs
4004 * and report it to the message listeners. The option is off by default.
4005 */
4006 static void SetCaptureStackTraceForUncaughtExceptions(
4007 bool capture,
4008 int frame_limit = 10,
4009 StackTrace::StackTraceOptions options = StackTrace::kOverview);
4010
4011 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004012 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004013 */
4014 static void SetFlagsFromString(const char* str, int length);
4015
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004016 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004017 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004018 */
4019 static void SetFlagsFromCommandLine(int* argc,
4020 char** argv,
4021 bool remove_flags);
4022
kasper.lund7276f142008-07-30 08:49:36 +00004023 /** Get the version string. */
4024 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004025
4026 /**
4027 * Enables the host application to provide a mechanism for recording
4028 * statistics counters.
4029 */
4030 static void SetCounterFunction(CounterLookupCallback);
4031
4032 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004033 * Enables the host application to provide a mechanism for recording
4034 * histograms. The CreateHistogram function returns a
4035 * histogram which will later be passed to the AddHistogramSample
4036 * function.
4037 */
4038 static void SetCreateHistogramFunction(CreateHistogramCallback);
4039 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
4040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004041 /** Callback function for reporting failed access checks.*/
4042 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
4043
4044 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004045 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004046 * garbage collection. Allocations are not allowed in the
4047 * callback function, you therefore cannot manipulate objects (set
4048 * or delete properties for example) since it is possible such
4049 * operations will result in the allocation of objects. It is possible
4050 * to specify the GCType filter for your callback. But it is not possible to
4051 * register the same callback function two times with different
4052 * GCType filters.
4053 */
4054 static void AddGCPrologueCallback(
4055 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
4056
4057 /**
4058 * This function removes callback which was installed by
4059 * AddGCPrologueCallback function.
4060 */
4061 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
4062
4063 /**
4064 * The function is deprecated. Please use AddGCPrologueCallback instead.
4065 * Enables the host application to receive a notification before a
4066 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00004067 * callback function, you therefore cannot manipulate objects (set
4068 * or delete properties for example) since it is possible such
4069 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004070 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004071 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004072
4073 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004074 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004075 * garbage collection. Allocations are not allowed in the
4076 * callback function, you therefore cannot manipulate objects (set
4077 * or delete properties for example) since it is possible such
4078 * operations will result in the allocation of objects. It is possible
4079 * to specify the GCType filter for your callback. But it is not possible to
4080 * register the same callback function two times with different
4081 * GCType filters.
4082 */
4083 static void AddGCEpilogueCallback(
4084 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
4085
4086 /**
4087 * This function removes callback which was installed by
4088 * AddGCEpilogueCallback function.
4089 */
4090 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
4091
4092 /**
4093 * The function is deprecated. Please use AddGCEpilogueCallback instead.
4094 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00004095 * major garbage collection. Allocations are not allowed in the
4096 * callback function, you therefore cannot manipulate objects (set
4097 * or delete properties for example) since it is possible such
4098 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004099 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004100 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004101
4102 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004103 * Enables the host application to provide a mechanism to be notified
4104 * and perform custom logging when V8 Allocates Executable Memory.
4105 */
4106 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
4107 ObjectSpace space,
4108 AllocationAction action);
4109
4110 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004111 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004112 */
4113 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
4114
4115 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004116 * Adds a callback to notify the host application when a script finished
4117 * running. If a script re-enters the runtime during executing, the
4118 * CallCompletedCallback is only invoked when the outer-most script
4119 * execution ends. Executing scripts inside the callback do not trigger
4120 * further callbacks.
4121 */
4122 static void AddCallCompletedCallback(CallCompletedCallback callback);
4123
4124 /**
4125 * Removes callback that was installed by AddCallCompletedCallback.
4126 */
4127 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
4128
4129 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004130 * Allows the host application to group objects together. If one
4131 * object in the group is alive, all objects in the group are alive.
4132 * After each garbage collection, object groups are removed. It is
4133 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00004134 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004135 * wrapper objects. Object groups for all dependent handles need to
4136 * be provided for kGCTypeMarkSweepCompact collections, for all other
4137 * garbage collection types it is sufficient to provide object groups
4138 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004139 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004140 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004141 // TODO(marja): deprecate AddObjectGroup. Use Isolate::SetObjectGroupId and
4142 // HeapProfiler::SetRetainedObjectInfo instead.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004143 static void AddObjectGroup(Persistent<Value>* objects,
4144 size_t length,
4145 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004146 static void AddObjectGroup(Isolate* isolate,
4147 Persistent<Value>* objects,
4148 size_t length,
4149 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004150
4151 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004152 * Allows the host application to declare implicit references between
4153 * the objects: if |parent| is alive, all |children| are alive too.
4154 * After each garbage collection, all implicit references
4155 * are removed. It is intended to be used in the before-garbage-collection
4156 * callback function.
4157 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004158 // TODO(marja): Deprecate AddImplicitReferences. Use
4159 // Isolate::SetReferenceFromGroup instead.
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004160 static void AddImplicitReferences(Persistent<Object> parent,
4161 Persistent<Value>* children,
4162 size_t length);
4163
4164 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004165 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004166 * initialize from scratch. This function is called implicitly if
4167 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004168 */
4169 static bool Initialize();
4170
kasper.lund7276f142008-07-30 08:49:36 +00004171 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004172 * Allows the host application to provide a callback which can be used
4173 * as a source of entropy for random number generators.
4174 */
4175 static void SetEntropySource(EntropySource source);
4176
4177 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004178 * Allows the host application to provide a callback that allows v8 to
4179 * cooperate with a profiler that rewrites return addresses on stack.
4180 */
4181 static void SetReturnAddressLocationResolver(
4182 ReturnAddressLocationResolver return_address_resolver);
4183
4184 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004185 * Allows the host application to provide the address of a function that's
4186 * invoked on entry to every V8-generated function.
4187 * Note that \p entry_hook is invoked at the very start of each
4188 * generated function.
4189 *
4190 * \param entry_hook a function that will be invoked on entry to every
4191 * V8-generated function.
4192 * \returns true on success on supported platforms, false on failure.
4193 * \note Setting a new entry hook function when one is already active will
4194 * fail.
4195 */
4196 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
4197
4198 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004199 * Allows the host application to provide the address of a function that is
4200 * notified each time code is added, moved or removed.
4201 *
4202 * \param options options for the JIT code event handler.
4203 * \param event_handler the JIT code event handler, which will be invoked
4204 * each time code is added, moved or removed.
4205 * \note \p event_handler won't get notified of existent code.
4206 * \note since code removal notifications are not currently issued, the
4207 * \p event_handler may get notifications of code that overlaps earlier
4208 * code notifications. This happens when code areas are reused, and the
4209 * earlier overlapping code areas should therefore be discarded.
4210 * \note the events passed to \p event_handler and the strings they point to
4211 * are not guaranteed to live past each call. The \p event_handler must
4212 * copy strings and other parameters it needs to keep around.
4213 * \note the set of events declared in JitCodeEvent::EventType is expected to
4214 * grow over time, and the JitCodeEvent structure is expected to accrue
4215 * new members. The \p event_handler function must ignore event codes
4216 * it does not recognize to maintain future compatibility.
4217 */
4218 static void SetJitCodeEventHandler(JitCodeEventOptions options,
4219 JitCodeEventHandler event_handler);
4220
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004221 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
4222 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004223 static intptr_t AdjustAmountOfExternalAllocatedMemory(
4224 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00004225
iposva@chromium.org245aa852009-02-10 00:49:54 +00004226 /**
4227 * Suspends recording of tick samples in the profiler.
4228 * When the V8 profiling mode is enabled (usually via command line
4229 * switches) this function suspends recording of tick samples.
4230 * Profiling ticks are discarded until ResumeProfiler() is called.
4231 *
4232 * See also the --prof and --prof_auto command line switches to
4233 * enable V8 profiling.
4234 */
4235 static void PauseProfiler();
4236
4237 /**
4238 * Resumes recording of tick samples in the profiler.
4239 * See also PauseProfiler().
4240 */
4241 static void ResumeProfiler();
4242
ager@chromium.org41826e72009-03-30 13:30:57 +00004243 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004244 * Return whether profiler is currently paused.
4245 */
4246 static bool IsProfilerPaused();
4247
4248 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004249 * Retrieve the V8 thread id of the calling thread.
4250 *
4251 * The thread id for a thread should only be retrieved after the V8
4252 * lock has been acquired with a Locker object with that thread.
4253 */
4254 static int GetCurrentThreadId();
4255
4256 /**
4257 * Forcefully terminate execution of a JavaScript thread. This can
4258 * be used to terminate long-running scripts.
4259 *
4260 * TerminateExecution should only be called when then V8 lock has
4261 * been acquired with a Locker object. Therefore, in order to be
4262 * able to terminate long-running threads, preemption must be
4263 * enabled to allow the user of TerminateExecution to acquire the
4264 * lock.
4265 *
4266 * The termination is achieved by throwing an exception that is
4267 * uncatchable by JavaScript exception handlers. Termination
4268 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004269 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004270 * exception handler that catches an exception should check if that
4271 * exception is a termination exception and immediately return if
4272 * that is the case. Returning immediately in that case will
4273 * continue the propagation of the termination exception if needed.
4274 *
4275 * The thread id passed to TerminateExecution must have been
4276 * obtained by calling GetCurrentThreadId on the thread in question.
4277 *
4278 * \param thread_id The thread id of the thread to terminate.
4279 */
4280 static void TerminateExecution(int thread_id);
4281
4282 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004283 * Forcefully terminate the current thread of JavaScript execution
4284 * in the given isolate. If no isolate is provided, the default
4285 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004286 *
4287 * This method can be used by any thread even if that thread has not
4288 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004289 *
4290 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004291 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004292 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00004293
4294 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004295 * Is V8 terminating JavaScript execution.
4296 *
4297 * Returns true if JavaScript execution is currently terminating
4298 * because of a call to TerminateExecution. In that case there are
4299 * still JavaScript frames on the stack and the termination
4300 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004301 *
4302 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004303 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004304 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004305
4306 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004307 * Resume execution capability in the given isolate, whose execution
4308 * was previously forcefully terminated using TerminateExecution().
4309 *
4310 * When execution is forcefully terminated using TerminateExecution(),
4311 * the isolate can not resume execution until all JavaScript frames
4312 * have propagated the uncatchable exception which is generated. This
4313 * method allows the program embedding the engine to handle the
4314 * termination event and resume execution capability, even if
4315 * JavaScript frames remain on the stack.
4316 *
4317 * This method can be used by any thread even if that thread has not
4318 * acquired the V8 lock with a Locker object.
4319 *
4320 * \param isolate The isolate in which to resume execution capability.
4321 */
4322 static void CancelTerminateExecution(Isolate* isolate);
4323
4324 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00004325 * Releases any resources used by v8 and stops any utility threads
4326 * that may be running. Note that disposing v8 is permanent, it
4327 * cannot be reinitialized.
4328 *
4329 * It should generally not be necessary to dispose v8 before exiting
4330 * a process, this should happen automatically. It is only necessary
4331 * to use if the process needs the resources taken up by v8.
4332 */
4333 static bool Dispose();
4334
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00004335 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
4336 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004337
4338 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004339 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004340 * heap. GC is not invoked prior to iterating, therefore there is no
4341 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004342 */
4343 static void VisitExternalResources(ExternalResourceVisitor* visitor);
4344
4345 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004346 * Iterates through all the persistent handles in the current isolate's heap
4347 * that have class_ids.
4348 */
4349 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
4350
4351 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004352 * Iterates through all the persistent handles in the current isolate's heap
4353 * that have class_ids and are candidates to be marked as partially dependent
4354 * handles. This will visit handles to young objects created since the last
4355 * garbage collection but is free to visit an arbitrary superset of these
4356 * objects.
4357 */
4358 static void VisitHandlesForPartialDependence(
4359 Isolate* isolate, PersistentHandleVisitor* visitor);
4360
4361 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004362 * Optional notification that the embedder is idle.
4363 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004364 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004365 * Returns true if the embedder should stop calling IdleNotification
4366 * until real work has been done. This indicates that V8 has done
4367 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004368 *
4369 * The hint argument specifies the amount of work to be done in the function
4370 * on scale from 1 to 1000. There is no guarantee that the actual work will
4371 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004372 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004373 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004374
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004375 /**
4376 * Optional notification that the system is running low on memory.
4377 * V8 uses these notifications to attempt to free memory.
4378 */
4379 static void LowMemoryNotification();
4380
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004381 /**
4382 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004383 * these notifications to guide the GC heuristic. Returns the number
4384 * of context disposals - including this one - since the last time
4385 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004386 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004387 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004388
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004389 private:
4390 V8();
4391
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004392 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
4393 internal::Object** handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004394 static void DisposeGlobal(internal::Isolate* isolate,
4395 internal::Object** global_handle);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004396 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004397 static void MakeWeak(internal::Isolate* isolate,
4398 internal::Object** global_handle,
4399 void* data,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004400 RevivableCallback weak_reference_callback,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004401 NearDeathCallback near_death_callback);
4402 static void ClearWeak(internal::Isolate* isolate,
4403 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004404
4405 template <class T> friend class Handle;
4406 template <class T> friend class Local;
4407 template <class T> friend class Persistent;
4408 friend class Context;
4409};
4410
4411
4412/**
4413 * An external exception handler.
4414 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004415class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004416 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004417 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004418 * Creates a new try/catch block and registers it with v8. Note that
4419 * all TryCatch blocks should be stack allocated because the memory
4420 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004421 */
4422 TryCatch();
4423
4424 /**
4425 * Unregisters and deletes this try/catch block.
4426 */
4427 ~TryCatch();
4428
4429 /**
4430 * Returns true if an exception has been caught by this try/catch block.
4431 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004432 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004433
4434 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004435 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004436 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004437 * If CanContinue returns false, the correct action is to perform any C++
4438 * cleanup needed and then return. If CanContinue returns false and
4439 * HasTerminated returns true, it is possible to call
4440 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004441 */
4442 bool CanContinue() const;
4443
4444 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004445 * Returns true if an exception has been caught due to script execution
4446 * being terminated.
4447 *
4448 * There is no JavaScript representation of an execution termination
4449 * exception. Such exceptions are thrown when the TerminateExecution
4450 * methods are called to terminate a long-running script.
4451 *
4452 * If such an exception has been thrown, HasTerminated will return true,
4453 * indicating that it is possible to call CancelTerminateExecution in order
4454 * to continue calling into the engine.
4455 */
4456 bool HasTerminated() const;
4457
4458 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004459 * Throws the exception caught by this TryCatch in a way that avoids
4460 * it being caught again by this same TryCatch. As with ThrowException
4461 * it is illegal to execute any JavaScript operations after calling
4462 * ReThrow; the caller must return immediately to where the exception
4463 * is caught.
4464 */
4465 Handle<Value> ReThrow();
4466
4467 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004468 * Returns the exception caught by this try/catch block. If no exception has
4469 * been caught an empty handle is returned.
4470 *
4471 * The returned handle is valid until this TryCatch block has been destroyed.
4472 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004473 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004474
4475 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004476 * Returns the .stack property of the thrown object. If no .stack
4477 * property is present an empty handle is returned.
4478 */
4479 Local<Value> StackTrace() const;
4480
4481 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004482 * Returns the message associated with this exception. If there is
4483 * no message associated an empty handle is returned.
4484 *
4485 * The returned handle is valid until this TryCatch block has been
4486 * destroyed.
4487 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004488 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004489
4490 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004491 * Clears any exceptions that may have been caught by this try/catch block.
4492 * After this method has been called, HasCaught() will return false.
4493 *
4494 * It is not necessary to clear a try/catch block before using it again; if
4495 * another exception is thrown the previously caught exception will just be
4496 * overwritten. However, it is often a good idea since it makes it easier
4497 * to determine which operation threw a given exception.
4498 */
4499 void Reset();
4500
v8.team.kasperl727e9952008-09-02 14:56:44 +00004501 /**
4502 * Set verbosity of the external exception handler.
4503 *
4504 * By default, exceptions that are caught by an external exception
4505 * handler are not reported. Call SetVerbose with true on an
4506 * external exception handler to have exceptions caught by the
4507 * handler reported as if they were not caught.
4508 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004509 void SetVerbose(bool value);
4510
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004511 /**
4512 * Set whether or not this TryCatch should capture a Message object
4513 * which holds source information about where the exception
4514 * occurred. True by default.
4515 */
4516 void SetCaptureMessage(bool value);
4517
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004518 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004519 // Make it hard to create heap-allocated TryCatch blocks.
4520 TryCatch(const TryCatch&);
4521 void operator=(const TryCatch&);
4522 void* operator new(size_t size);
4523 void operator delete(void*, size_t);
4524
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004525 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004526 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004527 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004528 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004529 bool is_verbose_ : 1;
4530 bool can_continue_ : 1;
4531 bool capture_message_ : 1;
4532 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004533 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004534
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004535 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004536};
4537
4538
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004539// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004540
4541
4542/**
4543 * Ignore
4544 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004545class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004546 public:
4547 ExtensionConfiguration(int name_count, const char* names[])
4548 : name_count_(name_count), names_(names) { }
4549 private:
4550 friend class ImplementationUtilities;
4551 int name_count_;
4552 const char** names_;
4553};
4554
4555
4556/**
4557 * A sandboxed execution context with its own set of built-in objects
4558 * and functions.
4559 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004560class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004561 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004562 /**
4563 * Returns the global proxy object or global object itself for
4564 * detached contexts.
4565 *
4566 * Global proxy object is a thin wrapper whose prototype points to
4567 * actual context's global object with the properties like Object, etc.
4568 * This is done that way for security reasons (for more details see
4569 * https://wiki.mozilla.org/Gecko:SplitWindow).
4570 *
4571 * Please note that changes to global proxy object prototype most probably
4572 * would break VM---v8 expects only global object as a prototype of
4573 * global proxy object.
4574 *
4575 * If DetachGlobal() has been invoked, Global() would return actual global
4576 * object until global is reattached with ReattachGlobal().
4577 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004578 Local<Object> Global();
4579
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004580 /**
4581 * Detaches the global object from its context before
4582 * the global object can be reused to create a new context.
4583 */
4584 void DetachGlobal();
4585
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00004586 /**
4587 * Reattaches a global object to a context. This can be used to
4588 * restore the connection between a global object and a context
4589 * after DetachGlobal has been called.
4590 *
4591 * \param global_object The global object to reattach to the
4592 * context. For this to work, the global object must be the global
4593 * object that was associated with this context before a call to
4594 * DetachGlobal.
4595 */
4596 void ReattachGlobal(Handle<Object> global_object);
4597
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004598 /**
4599 * Creates a new context and returns a handle to the newly allocated
4600 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004601 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004602 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004603 *
4604 * \param extensions An optional extension configuration containing
4605 * the extensions to be installed in the newly created context.
4606 *
4607 * \param global_template An optional object template from which the
4608 * global object for the newly created context will be created.
4609 *
4610 * \param global_object An optional global object to be reused for
4611 * the newly created context. This global object must have been
4612 * created by a previous call to Context::New with the same global
4613 * template. The state of the global object will be completely reset
4614 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004615 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004616 static Local<Context> New(
4617 Isolate* isolate,
4618 ExtensionConfiguration* extensions = NULL,
4619 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4620 Handle<Value> global_object = Handle<Value>());
4621
4622 /** Deprecated. Use Isolate version instead. */
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004623 V8_DEPRECATED(static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004624 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004625 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004626 Handle<Value> global_object = Handle<Value>()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004627
kasper.lund44510672008-07-25 07:37:58 +00004628 /** Returns the last entered context. */
4629 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004630
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004631 // TODO(svenpanne) Actually deprecate this.
4632 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004633 static Local<Context> GetCurrent();
4634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004635 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004636 * Returns the context of the calling JavaScript code. That is the
4637 * context of the top-most JavaScript frame. If there are no
4638 * JavaScript frames an empty handle is returned.
4639 */
4640 static Local<Context> GetCalling();
4641
4642 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004643 * Sets the security token for the context. To access an object in
4644 * another context, the security tokens must match.
4645 */
4646 void SetSecurityToken(Handle<Value> token);
4647
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004648 /** Restores the security token to the default value. */
4649 void UseDefaultSecurityToken();
4650
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004651 /** Returns the security token of this context.*/
4652 Handle<Value> GetSecurityToken();
4653
v8.team.kasperl727e9952008-09-02 14:56:44 +00004654 /**
4655 * Enter this context. After entering a context, all code compiled
4656 * and run is compiled and run in this context. If another context
4657 * is already entered, this old context is saved so it can be
4658 * restored when the new context is exited.
4659 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004660 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004661
4662 /**
4663 * Exit this context. Exiting the current context restores the
4664 * context that was in place when entering the current context.
4665 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004666 void Exit();
4667
v8.team.kasperl727e9952008-09-02 14:56:44 +00004668 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004669 bool HasOutOfMemoryException();
4670
v8.team.kasperl727e9952008-09-02 14:56:44 +00004671 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004672 static bool InContext();
4673
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004674 /** Returns an isolate associated with a current context. */
4675 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004676
4677 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004678 * Gets the embedder data with the given index, which must have been set by a
4679 * previous call to SetEmbedderData with the same index. Note that index 0
4680 * currently has a special meaning for Chrome's debugger.
4681 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004682 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004683
4684 /**
4685 * Sets the embedder data with the given index, growing the data as
4686 * needed. Note that index 0 currently has a special meaning for Chrome's
4687 * debugger.
4688 */
4689 void SetEmbedderData(int index, Handle<Value> value);
4690
4691 /**
4692 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4693 * index, which must have bees set by a previous call to
4694 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4695 * currently has a special meaning for Chrome's debugger.
4696 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004697 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004698
4699 /**
4700 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4701 * index, growing the data as needed. Note that index 0 currently has a
4702 * special meaning for Chrome's debugger.
4703 */
4704 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004705
4706 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004707 * Control whether code generation from strings is allowed. Calling
4708 * this method with false will disable 'eval' and the 'Function'
4709 * constructor for code running in this context. If 'eval' or the
4710 * 'Function' constructor are used an exception will be thrown.
4711 *
4712 * If code generation from strings is not allowed the
4713 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4714 * set before blocking the call to 'eval' or the 'Function'
4715 * constructor. If that callback returns true, the call will be
4716 * allowed, otherwise an exception will be thrown. If no callback is
4717 * set an exception will be thrown.
4718 */
4719 void AllowCodeGenerationFromStrings(bool allow);
4720
4721 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004722 * Returns true if code generation from strings is allowed for the context.
4723 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4724 */
4725 bool IsCodeGenerationFromStringsAllowed();
4726
4727 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004728 * Sets the error description for the exception that is thrown when
4729 * code generation from strings is not allowed and 'eval' or the 'Function'
4730 * constructor are called.
4731 */
4732 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4733
4734 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004735 * Stack-allocated class which sets the execution context for all
4736 * operations executed within a local scope.
4737 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004738 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004739 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004740 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004741 context_->Enter();
4742 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004743 V8_INLINE(Scope(Isolate* isolate, Persistent<Context>& context)) // NOLINT
4744#ifndef V8_USE_UNSAFE_HANDLES
4745 : context_(Handle<Context>::New(isolate, context)) {
4746#else
4747 : context_(Local<Context>::New(isolate, context)) {
4748#endif
4749 context_->Enter();
4750 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004751 V8_INLINE(~Scope()) { context_->Exit(); }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004752
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004753 private:
4754 Handle<Context> context_;
4755 };
4756
4757 private:
4758 friend class Value;
4759 friend class Script;
4760 friend class Object;
4761 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004762
4763 Local<Value> SlowGetEmbedderData(int index);
4764 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004765};
4766
4767
4768/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004769 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4770 * to use any given V8 isolate, see the comments in the Isolate class. The
4771 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4772 * object pointers obtained from V8 handles while in the particular V8 isolate.
4773 * It is up to the user of V8 to ensure, perhaps with locking, that this
4774 * constraint is not violated. In addition to any other synchronization
4775 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4776 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004777 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004778 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4779 * construction and destruction, the current thread is allowed to use the locked
4780 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4781 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004782 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004783 * Sample usage:
4784* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004785 * ...
4786 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004787 * v8::Locker locker(isolate);
4788 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004789 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004790 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004791 * ...
4792 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00004793 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004794 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004795 * If you wish to stop using V8 in a thread A you can do this either by
4796 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
4797 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004798 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004799 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004800 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004801 * isolate->Exit();
4802 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004803 * ...
4804 * // Code not using V8 goes here while V8 can run in another thread.
4805 * ...
4806 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004807 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004808 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004809 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004810 * The Unlocker object is intended for use in a long-running callback from V8,
4811 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004812 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004813 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
4814 * given thread. This can be useful if you have code that can be called either
4815 * from code that holds the lock or from code that does not. The Unlocker is
4816 * not recursive so you can not have several Unlockers on the stack at once, and
4817 * 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 +00004818 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004819 * An unlocker will unlock several lockers if it has to and reinstate the
4820 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004821 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004822 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004823 * // V8 not locked.
4824 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004825 * v8::Locker locker(isolate);
4826 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004827 * // V8 locked.
4828 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004829 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004830 * // V8 still locked (2 levels).
4831 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004832 * isolate->Exit();
4833 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004834 * // V8 not locked.
4835 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004836 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004837 * // V8 locked again (2 levels).
4838 * }
4839 * // V8 still locked (1 level).
4840 * }
4841 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004842 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004843 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004844class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004845 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004846 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004847 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004848 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004849 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
4850
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004851 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004852 V8_DEPRECATED(Unlocker());
4853
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004854 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00004855 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004856 void Initialize(Isolate* isolate);
4857
lrn@chromium.org1c092762011-05-09 09:42:16 +00004858 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004859};
4860
4861
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004862class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004863 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004864 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004865 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004866 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004867 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
4868
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004869 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004870 V8_DEPRECATED(Locker());
4871
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004872 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004873
4874 /**
4875 * Start preemption.
4876 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004877 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004878 * that will switch between multiple threads that are in contention
4879 * for the V8 lock.
4880 */
4881 static void StartPreemption(int every_n_ms);
4882
4883 /**
4884 * Stop preemption.
4885 */
4886 static void StopPreemption();
4887
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004888 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004889 * Returns whether or not the locker for a given isolate, is locked by the
4890 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004891 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004892 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004893
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004894 /**
4895 * Returns whether v8::Locker is being used by this V8 instance.
4896 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004897 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004898
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004899 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004900 void Initialize(Isolate* isolate);
4901
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004902 bool has_lock_;
4903 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004904 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004905
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004906 static bool active_;
4907
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004908 // Disallow copying and assigning.
4909 Locker(const Locker&);
4910 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004911};
4912
4913
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004914/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004915 * A struct for exporting HeapStats data from V8, using "push" model.
4916 */
4917struct HeapStatsUpdate;
4918
4919
4920/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004921 * An interface for exporting data from V8, using "push" model.
4922 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004923class V8EXPORT OutputStream { // NOLINT
4924 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004925 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004926 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004927 };
4928 enum WriteResult {
4929 kContinue = 0,
4930 kAbort = 1
4931 };
4932 virtual ~OutputStream() {}
4933 /** Notify about the end of stream. */
4934 virtual void EndOfStream() = 0;
4935 /** Get preferred output chunk size. Called only once. */
4936 virtual int GetChunkSize() { return 1024; }
4937 /** Get preferred output encoding. Called only once. */
4938 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4939 /**
4940 * Writes the next chunk of snapshot data into the stream. Writing
4941 * can be stopped by returning kAbort as function result. EndOfStream
4942 * will not be called in case writing was aborted.
4943 */
4944 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004945 /**
4946 * Writes the next chunk of heap stats data into the stream. Writing
4947 * can be stopped by returning kAbort as function result. EndOfStream
4948 * will not be called in case writing was aborted.
4949 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004950 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004951 return kAbort;
4952 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004953};
4954
4955
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004956/**
4957 * An interface for reporting progress and controlling long-running
4958 * activities.
4959 */
4960class V8EXPORT ActivityControl { // NOLINT
4961 public:
4962 enum ControlOption {
4963 kContinue = 0,
4964 kAbort = 1
4965 };
4966 virtual ~ActivityControl() {}
4967 /**
4968 * Notify about current progress. The activity can be stopped by
4969 * returning kAbort as the callback result.
4970 */
4971 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4972};
4973
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004974
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004975// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004976
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004977
4978namespace internal {
4979
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004980const int kApiPointerSize = sizeof(void*); // NOLINT
4981const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004982
4983// Tag information for HeapObject.
4984const int kHeapObjectTag = 1;
4985const int kHeapObjectTagSize = 2;
4986const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4987
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004988// Tag information for Smi.
4989const int kSmiTag = 0;
4990const int kSmiTagSize = 1;
4991const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4992
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004993template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004994
4995// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004996template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004997 static const int kSmiShiftSize = 0;
4998 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004999 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005000 int shift_bits = kSmiTagSize + kSmiShiftSize;
5001 // Throw away top 32 bits and shift down (requires >> to be sign extending).
5002 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
5003 }
5004};
5005
5006// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005007template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005008 static const int kSmiShiftSize = 31;
5009 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005010 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005011 int shift_bits = kSmiTagSize + kSmiShiftSize;
5012 // Shift down and throw away top 32 bits.
5013 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
5014 }
5015};
5016
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005017typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
5018const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
5019const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005020
5021/**
5022 * This class exports constants and functionality from within v8 that
5023 * is necessary to implement inline functions in the v8 api. Don't
5024 * depend on functions and constants defined here.
5025 */
5026class Internals {
5027 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005028 // These values match non-compiler-dependent values defined within
5029 // the implementation of v8.
5030 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005031 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005032 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005033
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005034 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00005035 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005036 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005037 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
5038 static const int kContextHeaderSize = 2 * kApiPointerSize;
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005039 static const int kContextEmbedderDataIndex = 64;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005040 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005041 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00005042 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005043 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005044
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005045 static const int kIsolateStateOffset = 0;
5046 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
5047 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
5048 static const int kUndefinedValueRootIndex = 5;
5049 static const int kNullValueRootIndex = 7;
5050 static const int kTrueValueRootIndex = 8;
5051 static const int kFalseValueRootIndex = 9;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005052 static const int kEmptyStringRootIndex = 118;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005053
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005054 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
5055 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
5056 static const int kNodeStateMask = 0xf;
5057 static const int kNodeStateIsWeakValue = 2;
5058 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005059 static const int kNodeIsIndependentShift = 4;
5060 static const int kNodeIsPartiallyDependentShift = 5;
5061
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005062 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005063 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005064 static const int kOddballType = 0x83;
5065 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005066
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005067 static const int kUndefinedOddballKind = 5;
5068 static const int kNullOddballKind = 3;
5069
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005070 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005071 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
5072 kHeapObjectTag);
5073 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005074
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005075 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005076 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005077 }
5078
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005079 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005080 typedef internal::Object O;
5081 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
5082 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
5083 }
5084
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005085 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005086 typedef internal::Object O;
5087 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
5088 }
5089
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005090 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005091 int representation = (instance_type & kFullStringRepresentationMask);
5092 return representation == kExternalTwoByteRepresentationTag;
5093 }
5094
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005095 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005096 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
5097 return *reinterpret_cast<int*>(addr) == 1;
5098 }
5099
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005100 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
5101 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5102 return *addr & (1 << shift);
5103 }
5104
5105 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
5106 bool value, int shift)) {
5107 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5108 uint8_t mask = 1 << shift;
5109 *addr = (*addr & ~mask) | (value << shift);
5110 }
5111
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005112 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
5113 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5114 return *addr & kNodeStateMask;
5115 }
5116
5117 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
5118 uint8_t value)) {
5119 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5120 *addr = (*addr & ~kNodeStateMask) | value;
5121 }
5122
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005123 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005124 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5125 kIsolateEmbedderDataOffset;
5126 *reinterpret_cast<void**>(addr) = data;
5127 }
5128
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005129 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005130 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5131 kIsolateEmbedderDataOffset;
5132 return *reinterpret_cast<void**>(addr);
5133 }
5134
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005135 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
5136 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005137 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
5138 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
5139 }
5140
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005141 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005142 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005143 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
5144 return *reinterpret_cast<T*>(addr);
5145 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005146
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005147 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005148 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005149 typedef internal::Object O;
5150 typedef internal::Internals I;
5151 O* ctx = *reinterpret_cast<O**>(context);
5152 int embedder_data_offset = I::kContextHeaderSize +
5153 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
5154 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
5155 int value_offset =
5156 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
5157 return I::ReadField<T>(embedder_data, value_offset);
5158 }
5159
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005160 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
5161 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
5162 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
5163 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
5164 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
5165 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
5166 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005167};
5168
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005169} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005170
5171
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005172template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005173Local<T>::Local() : Handle<T>() { }
5174
5175
5176template <class T>
5177Local<T> Local<T>::New(Handle<T> that) {
5178 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005179 T* that_ptr = *that;
5180 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5181 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
5182 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5183 reinterpret_cast<internal::HeapObject*>(*p))));
5184 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005185 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
5186}
5187
5188
5189template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005190Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005191 return New(isolate, that.val_);
5192}
5193
5194#ifndef V8_USE_UNSAFE_HANDLES
5195template <class T>
5196Local<T> Local<T>::New(Isolate* isolate, const Persistent<T>& that) {
5197 return New(isolate, that.val_);
5198}
5199
5200template <class T>
5201Handle<T> Handle<T>::New(Isolate* isolate, T* that) {
5202 if (that == NULL) return Handle<T>();
5203 T* that_ptr = that;
5204 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5205 return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5206 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5207}
5208#endif
5209
5210
5211template <class T>
5212Local<T> Local<T>::New(Isolate* isolate, T* that) {
5213 if (that == NULL) return Local<T>();
5214 T* that_ptr = that;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005215 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5216 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5217 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5218}
5219
5220
5221template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005222Persistent<T> Persistent<T>::New(Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005223 return New(Isolate::GetCurrent(), that.val_);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005224}
5225
5226
5227template <class T>
5228Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005229 return New(Isolate::GetCurrent(), that.val_);
5230}
5231
5232#ifndef V8_USE_UNSAFE_HANDLES
5233template <class T>
5234Persistent<T> Persistent<T>::New(Isolate* isolate, Persistent<T> that) {
5235 return New(Isolate::GetCurrent(), that.val_);
5236}
5237#endif
5238
5239template <class T>
5240Persistent<T> Persistent<T>::New(Isolate* isolate, T* that) {
5241 if (that == NULL) return Persistent<T>();
5242 internal::Object** p = reinterpret_cast<internal::Object**>(that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005243 return Persistent<T>(reinterpret_cast<T*>(
5244 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
5245 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005246}
5247
5248
5249template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005250bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005251 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005252}
5253
5254
5255template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005256bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005257 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005258 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005259 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005260 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005261 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005262}
5263
5264
5265template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005266bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005267 return IsNearDeath(Isolate::GetCurrent());
5268}
5269
5270
5271template <class T>
5272bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
5273 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005274 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005275 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005276 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005277 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005278}
5279
5280
5281template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005282bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005283 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005284}
5285
5286
5287template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005288bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005289 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005290 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005291 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005292 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005293 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005294}
5295
5296
5297template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005298void Persistent<T>::Dispose() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005299 Dispose(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005300}
5301
5302
5303template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005304void Persistent<T>::Dispose(Isolate* isolate) {
5305 if (this->IsEmpty()) return;
5306 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005307 reinterpret_cast<internal::Object**>(this->val_));
5308#ifndef V8_USE_UNSAFE_HANDLES
5309 val_ = 0;
5310#endif
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005311}
5312
5313
5314template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005315template <typename S, typename P>
5316void Persistent<T>::MakeWeak(
5317 Isolate* isolate,
5318 P* parameters,
5319 typename WeakReferenceCallbacks<S, P>::Revivable callback) {
5320 TYPE_CHECK(S, T);
5321 typedef typename WeakReferenceCallbacks<Value, void>::Revivable Revivable;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005322 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005323 reinterpret_cast<internal::Object**>(this->val_),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005324 parameters,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005325 reinterpret_cast<Revivable>(callback),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005326 NULL);
5327}
5328
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005329
5330template <class T>
5331template <typename P>
5332void Persistent<T>::MakeWeak(
5333 Isolate* isolate,
5334 P* parameters,
5335 typename WeakReferenceCallbacks<T, P>::Revivable callback) {
5336 MakeWeak<T, P>(isolate, parameters, callback);
5337}
5338
5339
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005340template <class T>
5341void Persistent<T>::MakeWeak(Isolate* isolate,
5342 void* parameters,
5343 NearDeathCallback callback) {
5344 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005345 reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005346 parameters,
5347 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005348 callback);
5349}
5350
5351template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005352void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005353 ClearWeak(Isolate::GetCurrent());
5354}
5355
5356template <class T>
5357void Persistent<T>::ClearWeak(Isolate* isolate) {
5358 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005359 reinterpret_cast<internal::Object**>(this->val_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005360}
5361
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005362template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005363void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005364 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005365}
5366
5367template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005368void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005369 typedef internal::Internals I;
5370 if (this->IsEmpty()) return;
5371 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005372 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005373 true,
5374 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005375}
5376
5377template <class T>
5378void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005379 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005380}
5381
5382template <class T>
5383void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005384 typedef internal::Internals I;
5385 if (this->IsEmpty()) return;
5386 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005387 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005388 true,
5389 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005390}
5391
5392template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005393void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005394 SetWrapperClassId(Isolate::GetCurrent(), class_id);
5395}
5396
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005397
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005398template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005399void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
5400 Dispose(isolate);
5401#ifdef V8_USE_UNSAFE_HANDLES
5402 *this = *New(isolate, other);
5403#else
5404 if (other.IsEmpty()) {
5405 this->val_ = NULL;
5406 return;
5407 }
5408 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5409 this->val_ = reinterpret_cast<T*>(
5410 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5411#endif
5412}
5413
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005414
5415template <class T>
5416T* Persistent<T>::ClearAndLeak() {
5417 T* old;
5418#ifdef V8_USE_UNSAFE_HANDLES
5419 old = **this;
5420 *this = Persistent<T>();
5421#else
5422 old = val_;
5423 val_ = NULL;
5424#endif
5425 return old;
5426}
5427
5428
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005429template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005430void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005431 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005432 if (this->IsEmpty()) return;
5433 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005434 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005435 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5436 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005437}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005438
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005439template <class T>
5440uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005441 return WrapperClassId(Isolate::GetCurrent());
5442}
5443
5444template <class T>
5445uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005446 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005447 if (this->IsEmpty()) return 0;
5448 if (!I::IsInitialized(isolate)) return 0;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005449 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005450 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5451 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005452}
5453
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005454Arguments::Arguments(internal::Object** implicit_args,
5455 internal::Object** values, int length,
5456 bool is_construct_call)
5457 : implicit_args_(implicit_args),
5458 values_(values),
5459 length_(length),
5460 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005461
5462
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005463Local<Value> Arguments::operator[](int i) const {
5464 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
5465 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
5466}
5467
5468
5469Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005470 return Local<Function>(reinterpret_cast<Function*>(
5471 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005472}
5473
5474
5475Local<Object> Arguments::This() const {
5476 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
5477}
5478
5479
5480Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005481 return Local<Object>(reinterpret_cast<Object*>(
5482 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005483}
5484
5485
5486Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005487 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005488}
5489
5490
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005491Isolate* Arguments::GetIsolate() const {
5492 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
5493}
5494
5495
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005496bool Arguments::IsConstructCall() const {
5497 return is_construct_call_;
5498}
5499
5500
5501int Arguments::Length() const {
5502 return length_;
5503}
5504
5505
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005506template <class T>
5507Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005508 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
5509 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005510 return Local<T>(reinterpret_cast<T*>(after));
5511}
5512
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005513Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005514 return resource_name_;
5515}
5516
5517
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005518Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005519 return resource_line_offset_;
5520}
5521
5522
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005523Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005524 return resource_column_offset_;
5525}
5526
5527
5528Handle<Boolean> Boolean::New(bool value) {
5529 return value ? True() : False();
5530}
5531
5532
5533void Template::Set(const char* name, v8::Handle<Data> value) {
5534 Set(v8::String::New(name), value);
5535}
5536
5537
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005538Local<Value> Object::GetInternalField(int index) {
5539#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005540 typedef internal::Object O;
5541 typedef internal::Internals I;
5542 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005543 // Fast path: If the object is a plain JSObject, which is the common case, we
5544 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005545 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005546 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005547 O* value = I::ReadField<O*>(obj, offset);
5548 O** result = HandleScope::CreateHandle(value);
5549 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005550 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005551#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005552 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005553}
5554
5555
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005556void* Object::GetAlignedPointerFromInternalField(int index) {
5557#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005558 typedef internal::Object O;
5559 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005560 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005561 // Fast path: If the object is a plain JSObject, which is the common case, we
5562 // know where to find the internal fields and can return the value directly.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005563 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005564 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005565 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005566 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005567#endif
5568 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005569}
5570
5571
5572String* String::Cast(v8::Value* value) {
5573#ifdef V8_ENABLE_CHECKS
5574 CheckCast(value);
5575#endif
5576 return static_cast<String*>(value);
5577}
5578
5579
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005580Local<String> String::Empty(Isolate* isolate) {
5581 typedef internal::Object* S;
5582 typedef internal::Internals I;
5583 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005584 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005585 return Local<String>(reinterpret_cast<String*>(slot));
5586}
5587
5588
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00005589Local<String> String::New(const char* data, int length) {
5590 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
5591}
5592
5593
5594Local<String> String::New(const uint16_t* data, int length) {
5595 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
5596}
5597
5598
5599Local<String> String::NewSymbol(const char* data, int length) {
5600 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
5601}
5602
5603
5604Local<String> String::NewUndetectable(const char* data, int length) {
5605 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
5606}
5607
5608
5609Local<String> String::NewUndetectable(const uint16_t* data, int length) {
5610 return NewFromTwoByte(
5611 Isolate::GetCurrent(), data, kUndetectableString, length);
5612}
5613
5614
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005615String::ExternalStringResource* String::GetExternalStringResource() const {
5616 typedef internal::Object O;
5617 typedef internal::Internals I;
5618 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005619 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005620 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005621 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5622 result = reinterpret_cast<String::ExternalStringResource*>(value);
5623 } else {
5624 result = NULL;
5625 }
5626#ifdef V8_ENABLE_CHECKS
5627 VerifyExternalStringResource(result);
5628#endif
5629 return result;
5630}
5631
5632
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005633String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
5634 String::Encoding* encoding_out) const {
5635 typedef internal::Object O;
5636 typedef internal::Internals I;
5637 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
5638 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
5639 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5640 ExternalStringResourceBase* resource = NULL;
5641 if (type == I::kExternalAsciiRepresentationTag ||
5642 type == I::kExternalTwoByteRepresentationTag) {
5643 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5644 resource = static_cast<ExternalStringResourceBase*>(value);
5645 }
5646#ifdef V8_ENABLE_CHECKS
5647 VerifyExternalStringResourceBase(resource, *encoding_out);
5648#endif
5649 return resource;
5650}
5651
5652
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005653bool Value::IsUndefined() const {
5654#ifdef V8_ENABLE_CHECKS
5655 return FullIsUndefined();
5656#else
5657 return QuickIsUndefined();
5658#endif
5659}
5660
5661bool Value::QuickIsUndefined() const {
5662 typedef internal::Object O;
5663 typedef internal::Internals I;
5664 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5665 if (!I::HasHeapObjectTag(obj)) return false;
5666 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5667 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
5668}
5669
5670
5671bool Value::IsNull() const {
5672#ifdef V8_ENABLE_CHECKS
5673 return FullIsNull();
5674#else
5675 return QuickIsNull();
5676#endif
5677}
5678
5679bool Value::QuickIsNull() const {
5680 typedef internal::Object O;
5681 typedef internal::Internals I;
5682 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5683 if (!I::HasHeapObjectTag(obj)) return false;
5684 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5685 return (I::GetOddballKind(obj) == I::kNullOddballKind);
5686}
5687
5688
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005689bool Value::IsString() const {
5690#ifdef V8_ENABLE_CHECKS
5691 return FullIsString();
5692#else
5693 return QuickIsString();
5694#endif
5695}
5696
5697bool Value::QuickIsString() const {
5698 typedef internal::Object O;
5699 typedef internal::Internals I;
5700 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5701 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005702 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005703}
5704
5705
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005706Symbol* Symbol::Cast(v8::Value* value) {
5707#ifdef V8_ENABLE_CHECKS
5708 CheckCast(value);
5709#endif
5710 return static_cast<Symbol*>(value);
5711}
5712
5713
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005714Number* Number::Cast(v8::Value* value) {
5715#ifdef V8_ENABLE_CHECKS
5716 CheckCast(value);
5717#endif
5718 return static_cast<Number*>(value);
5719}
5720
5721
5722Integer* Integer::Cast(v8::Value* value) {
5723#ifdef V8_ENABLE_CHECKS
5724 CheckCast(value);
5725#endif
5726 return static_cast<Integer*>(value);
5727}
5728
5729
5730Date* Date::Cast(v8::Value* value) {
5731#ifdef V8_ENABLE_CHECKS
5732 CheckCast(value);
5733#endif
5734 return static_cast<Date*>(value);
5735}
5736
5737
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00005738StringObject* StringObject::Cast(v8::Value* value) {
5739#ifdef V8_ENABLE_CHECKS
5740 CheckCast(value);
5741#endif
5742 return static_cast<StringObject*>(value);
5743}
5744
5745
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005746SymbolObject* SymbolObject::Cast(v8::Value* value) {
5747#ifdef V8_ENABLE_CHECKS
5748 CheckCast(value);
5749#endif
5750 return static_cast<SymbolObject*>(value);
5751}
5752
5753
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00005754NumberObject* NumberObject::Cast(v8::Value* value) {
5755#ifdef V8_ENABLE_CHECKS
5756 CheckCast(value);
5757#endif
5758 return static_cast<NumberObject*>(value);
5759}
5760
5761
5762BooleanObject* BooleanObject::Cast(v8::Value* value) {
5763#ifdef V8_ENABLE_CHECKS
5764 CheckCast(value);
5765#endif
5766 return static_cast<BooleanObject*>(value);
5767}
5768
5769
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00005770RegExp* RegExp::Cast(v8::Value* value) {
5771#ifdef V8_ENABLE_CHECKS
5772 CheckCast(value);
5773#endif
5774 return static_cast<RegExp*>(value);
5775}
5776
5777
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005778Object* Object::Cast(v8::Value* value) {
5779#ifdef V8_ENABLE_CHECKS
5780 CheckCast(value);
5781#endif
5782 return static_cast<Object*>(value);
5783}
5784
5785
5786Array* Array::Cast(v8::Value* value) {
5787#ifdef V8_ENABLE_CHECKS
5788 CheckCast(value);
5789#endif
5790 return static_cast<Array*>(value);
5791}
5792
5793
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005794ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
5795#ifdef V8_ENABLE_CHECKS
5796 CheckCast(value);
5797#endif
5798 return static_cast<ArrayBuffer*>(value);
5799}
5800
5801
danno@chromium.orgf005df62013-04-30 16:36:45 +00005802TypedArray* TypedArray::Cast(v8::Value* value) {
5803#ifdef V8_ENABLE_CHECKS
5804 CheckCast(value);
5805#endif
5806 return static_cast<TypedArray*>(value);
5807}
5808
5809
5810Uint8Array* Uint8Array::Cast(v8::Value* value) {
5811#ifdef V8_ENABLE_CHECKS
5812 CheckCast(value);
5813#endif
5814 return static_cast<Uint8Array*>(value);
5815}
5816
5817
5818Int8Array* Int8Array::Cast(v8::Value* value) {
5819#ifdef V8_ENABLE_CHECKS
5820 CheckCast(value);
5821#endif
5822 return static_cast<Int8Array*>(value);
5823}
5824
5825
5826Uint16Array* Uint16Array::Cast(v8::Value* value) {
5827#ifdef V8_ENABLE_CHECKS
5828 CheckCast(value);
5829#endif
5830 return static_cast<Uint16Array*>(value);
5831}
5832
5833
5834Int16Array* Int16Array::Cast(v8::Value* value) {
5835#ifdef V8_ENABLE_CHECKS
5836 CheckCast(value);
5837#endif
5838 return static_cast<Int16Array*>(value);
5839}
5840
5841
5842Uint32Array* Uint32Array::Cast(v8::Value* value) {
5843#ifdef V8_ENABLE_CHECKS
5844 CheckCast(value);
5845#endif
5846 return static_cast<Uint32Array*>(value);
5847}
5848
5849
5850Int32Array* Int32Array::Cast(v8::Value* value) {
5851#ifdef V8_ENABLE_CHECKS
5852 CheckCast(value);
5853#endif
5854 return static_cast<Int32Array*>(value);
5855}
5856
5857
5858Float32Array* Float32Array::Cast(v8::Value* value) {
5859#ifdef V8_ENABLE_CHECKS
5860 CheckCast(value);
5861#endif
5862 return static_cast<Float32Array*>(value);
5863}
5864
5865
5866Float64Array* Float64Array::Cast(v8::Value* value) {
5867#ifdef V8_ENABLE_CHECKS
5868 CheckCast(value);
5869#endif
5870 return static_cast<Float64Array*>(value);
5871}
5872
5873
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005874Function* Function::Cast(v8::Value* value) {
5875#ifdef V8_ENABLE_CHECKS
5876 CheckCast(value);
5877#endif
5878 return static_cast<Function*>(value);
5879}
5880
5881
5882External* External::Cast(v8::Value* value) {
5883#ifdef V8_ENABLE_CHECKS
5884 CheckCast(value);
5885#endif
5886 return static_cast<External*>(value);
5887}
5888
5889
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005890Isolate* AccessorInfo::GetIsolate() const {
5891 return *reinterpret_cast<Isolate**>(&args_[-3]);
5892}
5893
5894
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005895Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005896 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005897}
5898
5899
5900Local<Object> AccessorInfo::This() const {
5901 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
5902}
5903
5904
5905Local<Object> AccessorInfo::Holder() const {
5906 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
5907}
5908
5909
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005910Handle<Primitive> Undefined(Isolate* isolate) {
5911 typedef internal::Object* S;
5912 typedef internal::Internals I;
5913 if (!I::IsInitialized(isolate)) return Undefined();
5914 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
5915 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
5916}
5917
5918
5919Handle<Primitive> Null(Isolate* isolate) {
5920 typedef internal::Object* S;
5921 typedef internal::Internals I;
5922 if (!I::IsInitialized(isolate)) return Null();
5923 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
5924 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
5925}
5926
5927
5928Handle<Boolean> True(Isolate* isolate) {
5929 typedef internal::Object* S;
5930 typedef internal::Internals I;
5931 if (!I::IsInitialized(isolate)) return True();
5932 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
5933 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
5934}
5935
5936
5937Handle<Boolean> False(Isolate* isolate) {
5938 typedef internal::Object* S;
5939 typedef internal::Internals I;
5940 if (!I::IsInitialized(isolate)) return False();
5941 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
5942 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
5943}
5944
5945
5946void Isolate::SetData(void* data) {
5947 typedef internal::Internals I;
5948 I::SetEmbedderData(this, data);
5949}
5950
5951
5952void* Isolate::GetData() {
5953 typedef internal::Internals I;
5954 return I::GetEmbedderData(this);
5955}
5956
5957
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005958Local<Value> Context::GetEmbedderData(int index) {
5959#ifndef V8_ENABLE_CHECKS
5960 typedef internal::Object O;
5961 typedef internal::Internals I;
5962 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
5963 return Local<Value>(reinterpret_cast<Value*>(result));
5964#else
5965 return SlowGetEmbedderData(index);
5966#endif
5967}
5968
5969
5970void* Context::GetAlignedPointerFromEmbedderData(int index) {
5971#ifndef V8_ENABLE_CHECKS
5972 typedef internal::Internals I;
5973 return I::ReadEmbedderData<void*>(this, index);
5974#else
5975 return SlowGetAlignedPointerFromEmbedderData(index);
5976#endif
5977}
5978
5979
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005980/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00005981 * \example shell.cc
5982 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005983 * command-line and executes them.
5984 */
5985
5986
5987/**
5988 * \example process.cc
5989 */
5990
5991
5992} // namespace v8
5993
5994
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005995#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005996#undef TYPE_CHECK
5997
5998
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005999#endif // V8_H_