blob: e99c6d9a98035d9b6abe7d26705c7ae1b02a280e [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;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000147class FunctionTemplate;
148class ObjectTemplate;
149class Data;
150class AccessorInfo;
151template<typename T> class PropertyCallbackInfo;
152class StackTrace;
153class StackFrame;
154class Isolate;
155class DeclaredAccessorDescriptor;
156class ObjectOperationDescriptor;
157class RawOperationDescriptor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000159namespace internal {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000160class Arguments;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000161class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000162class HeapObject;
163class Isolate;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000164class Object;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000165template<typename T>
166class CustomArguments;
167class PropertyCallbackArguments;
168class FunctionCallbackArguments;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000169}
170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000172/**
173 * General purpose unique identifier.
174 */
175class UniqueId {
176 public:
177 explicit UniqueId(intptr_t data)
178 : data_(data) {}
179
180 bool operator==(const UniqueId& other) const {
181 return data_ == other.data_;
182 }
183
184 bool operator!=(const UniqueId& other) const {
185 return data_ != other.data_;
186 }
187
188 bool operator<(const UniqueId& other) const {
189 return data_ < other.data_;
190 }
191
192 private:
193 intptr_t data_;
194};
195
196
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000197// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198
199
200/**
201 * A weak reference callback function.
202 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000203 * This callback should either explicitly invoke Dispose on |object| if
204 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
205 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 * \param object the weak global object to be reclaimed by the garbage collector
207 * \param parameter the value passed in when making the weak global object
208 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000209template<typename T, typename P>
210class WeakReferenceCallbacks {
211 public:
212 typedef void (*Revivable)(Isolate* isolate,
213 Persistent<T>* object,
214 P* parameter);
215};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000217// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000219#define TYPE_CHECK(T, S) \
220 while (false) { \
221 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 }
223
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000224
225#define V8_USE_UNSAFE_HANDLES
226
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227/**
228 * An object reference managed by the v8 garbage collector.
229 *
230 * All objects returned from v8 have to be tracked by the garbage
231 * collector so that it knows that the objects are still alive. Also,
232 * because the garbage collector may move objects, it is unsafe to
233 * point directly to an object. Instead, all objects are stored in
234 * handles which are known by the garbage collector and updated
235 * whenever an object moves. Handles should always be passed by value
236 * (except in cases like out-parameters) and they should never be
237 * allocated on the heap.
238 *
239 * There are two types of handles: local and persistent handles.
240 * Local handles are light-weight and transient and typically used in
241 * local operations. They are managed by HandleScopes. Persistent
242 * handles can be used when storing objects across several independent
243 * operations and have to be explicitly deallocated when they're no
244 * longer used.
245 *
246 * It is safe to extract the object stored in the handle by
247 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000248 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 * behind the scenes and the same rules apply to these values as to
250 * their handles.
251 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000252template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254 /**
255 * Creates an empty handle.
256 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000257 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000259#ifdef V8_USE_UNSAFE_HANDLES
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 /**
261 * Creates a new handle for the specified value.
262 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000263 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000264#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265
266 /**
267 * Creates a handle for the contents of the specified handle. This
268 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000269 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000271 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 * between compatible handles, for instance assigning a
273 * Handle<String> to a variable declared as Handle<Value>, is legal
274 * because String is a subclass of Value.
275 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000276 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 : val_(reinterpret_cast<T*>(*that)) {
278 /**
279 * This check fails when trying to convert between incompatible
280 * handles. For example, converting from a Handle<String> to a
281 * Handle<Number>.
282 */
283 TYPE_CHECK(T, S);
284 }
285
286 /**
287 * Returns true if the handle is empty.
288 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000289 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 /**
292 * Sets the handle to be empty. IsEmpty() will then return true.
293 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000294 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000295
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000296 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000297
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000298 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299
300 /**
301 * Checks whether two handles are the same.
302 * Returns true if both are empty, or if the objects
303 * to which they refer are identical.
304 * The handles' references are not checked.
305 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000306 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000307 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
308 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 if (a == 0) return b == 0;
310 if (b == 0) return false;
311 return *a == *b;
312 }
313
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000314#ifndef V8_USE_UNSAFE_HANDLES
315 template <class S> V8_INLINE(
316 bool operator==(const Persistent<S>& that) const) {
317 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
318 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
319 if (a == 0) return b == 0;
320 if (b == 0) return false;
321 return *a == *b;
322 }
323#endif
324
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 /**
326 * Checks whether two handles are different.
327 * Returns true if only one of the handles is empty, or if
328 * the objects to which they refer are different.
329 * The handles' references are not checked.
330 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000331 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 return !operator==(that);
333 }
334
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000335 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000336#ifdef V8_ENABLE_CHECKS
337 // If we're going to perform the type check then we have to check
338 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000340#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 return Handle<T>(T::Cast(*that));
342 }
343
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000344 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000345 return Handle<S>::Cast(*this);
346 }
347
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000348#ifndef V8_USE_UNSAFE_HANDLES
349 V8_INLINE(static Handle<T> New(Isolate* isolate, Handle<T> that)) {
350 return New(isolate, that.val_);
351 }
352 // TODO(dcarney): remove before cutover
353 V8_INLINE(static Handle<T> New(Isolate* isolate, const Persistent<T>& that)) {
354 return New(isolate, that.val_);
355 }
356
357#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
358
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000360#endif
361 /**
362 * Creates a new handle for the specified value.
363 */
364 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
365#endif
366
367 private:
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000368 template<class F> friend class Persistent;
369 template<class F> friend class Local;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000370 friend class Arguments;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000371 template<class F> friend class FunctionCallbackInfo;
372 template<class F> friend class PropertyCallbackInfo;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000373 friend class String;
374 friend class Object;
375 friend class AccessorInfo;
376 friend Handle<Primitive> Undefined(Isolate* isolate);
377 friend Handle<Primitive> Null(Isolate* isolate);
378 friend Handle<Boolean> True(Isolate* isolate);
379 friend Handle<Boolean> False(Isolate* isolate);
380 friend class Context;
381 friend class InternalHandleHelper;
382 friend class LocalContext;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000383 friend class HandleScope;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000384
385#ifndef V8_USE_UNSAFE_HANDLES
386 V8_INLINE(static Handle<T> New(Isolate* isolate, T* that));
387#endif
388
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389 T* val_;
390};
391
392
393/**
394 * A light-weight stack-allocated object handle. All operations
395 * that return objects from within v8 return them in local handles. They
396 * are created within HandleScopes, and all local handles allocated within a
397 * handle scope are destroyed when the handle scope is destroyed. Hence it
398 * is not necessary to explicitly deallocate local handles.
399 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000400// TODO(dcarney): deprecate entire class
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000401template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000403 V8_INLINE(Local());
404 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405 : Handle<T>(reinterpret_cast<T*>(*that)) {
406 /**
407 * This check fails when trying to convert between incompatible
408 * handles. For example, converting from a Handle<String> to a
409 * Handle<Number>.
410 */
411 TYPE_CHECK(T, S);
412 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000413
414
415#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000416 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000417#endif
418
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000419 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000420#ifdef V8_ENABLE_CHECKS
421 // If we're going to perform the type check then we have to check
422 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000424#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 return Local<T>(T::Cast(*that));
426 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000427#ifndef V8_USE_UNSAFE_HANDLES
428 template <class S> V8_INLINE(Local(Handle<S> that))
429 : Handle<T>(reinterpret_cast<T*>(*that)) {
430 TYPE_CHECK(T, S);
431 }
432#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000434 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000435 return Local<S>::Cast(*this);
436 }
437
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000438 /**
439 * Create a local handle for the content of another handle.
440 * The referee is kept alive by the local handle even when
441 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000443 V8_INLINE(static Local<T> New(Handle<T> that));
444 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000445#ifndef V8_USE_UNSAFE_HANDLES
446 // TODO(dcarney): remove before cutover
447 V8_INLINE(static Local<T> New(Isolate* isolate, const Persistent<T>& that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000449#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
450
451 private:
452#endif
453 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
454#endif
455
456 private:
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000457 template<class F> friend class Persistent;
458 template<class F> friend class Handle;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000459 friend class Arguments;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000460 template<class F> friend class FunctionCallbackInfo;
461 template<class F> friend class PropertyCallbackInfo;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000462 friend class String;
463 friend class Object;
464 friend class AccessorInfo;
465 friend class Context;
466 friend class InternalHandleHelper;
467 friend class LocalContext;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000468 friend class HandleScope;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000469
470 V8_INLINE(static Local<T> New(Isolate* isolate, T* that));
471};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472
473/**
474 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000475 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 * allocated, a Persistent handle remains valid until it is explicitly
477 * disposed.
478 *
479 * A persistent handle contains a reference to a storage cell within
480 * the v8 engine which holds an object value and which is updated by
481 * the garbage collector whenever the object is moved. A new storage
482 * cell can be created using Persistent::New and existing handles can
483 * be disposed using Persistent::Dispose. Since persistent handles
484 * are passed by value you may have many persistent handle objects
485 * that point to the same storage cell. For instance, if you pass a
486 * persistent handle as an argument to a function you will not get two
487 * different storage cells but rather two references to the same
488 * storage cell.
489 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000490template <class T> class Persistent // NOLINT
491#ifdef V8_USE_UNSAFE_HANDLES
492 : public Handle<T> {
493#else
494 { // NOLINT
495#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 public:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000497#ifndef V8_USE_UNSAFE_HANDLES
498 V8_INLINE(Persistent()) : val_(0) { }
499 V8_INLINE(~Persistent()) {
500 // TODO(dcarney): add this back before cutover.
501 // Dispose();
502 }
503 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
504 // TODO(dcarney): remove somehow before cutover
505 // The handle should either be 0, or a pointer to a live cell.
506 V8_INLINE(void Clear()) { val_ = 0; }
507
508 /**
509 * A constructor that creates a new global cell pointing to that. In contrast
510 * to the copy constructor, this creates a new persistent handle which needs
511 * to be separately disposed.
512 */
513 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
514 : val_(*New(isolate, that)) { }
515
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000516 template <class S> V8_INLINE(Persistent(Isolate* isolate,
517 Persistent<S>& that)) // NOLINT
518 : val_(*New(isolate, that)) { }
519
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000520#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 /**
522 * Creates an empty persistent handle that doesn't point to any
523 * storage cell.
524 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000525 V8_INLINE(Persistent()) : Handle<T>() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526
527 /**
528 * Creates a persistent handle for the same storage cell as the
529 * specified handle. This constructor allows you to pass persistent
530 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000531 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000533 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 * between compatible persistent handles, for instance assigning a
535 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000536 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000538 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 : Handle<T>(reinterpret_cast<T*>(*that)) {
540 /**
541 * This check fails when trying to convert between incompatible
542 * handles. For example, converting from a Handle<String> to a
543 * Handle<Number>.
544 */
545 TYPE_CHECK(T, S);
546 }
547
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000548 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000550 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000551 * A constructor that creates a new global cell pointing to that. In contrast
552 * to the copy constructor, this creates a new persistent handle which needs
553 * to be separately disposed.
554 */
555 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
556 : Handle<T>(New(isolate, that)) { }
557
558 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000559 * "Casts" a plain handle which is known to be a persistent handle
560 * to a persistent handle.
561 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000562 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 : Handle<T>(*that) { }
564
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000565#endif
566
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000567#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000568 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000569#ifdef V8_ENABLE_CHECKS
570 // If we're going to perform the type check then we have to check
571 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000573#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 return Persistent<T>(T::Cast(*that));
575 }
576
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000577 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000578 return Persistent<S>::Cast(*this);
579 }
580
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000581#else
582 template <class S>
583 V8_INLINE(static Persistent<T>& Cast(Persistent<S>& that)) { // NOLINT
584#ifdef V8_ENABLE_CHECKS
585 // If we're going to perform the type check then we have to check
586 // that the handle isn't empty before doing the checked cast.
587 if (!that.IsEmpty()) T::Cast(*that);
588#endif
589 return reinterpret_cast<Persistent<T>&>(that);
590 }
591
592 template <class S> V8_INLINE(Persistent<S>& As()) { // NOLINT
593 return Persistent<S>::Cast(*this);
594 }
595#endif
596
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000597 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
598
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000600 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000602 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000603 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000604#ifndef V8_USE_UNSAFE_HANDLES
605 // TODO(dcarney): remove before cutover
606 V8_INLINE(static Persistent<T> New(Isolate* isolate, Persistent<T> that));
607#endif
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000608
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000609#ifndef V8_USE_UNSAFE_HANDLES
610 template <class S> V8_INLINE(
611 bool operator==(const Persistent<S>& that) const) {
612 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
613 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
614 if (a == 0) return b == 0;
615 if (b == 0) return false;
616 return *a == *b;
617 }
618
619 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
620 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
621 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
622 if (a == 0) return b == 0;
623 if (b == 0) return false;
624 return *a == *b;
625 }
626#endif
627
628 V8_INLINE(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629
630 /**
631 * Releases the storage cell referenced by this persistent handle.
632 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000633 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634 * cell remain and IsEmpty will still return false.
635 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000636 // TODO(dcarney): deprecate
637 V8_INLINE(void Dispose(Isolate* isolate)) { Dispose(); }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000638
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 /**
640 * Make the reference to this object weak. When only weak handles
641 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000642 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 * it the object reference and the given parameters.
644 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000645 template<typename S, typename P>
646 V8_INLINE(void MakeWeak(
647 P* parameters,
648 typename WeakReferenceCallbacks<S, P>::Revivable callback));
649
650 template<typename P>
651 V8_INLINE(void MakeWeak(
652 P* parameters,
653 typename WeakReferenceCallbacks<T, P>::Revivable callback));
654
655 // TODO(dcarney): deprecate
656 template<typename S, typename P>
657 V8_INLINE(void MakeWeak(
658 Isolate* isolate,
659 P* parameters,
660 typename WeakReferenceCallbacks<S, P>::Revivable callback)) {
661 MakeWeak<S, P>(parameters, callback);
662 }
663
664 // TODO(dcarney): deprecate
665 template<typename P>
666 V8_INLINE(void MakeWeak(
667 Isolate* isolate,
668 P* parameters,
669 typename WeakReferenceCallbacks<T, P>::Revivable callback)) {
670 MakeWeak<P>(parameters, callback);
671 }
672
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000673 V8_INLINE(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000675 // TODO(dcarney): deprecate
676 V8_INLINE(void ClearWeak(Isolate* isolate)) { ClearWeak(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677
678 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000679 * Marks the reference to this object independent. Garbage collector is free
680 * to ignore any object groups containing this object. Weak callback for an
681 * independent handle should not assume that it will be preceded by a global
682 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000683 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000684 V8_INLINE(void MarkIndependent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000685
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000686 // TODO(dcarney): deprecate
687 V8_INLINE(void MarkIndependent(Isolate* isolate)) { MarkIndependent(); }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000688
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000689 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000690 * Marks the reference to this object partially dependent. Partially dependent
691 * handles only depend on other partially dependent handles and these
692 * dependencies are provided through object groups. It provides a way to build
693 * smaller object groups for young objects that represent only a subset of all
694 * external dependencies. This mark is automatically cleared after each
695 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000696 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000697 V8_INLINE(void MarkPartiallyDependent());
698
699 // TODO(dcarney): deprecate
700 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate)) {
701 MarkPartiallyDependent();
702 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000703
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000704 V8_INLINE(bool IsIndependent() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000705
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000706 // TODO(dcarney): deprecate
707 V8_INLINE(bool IsIndependent(Isolate* isolate) const) {
708 return IsIndependent();
709 }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000710
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000711 /** Checks if the handle holds the only reference to an object. */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000712 V8_INLINE(bool IsNearDeath() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000713
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000714 // TODO(dcarney): deprecate
715 V8_INLINE(bool IsNearDeath(Isolate* isolate) const) { return IsNearDeath(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000717 /** Returns true if the handle's reference is weak. */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000718 V8_INLINE(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000720 // TODO(dcarney): deprecate
721 V8_INLINE(bool IsWeak(Isolate* isolate) const) { return IsWeak(); }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000722
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000723 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000724 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
725 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000726 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000727 V8_INLINE(void SetWrapperClassId(uint16_t class_id));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000728
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000729 // TODO(dcarney): deprecate
730 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id)) {
731 SetWrapperClassId(class_id);
732 }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000733
734 /**
735 * Returns the class ID previously assigned to this handle or 0 if no class ID
736 * was previously assigned.
737 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000738 V8_INLINE(uint16_t WrapperClassId() const);
739
740 // TODO(dcarney): deprecate
741 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const) {
742 return WrapperClassId();
743 }
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000744
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000745 /**
746 * Disposes the current contents of the handle and replaces it.
747 */
748 V8_INLINE(void Reset(Isolate* isolate, const Handle<T>& other));
749
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000750#ifndef V8_USE_UNSAFE_HANDLES
751 V8_INLINE(void Reset(Isolate* isolate, const Persistent<T>& other));
752#endif
753
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000754 /**
755 * Returns the underlying raw pointer and clears the handle. The caller is
756 * responsible of eventually destroying the underlying object (by creating a
757 * Persistent handle which points to it and Disposing it). In the future,
758 * destructing a Persistent will also Dispose it. With this function, the
759 * embedder can let the Persistent go out of scope without it getting
760 * disposed.
761 */
762 V8_INLINE(T* ClearAndLeak());
763
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000764#ifndef V8_USE_UNSAFE_HANDLES
765
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000767 // TODO(dcarney): make unlinkable before cutover
768 V8_INLINE(Persistent(const Persistent& that)) : val_(that.val_) {}
769 // TODO(dcarney): make unlinkable before cutover
770 V8_INLINE(Persistent& operator=(const Persistent& that)) { // NOLINT
771 this->val_ = that.val_;
772 return *this;
773 }
774
775 public:
776#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
777
778 private:
779#endif
780 // TODO(dcarney): remove before cutover
781 template <class S> V8_INLINE(Persistent(S* that)) : val_(that) { }
782 // TODO(dcarney): remove before cutover
783 template <class S> V8_INLINE(Persistent(Persistent<S> that))
784 : val_(*that) {
785 TYPE_CHECK(T, S);
786 }
787 // TODO(dcarney): remove before cutover
788 V8_INLINE(T* operator*() const) { return val_; }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000789
790 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000791 // TODO(dcarney): remove before cutover
792 V8_INLINE(T* operator->() const) { return val_; }
793 public:
794#endif
795
796 private:
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000797 template<class F> friend class Handle;
798 template<class F> friend class Local;
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000799 template<class F> friend class ReturnValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800 friend class ImplementationUtilities;
801 friend class ObjectTemplate;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000802 friend class Context;
803 friend class InternalHandleHelper;
804 friend class LocalContext;
805
806 V8_INLINE(static Persistent<T> New(Isolate* isolate, T* that));
807
808#ifndef V8_USE_UNSAFE_HANDLES
809 T* val_;
810#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811};
812
813
v8.team.kasperl727e9952008-09-02 14:56:44 +0000814 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 * A stack-allocated class that governs a number of local handles.
816 * After a handle scope has been created, all local handles will be
817 * allocated within that handle scope until either the handle scope is
818 * deleted or another handle scope is created. If there is already a
819 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000820 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821 * new handles will again be allocated in the original handle scope.
822 *
823 * After the handle scope of a local handle has been deleted the
824 * garbage collector will no longer track the object stored in the
825 * handle and may deallocate it. The behavior of accessing a handle
826 * for which the handle scope has been deleted is undefined.
827 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000828class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000830 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000831 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000833 HandleScope(Isolate* isolate);
834
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000835 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836
837 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838 * Closes the handle scope and returns the value as a handle in the
839 * previous scope, which is the new current scope after the call.
840 */
841 template <class T> Local<T> Close(Handle<T> value);
842
843 /**
844 * Counts the number of allocated handles.
845 */
846 static int NumberOfHandles();
847
848 /**
849 * Creates a new handle with the given value.
850 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000851 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000852 static internal::Object** CreateHandle(internal::Isolate* isolate,
853 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000854 // Faster version, uses HeapObject to obtain the current Isolate.
855 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856
857 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000858 // Make it hard to create heap-allocated or illegal handle scopes by
859 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 HandleScope(const HandleScope&);
861 void operator=(const HandleScope&);
862 void* operator new(size_t size);
863 void operator delete(void*, size_t);
864
ager@chromium.org3811b432009-10-28 14:53:37 +0000865 // This Data class is accessible internally as HandleScopeData through a
866 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000867 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000869 internal::Object** next;
870 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000871 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000872 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000874 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875 }
876 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000877
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000878 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000879 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000881 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000882 internal::Object** prev_next_;
883 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000885 // Allow for the active closing of HandleScopes which allows to pass a handle
886 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000888 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 friend class ImplementationUtilities;
891};
892
893
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000894// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
896
897/**
898 * The superclass of values and API object templates.
899 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000900class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 private:
902 Data();
903};
904
905
906/**
907 * Pre-compilation data that can be associated with a script. This
908 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000909 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000910 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000912class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 public:
914 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000915
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000916 /**
917 * Pre-compiles the specified script (context-independent).
918 *
919 * \param input Pointer to UTF-8 script source code.
920 * \param length Length of UTF-8 script source code.
921 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000924 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000925 * Pre-compiles the specified script (context-independent).
926 *
927 * NOTE: Pre-compilation using this method cannot happen on another thread
928 * without using Lockers.
929 *
930 * \param source Script source code.
931 */
932 static ScriptData* PreCompile(Handle<String> source);
933
934 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000935 * Load previous pre-compilation data.
936 *
937 * \param data Pointer to data returned by a call to Data() of a previous
938 * ScriptData. Ownership is not transferred.
939 * \param length Length of data.
940 */
941 static ScriptData* New(const char* data, int length);
942
943 /**
944 * Returns the length of Data().
945 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000947
948 /**
949 * Returns a serialized representation of this ScriptData that can later be
950 * passed to New(). NOTE: Serialized data is platform-dependent.
951 */
952 virtual const char* Data() = 0;
953
954 /**
955 * Returns true if the source code could not be parsed.
956 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000957 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958};
959
960
961/**
962 * The origin, within a file, of a script.
963 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000964class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000966 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000967 Handle<Value> resource_name,
968 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000969 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 : resource_name_(resource_name),
971 resource_line_offset_(resource_line_offset),
972 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000973 V8_INLINE(Handle<Value> ResourceName() const);
974 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
975 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000977 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 Handle<Integer> resource_line_offset_;
979 Handle<Integer> resource_column_offset_;
980};
981
982
983/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000984 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000986class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000989 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000990 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000991 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000992 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000993 * when New() returns
994 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
995 * using pre_data speeds compilation if it's done multiple times.
996 * Owned by caller, no references are kept when New() returns.
997 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000998 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000999 * available to compile event handlers.
1000 * \return Compiled script object (context independent; when run it
1001 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002 */
ager@chromium.org5c838252010-02-19 08:53:10 +00001003 static Local<Script> New(Handle<String> source,
1004 ScriptOrigin* origin = NULL,
1005 ScriptData* pre_data = NULL,
1006 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007
mads.s.agercbaa0602008-08-14 13:41:48 +00001008 /**
1009 * Compiles the specified script using the specified file name
1010 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001011 *
ager@chromium.org5c838252010-02-19 08:53:10 +00001012 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001013 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +00001014 * as the script's origin.
1015 * \return Compiled script object (context independent; when run it
1016 * will use the currently entered context).
1017 */
1018 static Local<Script> New(Handle<String> source,
1019 Handle<Value> file_name);
1020
1021 /**
1022 * Compiles the specified script (bound to current context).
1023 *
1024 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001025 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +00001026 * when Compile() returns
1027 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1028 * using pre_data speeds compilation if it's done multiple times.
1029 * Owned by caller, no references are kept when Compile() returns.
1030 * \param script_data Arbitrary data associated with script. Using
1031 * this has same effect as calling SetData(), but makes data available
1032 * earlier (i.e. to compile event handlers).
1033 * \return Compiled script object, bound to the context that was active
1034 * when this function was called. When run it will always use this
1035 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +00001036 */
1037 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +00001038 ScriptOrigin* origin = NULL,
1039 ScriptData* pre_data = NULL,
1040 Handle<String> script_data = Handle<String>());
1041
1042 /**
1043 * Compiles the specified script using the specified file name
1044 * object (typically a string) as the script's origin.
1045 *
1046 * \param source Script source code.
1047 * \param file_name File name to use as script's origin
1048 * \param script_data Arbitrary data associated with script. Using
1049 * this has same effect as calling SetData(), but makes data available
1050 * earlier (i.e. to compile event handlers).
1051 * \return Compiled script object, bound to the context that was active
1052 * when this function was called. When run it will always use this
1053 * context.
1054 */
1055 static Local<Script> Compile(Handle<String> source,
1056 Handle<Value> file_name,
1057 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +00001058
v8.team.kasperl727e9952008-09-02 14:56:44 +00001059 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001060 * Runs the script returning the resulting value. If the script is
1061 * context independent (created using ::New) it will be run in the
1062 * currently entered context. If it is context specific (created
1063 * using ::Compile) it will be run in the context in which it was
1064 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001065 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001067
1068 /**
1069 * Returns the script id value.
1070 */
1071 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001072
1073 /**
1074 * Associate an additional data object with the script. This is mainly used
1075 * with the debugger as this data object is only available through the
1076 * debugger API.
1077 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00001078 void SetData(Handle<String> data);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001079
1080 /**
1081 * Returns the name value of one Script.
1082 */
1083 Handle<Value> GetScriptName();
1084
1085 /**
1086 * Returns zero based line number of the code_pos location in the script.
1087 * -1 will be returned if no information available.
1088 */
1089 int GetLineNumber(int code_pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090};
1091
1092
1093/**
1094 * An error message.
1095 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001096class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001098 Local<String> Get() const;
1099 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001101 /**
1102 * Returns the resource name for the script from where the function causing
1103 * the error originates.
1104 */
ager@chromium.org32912102009-01-16 10:38:43 +00001105 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001107 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001108 * Returns the resource data for the script from where the function causing
1109 * the error originates.
1110 */
1111 Handle<Value> GetScriptData() const;
1112
1113 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001114 * Exception stack trace. By default stack traces are not captured for
1115 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1116 * to change this option.
1117 */
1118 Handle<StackTrace> GetStackTrace() const;
1119
1120 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001121 * Returns the number, 1-based, of the line where the error occurred.
1122 */
ager@chromium.org32912102009-01-16 10:38:43 +00001123 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001125 /**
1126 * Returns the index within the script of the first character where
1127 * the error occurred.
1128 */
ager@chromium.org32912102009-01-16 10:38:43 +00001129 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001130
1131 /**
1132 * Returns the index within the script of the last character where
1133 * the error occurred.
1134 */
ager@chromium.org32912102009-01-16 10:38:43 +00001135 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001136
1137 /**
1138 * Returns the index within the line of the first character where
1139 * the error occurred.
1140 */
ager@chromium.org32912102009-01-16 10:38:43 +00001141 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001142
1143 /**
1144 * Returns the index within the line of the last character where
1145 * the error occurred.
1146 */
ager@chromium.org32912102009-01-16 10:38:43 +00001147 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149 // TODO(1245381): Print to a string instead of on a FILE.
1150 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001151
1152 static const int kNoLineNumberInfo = 0;
1153 static const int kNoColumnInfo = 0;
1154};
1155
1156
1157/**
1158 * Representation of a JavaScript stack trace. The information collected is a
1159 * snapshot of the execution stack and the information remains valid after
1160 * execution continues.
1161 */
1162class V8EXPORT StackTrace {
1163 public:
1164 /**
1165 * Flags that determine what information is placed captured for each
1166 * StackFrame when grabbing the current stack trace.
1167 */
1168 enum StackTraceOptions {
1169 kLineNumber = 1,
1170 kColumnOffset = 1 << 1 | kLineNumber,
1171 kScriptName = 1 << 2,
1172 kFunctionName = 1 << 3,
1173 kIsEval = 1 << 4,
1174 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001175 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001176 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001177 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001178 };
1179
1180 /**
1181 * Returns a StackFrame at a particular index.
1182 */
1183 Local<StackFrame> GetFrame(uint32_t index) const;
1184
1185 /**
1186 * Returns the number of StackFrames.
1187 */
1188 int GetFrameCount() const;
1189
1190 /**
1191 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1192 */
1193 Local<Array> AsArray();
1194
1195 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001196 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001197 *
1198 * \param frame_limit The maximum number of stack frames we want to capture.
1199 * \param options Enumerates the set of things we will capture for each
1200 * StackFrame.
1201 */
1202 static Local<StackTrace> CurrentStackTrace(
1203 int frame_limit,
1204 StackTraceOptions options = kOverview);
1205};
1206
1207
1208/**
1209 * A single JavaScript stack frame.
1210 */
1211class V8EXPORT StackFrame {
1212 public:
1213 /**
1214 * Returns the number, 1-based, of the line for the associate function call.
1215 * This method will return Message::kNoLineNumberInfo if it is unable to
1216 * retrieve the line number, or if kLineNumber was not passed as an option
1217 * when capturing the StackTrace.
1218 */
1219 int GetLineNumber() const;
1220
1221 /**
1222 * Returns the 1-based column offset on the line for the associated function
1223 * call.
1224 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1225 * the column number, or if kColumnOffset was not passed as an option when
1226 * capturing the StackTrace.
1227 */
1228 int GetColumn() const;
1229
1230 /**
1231 * Returns the name of the resource that contains the script for the
1232 * function for this StackFrame.
1233 */
1234 Local<String> GetScriptName() const;
1235
1236 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001237 * Returns the name of the resource that contains the script for the
1238 * function for this StackFrame or sourceURL value if the script name
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001239 * is undefined and its source ends with //# sourceURL=... string or
1240 * deprecated //@ sourceURL=... string.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001241 */
1242 Local<String> GetScriptNameOrSourceURL() const;
1243
1244 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001245 * Returns the name of the function associated with this stack frame.
1246 */
1247 Local<String> GetFunctionName() const;
1248
1249 /**
1250 * Returns whether or not the associated function is compiled via a call to
1251 * eval().
1252 */
1253 bool IsEval() const;
1254
1255 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001256 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001257 * constructor via "new".
1258 */
1259 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260};
1261
1262
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001263// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264
1265
1266/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001267 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001269class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 /**
1272 * Returns true if this value is the undefined value. See ECMA-262
1273 * 4.3.10.
1274 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001275 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276
1277 /**
1278 * Returns true if this value is the null value. See ECMA-262
1279 * 4.3.11.
1280 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001281 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282
1283 /**
1284 * Returns true if this value is true.
1285 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001286 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287
1288 /**
1289 * Returns true if this value is false.
1290 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001291 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292
1293 /**
1294 * Returns true if this value is an instance of the String type.
1295 * See ECMA-262 8.4.
1296 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001297 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298
1299 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001300 * Returns true if this value is a symbol.
1301 * This is an experimental feature.
1302 */
1303 bool IsSymbol() const;
1304
1305 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 * Returns true if this value is a function.
1307 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001308 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309
1310 /**
1311 * Returns true if this value is an array.
1312 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001313 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314
v8.team.kasperl727e9952008-09-02 14:56:44 +00001315 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316 * Returns true if this value is an object.
1317 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001318 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319
v8.team.kasperl727e9952008-09-02 14:56:44 +00001320 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 * Returns true if this value is boolean.
1322 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001323 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001324
v8.team.kasperl727e9952008-09-02 14:56:44 +00001325 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 * Returns true if this value is a number.
1327 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001328 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329
v8.team.kasperl727e9952008-09-02 14:56:44 +00001330 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331 * Returns true if this value is external.
1332 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001333 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334
v8.team.kasperl727e9952008-09-02 14:56:44 +00001335 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 * Returns true if this value is a 32-bit signed integer.
1337 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001338 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001340 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001341 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001342 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001343 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001344
1345 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001346 * Returns true if this value is a Date.
1347 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001348 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001349
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001350 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001351 * Returns true if this value is a Boolean object.
1352 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001353 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001354
1355 /**
1356 * Returns true if this value is a Number object.
1357 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001358 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001359
1360 /**
1361 * Returns true if this value is a String object.
1362 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001363 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001364
1365 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001366 * Returns true if this value is a Symbol object.
1367 * This is an experimental feature.
1368 */
1369 bool IsSymbolObject() const;
1370
1371 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001372 * Returns true if this value is a NativeError.
1373 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001374 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001375
1376 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001377 * Returns true if this value is a RegExp.
1378 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001379 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001380
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001381
1382 /**
1383 * Returns true if this value is an ArrayBuffer.
1384 * This is an experimental feature.
1385 */
1386 bool IsArrayBuffer() const;
1387
1388 /**
1389 * Returns true if this value is one of TypedArrays.
1390 * This is an experimental feature.
1391 */
1392 bool IsTypedArray() const;
1393
1394 /**
1395 * Returns true if this value is an Uint8Array.
1396 * This is an experimental feature.
1397 */
1398 bool IsUint8Array() const;
1399
1400 /**
1401 * Returns true if this value is an Uint8ClampedArray.
1402 * This is an experimental feature.
1403 */
1404 bool IsUint8ClampedArray() const;
1405
1406 /**
1407 * Returns true if this value is an Int8Array.
1408 * This is an experimental feature.
1409 */
1410 bool IsInt8Array() const;
1411
1412 /**
1413 * Returns true if this value is an Uint16Array.
1414 * This is an experimental feature.
1415 */
1416 bool IsUint16Array() const;
1417
1418 /**
1419 * Returns true if this value is an Int16Array.
1420 * This is an experimental feature.
1421 */
1422 bool IsInt16Array() const;
1423
1424 /**
1425 * Returns true if this value is an Uint32Array.
1426 * This is an experimental feature.
1427 */
1428 bool IsUint32Array() const;
1429
1430 /**
1431 * Returns true if this value is an Int32Array.
1432 * This is an experimental feature.
1433 */
1434 bool IsInt32Array() const;
1435
1436 /**
1437 * Returns true if this value is a Float32Array.
1438 * This is an experimental feature.
1439 */
1440 bool IsFloat32Array() const;
1441
1442 /**
1443 * Returns true if this value is a Float64Array.
1444 * This is an experimental feature.
1445 */
1446 bool IsFloat64Array() const;
1447
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001448 Local<Boolean> ToBoolean() const;
1449 Local<Number> ToNumber() const;
1450 Local<String> ToString() const;
1451 Local<String> ToDetailString() const;
1452 Local<Object> ToObject() const;
1453 Local<Integer> ToInteger() const;
1454 Local<Uint32> ToUint32() const;
1455 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456
1457 /**
1458 * Attempts to convert a string to an array index.
1459 * Returns an empty handle if the conversion fails.
1460 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001461 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001463 bool BooleanValue() const;
1464 double NumberValue() const;
1465 int64_t IntegerValue() const;
1466 uint32_t Uint32Value() const;
1467 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468
1469 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001470 bool Equals(Handle<Value> that) const;
1471 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001472
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001473 template <class T> V8_INLINE(static Value* Cast(T* value));
1474
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001475 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001476 V8_INLINE(bool QuickIsUndefined() const);
1477 V8_INLINE(bool QuickIsNull() const);
1478 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001479 bool FullIsUndefined() const;
1480 bool FullIsNull() const;
1481 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482};
1483
1484
1485/**
1486 * The superclass of primitive values. See ECMA-262 4.3.2.
1487 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001488class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001489
1490
1491/**
1492 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1493 * or false value.
1494 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001495class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001497 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001498 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499};
1500
1501
1502/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001503 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001505class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001507 enum Encoding {
1508 UNKNOWN_ENCODING = 0x1,
1509 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001510 ASCII_ENCODING = 0x4,
1511 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001512 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001513 /**
1514 * Returns the number of characters in this string.
1515 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001516 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517
v8.team.kasperl727e9952008-09-02 14:56:44 +00001518 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001519 * Returns the number of bytes in the UTF-8 encoded
1520 * representation of this string.
1521 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001522 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001523
1524 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001525 * This function is no longer useful.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001526 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001527 V8_DEPRECATED(V8_INLINE(bool MayContainNonAscii()) const) { return true; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001528
1529 /**
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00001530 * Returns whether this string is known to contain only one byte data.
1531 * Does not read the string.
1532 * False negatives are possible.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001533 */
1534 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001535
1536 /**
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00001537 * Returns whether this string contain only one byte data.
1538 * Will read the entire string in some cases.
1539 */
1540 bool ContainsOnlyOneByte() const;
1541
1542 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001543 * Write the contents of the string to an external buffer.
1544 * If no arguments are given, expects the buffer to be large
1545 * enough to hold the entire string and NULL terminator. Copies
1546 * the contents of the string and the NULL terminator into the
1547 * buffer.
1548 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001549 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1550 * before the end of the buffer.
1551 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001552 * Copies up to length characters into the output buffer.
1553 * Only null-terminates if there is enough space in the buffer.
1554 *
1555 * \param buffer The buffer into which the string will be copied.
1556 * \param start The starting position within the string at which
1557 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001558 * \param length The number of characters to copy from the string. For
1559 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001560 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001561 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001562 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001563 * \return The number of characters copied to the buffer excluding the null
1564 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001565 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001566 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001567 enum WriteOptions {
1568 NO_OPTIONS = 0,
1569 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001570 NO_NULL_TERMINATION = 2,
1571 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001572 };
1573
lrn@chromium.org34e60782011-09-15 07:25:40 +00001574 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001575 int Write(uint16_t* buffer,
1576 int start = 0,
1577 int length = -1,
1578 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001579 // ASCII characters.
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001580 V8_DEPRECATED(int WriteAscii(char* buffer,
1581 int start = 0,
1582 int length = -1,
1583 int options = NO_OPTIONS) const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001584 // One byte characters.
1585 int WriteOneByte(uint8_t* buffer,
1586 int start = 0,
1587 int length = -1,
1588 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001589 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001590 int WriteUtf8(char* buffer,
1591 int length = -1,
1592 int* nchars_ref = NULL,
1593 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594
v8.team.kasperl727e9952008-09-02 14:56:44 +00001595 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001596 * A zero length string.
1597 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001598 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001599 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001600
1601 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001602 * Returns true if the string is external
1603 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001604 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605
v8.team.kasperl727e9952008-09-02 14:56:44 +00001606 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001607 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001608 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001609 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001610
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001611 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001612 public:
1613 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001614
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001615 protected:
1616 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001617
1618 /**
1619 * Internally V8 will call this Dispose method when the external string
1620 * resource is no longer needed. The default implementation will use the
1621 * delete operator. This method can be overridden in subclasses to
1622 * control how allocated external string resources are disposed.
1623 */
1624 virtual void Dispose() { delete this; }
1625
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001626 private:
1627 // Disallow copying and assigning.
1628 ExternalStringResourceBase(const ExternalStringResourceBase&);
1629 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001630
1631 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001632 };
1633
v8.team.kasperl727e9952008-09-02 14:56:44 +00001634 /**
1635 * An ExternalStringResource is a wrapper around a two-byte string
1636 * buffer that resides outside V8's heap. Implement an
1637 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001638 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001639 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001640 class V8EXPORT ExternalStringResource
1641 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 public:
1643 /**
1644 * Override the destructor to manage the life cycle of the underlying
1645 * buffer.
1646 */
1647 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001648
1649 /**
1650 * The string data from the underlying buffer.
1651 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001653
1654 /**
1655 * The length of the string. That is, the number of two-byte characters.
1656 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001658
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 protected:
1660 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661 };
1662
1663 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001664 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001665 * string buffer that resides outside V8's heap. Implement an
1666 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001667 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001668 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001669 * UTF-8, which would require special treatment internally in the
1670 * engine and, in the case of UTF-8, do not allow efficient indexing.
1671 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001672 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001674 class V8EXPORT ExternalAsciiStringResource
1675 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 public:
1677 /**
1678 * Override the destructor to manage the life cycle of the underlying
1679 * buffer.
1680 */
1681 virtual ~ExternalAsciiStringResource() {}
1682 /** The string data from the underlying buffer.*/
1683 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001684 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 virtual size_t length() const = 0;
1686 protected:
1687 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 };
1689
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001690 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1691
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001693 * If the string is an external string, return the ExternalStringResourceBase
1694 * regardless of the encoding, otherwise return NULL. The encoding of the
1695 * string is returned in encoding_out.
1696 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001697 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1698 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001699
1700 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001701 * Get the ExternalStringResource for an external string. Returns
1702 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001704 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705
1706 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001707 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001708 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001710 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001712 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001714 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001716 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001717 * The second parameter 'length' gives the buffer length. If omitted,
1718 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001720 V8_INLINE(static Local<String> New(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001722 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001723 /** Allocates a new string from 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001724 V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001726 // TODO(dcarney): deprecate
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001727 /**
1728 * Creates an internalized string (historically called a "symbol",
1729 * not to be confused with ES6 symbols). Returns one if it exists already.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001730 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001731 V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
1732
1733 enum NewStringType {
1734 kNormalString, kInternalizedString, kUndetectableString
1735 };
1736
1737 /** Allocates a new string from UTF-8 data.*/
1738 static Local<String> NewFromUtf8(Isolate* isolate,
1739 const char* data,
1740 NewStringType type = kNormalString,
1741 int length = -1);
1742
1743 /** Allocates a new string from Latin-1 data.*/
1744 static Local<String> NewFromOneByte(
1745 Isolate* isolate,
1746 const uint8_t* data,
1747 NewStringType type = kNormalString,
1748 int length = -1);
1749
1750 /** Allocates a new string from UTF-16 data.*/
1751 static Local<String> NewFromTwoByte(
1752 Isolate* isolate,
1753 const uint16_t* data,
1754 NewStringType type = kNormalString,
1755 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756
v8.team.kasperl727e9952008-09-02 14:56:44 +00001757 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001758 * Creates a new string by concatenating the left and the right strings
1759 * passed in as parameters.
1760 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001761 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001762
1763 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001764 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001765 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001766 * resource will be disposed by calling its Dispose method. The caller of
1767 * this function should not otherwise delete or modify the resource. Neither
1768 * should the underlying buffer be deallocated or modified except through the
1769 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001770 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001771 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001772
ager@chromium.org6f10e412009-02-13 10:11:16 +00001773 /**
1774 * Associate an external string resource with this string by transforming it
1775 * in place so that existing references to this string in the JavaScript heap
1776 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001777 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001778 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001779 * The string is not modified if the operation fails. See NewExternal for
1780 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001781 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001782 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783
v8.team.kasperl727e9952008-09-02 14:56:44 +00001784 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001785 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001786 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001787 * resource will be disposed by calling its Dispose method. The caller of
1788 * this function should not otherwise delete or modify the resource. Neither
1789 * should the underlying buffer be deallocated or modified except through the
1790 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001791 */
1792 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001793
ager@chromium.org6f10e412009-02-13 10:11:16 +00001794 /**
1795 * Associate an external string resource with this string by transforming it
1796 * in place so that existing references to this string in the JavaScript heap
1797 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001798 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001799 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001800 * The string is not modified if the operation fails. See NewExternal for
1801 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001802 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001803 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001805 /**
1806 * Returns true if this string can be made external.
1807 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001808 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001809
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001810 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001811 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001812 V8_INLINE(
1813 static Local<String> NewUndetectable(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001815 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001816 /** Creates an undetectable string from the supplied 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001817 V8_INLINE(static Local<String> NewUndetectable(
1818 const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819
1820 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001821 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001822 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001823 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001824 * then the length() method returns 0 and the * operator returns
1825 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001826 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001827 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001828 public:
1829 explicit Utf8Value(Handle<v8::Value> obj);
1830 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001831 char* operator*() { return str_; }
1832 const char* operator*() const { return str_; }
1833 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001834 private:
1835 char* str_;
1836 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001837
1838 // Disallow copying and assigning.
1839 Utf8Value(const Utf8Value&);
1840 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001841 };
1842
1843 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001844 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001846 * If conversion to a string fails (eg. due to an exception in the toString()
1847 * method of the object) then the length() method returns 0 and the * operator
1848 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001850 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 public:
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001852 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 explicit AsciiValue(Handle<v8::Value> obj);
1854 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001855 char* operator*() { return str_; }
1856 const char* operator*() const { return str_; }
1857 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 private:
1859 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001860 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001861
1862 // Disallow copying and assigning.
1863 AsciiValue(const AsciiValue&);
1864 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 };
1866
1867 /**
1868 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001869 * If conversion to a string fails (eg. due to an exception in the toString()
1870 * method of the object) then the length() method returns 0 and the * operator
1871 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001873 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 public:
1875 explicit Value(Handle<v8::Value> obj);
1876 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001877 uint16_t* operator*() { return str_; }
1878 const uint16_t* operator*() const { return str_; }
1879 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 private:
1881 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001882 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001883
1884 // Disallow copying and assigning.
1885 Value(const Value&);
1886 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001888
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001889 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001890 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1891 Encoding encoding) const;
1892 void VerifyExternalStringResource(ExternalStringResource* val) const;
1893 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894};
1895
1896
1897/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001898 * A JavaScript symbol (ECMA-262 edition 6)
1899 *
1900 * This is an experimental feature. Use at your own risk.
1901 */
1902class V8EXPORT Symbol : public Primitive {
1903 public:
1904 // Returns the print name string of the symbol, or undefined if none.
1905 Local<Value> Name() const;
1906
1907 // Create a symbol without a print name.
1908 static Local<Symbol> New(Isolate* isolate);
1909
1910 // Create a symbol with a print name.
1911 static Local<Symbol> New(Isolate *isolate, const char* data, int length = -1);
1912
1913 V8_INLINE(static Symbol* Cast(v8::Value* obj));
1914 private:
1915 Symbol();
1916 static void CheckCast(v8::Value* obj);
1917};
1918
1919
1920/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001921 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001923class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001925 double Value() const;
1926 static Local<Number> New(double value);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001927 static Local<Number> New(Isolate* isolate, double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001928 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001930 Number();
1931 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932};
1933
1934
1935/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001936 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001938class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001940 static Local<Integer> New(int32_t value);
1941 static Local<Integer> NewFromUnsigned(uint32_t value);
1942 static Local<Integer> New(int32_t value, Isolate*);
1943 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1944 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001945 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001947 Integer();
1948 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949};
1950
1951
1952/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001953 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001955class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001957 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001959 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960};
1961
1962
1963/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001964 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001966class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001968 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001970 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971};
1972
1973
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974enum PropertyAttribute {
1975 None = 0,
1976 ReadOnly = 1 << 0,
1977 DontEnum = 1 << 1,
1978 DontDelete = 1 << 2
1979};
1980
ager@chromium.org3811b432009-10-28 14:53:37 +00001981enum ExternalArrayType {
1982 kExternalByteArray = 1,
1983 kExternalUnsignedByteArray,
1984 kExternalShortArray,
1985 kExternalUnsignedShortArray,
1986 kExternalIntArray,
1987 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001988 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001989 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001990 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001991};
1992
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001994 * Accessor[Getter|Setter] are used as callback functions when
1995 * setting|getting a particular property. See Object and ObjectTemplate's
1996 * method SetAccessor.
1997 */
1998typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1999 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002000typedef void (*AccessorGetterCallback)(
2001 Local<String> property,
2002 const PropertyCallbackInfo<Value>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002003
2004
2005typedef void (*AccessorSetter)(Local<String> property,
2006 Local<Value> value,
2007 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002008typedef void (*AccessorSetterCallback)(
2009 Local<String> property,
2010 Local<Value> value,
2011 const PropertyCallbackInfo<void>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002012
2013
2014/**
2015 * Access control specifications.
2016 *
2017 * Some accessors should be accessible across contexts. These
2018 * accessors have an explicit access control parameter which specifies
2019 * the kind of cross-context access that should be allowed.
2020 *
2021 * Additionally, for security, accessors can prohibit overwriting by
2022 * accessors defined in JavaScript. For objects that have such
2023 * accessors either locally or in their prototype chain it is not
2024 * possible to overwrite the accessor by using __defineGetter__ or
2025 * __defineSetter__ from JavaScript code.
2026 */
2027enum AccessControl {
2028 DEFAULT = 0,
2029 ALL_CAN_READ = 1,
2030 ALL_CAN_WRITE = 1 << 1,
2031 PROHIBITS_OVERWRITING = 1 << 2
2032};
2033
2034
2035/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002036 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002038class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002040 bool Set(Handle<Value> key,
2041 Handle<Value> value,
2042 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002043
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002044 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002045
ager@chromium.orge2902be2009-06-08 12:21:35 +00002046 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002047 // overriding accessors or read-only properties.
2048 //
2049 // Note that if the object has an interceptor the property will be set
2050 // locally, but since the interceptor takes precedence the local property
2051 // will only be returned if the interceptor doesn't return a value.
2052 //
2053 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002054 bool ForceSet(Handle<Value> key,
2055 Handle<Value> value,
2056 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002057
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002058 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002060 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002061
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002062 /**
2063 * Gets the property attributes of a property which can be None or
2064 * any combination of ReadOnly, DontEnum and DontDelete. Returns
2065 * None when the property doesn't exist.
2066 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002067 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002068
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002069 bool Has(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002070
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002071 bool Delete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002072
2073 // Delete a property on this object bypassing interceptors and
2074 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002075 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002076
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002077 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002078
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002079 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002081 // TODO(dcarney): deprecate
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002082 bool SetAccessor(Handle<String> name,
2083 AccessorGetter getter,
2084 AccessorSetter setter = 0,
2085 Handle<Value> data = Handle<Value>(),
2086 AccessControl settings = DEFAULT,
2087 PropertyAttribute attribute = None);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002088 bool SetAccessor(Handle<String> name,
2089 AccessorGetterCallback getter,
2090 AccessorSetterCallback setter = 0,
2091 Handle<Value> data = Handle<Value>(),
2092 AccessControl settings = DEFAULT,
2093 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002094
ulan@chromium.org750145a2013-03-07 15:14:13 +00002095 // This function is not yet stable and should not be used at this time.
2096 bool SetAccessor(Handle<String> name,
2097 Handle<DeclaredAccessorDescriptor> descriptor,
2098 AccessControl settings = DEFAULT,
2099 PropertyAttribute attribute = None);
2100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002102 * Returns an array containing the names of the enumerable properties
2103 * of this object, including properties from prototype objects. The
2104 * array returned by this method contains the same values as would
2105 * be enumerated by a for-in statement over this object.
2106 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002107 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002108
2109 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002110 * This function has the same functionality as GetPropertyNames but
2111 * the returned array doesn't contain the names of properties from
2112 * prototype objects.
2113 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002114 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002115
2116 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117 * Get the prototype object. This does not skip objects marked to
2118 * be skipped by __proto__ and it does not consult the security
2119 * handler.
2120 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002121 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122
2123 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002124 * Set the prototype object. This does not skip objects marked to
2125 * be skipped by __proto__ and it does not consult the security
2126 * handler.
2127 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002128 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00002129
2130 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002131 * Finds an instance of the given function template in the prototype
2132 * chain.
2133 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002134 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002135
2136 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 * Call builtin Object.prototype.toString on this object.
2138 * This is different from Value::ToString() that may call
2139 * user-defined toString function. This one does not.
2140 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002141 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002143 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002144 * Returns the function invoked as a constructor for this object.
2145 * May be the null value.
2146 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002147 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002148
2149 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002150 * Returns the name of the function invoked as a constructor for this object.
2151 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002152 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002153
kasper.lund212ac232008-07-16 07:07:30 +00002154 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002155 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002156
2157 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002158 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002159
kasper.lund212ac232008-07-16 07:07:30 +00002160 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002161 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002162
2163 /**
2164 * Gets a 2-byte-aligned native pointer from an internal field. This field
2165 * must have been set by SetAlignedPointerInInternalField, everything else
2166 * leads to undefined behavior.
2167 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002168 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002169
2170 /**
2171 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2172 * a field, GetAlignedPointerFromInternalField must be used, everything else
2173 * leads to undefined behavior.
2174 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002175 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002178 bool HasOwnProperty(Handle<String> key);
2179 bool HasRealNamedProperty(Handle<String> key);
2180 bool HasRealIndexedProperty(uint32_t index);
2181 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182
2183 /**
2184 * If result.IsEmpty() no real property was located in the prototype chain.
2185 * This means interceptors in the prototype chain are not called.
2186 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002187 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00002188
2189 /**
2190 * If result.IsEmpty() no real property was located on the object or
2191 * in the prototype chain.
2192 * This means interceptors in the prototype chain are not called.
2193 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002194 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195
2196 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002197 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198
kasper.lund212ac232008-07-16 07:07:30 +00002199 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002200 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002202 /**
2203 * Turns on access check on the object if the object is an instance of
2204 * a template that has access check callbacks. If an object has no
2205 * access check info, the object cannot be accessed by anyone.
2206 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002207 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00002208
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002209 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002210 * Returns the identity hash for this object. The current implementation
2211 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002212 *
ager@chromium.org9085a012009-05-11 19:22:57 +00002213 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00002214 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002215 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002216 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00002217
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002218 /**
2219 * Access hidden properties on JavaScript objects. These properties are
2220 * hidden from the executing JavaScript and only accessible through the V8
2221 * C++ API. Hidden properties introduced by V8 internally (for example the
2222 * identity hash) are prefixed with "v8::".
2223 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002224 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
2225 Local<Value> GetHiddenValue(Handle<String> key);
2226 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002227
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002228 /**
2229 * Returns true if this is an instance of an api function (one
2230 * created from a function created from a function template) and has
2231 * been modified since it was created. Note that this method is
2232 * conservative and may return true for objects that haven't actually
2233 * been modified.
2234 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002235 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002236
2237 /**
2238 * Clone this object with a fast but shallow copy. Values will point
2239 * to the same values as the original object.
2240 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002241 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002243 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002244 * Returns the context in which the object was created.
2245 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002246 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002247
2248 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002249 * Set the backing store of the indexed properties to be managed by the
2250 * embedding layer. Access to the indexed properties will follow the rules
2251 * spelled out in CanvasPixelArray.
2252 * Note: The embedding program still owns the data and needs to ensure that
2253 * the backing store is preserved while V8 has a reference.
2254 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002255 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
2256 bool HasIndexedPropertiesInPixelData();
2257 uint8_t* GetIndexedPropertiesPixelData();
2258 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002259
ager@chromium.org3811b432009-10-28 14:53:37 +00002260 /**
2261 * Set the backing store of the indexed properties to be managed by the
2262 * embedding layer. Access to the indexed properties will follow the rules
2263 * spelled out for the CanvasArray subtypes in the WebGL specification.
2264 * Note: The embedding program still owns the data and needs to ensure that
2265 * the backing store is preserved while V8 has a reference.
2266 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002267 void SetIndexedPropertiesToExternalArrayData(void* data,
2268 ExternalArrayType array_type,
2269 int number_of_elements);
2270 bool HasIndexedPropertiesInExternalArrayData();
2271 void* GetIndexedPropertiesExternalArrayData();
2272 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
2273 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00002274
lrn@chromium.org1c092762011-05-09 09:42:16 +00002275 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002276 * Checks whether a callback is set by the
2277 * ObjectTemplate::SetCallAsFunctionHandler method.
2278 * When an Object is callable this method returns true.
2279 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002280 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002281
2282 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002283 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002284 * ObjectTemplate::SetCallAsFunctionHandler method.
2285 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002286 Local<Value> CallAsFunction(Handle<Object> recv,
2287 int argc,
2288 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002289
2290 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002291 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002292 * ObjectTemplate::SetCallAsFunctionHandler method.
2293 * Note: This method behaves like the Function::NewInstance method.
2294 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002295 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002296
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002297 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002298 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002299
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002301 Object();
2302 static void CheckCast(Value* obj);
2303 Local<Value> SlowGetInternalField(int index);
2304 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305};
2306
2307
2308/**
2309 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
2310 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002311class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002313 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314
ager@chromium.org3e875802009-06-29 08:26:34 +00002315 /**
2316 * Clones an element at index |index|. Returns an empty
2317 * handle if cloning fails (for any reason).
2318 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002319 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00002320
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002321 /**
2322 * Creates a JavaScript array with the given length. If the length
2323 * is negative the returned array will have length 0.
2324 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002325 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002326
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002327 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002329 Array();
2330 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331};
2332
2333
2334/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002335 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002337class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002339 Local<Object> NewInstance() const;
2340 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
2341 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
2342 void SetName(Handle<String> name);
2343 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00002344
2345 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002346 * Name inferred from variable or property assignment of this function.
2347 * Used to facilitate debugging and profiling of JavaScript code written
2348 * in an OO style, where many functions are anonymous but are assigned
2349 * to object properties.
2350 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002351 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002352
2353 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002354 * Returns zero based line number of function body and
2355 * kLineOffsetNotFound if no information available.
2356 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002357 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002358 /**
2359 * Returns zero based column number of function body and
2360 * kLineOffsetNotFound if no information available.
2361 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002362 int GetScriptColumnNumber() const;
2363 Handle<Value> GetScriptId() const;
2364 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002365 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002366 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002367
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002369 Function();
2370 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002371};
2372
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002373/**
2374 * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
2375 * populates an instance of this class with a pointer to data and byte length.
2376 *
2377 * |ArrayBufferContents| is the owner of its data. When an instance of
2378 * this class is destructed, the |Data| is freed.
2379 *
2380 * This API is experimental and may change significantly.
2381 */
2382class V8EXPORT ArrayBufferContents {
2383 public:
2384 ArrayBufferContents() : data_(NULL), byte_length_(0) {}
2385 ~ArrayBufferContents();
2386
2387 void* Data() const { return data_; }
2388 size_t ByteLength() const { return byte_length_; }
2389
2390 private:
2391 void* data_;
2392 size_t byte_length_;
2393
2394 friend class ArrayBuffer;
2395};
2396
2397#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
2398#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
2399#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002400
2401/**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002402 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
2403 * This API is experimental and may change significantly.
2404 */
2405class V8EXPORT ArrayBuffer : public Object {
2406 public:
2407 /**
2408 * Data length in bytes.
2409 */
2410 size_t ByteLength() const;
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002411
2412 /**
2413 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
2414 * Allocated memory will be owned by a created ArrayBuffer and
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002415 * will be deallocated when it is garbage-collected,
2416 * unless the object is externalized.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002417 */
2418 static Local<ArrayBuffer> New(size_t byte_length);
2419
2420 /**
2421 * Create a new ArrayBuffer over an existing memory block.
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002422 * The created array buffer is immediately in externalized state.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002423 * The memory block will not be reclaimed when a created ArrayBuffer
2424 * is garbage-collected.
2425 */
2426 static Local<ArrayBuffer> New(void* data, size_t byte_length);
2427
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002428 /**
2429 * Returns true if ArrayBuffer is extrenalized, that is, does not
2430 * own its memory block.
2431 */
2432 bool IsExternal() const;
2433
2434 /**
danno@chromium.org1fd77d52013-06-07 16:01:45 +00002435 * Neuters this ArrayBuffer and all its views (typed arrays).
2436 * Neutering sets the byte length of the buffer and all typed arrays to zero,
2437 * preventing JavaScript from ever accessing underlying backing store.
2438 * ArrayBuffer should have been externalized.
2439 */
2440 void Neuter();
2441
2442 /**
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002443 * Pass the ownership of this ArrayBuffer's backing store to
2444 * a given ArrayBufferContents.
2445 */
2446 void Externalize(ArrayBufferContents* contents);
2447
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002448 V8_INLINE(static ArrayBuffer* Cast(Value* obj));
2449
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002450
2451 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
2452
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002453 private:
2454 ArrayBuffer();
2455 static void CheckCast(Value* obj);
2456};
2457
2458
2459/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002460 * A base class for an instance of TypedArray series of constructors
2461 * (ES6 draft 15.13.6).
2462 * This API is experimental and may change significantly.
2463 */
2464class V8EXPORT TypedArray : public Object {
2465 public:
2466 /**
2467 * Returns underlying ArrayBuffer.
2468 */
2469 Local<ArrayBuffer> Buffer();
2470 /**
2471 * Byte offset in |Buffer|
2472 */
2473 size_t ByteOffset();
2474 /**
2475 * Numbe of elements in this typed array.
2476 */
2477 size_t Length();
2478 /**
2479 * Size of typed array in bytes (e.g. for Int16Array, 2*|Length|).
2480 */
2481 size_t ByteLength();
2482 /**
2483 * Base address of typed array.
2484 */
2485 void* BaseAddress();
2486
2487 V8_INLINE(static TypedArray* Cast(Value* obj));
2488
2489 private:
2490 TypedArray();
2491 static void CheckCast(Value* obj);
2492};
2493
2494
2495/**
2496 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
2497 * This API is experimental and may change significantly.
2498 */
2499class V8EXPORT Uint8Array : public TypedArray {
2500 public:
2501 static Local<Uint8Array> New(Handle<ArrayBuffer> array_buffer,
2502 size_t byte_offset, size_t length);
2503 V8_INLINE(static Uint8Array* Cast(Value* obj));
2504
2505 private:
2506 Uint8Array();
2507 static void CheckCast(Value* obj);
2508};
2509
2510
2511/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002512 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
2513 * This API is experimental and may change significantly.
2514 */
2515class V8EXPORT Uint8ClampedArray : public TypedArray {
2516 public:
2517 static Local<Uint8ClampedArray> New(Handle<ArrayBuffer> array_buffer,
2518 size_t byte_offset, size_t length);
2519 V8_INLINE(static Uint8ClampedArray* Cast(Value* obj));
2520
2521 private:
2522 Uint8ClampedArray();
2523 static void CheckCast(Value* obj);
2524};
2525
2526/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002527 * An instance of Int8Array constructor (ES6 draft 15.13.6).
2528 * This API is experimental and may change significantly.
2529 */
2530class V8EXPORT Int8Array : public TypedArray {
2531 public:
2532 static Local<Int8Array> New(Handle<ArrayBuffer> array_buffer,
2533 size_t byte_offset, size_t length);
2534 V8_INLINE(static Int8Array* Cast(Value* obj));
2535
2536 private:
2537 Int8Array();
2538 static void CheckCast(Value* obj);
2539};
2540
2541
2542/**
2543 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
2544 * This API is experimental and may change significantly.
2545 */
2546class V8EXPORT Uint16Array : public TypedArray {
2547 public:
2548 static Local<Uint16Array> New(Handle<ArrayBuffer> array_buffer,
2549 size_t byte_offset, size_t length);
2550 V8_INLINE(static Uint16Array* Cast(Value* obj));
2551
2552 private:
2553 Uint16Array();
2554 static void CheckCast(Value* obj);
2555};
2556
2557
2558/**
2559 * An instance of Int16Array constructor (ES6 draft 15.13.6).
2560 * This API is experimental and may change significantly.
2561 */
2562class V8EXPORT Int16Array : public TypedArray {
2563 public:
2564 static Local<Int16Array> New(Handle<ArrayBuffer> array_buffer,
2565 size_t byte_offset, size_t length);
2566 V8_INLINE(static Int16Array* Cast(Value* obj));
2567
2568 private:
2569 Int16Array();
2570 static void CheckCast(Value* obj);
2571};
2572
2573
2574/**
2575 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
2576 * This API is experimental and may change significantly.
2577 */
2578class V8EXPORT Uint32Array : public TypedArray {
2579 public:
2580 static Local<Uint32Array> New(Handle<ArrayBuffer> array_buffer,
2581 size_t byte_offset, size_t length);
2582 V8_INLINE(static Uint32Array* Cast(Value* obj));
2583
2584 private:
2585 Uint32Array();
2586 static void CheckCast(Value* obj);
2587};
2588
2589
2590/**
2591 * An instance of Int32Array constructor (ES6 draft 15.13.6).
2592 * This API is experimental and may change significantly.
2593 */
2594class V8EXPORT Int32Array : public TypedArray {
2595 public:
2596 static Local<Int32Array> New(Handle<ArrayBuffer> array_buffer,
2597 size_t byte_offset, size_t length);
2598 V8_INLINE(static Int32Array* Cast(Value* obj));
2599
2600 private:
2601 Int32Array();
2602 static void CheckCast(Value* obj);
2603};
2604
2605
2606/**
2607 * An instance of Float32Array constructor (ES6 draft 15.13.6).
2608 * This API is experimental and may change significantly.
2609 */
2610class V8EXPORT Float32Array : public TypedArray {
2611 public:
2612 static Local<Float32Array> New(Handle<ArrayBuffer> array_buffer,
2613 size_t byte_offset, size_t length);
2614 V8_INLINE(static Float32Array* Cast(Value* obj));
2615
2616 private:
2617 Float32Array();
2618 static void CheckCast(Value* obj);
2619};
2620
2621
2622/**
2623 * An instance of Float64Array constructor (ES6 draft 15.13.6).
2624 * This API is experimental and may change significantly.
2625 */
2626class V8EXPORT Float64Array : public TypedArray {
2627 public:
2628 static Local<Float64Array> New(Handle<ArrayBuffer> array_buffer,
2629 size_t byte_offset, size_t length);
2630 V8_INLINE(static Float64Array* Cast(Value* obj));
2631
2632 private:
2633 Float64Array();
2634 static void CheckCast(Value* obj);
2635};
2636
2637
2638/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002639 * An instance of the built-in Date constructor (ECMA-262, 15.9).
2640 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002641class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002642 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002643 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002644
2645 /**
2646 * A specialization of Value::NumberValue that is more efficient
2647 * because we know the structure of this object.
2648 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002649 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002650
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002651 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002652
2653 /**
2654 * Notification that the embedder has changed the time zone,
2655 * daylight savings time, or other date / time configuration
2656 * parameters. V8 keeps a cache of various values used for
2657 * date / time computation. This notification will reset
2658 * those cached values for the current context so that date /
2659 * time configuration changes would be reflected in the Date
2660 * object.
2661 *
2662 * This API should not be called more than needed as it will
2663 * negatively impact the performance of date operations.
2664 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002665 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002666
2667 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002668 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002669};
2670
2671
2672/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002673 * A Number object (ECMA-262, 4.3.21).
2674 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002675class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002676 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002677 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002678
2679 /**
2680 * Returns the Number held by the object.
2681 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002682 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002683
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002684 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002685
2686 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002687 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002688};
2689
2690
2691/**
2692 * A Boolean object (ECMA-262, 4.3.15).
2693 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002694class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002695 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002696 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002697
2698 /**
2699 * Returns the Boolean held by the object.
2700 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002701 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002702
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002703 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002704
2705 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002706 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002707};
2708
2709
2710/**
2711 * A String object (ECMA-262, 4.3.18).
2712 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002713class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002714 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002715 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002716
2717 /**
2718 * Returns the String held by the object.
2719 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002720 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002721
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002722 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002723
2724 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002725 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002726};
2727
2728
2729/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002730 * A Symbol object (ECMA-262 edition 6).
2731 *
2732 * This is an experimental feature. Use at your own risk.
2733 */
2734class V8EXPORT SymbolObject : public Object {
2735 public:
2736 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2737
2738 /**
2739 * Returns the Symbol held by the object.
2740 */
2741 Local<Symbol> SymbolValue() const;
2742
2743 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2744
2745 private:
2746 static void CheckCast(v8::Value* obj);
2747};
2748
2749
2750/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002751 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2752 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002753class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002754 public:
2755 /**
2756 * Regular expression flag bits. They can be or'ed to enable a set
2757 * of flags.
2758 */
2759 enum Flags {
2760 kNone = 0,
2761 kGlobal = 1,
2762 kIgnoreCase = 2,
2763 kMultiline = 4
2764 };
2765
2766 /**
2767 * Creates a regular expression from the given pattern string and
2768 * the flags bit field. May throw a JavaScript exception as
2769 * described in ECMA-262, 15.10.4.1.
2770 *
2771 * For example,
2772 * RegExp::New(v8::String::New("foo"),
2773 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2774 * is equivalent to evaluating "/foo/gm".
2775 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002776 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002777
2778 /**
2779 * Returns the value of the source property: a string representing
2780 * the regular expression.
2781 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002782 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002783
2784 /**
2785 * Returns the flags bit field.
2786 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002787 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002788
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002789 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002790
2791 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002792 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002793};
2794
2795
2796/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002797 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2798 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002800class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002801 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002802 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002803 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002804 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002806 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002807};
2808
2809
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002810// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811
2812
2813/**
2814 * The superclass of object and function templates.
2815 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002816class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002817 public:
2818 /** Adds a property to each instance created by this template.*/
2819 void Set(Handle<String> name, Handle<Data> value,
2820 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002821 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002822 private:
2823 Template();
2824
2825 friend class ObjectTemplate;
2826 friend class FunctionTemplate;
2827};
2828
2829
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002830template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002831class ReturnValue {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002832 public:
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00002833 template <class S> V8_INLINE(ReturnValue(const ReturnValue<S>& that))
2834 : value_(that.value_) {
2835 TYPE_CHECK(T, S);
2836 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002837 // Handle setters
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00002838 template <typename S> V8_INLINE(void Set(const Persistent<S>& handle));
2839 template <typename S> V8_INLINE(void Set(const Handle<S> handle));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002840 // Fast primitive setters
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002841 V8_INLINE(void Set(bool value));
2842 V8_INLINE(void Set(double i));
2843 V8_INLINE(void Set(int32_t i));
2844 V8_INLINE(void Set(uint32_t i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002845 // Fast JS primitive setters
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002846 V8_INLINE(void SetNull());
2847 V8_INLINE(void SetUndefined());
2848 // Convenience getter for Isolate
2849 V8_INLINE(Isolate* GetIsolate());
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00002850
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002851 private:
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00002852 template<class F> friend class ReturnValue;
2853 template<class F> friend class FunctionCallbackInfo;
2854 template<class F> friend class PropertyCallbackInfo;
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002855 V8_INLINE(internal::Object* GetDefaultValue());
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00002856 V8_INLINE(explicit ReturnValue(internal::Object** slot));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002857 internal::Object** value_;
2858};
2859
2860
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861/**
2862 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002863 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002864 * including the receiver, the number and values of arguments, and
2865 * the holder of the function.
2866 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002867template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002868class FunctionCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002869 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002870 V8_INLINE(int Length() const);
2871 V8_INLINE(Local<Value> operator[](int i) const);
2872 V8_INLINE(Local<Function> Callee() const);
2873 V8_INLINE(Local<Object> This() const);
2874 V8_INLINE(Local<Object> Holder() const);
2875 V8_INLINE(bool IsConstructCall() const);
2876 V8_INLINE(Local<Value> Data() const);
2877 V8_INLINE(Isolate* GetIsolate() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002878 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2879 // This shouldn't be public, but the arm compiler needs it.
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002880 static const int kArgsLength = 6;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002881
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002882 protected:
2883 friend class internal::FunctionCallbackArguments;
2884 friend class internal::CustomArguments<FunctionCallbackInfo>;
2885 static const int kReturnValueIndex = 0;
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002886 static const int kReturnValueDefaultValueIndex = -1;
2887 static const int kIsolateIndex = -2;
2888 static const int kDataIndex = -3;
2889 static const int kCalleeIndex = -4;
2890 static const int kHolderIndex = -5;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002891
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002892 V8_INLINE(FunctionCallbackInfo(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002893 internal::Object** values,
2894 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002895 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002896 internal::Object** implicit_args_;
2897 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002898 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002899 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900};
2901
2902
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002903class V8EXPORT Arguments : public FunctionCallbackInfo<Value> {
2904 private:
2905 friend class internal::FunctionCallbackArguments;
2906 V8_INLINE(Arguments(internal::Object** implicit_args,
2907 internal::Object** values,
2908 int length,
2909 bool is_construct_call));
2910};
2911
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002912/**
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002913 * The information passed to a property callback about the context
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002914 * of the property access.
2915 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002916template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002917class PropertyCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002919 V8_INLINE(Isolate* GetIsolate() const);
2920 V8_INLINE(Local<Value> Data() const);
2921 V8_INLINE(Local<Object> This() const);
2922 V8_INLINE(Local<Object> Holder() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002923 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2924 // This shouldn't be public, but the arm compiler needs it.
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002925 static const int kArgsLength = 6;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002926
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002927 protected:
2928 friend class MacroAssembler;
2929 friend class internal::PropertyCallbackArguments;
2930 friend class internal::CustomArguments<PropertyCallbackInfo>;
2931 static const int kThisIndex = 0;
2932 static const int kHolderIndex = -1;
2933 static const int kDataIndex = -2;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002934 static const int kReturnValueIndex = -3;
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002935 static const int kReturnValueDefaultValueIndex = -4;
2936 static const int kIsolateIndex = -5;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002937
2938 V8_INLINE(PropertyCallbackInfo(internal::Object** args))
2939 : args_(args) { }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002940 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941};
2942
2943
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002944class V8EXPORT AccessorInfo : public PropertyCallbackInfo<Value> {
2945 private:
2946 friend class internal::PropertyCallbackArguments;
2947 V8_INLINE(AccessorInfo(internal::Object** args))
2948 : PropertyCallbackInfo<Value>(args) { }
2949};
2950
2951
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002953typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002954
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002956 * NamedProperty[Getter|Setter] are used as interceptors on object.
2957 * See ObjectTemplate::SetNamedPropertyHandler.
2958 */
2959typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2960 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002961typedef void (*NamedPropertyGetterCallback)(
2962 Local<String> property,
2963 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964
2965
2966/**
2967 * Returns the value if the setter intercepts the request.
2968 * Otherwise, returns an empty handle.
2969 */
2970typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2971 Local<Value> value,
2972 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002973typedef void (*NamedPropertySetterCallback)(
2974 Local<String> property,
2975 Local<Value> value,
2976 const PropertyCallbackInfo<Value>& info);
2977
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002978
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002979/**
2980 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002981 * The result is an integer encoding property attributes (like v8::None,
2982 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002983 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002984typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2985 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002986typedef void (*NamedPropertyQueryCallback)(
2987 Local<String> property,
2988 const PropertyCallbackInfo<Integer>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002989
2990
2991/**
2992 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002993 * The return value is true if the property could be deleted and false
2994 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995 */
2996typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2997 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002998typedef void (*NamedPropertyDeleterCallback)(
2999 Local<String> property,
3000 const PropertyCallbackInfo<Boolean>& info);
3001
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002
3003/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003004 * Returns an array containing the names of the properties the named
3005 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003006 */
3007typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003008typedef void (*NamedPropertyEnumeratorCallback)(
3009 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003010
v8.team.kasperl727e9952008-09-02 14:56:44 +00003011
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003012/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003013 * Returns the value of the property if the getter intercepts the
3014 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015 */
3016typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
3017 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003018typedef void (*IndexedPropertyGetterCallback)(
3019 uint32_t index,
3020 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021
3022
3023/**
3024 * Returns the value if the setter intercepts the request.
3025 * Otherwise, returns an empty handle.
3026 */
3027typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
3028 Local<Value> value,
3029 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003030typedef void (*IndexedPropertySetterCallback)(
3031 uint32_t index,
3032 Local<Value> value,
3033 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003034
3035
3036/**
3037 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003038 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003039 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003040typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
3041 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003042typedef void (*IndexedPropertyQueryCallback)(
3043 uint32_t index,
3044 const PropertyCallbackInfo<Integer>& info);
3045
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046
3047/**
3048 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003049 * The return value is true if the property could be deleted and false
3050 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003051 */
3052typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
3053 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003054typedef void (*IndexedPropertyDeleterCallback)(
3055 uint32_t index,
3056 const PropertyCallbackInfo<Boolean>& info);
3057
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003058
v8.team.kasperl727e9952008-09-02 14:56:44 +00003059/**
3060 * Returns an array containing the indices of the properties the
3061 * indexed property getter intercepts.
3062 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003063typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003064typedef void (*IndexedPropertyEnumeratorCallback)(
3065 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003066
3067
3068/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003069 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003070 */
3071enum AccessType {
3072 ACCESS_GET,
3073 ACCESS_SET,
3074 ACCESS_HAS,
3075 ACCESS_DELETE,
3076 ACCESS_KEYS
3077};
3078
v8.team.kasperl727e9952008-09-02 14:56:44 +00003079
3080/**
3081 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003082 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003083 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003084typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085 Local<Value> key,
3086 AccessType type,
3087 Local<Value> data);
3088
v8.team.kasperl727e9952008-09-02 14:56:44 +00003089
3090/**
3091 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003092 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003093 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003094typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003095 uint32_t index,
3096 AccessType type,
3097 Local<Value> data);
3098
3099
3100/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003101 * A FunctionTemplate is used to create functions at runtime. There
3102 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003103 * context. The lifetime of the created function is equal to the
3104 * lifetime of the context. So in case the embedder needs to create
3105 * temporary functions that can be collected using Scripts is
3106 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003107 *
3108 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003109 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003110 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003111 * A FunctionTemplate has a corresponding instance template which is
3112 * used to create object instances when the function is used as a
3113 * constructor. Properties added to the instance template are added to
3114 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003115 *
3116 * A FunctionTemplate can have a prototype template. The prototype template
3117 * is used to create the prototype object of the function.
3118 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003119 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003121 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003122 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
3123 * t->Set("func_property", v8::Number::New(1));
3124 *
3125 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
3126 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
3127 * proto_t->Set("proto_const", v8::Number::New(2));
3128 *
3129 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
3130 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
3131 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
3132 * instance_t->Set("instance_property", Number::New(3));
3133 *
3134 * v8::Local<v8::Function> function = t->GetFunction();
3135 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003136 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003137 *
3138 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00003139 * and "instance" for the instance object created above. The function
3140 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003142 * \code
3143 * func_property in function == true;
3144 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003145 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003146 * function.prototype.proto_method() invokes 'InvokeCallback'
3147 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003148 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003149 * instance instanceof function == true;
3150 * instance.instance_accessor calls 'InstanceAccessorCallback'
3151 * instance.instance_property == 3;
3152 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003153 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003154 * A FunctionTemplate can inherit from another one by calling the
3155 * FunctionTemplate::Inherit method. The following graph illustrates
3156 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003157 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003158 * \code
3159 * FunctionTemplate Parent -> Parent() . prototype -> { }
3160 * ^ ^
3161 * | Inherit(Parent) | .__proto__
3162 * | |
3163 * FunctionTemplate Child -> Child() . prototype -> { }
3164 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003165 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003166 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
3167 * object of the Child() function has __proto__ pointing to the
3168 * Parent() function's prototype object. An instance of the Child
3169 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003170 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003171 * Let Parent be the FunctionTemplate initialized in the previous
3172 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003173 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003174 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003175 * Local<FunctionTemplate> parent = t;
3176 * Local<FunctionTemplate> child = FunctionTemplate::New();
3177 * child->Inherit(parent);
3178 *
3179 * Local<Function> child_function = child->GetFunction();
3180 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003181 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003182 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003183 * The Child function and Child instance will have the following
3184 * properties:
3185 *
3186 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003187 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003188 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003189 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003190 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003191 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003192class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003193 public:
3194 /** Creates a function template.*/
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003195 // TODO(dcarney): deprecate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003196 static Local<FunctionTemplate> New(
3197 InvocationCallback callback = 0,
3198 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003199 Handle<Signature> signature = Handle<Signature>(),
3200 int length = 0);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003201 static Local<FunctionTemplate> New(
3202 FunctionCallback callback, // TODO(dcarney): add back default param.
3203 Handle<Value> data = Handle<Value>(),
3204 Handle<Signature> signature = Handle<Signature>(),
3205 int length = 0);
3206
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207 /** Returns the unique function instance in the current execution context.*/
3208 Local<Function> GetFunction();
3209
v8.team.kasperl727e9952008-09-02 14:56:44 +00003210 /**
3211 * Set the call-handler callback for a FunctionTemplate. This
3212 * callback is called whenever the function created from this
3213 * FunctionTemplate is called.
3214 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003215 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003216 void SetCallHandler(InvocationCallback callback,
3217 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003218 void SetCallHandler(FunctionCallback callback,
3219 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003221 /** Set the predefined length property for the FunctionTemplate. */
3222 void SetLength(int length);
3223
v8.team.kasperl727e9952008-09-02 14:56:44 +00003224 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003225 Local<ObjectTemplate> InstanceTemplate();
3226
3227 /** Causes the function template to inherit from a parent function template.*/
3228 void Inherit(Handle<FunctionTemplate> parent);
3229
3230 /**
3231 * A PrototypeTemplate is the template used to create the prototype object
3232 * of the function created by this template.
3233 */
3234 Local<ObjectTemplate> PrototypeTemplate();
3235
v8.team.kasperl727e9952008-09-02 14:56:44 +00003236 /**
3237 * Set the class name of the FunctionTemplate. This is used for
3238 * printing objects created with the function created from the
3239 * FunctionTemplate as its constructor.
3240 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241 void SetClassName(Handle<String> name);
3242
3243 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003244 * Determines whether the __proto__ accessor ignores instances of
3245 * the function template. If instances of the function template are
3246 * ignored, __proto__ skips all instances and instead returns the
3247 * next object in the prototype chain.
3248 *
3249 * Call with a value of true to make the __proto__ accessor ignore
3250 * instances of the function template. Call with a value of false
3251 * to make the __proto__ accessor not ignore instances of the
3252 * function template. By default, instances of a function template
3253 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003254 */
3255 void SetHiddenPrototype(bool value);
3256
3257 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003258 * Sets the ReadOnly flag in the attributes of the 'prototype' property
3259 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00003260 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003261 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00003262
3263 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003264 * Returns true if the given object is an instance of this function
3265 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003266 */
3267 bool HasInstance(Handle<Value> object);
3268
3269 private:
3270 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003271 friend class Context;
3272 friend class ObjectTemplate;
3273};
3274
3275
3276/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003277 * An ObjectTemplate is used to create objects at runtime.
3278 *
3279 * Properties added to an ObjectTemplate are added to each object
3280 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003282class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003283 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003284 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003285 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287 /** Creates a new instance of this template.*/
3288 Local<Object> NewInstance();
3289
3290 /**
3291 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003292 *
3293 * Whenever the property with the given name is accessed on objects
3294 * created from this ObjectTemplate the getter and setter callbacks
3295 * are called instead of getting and setting the property directly
3296 * on the JavaScript object.
3297 *
3298 * \param name The name of the property for which an accessor is added.
3299 * \param getter The callback to invoke when getting the property.
3300 * \param setter The callback to invoke when setting the property.
3301 * \param data A piece of data that will be passed to the getter and setter
3302 * callbacks whenever they are invoked.
3303 * \param settings Access control settings for the accessor. This is a bit
3304 * field consisting of one of more of
3305 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
3306 * The default is to not allow cross-context access.
3307 * ALL_CAN_READ means that all cross-context reads are allowed.
3308 * ALL_CAN_WRITE means that all cross-context writes are allowed.
3309 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
3310 * cross-context access.
3311 * \param attribute The attributes of the property for which an accessor
3312 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003313 * \param signature The signature describes valid receivers for the accessor
3314 * and is used to perform implicit instance checks against them. If the
3315 * receiver is incompatible (i.e. is not an instance of the constructor as
3316 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
3317 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003318 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003319 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003320 void SetAccessor(Handle<String> name,
3321 AccessorGetter getter,
3322 AccessorSetter setter = 0,
3323 Handle<Value> data = Handle<Value>(),
3324 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003325 PropertyAttribute attribute = None,
3326 Handle<AccessorSignature> signature =
3327 Handle<AccessorSignature>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003328 void SetAccessor(Handle<String> name,
3329 AccessorGetterCallback getter,
3330 AccessorSetterCallback setter = 0,
3331 Handle<Value> data = Handle<Value>(),
3332 AccessControl settings = DEFAULT,
3333 PropertyAttribute attribute = None,
3334 Handle<AccessorSignature> signature =
3335 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003336
ulan@chromium.org750145a2013-03-07 15:14:13 +00003337 // This function is not yet stable and should not be used at this time.
3338 bool SetAccessor(Handle<String> name,
3339 Handle<DeclaredAccessorDescriptor> descriptor,
3340 AccessControl settings = DEFAULT,
3341 PropertyAttribute attribute = None,
3342 Handle<AccessorSignature> signature =
3343 Handle<AccessorSignature>());
3344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003345 /**
3346 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003347 *
3348 * Whenever a named property is accessed on objects created from
3349 * this object template, the provided callback is invoked instead of
3350 * accessing the property directly on the JavaScript object.
3351 *
3352 * \param getter The callback to invoke when getting a property.
3353 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003354 * \param query The callback to invoke to check if a property is present,
3355 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003356 * \param deleter The callback to invoke when deleting a property.
3357 * \param enumerator The callback to invoke to enumerate all the named
3358 * properties of an object.
3359 * \param data A piece of data that will be passed to the callbacks
3360 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003361 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003362 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003363 void SetNamedPropertyHandler(NamedPropertyGetter getter,
3364 NamedPropertySetter setter = 0,
3365 NamedPropertyQuery query = 0,
3366 NamedPropertyDeleter deleter = 0,
3367 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003368 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003369 void SetNamedPropertyHandler(
3370 NamedPropertyGetterCallback getter,
3371 NamedPropertySetterCallback setter = 0,
3372 NamedPropertyQueryCallback query = 0,
3373 NamedPropertyDeleterCallback deleter = 0,
3374 NamedPropertyEnumeratorCallback enumerator = 0,
3375 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003376
3377 /**
3378 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003379 *
3380 * Whenever an indexed property is accessed on objects created from
3381 * this object template, the provided callback is invoked instead of
3382 * accessing the property directly on the JavaScript object.
3383 *
3384 * \param getter The callback to invoke when getting a property.
3385 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003386 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003387 * \param deleter The callback to invoke when deleting a property.
3388 * \param enumerator The callback to invoke to enumerate all the indexed
3389 * properties of an object.
3390 * \param data A piece of data that will be passed to the callbacks
3391 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003392 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003393 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
3395 IndexedPropertySetter setter = 0,
3396 IndexedPropertyQuery query = 0,
3397 IndexedPropertyDeleter deleter = 0,
3398 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003399 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003400 void SetIndexedPropertyHandler(
3401 IndexedPropertyGetterCallback getter,
3402 IndexedPropertySetterCallback setter = 0,
3403 IndexedPropertyQueryCallback query = 0,
3404 IndexedPropertyDeleterCallback deleter = 0,
3405 IndexedPropertyEnumeratorCallback enumerator = 0,
3406 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003407
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003408 /**
3409 * Sets the callback to be used when calling instances created from
3410 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00003411 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003412 * function.
3413 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003414 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003415 void SetCallAsFunctionHandler(InvocationCallback callback,
3416 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003417 void SetCallAsFunctionHandler(FunctionCallback callback,
3418 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419
v8.team.kasperl727e9952008-09-02 14:56:44 +00003420 /**
3421 * Mark object instances of the template as undetectable.
3422 *
3423 * In many ways, undetectable objects behave as though they are not
3424 * there. They behave like 'undefined' in conditionals and when
3425 * printed. However, properties can be accessed and called as on
3426 * normal objects.
3427 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003428 void MarkAsUndetectable();
3429
v8.team.kasperl727e9952008-09-02 14:56:44 +00003430 /**
3431 * Sets access check callbacks on the object template.
3432 *
3433 * When accessing properties on instances of this object template,
3434 * the access check callback will be called to determine whether or
3435 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003436 * The last parameter specifies whether access checks are turned
3437 * on by default on instances. If access checks are off by default,
3438 * they can be turned on on individual instances by calling
3439 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00003440 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
3442 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003443 Handle<Value> data = Handle<Value>(),
3444 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003445
kasper.lund212ac232008-07-16 07:07:30 +00003446 /**
3447 * Gets the number of internal fields for objects generated from
3448 * this template.
3449 */
3450 int InternalFieldCount();
3451
3452 /**
3453 * Sets the number of internal fields for objects generated from
3454 * this template.
3455 */
3456 void SetInternalFieldCount(int value);
3457
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003458 private:
3459 ObjectTemplate();
3460 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
3461 friend class FunctionTemplate;
3462};
3463
3464
3465/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003466 * A Signature specifies which receivers and arguments are valid
3467 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003468 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003469class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003470 public:
3471 static Local<Signature> New(Handle<FunctionTemplate> receiver =
3472 Handle<FunctionTemplate>(),
3473 int argc = 0,
3474 Handle<FunctionTemplate> argv[] = 0);
3475 private:
3476 Signature();
3477};
3478
3479
3480/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003481 * An AccessorSignature specifies which receivers are valid parameters
3482 * to an accessor callback.
3483 */
3484class V8EXPORT AccessorSignature : public Data {
3485 public:
3486 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
3487 Handle<FunctionTemplate>());
3488 private:
3489 AccessorSignature();
3490};
3491
3492
ulan@chromium.org750145a2013-03-07 15:14:13 +00003493class V8EXPORT DeclaredAccessorDescriptor : public Data {
3494 private:
3495 DeclaredAccessorDescriptor();
3496};
3497
3498
3499class V8EXPORT ObjectOperationDescriptor : public Data {
3500 public:
3501 // This function is not yet stable and should not be used at this time.
3502 static Local<RawOperationDescriptor> NewInternalFieldDereference(
3503 Isolate* isolate,
3504 int internal_field);
3505 private:
3506 ObjectOperationDescriptor();
3507};
3508
3509
3510enum DeclaredAccessorDescriptorDataType {
3511 kDescriptorBoolType,
3512 kDescriptorInt8Type, kDescriptorUint8Type,
3513 kDescriptorInt16Type, kDescriptorUint16Type,
3514 kDescriptorInt32Type, kDescriptorUint32Type,
3515 kDescriptorFloatType, kDescriptorDoubleType
3516};
3517
3518
3519class V8EXPORT RawOperationDescriptor : public Data {
3520 public:
3521 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
3522 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
3523 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
3524 int16_t byte_offset);
3525 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
3526 void* compare_value);
3527 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
3528 Isolate* isolate,
3529 DeclaredAccessorDescriptorDataType data_type,
3530 uint8_t bool_offset = 0);
3531 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
3532 uint8_t bitmask,
3533 uint8_t compare_value);
3534 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
3535 Isolate* isolate,
3536 uint16_t bitmask,
3537 uint16_t compare_value);
3538 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
3539 Isolate* isolate,
3540 uint32_t bitmask,
3541 uint32_t compare_value);
3542
3543 private:
3544 RawOperationDescriptor();
3545};
3546
3547
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003548/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003549 * A utility for determining the type of objects based on the template
3550 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003552class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553 public:
3554 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
3555 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
3556 int match(Handle<Value> value);
3557 private:
3558 TypeSwitch();
3559};
3560
3561
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003562// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003563
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003564class V8EXPORT ExternalAsciiStringResourceImpl
3565 : public String::ExternalAsciiStringResource {
3566 public:
3567 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
3568 ExternalAsciiStringResourceImpl(const char* data, size_t length)
3569 : data_(data), length_(length) {}
3570 const char* data() const { return data_; }
3571 size_t length() const { return length_; }
3572
3573 private:
3574 const char* data_;
3575 size_t length_;
3576};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003577
3578/**
3579 * Ignore
3580 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003581class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003582 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003583 // Note that the strings passed into this constructor must live as long
3584 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003585 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003586 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003588 const char** deps = 0,
3589 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003590 virtual ~Extension() { }
3591 virtual v8::Handle<v8::FunctionTemplate>
3592 GetNativeFunction(v8::Handle<v8::String> name) {
3593 return v8::Handle<v8::FunctionTemplate>();
3594 }
3595
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003596 const char* name() const { return name_; }
3597 size_t source_length() const { return source_length_; }
3598 const String::ExternalAsciiStringResource* source() const {
3599 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003600 int dependency_count() { return dep_count_; }
3601 const char** dependencies() { return deps_; }
3602 void set_auto_enable(bool value) { auto_enable_ = value; }
3603 bool auto_enable() { return auto_enable_; }
3604
3605 private:
3606 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003607 size_t source_length_; // expected to initialize before source_
3608 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003609 int dep_count_;
3610 const char** deps_;
3611 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003612
3613 // Disallow copying and assigning.
3614 Extension(const Extension&);
3615 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003616};
3617
3618
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003619void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003620
3621
3622/**
3623 * Ignore
3624 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003625class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003626 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003627 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628 RegisterExtension(extension);
3629 }
3630};
3631
3632
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003633// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003634
3635
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003636Handle<Primitive> V8EXPORT Undefined();
3637Handle<Primitive> V8EXPORT Null();
3638Handle<Boolean> V8EXPORT True();
3639Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003640
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003641V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
3642V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
3643V8_INLINE(Handle<Boolean> True(Isolate* isolate));
3644V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003646
3647/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003648 * A set of constraints that specifies the limits of the runtime's memory use.
3649 * You must set the heap size before initializing the VM - the size cannot be
3650 * adjusted after the VM is initialized.
3651 *
3652 * If you are using threads then you should hold the V8::Locker lock while
3653 * setting the stack limit and you must set a non-default stack limit separately
3654 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003655 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003656class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003657 public:
3658 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003659 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003661 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003662 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003663 int max_executable_size() { return max_executable_size_; }
3664 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003665 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003666 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003667 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
3668 private:
3669 int max_young_space_size_;
3670 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003671 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003672 uint32_t* stack_limit_;
3673};
3674
3675
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003676bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003677
3678
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003679// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003680
3681
3682typedef void (*FatalErrorCallback)(const char* location, const char* message);
3683
3684
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003685typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003686
3687
3688/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003689 * Schedules an exception to be thrown when returning to JavaScript. When an
3690 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003691 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00003692 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003693 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003694Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695
3696/**
3697 * Create new error objects by calling the corresponding error object
3698 * constructor with the message.
3699 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003700class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003701 public:
3702 static Local<Value> RangeError(Handle<String> message);
3703 static Local<Value> ReferenceError(Handle<String> message);
3704 static Local<Value> SyntaxError(Handle<String> message);
3705 static Local<Value> TypeError(Handle<String> message);
3706 static Local<Value> Error(Handle<String> message);
3707};
3708
3709
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003710// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003712typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003713
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003714typedef void* (*CreateHistogramCallback)(const char* name,
3715 int min,
3716 int max,
3717 size_t buckets);
3718
3719typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
3720
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003721// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003722 enum ObjectSpace {
3723 kObjectSpaceNewSpace = 1 << 0,
3724 kObjectSpaceOldPointerSpace = 1 << 1,
3725 kObjectSpaceOldDataSpace = 1 << 2,
3726 kObjectSpaceCodeSpace = 1 << 3,
3727 kObjectSpaceMapSpace = 1 << 4,
3728 kObjectSpaceLoSpace = 1 << 5,
3729
3730 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
3731 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
3732 kObjectSpaceLoSpace
3733 };
3734
3735 enum AllocationAction {
3736 kAllocationActionAllocate = 1 << 0,
3737 kAllocationActionFree = 1 << 1,
3738 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
3739 };
3740
3741typedef void (*MemoryAllocationCallback)(ObjectSpace space,
3742 AllocationAction action,
3743 int size);
3744
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003745// --- Leave Script Callback ---
3746typedef void (*CallCompletedCallback)();
3747
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003748// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003749typedef void (*FailedAccessCheckCallback)(Local<Object> target,
3750 AccessType type,
3751 Local<Value> data);
3752
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003753// --- AllowCodeGenerationFromStrings callbacks ---
3754
3755/**
3756 * Callback to check if code generation from strings is allowed. See
3757 * Context::AllowCodeGenerationFromStrings.
3758 */
3759typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
3760
3761// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003762
3763/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003764 * Applications can register callback functions which will be called
3765 * before and after a garbage collection. Allocations are not
3766 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003767 * objects (set or delete properties for example) since it is possible
3768 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003769 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003770enum GCType {
3771 kGCTypeScavenge = 1 << 0,
3772 kGCTypeMarkSweepCompact = 1 << 1,
3773 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
3774};
3775
3776enum GCCallbackFlags {
3777 kNoGCCallbackFlags = 0,
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003778 kGCCallbackFlagCompacted = 1 << 0,
3779 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003780};
3781
3782typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
3783typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
3784
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003785typedef void (*GCCallback)();
3786
3787
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003788/**
ager@chromium.org3811b432009-10-28 14:53:37 +00003789 * Collection of V8 heap information.
3790 *
3791 * Instances of this class can be passed to v8::V8::HeapStatistics to
3792 * get heap statistics from V8.
3793 */
3794class V8EXPORT HeapStatistics {
3795 public:
3796 HeapStatistics();
3797 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003798 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003799 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003800 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003801 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003802
3803 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003804 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003805 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003806 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003807 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003808 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003809
3810 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003811 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003812};
3813
3814
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003815class RetainedObjectInfo;
3816
ager@chromium.org3811b432009-10-28 14:53:37 +00003817/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003818 * Isolate represents an isolated instance of the V8 engine. V8
3819 * isolates have completely separate states. Objects from one isolate
3820 * must not be used in other isolates. When V8 is initialized a
3821 * default isolate is implicitly created and entered. The embedder
3822 * can create additional isolates and use them in parallel in multiple
3823 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003824 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003825 */
3826class V8EXPORT Isolate {
3827 public:
3828 /**
3829 * Stack-allocated class which sets the isolate for all operations
3830 * executed within a local scope.
3831 */
3832 class V8EXPORT Scope {
3833 public:
3834 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3835 isolate->Enter();
3836 }
3837
3838 ~Scope() { isolate_->Exit(); }
3839
3840 private:
3841 Isolate* const isolate_;
3842
3843 // Prevent copying of Scope objects.
3844 Scope(const Scope&);
3845 Scope& operator=(const Scope&);
3846 };
3847
3848 /**
3849 * Creates a new isolate. Does not change the currently entered
3850 * isolate.
3851 *
3852 * When an isolate is no longer used its resources should be freed
3853 * by calling Dispose(). Using the delete operator is not allowed.
3854 */
3855 static Isolate* New();
3856
3857 /**
3858 * Returns the entered isolate for the current thread or NULL in
3859 * case there is no current isolate.
3860 */
3861 static Isolate* GetCurrent();
3862
3863 /**
3864 * Methods below this point require holding a lock (using Locker) in
3865 * a multi-threaded environment.
3866 */
3867
3868 /**
3869 * Sets this isolate as the entered one for the current thread.
3870 * Saves the previously entered one (if any), so that it can be
3871 * restored when exiting. Re-entering an isolate is allowed.
3872 */
3873 void Enter();
3874
3875 /**
3876 * Exits this isolate by restoring the previously entered one in the
3877 * current thread. The isolate may still stay the same, if it was
3878 * entered more than once.
3879 *
3880 * Requires: this == Isolate::GetCurrent().
3881 */
3882 void Exit();
3883
3884 /**
3885 * Disposes the isolate. The isolate must not be entered by any
3886 * thread to be disposable.
3887 */
3888 void Dispose();
3889
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003890 /**
3891 * Associate embedder-specific data with the isolate
3892 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003893 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003894
3895 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003896 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003897 * Returns NULL if SetData has never been called.
3898 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003899 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003900
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003901 /**
3902 * Get statistics about the heap memory usage.
3903 */
3904 void GetHeapStatistics(HeapStatistics* heap_statistics);
3905
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003906 /**
3907 * Adjusts the amount of registered external memory. Used to give V8 an
3908 * indication of the amount of externally allocated memory that is kept alive
3909 * by JavaScript objects. V8 uses this to decide when to perform global
3910 * garbage collections. Registering externally allocated memory will trigger
3911 * global garbage collections more often than it would otherwise in an attempt
3912 * to garbage collect the JavaScript objects that keep the externally
3913 * allocated memory alive.
3914 *
3915 * \param change_in_bytes the change in externally allocated memory that is
3916 * kept alive by JavaScript objects.
3917 * \returns the adjusted value.
3918 */
3919 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3920
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003921 /**
3922 * Returns heap profiler for this isolate. Will return NULL until the isolate
3923 * is initialized.
3924 */
3925 HeapProfiler* GetHeapProfiler();
3926
3927 /**
3928 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3929 * is initialized.
3930 */
3931 CpuProfiler* GetCpuProfiler();
3932
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003933 /** Returns the context that is on the top of the stack. */
3934 Local<Context> GetCurrentContext();
3935
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003936 /**
3937 * Allows the host application to group objects together. If one
3938 * object in the group is alive, all objects in the group are alive.
3939 * After each garbage collection, object groups are removed. It is
3940 * intended to be used in the before-garbage-collection callback
3941 * function, for instance to simulate DOM tree connections among JS
3942 * wrapper objects. Object groups for all dependent handles need to
3943 * be provided for kGCTypeMarkSweepCompact collections, for all other
3944 * garbage collection types it is sufficient to provide object groups
3945 * for partially dependent handles only.
3946 */
3947 void SetObjectGroupId(const Persistent<Value>& object,
3948 UniqueId id);
3949
3950 /**
3951 * Allows the host application to declare implicit references from an object
3952 * group to an object. If the objects of the object group are alive, the child
3953 * object is alive too. After each garbage collection, all implicit references
3954 * are removed. It is intended to be used in the before-garbage-collection
3955 * callback function.
3956 */
3957 void SetReferenceFromGroup(UniqueId id,
3958 const Persistent<Value>& child);
3959
3960 /**
3961 * Allows the host application to declare implicit references from an object
3962 * to another object. If the parent object is alive, the child object is alive
3963 * too. After each garbage collection, all implicit references are removed. It
3964 * is intended to be used in the before-garbage-collection callback function.
3965 */
3966 void SetReference(const Persistent<Object>& parent,
3967 const Persistent<Value>& child);
3968
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003969 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003970 Isolate();
3971 Isolate(const Isolate&);
3972 ~Isolate();
3973 Isolate& operator=(const Isolate&);
3974 void* operator new(size_t size);
3975 void operator delete(void*, size_t);
3976};
3977
3978
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003979class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003980 public:
3981 enum CompressionAlgorithm {
3982 kUncompressed,
3983 kBZip2
3984 };
3985
3986 const char* data;
3987 int compressed_size;
3988 int raw_size;
3989};
3990
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003991
3992/**
3993 * A helper class for driving V8 startup data decompression. It is based on
3994 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3995 * for an embedder to use this class, instead, API functions can be used
3996 * directly.
3997 *
3998 * For an example of the class usage, see the "shell.cc" sample application.
3999 */
4000class V8EXPORT StartupDataDecompressor { // NOLINT
4001 public:
4002 StartupDataDecompressor();
4003 virtual ~StartupDataDecompressor();
4004 int Decompress();
4005
4006 protected:
4007 virtual int DecompressData(char* raw_data,
4008 int* raw_data_size,
4009 const char* compressed_data,
4010 int compressed_data_size) = 0;
4011
4012 private:
4013 char** raw_data;
4014};
4015
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004016
4017/**
4018 * EntropySource is used as a callback function when v8 needs a source
4019 * of entropy.
4020 */
4021typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
4022
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004023
4024/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004025 * ReturnAddressLocationResolver is used as a callback function when v8 is
4026 * resolving the location of a return address on the stack. Profilers that
4027 * change the return address on the stack can use this to resolve the stack
4028 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004029 *
4030 * \param return_addr_location points to a location on stack where a machine
4031 * return address resides.
4032 * \returns either return_addr_location, or else a pointer to the profiler's
4033 * copy of the original return address.
4034 *
4035 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00004036 */
4037typedef uintptr_t (*ReturnAddressLocationResolver)(
4038 uintptr_t return_addr_location);
4039
4040
4041/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004042 * FunctionEntryHook is the type of the profile entry hook called at entry to
4043 * any generated function when function-level profiling is enabled.
4044 *
4045 * \param function the address of the function that's being entered.
4046 * \param return_addr_location points to a location on stack where the machine
4047 * return address resides. This can be used to identify the caller of
4048 * \p function, and/or modified to divert execution when \p function exits.
4049 *
4050 * \note the entry hook must not cause garbage collection.
4051 */
4052typedef void (*FunctionEntryHook)(uintptr_t function,
4053 uintptr_t return_addr_location);
4054
4055
4056/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004057 * A JIT code event is issued each time code is added, moved or removed.
4058 *
4059 * \note removal events are not currently issued.
4060 */
4061struct JitCodeEvent {
4062 enum EventType {
4063 CODE_ADDED,
4064 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004065 CODE_REMOVED,
4066 CODE_ADD_LINE_POS_INFO,
4067 CODE_START_LINE_INFO_RECORDING,
4068 CODE_END_LINE_INFO_RECORDING
4069 };
4070 // Definition of the code position type. The "POSITION" type means the place
4071 // in the source code which are of interest when making stack traces to
4072 // pin-point the source location of a stack frame as close as possible.
4073 // The "STATEMENT_POSITION" means the place at the beginning of each
4074 // statement, and is used to indicate possible break locations.
4075 enum PositionType {
4076 POSITION,
4077 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004078 };
4079
4080 // Type of event.
4081 EventType type;
4082 // Start of the instructions.
4083 void* code_start;
4084 // Size of the instructions.
4085 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004086 // Script info for CODE_ADDED event.
4087 Handle<Script> script;
4088 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
4089 // code line information which is returned from the
4090 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
4091 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
4092 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004093
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004094 struct name_t {
4095 // Name of the object associated with the code, note that the string is not
4096 // zero-terminated.
4097 const char* str;
4098 // Number of chars in str.
4099 size_t len;
4100 };
4101
4102 struct line_info_t {
4103 // PC offset
4104 size_t offset;
4105 // Code postion
4106 size_t pos;
4107 // The position type.
4108 PositionType position_type;
4109 };
4110
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004111 union {
4112 // Only valid for CODE_ADDED.
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004113 struct name_t name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004114
4115 // Only valid for CODE_ADD_LINE_POS_INFO
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004116 struct line_info_t line_info;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004117
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004118 // New location of instructions. Only valid for CODE_MOVED.
4119 void* new_code_start;
4120 };
4121};
4122
4123/**
4124 * Option flags passed to the SetJitCodeEventHandler function.
4125 */
4126enum JitCodeEventOptions {
4127 kJitCodeEventDefault = 0,
4128 // Generate callbacks for already existent code.
4129 kJitCodeEventEnumExisting = 1
4130};
4131
4132
4133/**
4134 * Callback function passed to SetJitCodeEventHandler.
4135 *
4136 * \param event code add, move or removal event.
4137 */
4138typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
4139
4140
4141/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004142 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004143 */
4144class V8EXPORT ExternalResourceVisitor { // NOLINT
4145 public:
4146 virtual ~ExternalResourceVisitor() {}
4147 virtual void VisitExternalString(Handle<String> string) {}
4148};
4149
4150
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004151/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004152 * Interface for iterating through all the persistent handles in the heap.
4153 */
4154class V8EXPORT PersistentHandleVisitor { // NOLINT
4155 public:
4156 virtual ~PersistentHandleVisitor() {}
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004157 virtual void VisitPersistentHandle(Persistent<Value>* value,
4158 uint16_t class_id) {}
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004159};
4160
4161
4162/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004163 * Asserts that no action is performed that could cause a handle's value
4164 * to be modified. Useful when otherwise unsafe handle operations need to
4165 * be performed.
4166 */
4167class V8EXPORT AssertNoGCScope {
4168#ifndef DEBUG
rossberg@chromium.org79e79022013-06-03 15:43:46 +00004169 // TODO(yangguo): remove isolate argument.
4170 V8_INLINE(AssertNoGCScope(Isolate* isolate)) { }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004171#else
4172 AssertNoGCScope(Isolate* isolate);
4173 ~AssertNoGCScope();
4174 private:
rossberg@chromium.org79e79022013-06-03 15:43:46 +00004175 void* disallow_heap_allocation_;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004176#endif
4177};
4178
4179
4180/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004181 * Container class for static utility functions.
4182 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004183class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004184 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00004185 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004186 static void SetFatalErrorHandler(FatalErrorCallback that);
4187
v8.team.kasperl727e9952008-09-02 14:56:44 +00004188 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004189 * Set the callback to invoke to check if code generation from
4190 * strings should be allowed.
4191 */
4192 static void SetAllowCodeGenerationFromStringsCallback(
4193 AllowCodeGenerationFromStringsCallback that);
4194
4195 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004196 * Ignore out-of-memory exceptions.
4197 *
4198 * V8 running out of memory is treated as a fatal error by default.
4199 * This means that the fatal error handler is called and that V8 is
4200 * terminated.
4201 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004202 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00004203 * out-of-memory situation as a fatal error. This way, the contexts
4204 * that did not cause the out of memory problem might be able to
4205 * continue execution.
4206 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004207 static void IgnoreOutOfMemoryException();
4208
v8.team.kasperl727e9952008-09-02 14:56:44 +00004209 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004210 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00004211 * fatal errors such as out-of-memory situations.
4212 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004213 static bool IsDead();
4214
4215 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004216 * The following 4 functions are to be used when V8 is built with
4217 * the 'compress_startup_data' flag enabled. In this case, the
4218 * embedder must decompress startup data prior to initializing V8.
4219 *
4220 * This is how interaction with V8 should look like:
4221 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
4222 * v8::StartupData* compressed_data =
4223 * new v8::StartupData[compressed_data_count];
4224 * v8::V8::GetCompressedStartupData(compressed_data);
4225 * ... decompress data (compressed_data can be updated in-place) ...
4226 * v8::V8::SetDecompressedStartupData(compressed_data);
4227 * ... now V8 can be initialized
4228 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004229 *
4230 * A helper class StartupDataDecompressor is provided. It implements
4231 * the protocol of the interaction described above, and can be used in
4232 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004233 */
4234 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
4235 static int GetCompressedStartupDataCount();
4236 static void GetCompressedStartupData(StartupData* compressed_data);
4237 static void SetDecompressedStartupData(StartupData* decompressed_data);
4238
4239 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004240 * Adds a message listener.
4241 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004242 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00004243 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004244 *
4245 * If data is specified, it will be passed to the callback when it is called.
4246 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004247 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004248 static bool AddMessageListener(MessageCallback that,
4249 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004250
4251 /**
4252 * Remove all message listeners from the specified callback function.
4253 */
4254 static void RemoveMessageListeners(MessageCallback that);
4255
4256 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004257 * Tells V8 to capture current stack trace when uncaught exception occurs
4258 * and report it to the message listeners. The option is off by default.
4259 */
4260 static void SetCaptureStackTraceForUncaughtExceptions(
4261 bool capture,
4262 int frame_limit = 10,
4263 StackTrace::StackTraceOptions options = StackTrace::kOverview);
4264
4265 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004266 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004267 */
4268 static void SetFlagsFromString(const char* str, int length);
4269
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004270 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004271 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004272 */
4273 static void SetFlagsFromCommandLine(int* argc,
4274 char** argv,
4275 bool remove_flags);
4276
kasper.lund7276f142008-07-30 08:49:36 +00004277 /** Get the version string. */
4278 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004279
4280 /**
4281 * Enables the host application to provide a mechanism for recording
4282 * statistics counters.
4283 */
4284 static void SetCounterFunction(CounterLookupCallback);
4285
4286 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004287 * Enables the host application to provide a mechanism for recording
4288 * histograms. The CreateHistogram function returns a
4289 * histogram which will later be passed to the AddHistogramSample
4290 * function.
4291 */
4292 static void SetCreateHistogramFunction(CreateHistogramCallback);
4293 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
4294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004295 /** Callback function for reporting failed access checks.*/
4296 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
4297
4298 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004299 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004300 * garbage collection. Allocations are not allowed in the
4301 * callback function, you therefore cannot manipulate objects (set
4302 * or delete properties for example) since it is possible such
4303 * operations will result in the allocation of objects. It is possible
4304 * to specify the GCType filter for your callback. But it is not possible to
4305 * register the same callback function two times with different
4306 * GCType filters.
4307 */
4308 static void AddGCPrologueCallback(
4309 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
4310
4311 /**
4312 * This function removes callback which was installed by
4313 * AddGCPrologueCallback function.
4314 */
4315 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
4316
4317 /**
4318 * The function is deprecated. Please use AddGCPrologueCallback instead.
4319 * Enables the host application to receive a notification before a
4320 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00004321 * callback function, you therefore cannot manipulate objects (set
4322 * or delete properties for example) since it is possible such
4323 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004324 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004325 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004326
4327 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004328 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004329 * garbage collection. Allocations are not allowed in the
4330 * callback function, you therefore cannot manipulate objects (set
4331 * or delete properties for example) since it is possible such
4332 * operations will result in the allocation of objects. It is possible
4333 * to specify the GCType filter for your callback. But it is not possible to
4334 * register the same callback function two times with different
4335 * GCType filters.
4336 */
4337 static void AddGCEpilogueCallback(
4338 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
4339
4340 /**
4341 * This function removes callback which was installed by
4342 * AddGCEpilogueCallback function.
4343 */
4344 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
4345
4346 /**
4347 * The function is deprecated. Please use AddGCEpilogueCallback instead.
4348 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00004349 * major garbage collection. Allocations are not allowed in the
4350 * callback function, you therefore cannot manipulate objects (set
4351 * or delete properties for example) since it is possible such
4352 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004353 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004354 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004355
4356 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004357 * Enables the host application to provide a mechanism to be notified
4358 * and perform custom logging when V8 Allocates Executable Memory.
4359 */
4360 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
4361 ObjectSpace space,
4362 AllocationAction action);
4363
4364 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004365 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004366 */
4367 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
4368
4369 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004370 * Adds a callback to notify the host application when a script finished
4371 * running. If a script re-enters the runtime during executing, the
4372 * CallCompletedCallback is only invoked when the outer-most script
4373 * execution ends. Executing scripts inside the callback do not trigger
4374 * further callbacks.
4375 */
4376 static void AddCallCompletedCallback(CallCompletedCallback callback);
4377
4378 /**
4379 * Removes callback that was installed by AddCallCompletedCallback.
4380 */
4381 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
4382
4383 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004384 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004385 * initialize from scratch. This function is called implicitly if
4386 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004387 */
4388 static bool Initialize();
4389
kasper.lund7276f142008-07-30 08:49:36 +00004390 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004391 * Allows the host application to provide a callback which can be used
4392 * as a source of entropy for random number generators.
4393 */
4394 static void SetEntropySource(EntropySource source);
4395
4396 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004397 * Allows the host application to provide a callback that allows v8 to
4398 * cooperate with a profiler that rewrites return addresses on stack.
4399 */
4400 static void SetReturnAddressLocationResolver(
4401 ReturnAddressLocationResolver return_address_resolver);
4402
4403 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004404 * Allows the host application to provide the address of a function that's
4405 * invoked on entry to every V8-generated function.
4406 * Note that \p entry_hook is invoked at the very start of each
4407 * generated function.
4408 *
4409 * \param entry_hook a function that will be invoked on entry to every
4410 * V8-generated function.
4411 * \returns true on success on supported platforms, false on failure.
4412 * \note Setting a new entry hook function when one is already active will
4413 * fail.
4414 */
4415 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
4416
4417 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004418 * Allows the host application to provide the address of a function that is
4419 * notified each time code is added, moved or removed.
4420 *
4421 * \param options options for the JIT code event handler.
4422 * \param event_handler the JIT code event handler, which will be invoked
4423 * each time code is added, moved or removed.
4424 * \note \p event_handler won't get notified of existent code.
4425 * \note since code removal notifications are not currently issued, the
4426 * \p event_handler may get notifications of code that overlaps earlier
4427 * code notifications. This happens when code areas are reused, and the
4428 * earlier overlapping code areas should therefore be discarded.
4429 * \note the events passed to \p event_handler and the strings they point to
4430 * are not guaranteed to live past each call. The \p event_handler must
4431 * copy strings and other parameters it needs to keep around.
4432 * \note the set of events declared in JitCodeEvent::EventType is expected to
4433 * grow over time, and the JitCodeEvent structure is expected to accrue
4434 * new members. The \p event_handler function must ignore event codes
4435 * it does not recognize to maintain future compatibility.
4436 */
4437 static void SetJitCodeEventHandler(JitCodeEventOptions options,
4438 JitCodeEventHandler event_handler);
4439
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004440 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
4441 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004442 static intptr_t AdjustAmountOfExternalAllocatedMemory(
4443 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00004444
iposva@chromium.org245aa852009-02-10 00:49:54 +00004445 /**
4446 * Suspends recording of tick samples in the profiler.
4447 * When the V8 profiling mode is enabled (usually via command line
4448 * switches) this function suspends recording of tick samples.
4449 * Profiling ticks are discarded until ResumeProfiler() is called.
4450 *
4451 * See also the --prof and --prof_auto command line switches to
4452 * enable V8 profiling.
4453 */
4454 static void PauseProfiler();
4455
4456 /**
4457 * Resumes recording of tick samples in the profiler.
4458 * See also PauseProfiler().
4459 */
4460 static void ResumeProfiler();
4461
ager@chromium.org41826e72009-03-30 13:30:57 +00004462 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004463 * Return whether profiler is currently paused.
4464 */
4465 static bool IsProfilerPaused();
4466
4467 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004468 * Retrieve the V8 thread id of the calling thread.
4469 *
4470 * The thread id for a thread should only be retrieved after the V8
4471 * lock has been acquired with a Locker object with that thread.
4472 */
4473 static int GetCurrentThreadId();
4474
4475 /**
4476 * Forcefully terminate execution of a JavaScript thread. This can
4477 * be used to terminate long-running scripts.
4478 *
4479 * TerminateExecution should only be called when then V8 lock has
4480 * been acquired with a Locker object. Therefore, in order to be
4481 * able to terminate long-running threads, preemption must be
4482 * enabled to allow the user of TerminateExecution to acquire the
4483 * lock.
4484 *
4485 * The termination is achieved by throwing an exception that is
4486 * uncatchable by JavaScript exception handlers. Termination
4487 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004488 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004489 * exception handler that catches an exception should check if that
4490 * exception is a termination exception and immediately return if
4491 * that is the case. Returning immediately in that case will
4492 * continue the propagation of the termination exception if needed.
4493 *
4494 * The thread id passed to TerminateExecution must have been
4495 * obtained by calling GetCurrentThreadId on the thread in question.
4496 *
4497 * \param thread_id The thread id of the thread to terminate.
4498 */
4499 static void TerminateExecution(int thread_id);
4500
4501 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004502 * Forcefully terminate the current thread of JavaScript execution
4503 * in the given isolate. If no isolate is provided, the default
4504 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004505 *
4506 * This method can be used by any thread even if that thread has not
4507 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004508 *
4509 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004510 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004511 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00004512
4513 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004514 * Is V8 terminating JavaScript execution.
4515 *
4516 * Returns true if JavaScript execution is currently terminating
4517 * because of a call to TerminateExecution. In that case there are
4518 * still JavaScript frames on the stack and the termination
4519 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004520 *
4521 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004522 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004523 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004524
4525 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004526 * Resume execution capability in the given isolate, whose execution
4527 * was previously forcefully terminated using TerminateExecution().
4528 *
4529 * When execution is forcefully terminated using TerminateExecution(),
4530 * the isolate can not resume execution until all JavaScript frames
4531 * have propagated the uncatchable exception which is generated. This
4532 * method allows the program embedding the engine to handle the
4533 * termination event and resume execution capability, even if
4534 * JavaScript frames remain on the stack.
4535 *
4536 * This method can be used by any thread even if that thread has not
4537 * acquired the V8 lock with a Locker object.
4538 *
4539 * \param isolate The isolate in which to resume execution capability.
4540 */
4541 static void CancelTerminateExecution(Isolate* isolate);
4542
4543 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00004544 * Releases any resources used by v8 and stops any utility threads
4545 * that may be running. Note that disposing v8 is permanent, it
4546 * cannot be reinitialized.
4547 *
4548 * It should generally not be necessary to dispose v8 before exiting
4549 * a process, this should happen automatically. It is only necessary
4550 * to use if the process needs the resources taken up by v8.
4551 */
4552 static bool Dispose();
4553
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00004554 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
4555 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004556
4557 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004558 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004559 * heap. GC is not invoked prior to iterating, therefore there is no
4560 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004561 */
4562 static void VisitExternalResources(ExternalResourceVisitor* visitor);
4563
4564 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004565 * Iterates through all the persistent handles in the current isolate's heap
4566 * that have class_ids.
4567 */
4568 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
4569
4570 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004571 * Iterates through all the persistent handles in the current isolate's heap
4572 * that have class_ids and are candidates to be marked as partially dependent
4573 * handles. This will visit handles to young objects created since the last
4574 * garbage collection but is free to visit an arbitrary superset of these
4575 * objects.
4576 */
4577 static void VisitHandlesForPartialDependence(
4578 Isolate* isolate, PersistentHandleVisitor* visitor);
4579
4580 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004581 * Optional notification that the embedder is idle.
4582 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004583 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004584 * Returns true if the embedder should stop calling IdleNotification
4585 * until real work has been done. This indicates that V8 has done
4586 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004587 *
4588 * The hint argument specifies the amount of work to be done in the function
4589 * on scale from 1 to 1000. There is no guarantee that the actual work will
4590 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004591 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004592 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004593
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004594 /**
4595 * Optional notification that the system is running low on memory.
4596 * V8 uses these notifications to attempt to free memory.
4597 */
4598 static void LowMemoryNotification();
4599
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004600 /**
4601 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004602 * these notifications to guide the GC heuristic. Returns the number
4603 * of context disposals - including this one - since the last time
4604 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004605 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004606 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004607
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004608 private:
4609 V8();
4610
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004611 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
4612 internal::Object** handle);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004613 static void DisposeGlobal(internal::Object** global_handle);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004614 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004615 static void MakeWeak(internal::Object** global_handle,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004616 void* data,
rossberg@chromium.org79e79022013-06-03 15:43:46 +00004617 RevivableCallback weak_reference_callback);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004618 static void ClearWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004619
4620 template <class T> friend class Handle;
4621 template <class T> friend class Local;
4622 template <class T> friend class Persistent;
4623 friend class Context;
4624};
4625
4626
4627/**
4628 * An external exception handler.
4629 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004630class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004631 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004632 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004633 * Creates a new try/catch block and registers it with v8. Note that
4634 * all TryCatch blocks should be stack allocated because the memory
4635 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004636 */
4637 TryCatch();
4638
4639 /**
4640 * Unregisters and deletes this try/catch block.
4641 */
4642 ~TryCatch();
4643
4644 /**
4645 * Returns true if an exception has been caught by this try/catch block.
4646 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004647 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004648
4649 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004650 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004651 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004652 * If CanContinue returns false, the correct action is to perform any C++
4653 * cleanup needed and then return. If CanContinue returns false and
4654 * HasTerminated returns true, it is possible to call
4655 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004656 */
4657 bool CanContinue() const;
4658
4659 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004660 * Returns true if an exception has been caught due to script execution
4661 * being terminated.
4662 *
4663 * There is no JavaScript representation of an execution termination
4664 * exception. Such exceptions are thrown when the TerminateExecution
4665 * methods are called to terminate a long-running script.
4666 *
4667 * If such an exception has been thrown, HasTerminated will return true,
4668 * indicating that it is possible to call CancelTerminateExecution in order
4669 * to continue calling into the engine.
4670 */
4671 bool HasTerminated() const;
4672
4673 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004674 * Throws the exception caught by this TryCatch in a way that avoids
4675 * it being caught again by this same TryCatch. As with ThrowException
4676 * it is illegal to execute any JavaScript operations after calling
4677 * ReThrow; the caller must return immediately to where the exception
4678 * is caught.
4679 */
4680 Handle<Value> ReThrow();
4681
4682 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004683 * Returns the exception caught by this try/catch block. If no exception has
4684 * been caught an empty handle is returned.
4685 *
4686 * The returned handle is valid until this TryCatch block has been destroyed.
4687 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004688 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004689
4690 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004691 * Returns the .stack property of the thrown object. If no .stack
4692 * property is present an empty handle is returned.
4693 */
4694 Local<Value> StackTrace() const;
4695
4696 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004697 * Returns the message associated with this exception. If there is
4698 * no message associated an empty handle is returned.
4699 *
4700 * The returned handle is valid until this TryCatch block has been
4701 * destroyed.
4702 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004703 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004704
4705 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004706 * Clears any exceptions that may have been caught by this try/catch block.
4707 * After this method has been called, HasCaught() will return false.
4708 *
4709 * It is not necessary to clear a try/catch block before using it again; if
4710 * another exception is thrown the previously caught exception will just be
4711 * overwritten. However, it is often a good idea since it makes it easier
4712 * to determine which operation threw a given exception.
4713 */
4714 void Reset();
4715
v8.team.kasperl727e9952008-09-02 14:56:44 +00004716 /**
4717 * Set verbosity of the external exception handler.
4718 *
4719 * By default, exceptions that are caught by an external exception
4720 * handler are not reported. Call SetVerbose with true on an
4721 * external exception handler to have exceptions caught by the
4722 * handler reported as if they were not caught.
4723 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004724 void SetVerbose(bool value);
4725
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004726 /**
4727 * Set whether or not this TryCatch should capture a Message object
4728 * which holds source information about where the exception
4729 * occurred. True by default.
4730 */
4731 void SetCaptureMessage(bool value);
4732
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004733 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004734 // Make it hard to create heap-allocated TryCatch blocks.
4735 TryCatch(const TryCatch&);
4736 void operator=(const TryCatch&);
4737 void* operator new(size_t size);
4738 void operator delete(void*, size_t);
4739
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004740 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004741 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004742 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004743 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004744 bool is_verbose_ : 1;
4745 bool can_continue_ : 1;
4746 bool capture_message_ : 1;
4747 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004748 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004749
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004750 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004751};
4752
4753
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004754// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004755
4756
4757/**
4758 * Ignore
4759 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004760class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004761 public:
4762 ExtensionConfiguration(int name_count, const char* names[])
4763 : name_count_(name_count), names_(names) { }
4764 private:
4765 friend class ImplementationUtilities;
4766 int name_count_;
4767 const char** names_;
4768};
4769
4770
4771/**
4772 * A sandboxed execution context with its own set of built-in objects
4773 * and functions.
4774 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004775class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004776 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004777 /**
4778 * Returns the global proxy object or global object itself for
4779 * detached contexts.
4780 *
4781 * Global proxy object is a thin wrapper whose prototype points to
4782 * actual context's global object with the properties like Object, etc.
4783 * This is done that way for security reasons (for more details see
4784 * https://wiki.mozilla.org/Gecko:SplitWindow).
4785 *
4786 * Please note that changes to global proxy object prototype most probably
4787 * would break VM---v8 expects only global object as a prototype of
4788 * global proxy object.
4789 *
4790 * If DetachGlobal() has been invoked, Global() would return actual global
4791 * object until global is reattached with ReattachGlobal().
4792 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004793 Local<Object> Global();
4794
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004795 /**
4796 * Detaches the global object from its context before
4797 * the global object can be reused to create a new context.
4798 */
4799 void DetachGlobal();
4800
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00004801 /**
4802 * Reattaches a global object to a context. This can be used to
4803 * restore the connection between a global object and a context
4804 * after DetachGlobal has been called.
4805 *
4806 * \param global_object The global object to reattach to the
4807 * context. For this to work, the global object must be the global
4808 * object that was associated with this context before a call to
4809 * DetachGlobal.
4810 */
4811 void ReattachGlobal(Handle<Object> global_object);
4812
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004813 /**
4814 * Creates a new context and returns a handle to the newly allocated
4815 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004816 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004817 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004818 *
4819 * \param extensions An optional extension configuration containing
4820 * the extensions to be installed in the newly created context.
4821 *
4822 * \param global_template An optional object template from which the
4823 * global object for the newly created context will be created.
4824 *
4825 * \param global_object An optional global object to be reused for
4826 * the newly created context. This global object must have been
4827 * created by a previous call to Context::New with the same global
4828 * template. The state of the global object will be completely reset
4829 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004830 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004831 static Local<Context> New(
4832 Isolate* isolate,
4833 ExtensionConfiguration* extensions = NULL,
4834 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4835 Handle<Value> global_object = Handle<Value>());
4836
4837 /** Deprecated. Use Isolate version instead. */
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004838 V8_DEPRECATED(static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004839 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004840 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004841 Handle<Value> global_object = Handle<Value>()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004842
kasper.lund44510672008-07-25 07:37:58 +00004843 /** Returns the last entered context. */
4844 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004845
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004846 // TODO(svenpanne) Actually deprecate this.
4847 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004848 static Local<Context> GetCurrent();
4849
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004850 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004851 * Returns the context of the calling JavaScript code. That is the
4852 * context of the top-most JavaScript frame. If there are no
4853 * JavaScript frames an empty handle is returned.
4854 */
4855 static Local<Context> GetCalling();
4856
4857 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004858 * Sets the security token for the context. To access an object in
4859 * another context, the security tokens must match.
4860 */
4861 void SetSecurityToken(Handle<Value> token);
4862
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004863 /** Restores the security token to the default value. */
4864 void UseDefaultSecurityToken();
4865
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004866 /** Returns the security token of this context.*/
4867 Handle<Value> GetSecurityToken();
4868
v8.team.kasperl727e9952008-09-02 14:56:44 +00004869 /**
4870 * Enter this context. After entering a context, all code compiled
4871 * and run is compiled and run in this context. If another context
4872 * is already entered, this old context is saved so it can be
4873 * restored when the new context is exited.
4874 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004875 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004876
4877 /**
4878 * Exit this context. Exiting the current context restores the
4879 * context that was in place when entering the current context.
4880 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004881 void Exit();
4882
v8.team.kasperl727e9952008-09-02 14:56:44 +00004883 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004884 bool HasOutOfMemoryException();
4885
v8.team.kasperl727e9952008-09-02 14:56:44 +00004886 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004887 static bool InContext();
4888
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004889 /** Returns an isolate associated with a current context. */
4890 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004891
4892 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004893 * Gets the embedder data with the given index, which must have been set by a
4894 * previous call to SetEmbedderData with the same index. Note that index 0
4895 * currently has a special meaning for Chrome's debugger.
4896 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004897 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004898
4899 /**
4900 * Sets the embedder data with the given index, growing the data as
4901 * needed. Note that index 0 currently has a special meaning for Chrome's
4902 * debugger.
4903 */
4904 void SetEmbedderData(int index, Handle<Value> value);
4905
4906 /**
4907 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4908 * index, which must have bees set by a previous call to
4909 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4910 * currently has a special meaning for Chrome's debugger.
4911 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004912 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004913
4914 /**
4915 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4916 * index, growing the data as needed. Note that index 0 currently has a
4917 * special meaning for Chrome's debugger.
4918 */
4919 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004920
4921 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004922 * Control whether code generation from strings is allowed. Calling
4923 * this method with false will disable 'eval' and the 'Function'
4924 * constructor for code running in this context. If 'eval' or the
4925 * 'Function' constructor are used an exception will be thrown.
4926 *
4927 * If code generation from strings is not allowed the
4928 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4929 * set before blocking the call to 'eval' or the 'Function'
4930 * constructor. If that callback returns true, the call will be
4931 * allowed, otherwise an exception will be thrown. If no callback is
4932 * set an exception will be thrown.
4933 */
4934 void AllowCodeGenerationFromStrings(bool allow);
4935
4936 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004937 * Returns true if code generation from strings is allowed for the context.
4938 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4939 */
4940 bool IsCodeGenerationFromStringsAllowed();
4941
4942 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004943 * Sets the error description for the exception that is thrown when
4944 * code generation from strings is not allowed and 'eval' or the 'Function'
4945 * constructor are called.
4946 */
4947 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4948
4949 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004950 * Stack-allocated class which sets the execution context for all
4951 * operations executed within a local scope.
4952 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004953 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004954 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004955 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004956 context_->Enter();
4957 }
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00004958 // TODO(dcarney): deprecate
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004959 V8_INLINE(Scope(Isolate* isolate, Persistent<Context>& context)) // NOLINT
4960#ifndef V8_USE_UNSAFE_HANDLES
4961 : context_(Handle<Context>::New(isolate, context)) {
4962#else
4963 : context_(Local<Context>::New(isolate, context)) {
4964#endif
4965 context_->Enter();
4966 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004967 V8_INLINE(~Scope()) { context_->Exit(); }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004968
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004969 private:
4970 Handle<Context> context_;
4971 };
4972
4973 private:
4974 friend class Value;
4975 friend class Script;
4976 friend class Object;
4977 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004978
4979 Local<Value> SlowGetEmbedderData(int index);
4980 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004981};
4982
4983
4984/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004985 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4986 * to use any given V8 isolate, see the comments in the Isolate class. The
4987 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4988 * object pointers obtained from V8 handles while in the particular V8 isolate.
4989 * It is up to the user of V8 to ensure, perhaps with locking, that this
4990 * constraint is not violated. In addition to any other synchronization
4991 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4992 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004993 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004994 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4995 * construction and destruction, the current thread is allowed to use the locked
4996 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4997 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004998 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004999 * Sample usage:
5000* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005001 * ...
5002 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005003 * v8::Locker locker(isolate);
5004 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005005 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00005006 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005007 * ...
5008 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00005009 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005010 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005011 * If you wish to stop using V8 in a thread A you can do this either by
5012 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
5013 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005014 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00005015 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005016 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005017 * isolate->Exit();
5018 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005019 * ...
5020 * // Code not using V8 goes here while V8 can run in another thread.
5021 * ...
5022 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005023 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005024 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005025 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005026 * The Unlocker object is intended for use in a long-running callback from V8,
5027 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005028 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005029 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
5030 * given thread. This can be useful if you have code that can be called either
5031 * from code that holds the lock or from code that does not. The Unlocker is
5032 * not recursive so you can not have several Unlockers on the stack at once, and
5033 * 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 +00005034 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005035 * An unlocker will unlock several lockers if it has to and reinstate the
5036 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005037 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00005038 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005039 * // V8 not locked.
5040 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005041 * v8::Locker locker(isolate);
5042 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005043 * // V8 locked.
5044 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005045 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005046 * // V8 still locked (2 levels).
5047 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005048 * isolate->Exit();
5049 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005050 * // V8 not locked.
5051 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00005052 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005053 * // V8 locked again (2 levels).
5054 * }
5055 * // V8 still locked (1 level).
5056 * }
5057 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00005058 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005059 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005060class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005061 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005062 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005063 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005064 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005065 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
5066
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005067 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005068 V8_DEPRECATED(Unlocker());
5069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005070 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00005071 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005072 void Initialize(Isolate* isolate);
5073
lrn@chromium.org1c092762011-05-09 09:42:16 +00005074 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005075};
5076
5077
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005078class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005079 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005080 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005081 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005082 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005083 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
5084
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005085 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005086 V8_DEPRECATED(Locker());
5087
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005088 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005089
5090 /**
5091 * Start preemption.
5092 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005093 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00005094 * that will switch between multiple threads that are in contention
5095 * for the V8 lock.
5096 */
5097 static void StartPreemption(int every_n_ms);
5098
5099 /**
5100 * Stop preemption.
5101 */
5102 static void StopPreemption();
5103
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005104 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005105 * Returns whether or not the locker for a given isolate, is locked by the
5106 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005107 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005108 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005109
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005110 /**
5111 * Returns whether v8::Locker is being used by this V8 instance.
5112 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00005113 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005114
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005115 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005116 void Initialize(Isolate* isolate);
5117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005118 bool has_lock_;
5119 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005120 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005121
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005122 static bool active_;
5123
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005124 // Disallow copying and assigning.
5125 Locker(const Locker&);
5126 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005127};
5128
5129
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005130/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005131 * A struct for exporting HeapStats data from V8, using "push" model.
5132 */
5133struct HeapStatsUpdate;
5134
5135
5136/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005137 * An interface for exporting data from V8, using "push" model.
5138 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005139class V8EXPORT OutputStream { // NOLINT
5140 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005141 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005142 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005143 };
5144 enum WriteResult {
5145 kContinue = 0,
5146 kAbort = 1
5147 };
5148 virtual ~OutputStream() {}
5149 /** Notify about the end of stream. */
5150 virtual void EndOfStream() = 0;
5151 /** Get preferred output chunk size. Called only once. */
5152 virtual int GetChunkSize() { return 1024; }
5153 /** Get preferred output encoding. Called only once. */
5154 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
5155 /**
5156 * Writes the next chunk of snapshot data into the stream. Writing
5157 * can be stopped by returning kAbort as function result. EndOfStream
5158 * will not be called in case writing was aborted.
5159 */
5160 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005161 /**
5162 * Writes the next chunk of heap stats data into the stream. Writing
5163 * can be stopped by returning kAbort as function result. EndOfStream
5164 * will not be called in case writing was aborted.
5165 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005166 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00005167 return kAbort;
5168 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005169};
5170
5171
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005172/**
5173 * An interface for reporting progress and controlling long-running
5174 * activities.
5175 */
5176class V8EXPORT ActivityControl { // NOLINT
5177 public:
5178 enum ControlOption {
5179 kContinue = 0,
5180 kAbort = 1
5181 };
5182 virtual ~ActivityControl() {}
5183 /**
5184 * Notify about current progress. The activity can be stopped by
5185 * returning kAbort as the callback result.
5186 */
5187 virtual ControlOption ReportProgressValue(int done, int total) = 0;
5188};
5189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005190
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00005191// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005192
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005193
5194namespace internal {
5195
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00005196const int kApiPointerSize = sizeof(void*); // NOLINT
5197const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005198
5199// Tag information for HeapObject.
5200const int kHeapObjectTag = 1;
5201const int kHeapObjectTagSize = 2;
5202const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
5203
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005204// Tag information for Smi.
5205const int kSmiTag = 0;
5206const int kSmiTagSize = 1;
5207const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
5208
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005209template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005210
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005211template<int kSmiShiftSize>
5212V8_INLINE(internal::Object* IntToSmi(int value)) {
5213 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
5214 intptr_t tagged_value =
5215 (static_cast<intptr_t>(value) << smi_shift_bits) | kSmiTag;
5216 return reinterpret_cast<internal::Object*>(tagged_value);
5217}
5218
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005219// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005220template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005221 static const int kSmiShiftSize = 0;
5222 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005223 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005224 int shift_bits = kSmiTagSize + kSmiShiftSize;
5225 // Throw away top 32 bits and shift down (requires >> to be sign extending).
5226 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
5227 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005228 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5229 return internal::IntToSmi<kSmiShiftSize>(value);
5230 }
5231 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5232 // To be representable as an tagged small integer, the two
5233 // most-significant bits of 'value' must be either 00 or 11 due to
5234 // sign-extension. To check this we add 01 to the two
5235 // most-significant bits, and check if the most-significant bit is 0
5236 //
5237 // CAUTION: The original code below:
5238 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
5239 // may lead to incorrect results according to the C language spec, and
5240 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
5241 // compiler may produce undefined results in case of signed integer
5242 // overflow. The computation must be done w/ unsigned ints.
5243 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
5244 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005245};
5246
5247// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005248template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005249 static const int kSmiShiftSize = 31;
5250 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005251 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005252 int shift_bits = kSmiTagSize + kSmiShiftSize;
5253 // Shift down and throw away top 32 bits.
5254 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
5255 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005256 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5257 return internal::IntToSmi<kSmiShiftSize>(value);
5258 }
5259 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5260 // To be representable as a long smi, the value must be a 32-bit integer.
5261 return (value == static_cast<int32_t>(value));
5262 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005263};
5264
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005265typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
5266const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
5267const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005268
5269/**
5270 * This class exports constants and functionality from within v8 that
5271 * is necessary to implement inline functions in the v8 api. Don't
5272 * depend on functions and constants defined here.
5273 */
5274class Internals {
5275 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005276 // These values match non-compiler-dependent values defined within
5277 // the implementation of v8.
5278 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005279 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005280 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005281
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005282 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00005283 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005284 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005285 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
5286 static const int kContextHeaderSize = 2 * kApiPointerSize;
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005287 static const int kContextEmbedderDataIndex = 64;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005288 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005289 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00005290 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005291 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005292
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005293 static const int kIsolateStateOffset = 0;
5294 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
5295 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
5296 static const int kUndefinedValueRootIndex = 5;
5297 static const int kNullValueRootIndex = 7;
5298 static const int kTrueValueRootIndex = 8;
5299 static const int kFalseValueRootIndex = 9;
danno@chromium.org1fd77d52013-06-07 16:01:45 +00005300 static const int kEmptyStringRootIndex = 130;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005301
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005302 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
5303 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
5304 static const int kNodeStateMask = 0xf;
5305 static const int kNodeStateIsWeakValue = 2;
5306 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005307 static const int kNodeIsIndependentShift = 4;
5308 static const int kNodeIsPartiallyDependentShift = 5;
5309
danno@chromium.org1fd77d52013-06-07 16:01:45 +00005310 static const int kJSObjectType = 0xaf;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005311 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005312 static const int kOddballType = 0x83;
danno@chromium.org1fd77d52013-06-07 16:01:45 +00005313 static const int kForeignType = 0x87;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005314
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005315 static const int kUndefinedOddballKind = 5;
5316 static const int kNullOddballKind = 3;
5317
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005318 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005319 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
5320 kHeapObjectTag);
5321 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005322
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005323 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005324 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005325 }
5326
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005327 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5328 return PlatformSmiTagging::IntToSmi(value);
5329 }
5330
5331 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5332 return PlatformSmiTagging::IsValidSmi(value);
5333 }
5334
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005335 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005336 typedef internal::Object O;
5337 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
5338 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
5339 }
5340
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005341 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005342 typedef internal::Object O;
5343 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
5344 }
5345
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005346 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005347 int representation = (instance_type & kFullStringRepresentationMask);
5348 return representation == kExternalTwoByteRepresentationTag;
5349 }
5350
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005351 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005352 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
5353 return *reinterpret_cast<int*>(addr) == 1;
5354 }
5355
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005356 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
5357 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5358 return *addr & (1 << shift);
5359 }
5360
5361 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
5362 bool value, int shift)) {
5363 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5364 uint8_t mask = 1 << shift;
5365 *addr = (*addr & ~mask) | (value << shift);
5366 }
5367
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005368 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
5369 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5370 return *addr & kNodeStateMask;
5371 }
5372
5373 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
5374 uint8_t value)) {
5375 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5376 *addr = (*addr & ~kNodeStateMask) | value;
5377 }
5378
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005379 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005380 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5381 kIsolateEmbedderDataOffset;
5382 *reinterpret_cast<void**>(addr) = data;
5383 }
5384
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005385 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005386 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5387 kIsolateEmbedderDataOffset;
5388 return *reinterpret_cast<void**>(addr);
5389 }
5390
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005391 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
5392 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005393 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
5394 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
5395 }
5396
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005397 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005398 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005399 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
5400 return *reinterpret_cast<T*>(addr);
5401 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005402
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005403 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005404 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005405 typedef internal::Object O;
5406 typedef internal::Internals I;
5407 O* ctx = *reinterpret_cast<O**>(context);
5408 int embedder_data_offset = I::kContextHeaderSize +
5409 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
5410 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
5411 int value_offset =
5412 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
5413 return I::ReadField<T>(embedder_data, value_offset);
5414 }
5415
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005416 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
5417 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
5418 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
5419 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
5420 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
5421 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
5422 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005423};
5424
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005425} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005426
5427
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005428template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005429Local<T>::Local() : Handle<T>() { }
5430
5431
5432template <class T>
5433Local<T> Local<T>::New(Handle<T> that) {
5434 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005435 T* that_ptr = *that;
5436 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5437 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
5438 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5439 reinterpret_cast<internal::HeapObject*>(*p))));
5440 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005441 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
5442}
5443
5444
5445template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005446Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005447 return New(isolate, that.val_);
5448}
5449
5450#ifndef V8_USE_UNSAFE_HANDLES
5451template <class T>
5452Local<T> Local<T>::New(Isolate* isolate, const Persistent<T>& that) {
5453 return New(isolate, that.val_);
5454}
5455
5456template <class T>
5457Handle<T> Handle<T>::New(Isolate* isolate, T* that) {
5458 if (that == NULL) return Handle<T>();
5459 T* that_ptr = that;
5460 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5461 return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5462 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5463}
5464#endif
5465
5466
5467template <class T>
5468Local<T> Local<T>::New(Isolate* isolate, T* that) {
5469 if (that == NULL) return Local<T>();
5470 T* that_ptr = that;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005471 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5472 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5473 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5474}
5475
5476
5477template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005478Persistent<T> Persistent<T>::New(Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005479 return New(Isolate::GetCurrent(), that.val_);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005480}
5481
5482
5483template <class T>
5484Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005485 return New(Isolate::GetCurrent(), that.val_);
5486}
5487
5488#ifndef V8_USE_UNSAFE_HANDLES
5489template <class T>
5490Persistent<T> Persistent<T>::New(Isolate* isolate, Persistent<T> that) {
5491 return New(Isolate::GetCurrent(), that.val_);
5492}
5493#endif
5494
5495template <class T>
5496Persistent<T> Persistent<T>::New(Isolate* isolate, T* that) {
5497 if (that == NULL) return Persistent<T>();
5498 internal::Object** p = reinterpret_cast<internal::Object**>(that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005499 return Persistent<T>(reinterpret_cast<T*>(
5500 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
5501 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005502}
5503
5504
5505template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005506bool Persistent<T>::IsIndependent() const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005507 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005508 if (this->IsEmpty()) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005509 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005510 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005511}
5512
5513
5514template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005515bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005516 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005517 if (this->IsEmpty()) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005518 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005519 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005520}
5521
5522
5523template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005524bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005525 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005526 if (this->IsEmpty()) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005527 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005528 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005529}
5530
5531
5532template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005533void Persistent<T>::Dispose() {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005534 if (this->IsEmpty()) return;
5535 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
5536#ifndef V8_USE_UNSAFE_HANDLES
5537 val_ = 0;
5538#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005539}
5540
5541
5542template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005543template <typename S, typename P>
5544void Persistent<T>::MakeWeak(
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005545 P* parameters,
5546 typename WeakReferenceCallbacks<S, P>::Revivable callback) {
5547 TYPE_CHECK(S, T);
5548 typedef typename WeakReferenceCallbacks<Value, void>::Revivable Revivable;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005549 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005550 parameters,
rossberg@chromium.org79e79022013-06-03 15:43:46 +00005551 reinterpret_cast<Revivable>(callback));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005552}
5553
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005554
5555template <class T>
5556template <typename P>
5557void Persistent<T>::MakeWeak(
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005558 P* parameters,
5559 typename WeakReferenceCallbacks<T, P>::Revivable callback) {
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005560 MakeWeak<T, P>(parameters, callback);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005561}
5562
5563
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005564template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005565void Persistent<T>::ClearWeak() {
5566 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005567}
5568
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005569
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005570template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005571void Persistent<T>::MarkIndependent() {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005572 typedef internal::Internals I;
5573 if (this->IsEmpty()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005574 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005575 true,
5576 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005577}
5578
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005579
5580template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005581void Persistent<T>::MarkPartiallyDependent() {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005582 typedef internal::Internals I;
5583 if (this->IsEmpty()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005584 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005585 true,
5586 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005587}
5588
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005589
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005590template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005591void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
5592 Dispose(isolate);
5593#ifdef V8_USE_UNSAFE_HANDLES
5594 *this = *New(isolate, other);
5595#else
5596 if (other.IsEmpty()) {
5597 this->val_ = NULL;
5598 return;
5599 }
5600 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5601 this->val_ = reinterpret_cast<T*>(
5602 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5603#endif
5604}
5605
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005606
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005607#ifndef V8_USE_UNSAFE_HANDLES
5608template <class T>
5609void Persistent<T>::Reset(Isolate* isolate, const Persistent<T>& other) {
5610 Dispose(isolate);
5611 if (other.IsEmpty()) {
5612 this->val_ = NULL;
5613 return;
5614 }
5615 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5616 this->val_ = reinterpret_cast<T*>(
5617 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5618}
5619#endif
5620
5621
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005622template <class T>
5623T* Persistent<T>::ClearAndLeak() {
5624 T* old;
5625#ifdef V8_USE_UNSAFE_HANDLES
5626 old = **this;
5627 *this = Persistent<T>();
5628#else
5629 old = val_;
5630 val_ = NULL;
5631#endif
5632 return old;
5633}
5634
5635
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005636template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005637void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005638 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005639 if (this->IsEmpty()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005640 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005641 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5642 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005643}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005644
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005645
5646template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005647uint16_t Persistent<T>::WrapperClassId() const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005648 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005649 if (this->IsEmpty()) return 0;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005650 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005651 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5652 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005653}
5654
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005655
5656template<typename T>
5657ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
5658
5659template<typename T>
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00005660template<typename S>
5661void ReturnValue<T>::Set(const Persistent<S>& handle) {
5662 TYPE_CHECK(T, S);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005663 if (V8_UNLIKELY(handle.IsEmpty())) {
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00005664 *value_ = GetDefaultValue();
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005665 } else {
5666 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5667 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005668}
5669
5670template<typename T>
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00005671template<typename S>
5672void ReturnValue<T>::Set(const Handle<S> handle) {
5673 TYPE_CHECK(T, S);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005674 if (V8_UNLIKELY(handle.IsEmpty())) {
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00005675 *value_ = GetDefaultValue();
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005676 } else {
5677 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5678 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005679}
5680
5681template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005682void ReturnValue<T>::Set(double i) {
5683 Set(Number::New(GetIsolate(), i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005684}
5685
5686template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005687void ReturnValue<T>::Set(int32_t i) {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005688 typedef internal::Internals I;
5689 if (V8_LIKELY(I::IsValidSmi(i))) {
5690 *value_ = I::IntToSmi(i);
5691 return;
5692 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005693 Set(Integer::New(i, GetIsolate()));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005694}
5695
5696template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005697void ReturnValue<T>::Set(uint32_t i) {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005698 typedef internal::Internals I;
danno@chromium.org1fd77d52013-06-07 16:01:45 +00005699 // Can't simply use INT32_MAX here for whatever reason.
5700 bool fits_into_int32_t = (i & (1 << 31)) == 0;
5701 if (V8_LIKELY(fits_into_int32_t)) {
5702 Set(static_cast<int32_t>(i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005703 return;
5704 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005705 Set(Integer::NewFromUnsigned(i, GetIsolate()));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005706}
5707
5708template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005709void ReturnValue<T>::Set(bool value) {
5710 typedef internal::Internals I;
5711 int root_index;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005712 if (value) {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005713 root_index = I::kTrueValueRootIndex;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005714 } else {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005715 root_index = I::kFalseValueRootIndex;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005716 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005717 *value_ = *I::GetRoot(GetIsolate(), root_index);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005718}
5719
5720template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005721void ReturnValue<T>::SetNull() {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005722 typedef internal::Internals I;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005723 *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005724}
5725
5726template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005727void ReturnValue<T>::SetUndefined() {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005728 typedef internal::Internals I;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005729 *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005730}
5731
5732template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005733Isolate* ReturnValue<T>::GetIsolate() {
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00005734 // Isolate is always the pointer below the default value on the stack.
5735 return *reinterpret_cast<Isolate**>(&value_[-2]);
5736}
5737
5738template<typename T>
5739internal::Object* ReturnValue<T>::GetDefaultValue() {
5740 // Default value is always the pointer below value_ on the stack.
5741 return value_[-1];
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005742}
5743
5744
5745template<typename T>
5746FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
5747 internal::Object** values,
5748 int length,
5749 bool is_construct_call)
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005750 : implicit_args_(implicit_args),
5751 values_(values),
5752 length_(length),
5753 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005754
5755
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005756Arguments::Arguments(internal::Object** args,
5757 internal::Object** values,
5758 int length,
5759 bool is_construct_call)
5760 : FunctionCallbackInfo<Value>(args, values, length, is_construct_call) { }
5761
5762
5763template<typename T>
5764Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005765 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
5766 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
5767}
5768
5769
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005770template<typename T>
5771Local<Function> FunctionCallbackInfo<T>::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005772 return Local<Function>(reinterpret_cast<Function*>(
5773 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005774}
5775
5776
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005777template<typename T>
5778Local<Object> FunctionCallbackInfo<T>::This() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005779 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
5780}
5781
5782
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005783template<typename T>
5784Local<Object> FunctionCallbackInfo<T>::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005785 return Local<Object>(reinterpret_cast<Object*>(
5786 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005787}
5788
5789
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005790template<typename T>
5791Local<Value> FunctionCallbackInfo<T>::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005792 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005793}
5794
5795
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005796template<typename T>
5797Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005798 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
5799}
5800
5801
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005802template<typename T>
5803ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
5804 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
5805}
5806
5807
5808template<typename T>
5809bool FunctionCallbackInfo<T>::IsConstructCall() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005810 return is_construct_call_;
5811}
5812
5813
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005814template<typename T>
5815int FunctionCallbackInfo<T>::Length() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005816 return length_;
5817}
5818
5819
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005820template <class T>
5821Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005822 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
5823 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005824 return Local<T>(reinterpret_cast<T*>(after));
5825}
5826
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005827Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005828 return resource_name_;
5829}
5830
5831
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005832Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005833 return resource_line_offset_;
5834}
5835
5836
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005837Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005838 return resource_column_offset_;
5839}
5840
5841
5842Handle<Boolean> Boolean::New(bool value) {
5843 return value ? True() : False();
5844}
5845
5846
5847void Template::Set(const char* name, v8::Handle<Data> value) {
5848 Set(v8::String::New(name), value);
5849}
5850
5851
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005852Local<Value> Object::GetInternalField(int index) {
5853#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005854 typedef internal::Object O;
5855 typedef internal::Internals I;
5856 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005857 // Fast path: If the object is a plain JSObject, which is the common case, we
5858 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005859 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005860 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005861 O* value = I::ReadField<O*>(obj, offset);
5862 O** result = HandleScope::CreateHandle(value);
5863 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005864 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005865#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005866 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005867}
5868
5869
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005870void* Object::GetAlignedPointerFromInternalField(int index) {
5871#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005872 typedef internal::Object O;
5873 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005874 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005875 // Fast path: If the object is a plain JSObject, which is the common case, we
5876 // know where to find the internal fields and can return the value directly.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005877 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005878 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005879 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005880 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005881#endif
5882 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005883}
5884
5885
5886String* String::Cast(v8::Value* value) {
5887#ifdef V8_ENABLE_CHECKS
5888 CheckCast(value);
5889#endif
5890 return static_cast<String*>(value);
5891}
5892
5893
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005894Local<String> String::Empty(Isolate* isolate) {
5895 typedef internal::Object* S;
5896 typedef internal::Internals I;
5897 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005898 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005899 return Local<String>(reinterpret_cast<String*>(slot));
5900}
5901
5902
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00005903Local<String> String::New(const char* data, int length) {
5904 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
5905}
5906
5907
5908Local<String> String::New(const uint16_t* data, int length) {
5909 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
5910}
5911
5912
5913Local<String> String::NewSymbol(const char* data, int length) {
5914 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
5915}
5916
5917
5918Local<String> String::NewUndetectable(const char* data, int length) {
5919 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
5920}
5921
5922
5923Local<String> String::NewUndetectable(const uint16_t* data, int length) {
5924 return NewFromTwoByte(
5925 Isolate::GetCurrent(), data, kUndetectableString, length);
5926}
5927
5928
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005929String::ExternalStringResource* String::GetExternalStringResource() const {
5930 typedef internal::Object O;
5931 typedef internal::Internals I;
5932 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005933 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005934 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005935 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5936 result = reinterpret_cast<String::ExternalStringResource*>(value);
5937 } else {
5938 result = NULL;
5939 }
5940#ifdef V8_ENABLE_CHECKS
5941 VerifyExternalStringResource(result);
5942#endif
5943 return result;
5944}
5945
5946
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005947String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
5948 String::Encoding* encoding_out) const {
5949 typedef internal::Object O;
5950 typedef internal::Internals I;
5951 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
5952 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
5953 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5954 ExternalStringResourceBase* resource = NULL;
5955 if (type == I::kExternalAsciiRepresentationTag ||
5956 type == I::kExternalTwoByteRepresentationTag) {
5957 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5958 resource = static_cast<ExternalStringResourceBase*>(value);
5959 }
5960#ifdef V8_ENABLE_CHECKS
5961 VerifyExternalStringResourceBase(resource, *encoding_out);
5962#endif
5963 return resource;
5964}
5965
5966
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005967bool Value::IsUndefined() const {
5968#ifdef V8_ENABLE_CHECKS
5969 return FullIsUndefined();
5970#else
5971 return QuickIsUndefined();
5972#endif
5973}
5974
5975bool Value::QuickIsUndefined() const {
5976 typedef internal::Object O;
5977 typedef internal::Internals I;
5978 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5979 if (!I::HasHeapObjectTag(obj)) return false;
5980 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5981 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
5982}
5983
5984
5985bool Value::IsNull() const {
5986#ifdef V8_ENABLE_CHECKS
5987 return FullIsNull();
5988#else
5989 return QuickIsNull();
5990#endif
5991}
5992
5993bool Value::QuickIsNull() const {
5994 typedef internal::Object O;
5995 typedef internal::Internals I;
5996 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5997 if (!I::HasHeapObjectTag(obj)) return false;
5998 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5999 return (I::GetOddballKind(obj) == I::kNullOddballKind);
6000}
6001
6002
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006003bool Value::IsString() const {
6004#ifdef V8_ENABLE_CHECKS
6005 return FullIsString();
6006#else
6007 return QuickIsString();
6008#endif
6009}
6010
6011bool Value::QuickIsString() const {
6012 typedef internal::Object O;
6013 typedef internal::Internals I;
6014 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6015 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006016 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006017}
6018
6019
danno@chromium.org1fd77d52013-06-07 16:01:45 +00006020template <class T> Value* Value::Cast(T* value) {
6021 return static_cast<Value*>(value);
6022}
6023
6024
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006025Symbol* Symbol::Cast(v8::Value* value) {
6026#ifdef V8_ENABLE_CHECKS
6027 CheckCast(value);
6028#endif
6029 return static_cast<Symbol*>(value);
6030}
6031
6032
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006033Number* Number::Cast(v8::Value* value) {
6034#ifdef V8_ENABLE_CHECKS
6035 CheckCast(value);
6036#endif
6037 return static_cast<Number*>(value);
6038}
6039
6040
6041Integer* Integer::Cast(v8::Value* value) {
6042#ifdef V8_ENABLE_CHECKS
6043 CheckCast(value);
6044#endif
6045 return static_cast<Integer*>(value);
6046}
6047
6048
6049Date* Date::Cast(v8::Value* value) {
6050#ifdef V8_ENABLE_CHECKS
6051 CheckCast(value);
6052#endif
6053 return static_cast<Date*>(value);
6054}
6055
6056
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006057StringObject* StringObject::Cast(v8::Value* value) {
6058#ifdef V8_ENABLE_CHECKS
6059 CheckCast(value);
6060#endif
6061 return static_cast<StringObject*>(value);
6062}
6063
6064
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006065SymbolObject* SymbolObject::Cast(v8::Value* value) {
6066#ifdef V8_ENABLE_CHECKS
6067 CheckCast(value);
6068#endif
6069 return static_cast<SymbolObject*>(value);
6070}
6071
6072
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006073NumberObject* NumberObject::Cast(v8::Value* value) {
6074#ifdef V8_ENABLE_CHECKS
6075 CheckCast(value);
6076#endif
6077 return static_cast<NumberObject*>(value);
6078}
6079
6080
6081BooleanObject* BooleanObject::Cast(v8::Value* value) {
6082#ifdef V8_ENABLE_CHECKS
6083 CheckCast(value);
6084#endif
6085 return static_cast<BooleanObject*>(value);
6086}
6087
6088
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00006089RegExp* RegExp::Cast(v8::Value* value) {
6090#ifdef V8_ENABLE_CHECKS
6091 CheckCast(value);
6092#endif
6093 return static_cast<RegExp*>(value);
6094}
6095
6096
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006097Object* Object::Cast(v8::Value* value) {
6098#ifdef V8_ENABLE_CHECKS
6099 CheckCast(value);
6100#endif
6101 return static_cast<Object*>(value);
6102}
6103
6104
6105Array* Array::Cast(v8::Value* value) {
6106#ifdef V8_ENABLE_CHECKS
6107 CheckCast(value);
6108#endif
6109 return static_cast<Array*>(value);
6110}
6111
6112
danno@chromium.orgca29dd82013-04-26 11:59:48 +00006113ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
6114#ifdef V8_ENABLE_CHECKS
6115 CheckCast(value);
6116#endif
6117 return static_cast<ArrayBuffer*>(value);
6118}
6119
6120
danno@chromium.orgf005df62013-04-30 16:36:45 +00006121TypedArray* TypedArray::Cast(v8::Value* value) {
6122#ifdef V8_ENABLE_CHECKS
6123 CheckCast(value);
6124#endif
6125 return static_cast<TypedArray*>(value);
6126}
6127
6128
6129Uint8Array* Uint8Array::Cast(v8::Value* value) {
6130#ifdef V8_ENABLE_CHECKS
6131 CheckCast(value);
6132#endif
6133 return static_cast<Uint8Array*>(value);
6134}
6135
6136
6137Int8Array* Int8Array::Cast(v8::Value* value) {
6138#ifdef V8_ENABLE_CHECKS
6139 CheckCast(value);
6140#endif
6141 return static_cast<Int8Array*>(value);
6142}
6143
6144
6145Uint16Array* Uint16Array::Cast(v8::Value* value) {
6146#ifdef V8_ENABLE_CHECKS
6147 CheckCast(value);
6148#endif
6149 return static_cast<Uint16Array*>(value);
6150}
6151
6152
6153Int16Array* Int16Array::Cast(v8::Value* value) {
6154#ifdef V8_ENABLE_CHECKS
6155 CheckCast(value);
6156#endif
6157 return static_cast<Int16Array*>(value);
6158}
6159
6160
6161Uint32Array* Uint32Array::Cast(v8::Value* value) {
6162#ifdef V8_ENABLE_CHECKS
6163 CheckCast(value);
6164#endif
6165 return static_cast<Uint32Array*>(value);
6166}
6167
6168
6169Int32Array* Int32Array::Cast(v8::Value* value) {
6170#ifdef V8_ENABLE_CHECKS
6171 CheckCast(value);
6172#endif
6173 return static_cast<Int32Array*>(value);
6174}
6175
6176
6177Float32Array* Float32Array::Cast(v8::Value* value) {
6178#ifdef V8_ENABLE_CHECKS
6179 CheckCast(value);
6180#endif
6181 return static_cast<Float32Array*>(value);
6182}
6183
6184
6185Float64Array* Float64Array::Cast(v8::Value* value) {
6186#ifdef V8_ENABLE_CHECKS
6187 CheckCast(value);
6188#endif
6189 return static_cast<Float64Array*>(value);
6190}
6191
6192
danno@chromium.org1fd77d52013-06-07 16:01:45 +00006193Uint8ClampedArray* Uint8ClampedArray::Cast(v8::Value* value) {
6194#ifdef V8_ENABLE_CHECKS
6195 CheckCast(value);
6196#endif
6197 return static_cast<Uint8ClampedArray*>(value);
6198}
6199
6200
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006201Function* Function::Cast(v8::Value* value) {
6202#ifdef V8_ENABLE_CHECKS
6203 CheckCast(value);
6204#endif
6205 return static_cast<Function*>(value);
6206}
6207
6208
6209External* External::Cast(v8::Value* value) {
6210#ifdef V8_ENABLE_CHECKS
6211 CheckCast(value);
6212#endif
6213 return static_cast<External*>(value);
6214}
6215
6216
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006217template<typename T>
6218Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
6219 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00006220}
6221
6222
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006223template<typename T>
6224Local<Value> PropertyCallbackInfo<T>::Data() const {
6225 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006226}
6227
6228
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006229template<typename T>
6230Local<Object> PropertyCallbackInfo<T>::This() const {
6231 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006232}
6233
6234
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006235template<typename T>
6236Local<Object> PropertyCallbackInfo<T>::Holder() const {
6237 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
6238}
6239
6240
6241template<typename T>
6242ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
6243 return ReturnValue<T>(&args_[kReturnValueIndex]);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006244}
6245
6246
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00006247Handle<Primitive> Undefined(Isolate* isolate) {
6248 typedef internal::Object* S;
6249 typedef internal::Internals I;
6250 if (!I::IsInitialized(isolate)) return Undefined();
6251 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
6252 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6253}
6254
6255
6256Handle<Primitive> Null(Isolate* isolate) {
6257 typedef internal::Object* S;
6258 typedef internal::Internals I;
6259 if (!I::IsInitialized(isolate)) return Null();
6260 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
6261 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6262}
6263
6264
6265Handle<Boolean> True(Isolate* isolate) {
6266 typedef internal::Object* S;
6267 typedef internal::Internals I;
6268 if (!I::IsInitialized(isolate)) return True();
6269 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
6270 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6271}
6272
6273
6274Handle<Boolean> False(Isolate* isolate) {
6275 typedef internal::Object* S;
6276 typedef internal::Internals I;
6277 if (!I::IsInitialized(isolate)) return False();
6278 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
6279 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6280}
6281
6282
6283void Isolate::SetData(void* data) {
6284 typedef internal::Internals I;
6285 I::SetEmbedderData(this, data);
6286}
6287
6288
6289void* Isolate::GetData() {
6290 typedef internal::Internals I;
6291 return I::GetEmbedderData(this);
6292}
6293
6294
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00006295Local<Value> Context::GetEmbedderData(int index) {
6296#ifndef V8_ENABLE_CHECKS
6297 typedef internal::Object O;
6298 typedef internal::Internals I;
6299 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
6300 return Local<Value>(reinterpret_cast<Value*>(result));
6301#else
6302 return SlowGetEmbedderData(index);
6303#endif
6304}
6305
6306
6307void* Context::GetAlignedPointerFromEmbedderData(int index) {
6308#ifndef V8_ENABLE_CHECKS
6309 typedef internal::Internals I;
6310 return I::ReadEmbedderData<void*>(this, index);
6311#else
6312 return SlowGetAlignedPointerFromEmbedderData(index);
6313#endif
6314}
6315
6316
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006317/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00006318 * \example shell.cc
6319 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006320 * command-line and executes them.
6321 */
6322
6323
6324/**
6325 * \example process.cc
6326 */
6327
6328
6329} // namespace v8
6330
6331
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006332#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006333#undef TYPE_CHECK
6334
6335
ager@chromium.org9258b6b2008-09-11 09:11:10 +00006336#endif // V8_H_