blob: 0f949a8b89f3ca6bcf815447ff45a4ccbe7c2a17 [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000065// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000066#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
67 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070#else
71#define V8EXPORT
72#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000075#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076
77#endif // _WIN32
78
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000079#if defined(__GNUC__) && !defined(DEBUG)
80#define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
81#elif defined(_MSC_VER) && !defined(DEBUG)
82#define V8_INLINE(declarator) __forceinline declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000083#else
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000084#define V8_INLINE(declarator) inline declarator
85#endif
86
87#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
88#define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
89#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
90#define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
91#else
92#define V8_DEPRECATED(declarator) declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000093#endif
94
danno@chromium.orgca29dd82013-04-26 11:59:48 +000095#if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))
96 #define V8_UNLIKELY(condition) __builtin_expect((condition), 0)
97 #define V8_LIKELY(condition) __builtin_expect((condition), 1)
98#else
99 #define V8_UNLIKELY(condition) (condition)
100 #define V8_LIKELY(condition) (condition)
101#endif
102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000104 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 */
106namespace v8 {
107
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000108class AccessorInfo;
109class AccessorSignature;
110class Array;
111class Boolean;
112class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113class Context;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000114class CpuProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000115class Data;
116class Date;
117class DeclaredAccessorDescriptor;
118class External;
119class Function;
120class FunctionTemplate;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000121class HeapProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000122class ImplementationUtilities;
123class Int32;
124class Integer;
125class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000127class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128class Object;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000129class ObjectOperationDescriptor;
130class ObjectTemplate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131class Primitive;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000132class RawOperationDescriptor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133class Signature;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000134class StackFrame;
135class StackTrace;
136class String;
137class StringObject;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000138class Symbol;
139class SymbolObject;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000140class Uint32;
141class Utils;
142class Value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143template <class T> class Handle;
144template <class T> class Local;
145template <class T> class Persistent;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000146class FunctionTemplate;
147class ObjectTemplate;
148class Data;
149class AccessorInfo;
150template<typename T> class PropertyCallbackInfo;
151class StackTrace;
152class StackFrame;
153class Isolate;
154class DeclaredAccessorDescriptor;
155class ObjectOperationDescriptor;
156class RawOperationDescriptor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000158namespace internal {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000159class Arguments;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000160class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000161class HeapObject;
162class Isolate;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000163class Object;
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000164template<typename T> class CustomArguments;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000165class PropertyCallbackArguments;
166class FunctionCallbackArguments;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000167}
168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000170/**
171 * General purpose unique identifier.
172 */
173class UniqueId {
174 public:
175 explicit UniqueId(intptr_t data)
176 : data_(data) {}
177
178 bool operator==(const UniqueId& other) const {
179 return data_ == other.data_;
180 }
181
182 bool operator!=(const UniqueId& other) const {
183 return data_ != other.data_;
184 }
185
186 bool operator<(const UniqueId& other) const {
187 return data_ < other.data_;
188 }
189
190 private:
191 intptr_t data_;
192};
193
194
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000195// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196
197
198/**
199 * A weak reference callback function.
200 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000201 * This callback should either explicitly invoke Dispose on |object| if
202 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
203 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204 * \param object the weak global object to be reclaimed by the garbage collector
205 * \param parameter the value passed in when making the weak global object
206 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000207template<typename T, typename P>
208class WeakReferenceCallbacks {
209 public:
210 typedef void (*Revivable)(Isolate* isolate,
211 Persistent<T>* object,
212 P* parameter);
213};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000215// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000217#define TYPE_CHECK(T, S) \
218 while (false) { \
219 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220 }
221
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000222
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223/**
224 * An object reference managed by the v8 garbage collector.
225 *
226 * All objects returned from v8 have to be tracked by the garbage
227 * collector so that it knows that the objects are still alive. Also,
228 * because the garbage collector may move objects, it is unsafe to
229 * point directly to an object. Instead, all objects are stored in
230 * handles which are known by the garbage collector and updated
231 * whenever an object moves. Handles should always be passed by value
232 * (except in cases like out-parameters) and they should never be
233 * allocated on the heap.
234 *
235 * There are two types of handles: local and persistent handles.
236 * Local handles are light-weight and transient and typically used in
237 * local operations. They are managed by HandleScopes. Persistent
238 * handles can be used when storing objects across several independent
239 * operations and have to be explicitly deallocated when they're no
240 * longer used.
241 *
242 * It is safe to extract the object stored in the handle by
243 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000244 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245 * behind the scenes and the same rules apply to these values as to
246 * their handles.
247 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000248template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 /**
251 * Creates an empty handle.
252 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000253 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000255#ifdef V8_USE_UNSAFE_HANDLES
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 /**
257 * Creates a new handle for the specified value.
258 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000259 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000260#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261
262 /**
263 * Creates a handle for the contents of the specified handle. This
264 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000265 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000267 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 * between compatible handles, for instance assigning a
269 * Handle<String> to a variable declared as Handle<Value>, is legal
270 * because String is a subclass of Value.
271 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000272 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 : val_(reinterpret_cast<T*>(*that)) {
274 /**
275 * This check fails when trying to convert between incompatible
276 * handles. For example, converting from a Handle<String> to a
277 * Handle<Number>.
278 */
279 TYPE_CHECK(T, S);
280 }
281
282 /**
283 * Returns true if the handle is empty.
284 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000285 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 /**
288 * Sets the handle to be empty. IsEmpty() will then return true.
289 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000290 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000291
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000292 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000293
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000294 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295
296 /**
297 * Checks whether two handles are the same.
298 * Returns true if both are empty, or if the objects
299 * to which they refer are identical.
300 * The handles' references are not checked.
301 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000302 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000303 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
304 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 if (a == 0) return b == 0;
306 if (b == 0) return false;
307 return *a == *b;
308 }
309
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000310#ifndef V8_USE_UNSAFE_HANDLES
311 template <class S> V8_INLINE(
312 bool operator==(const Persistent<S>& that) const) {
313 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
314 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
315 if (a == 0) return b == 0;
316 if (b == 0) return false;
317 return *a == *b;
318 }
319#endif
320
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 /**
322 * Checks whether two handles are different.
323 * Returns true if only one of the handles is empty, or if
324 * the objects to which they refer are different.
325 * The handles' references are not checked.
326 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000327 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 return !operator==(that);
329 }
330
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000331 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000332#ifdef V8_ENABLE_CHECKS
333 // If we're going to perform the type check then we have to check
334 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000336#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 return Handle<T>(T::Cast(*that));
338 }
339
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000340 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000341 return Handle<S>::Cast(*this);
342 }
343
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000344#ifndef V8_USE_UNSAFE_HANDLES
345 V8_INLINE(static Handle<T> New(Isolate* isolate, Handle<T> that)) {
346 return New(isolate, that.val_);
347 }
348 // TODO(dcarney): remove before cutover
349 V8_INLINE(static Handle<T> New(Isolate* isolate, const Persistent<T>& that)) {
350 return New(isolate, that.val_);
351 }
352
353#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
354
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000356#endif
357 /**
358 * Creates a new handle for the specified value.
359 */
360 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
361#endif
362
363 private:
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000364 friend class Utils;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000365 template<class F> friend class Persistent;
366 template<class F> friend class Local;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000367 friend class Arguments;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000368 template<class F> friend class FunctionCallbackInfo;
369 template<class F> friend class PropertyCallbackInfo;
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000370 template<class F> friend class internal::CustomArguments;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000371 friend class AccessorInfo;
372 friend Handle<Primitive> Undefined(Isolate* isolate);
373 friend Handle<Primitive> Null(Isolate* isolate);
374 friend Handle<Boolean> True(Isolate* isolate);
375 friend Handle<Boolean> False(Isolate* isolate);
376 friend class Context;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000377 friend class HandleScope;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000378
379#ifndef V8_USE_UNSAFE_HANDLES
380 V8_INLINE(static Handle<T> New(Isolate* isolate, T* that));
381#endif
382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 T* val_;
384};
385
386
387/**
388 * A light-weight stack-allocated object handle. All operations
389 * that return objects from within v8 return them in local handles. They
390 * are created within HandleScopes, and all local handles allocated within a
391 * handle scope are destroyed when the handle scope is destroyed. Hence it
392 * is not necessary to explicitly deallocate local handles.
393 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000394// TODO(dcarney): deprecate entire class
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000395template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000397 V8_INLINE(Local());
398 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399 : Handle<T>(reinterpret_cast<T*>(*that)) {
400 /**
401 * This check fails when trying to convert between incompatible
402 * handles. For example, converting from a Handle<String> to a
403 * Handle<Number>.
404 */
405 TYPE_CHECK(T, S);
406 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000407
408
409#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000410 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000411#endif
412
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000413 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000414#ifdef V8_ENABLE_CHECKS
415 // If we're going to perform the type check then we have to check
416 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000418#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 return Local<T>(T::Cast(*that));
420 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000421#ifndef V8_USE_UNSAFE_HANDLES
422 template <class S> V8_INLINE(Local(Handle<S> that))
423 : Handle<T>(reinterpret_cast<T*>(*that)) {
424 TYPE_CHECK(T, S);
425 }
426#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000428 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000429 return Local<S>::Cast(*this);
430 }
431
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000432 /**
433 * Create a local handle for the content of another handle.
434 * The referee is kept alive by the local handle even when
435 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000437 V8_INLINE(static Local<T> New(Handle<T> that));
438 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000439#ifndef V8_USE_UNSAFE_HANDLES
440 // TODO(dcarney): remove before cutover
441 V8_INLINE(static Local<T> New(Isolate* isolate, const Persistent<T>& that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000443#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
444
445 private:
446#endif
447 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
448#endif
449
450 private:
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000451 friend class Utils;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000452 template<class F> friend class Persistent;
453 template<class F> friend class Handle;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000454 friend class Arguments;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000455 template<class F> friend class FunctionCallbackInfo;
456 template<class F> friend class PropertyCallbackInfo;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000457 friend class String;
458 friend class Object;
459 friend class AccessorInfo;
460 friend class Context;
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000461 template<class F> friend class internal::CustomArguments;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000462 friend class HandleScope;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000463
464 V8_INLINE(static Local<T> New(Isolate* isolate, T* that));
465};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466
467/**
468 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000469 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 * allocated, a Persistent handle remains valid until it is explicitly
471 * disposed.
472 *
473 * A persistent handle contains a reference to a storage cell within
474 * the v8 engine which holds an object value and which is updated by
475 * the garbage collector whenever the object is moved. A new storage
476 * cell can be created using Persistent::New and existing handles can
477 * be disposed using Persistent::Dispose. Since persistent handles
478 * are passed by value you may have many persistent handle objects
479 * that point to the same storage cell. For instance, if you pass a
480 * persistent handle as an argument to a function you will not get two
481 * different storage cells but rather two references to the same
482 * storage cell.
483 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000484template <class T> class Persistent // NOLINT
485#ifdef V8_USE_UNSAFE_HANDLES
486 : public Handle<T> {
487#else
488 { // NOLINT
489#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 public:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000491#ifndef V8_USE_UNSAFE_HANDLES
492 V8_INLINE(Persistent()) : val_(0) { }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000493 // TODO(dcarney): add this back before cutover.
494// V8_INLINE(~Persistent()) {
495// Dispose();
496// }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000497 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
498 // TODO(dcarney): remove somehow before cutover
499 // The handle should either be 0, or a pointer to a live cell.
500 V8_INLINE(void Clear()) { val_ = 0; }
501
502 /**
503 * A constructor that creates a new global cell pointing to that. In contrast
504 * to the copy constructor, this creates a new persistent handle which needs
505 * to be separately disposed.
506 */
507 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000508 : val_(New(isolate, *that)) { }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000509
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000510 template <class S> V8_INLINE(Persistent(Isolate* isolate,
511 Persistent<S>& that)) // NOLINT
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000512 : val_(New(isolate, *that)) { }
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000513
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000514#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 /**
516 * Creates an empty persistent handle that doesn't point to any
517 * storage cell.
518 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000519 V8_INLINE(Persistent()) : Handle<T>() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
521 /**
522 * Creates a persistent handle for the same storage cell as the
523 * specified handle. This constructor allows you to pass persistent
524 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000525 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000527 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 * between compatible persistent handles, for instance assigning a
529 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000530 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000532 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 : Handle<T>(reinterpret_cast<T*>(*that)) {
534 /**
535 * This check fails when trying to convert between incompatible
536 * handles. For example, converting from a Handle<String> to a
537 * Handle<Number>.
538 */
539 TYPE_CHECK(T, S);
540 }
541
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000542 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000544 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000545 * A constructor that creates a new global cell pointing to that. In contrast
546 * to the copy constructor, this creates a new persistent handle which needs
547 * to be separately disposed.
548 */
549 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
550 : Handle<T>(New(isolate, that)) { }
551
552 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000553 * "Casts" a plain handle which is known to be a persistent handle
554 * to a persistent handle.
555 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000556 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 : Handle<T>(*that) { }
558
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000559#endif
560
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000561#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000562 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000563#ifdef V8_ENABLE_CHECKS
564 // If we're going to perform the type check then we have to check
565 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000567#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 return Persistent<T>(T::Cast(*that));
569 }
570
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000571 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000572 return Persistent<S>::Cast(*this);
573 }
574
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000575#else
576 template <class S>
577 V8_INLINE(static Persistent<T>& Cast(Persistent<S>& that)) { // NOLINT
578#ifdef V8_ENABLE_CHECKS
579 // If we're going to perform the type check then we have to check
580 // that the handle isn't empty before doing the checked cast.
581 if (!that.IsEmpty()) T::Cast(*that);
582#endif
583 return reinterpret_cast<Persistent<T>&>(that);
584 }
585
586 template <class S> V8_INLINE(Persistent<S>& As()) { // NOLINT
587 return Persistent<S>::Cast(*this);
588 }
589#endif
590
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000591#ifdef V8_USE_UNSAFE_HANDLES
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000592 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000593 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000594 V8_INLINE(static Persistent<T> New(Isolate* isolate, Persistent<T> that));
595#endif
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000596
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000597#ifndef V8_USE_UNSAFE_HANDLES
598 template <class S> V8_INLINE(
599 bool operator==(const Persistent<S>& that) const) {
600 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
601 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
602 if (a == 0) return b == 0;
603 if (b == 0) return false;
604 return *a == *b;
605 }
606
607 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
608 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
609 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
610 if (a == 0) return b == 0;
611 if (b == 0) return false;
612 return *a == *b;
613 }
614#endif
615
616 V8_INLINE(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617
618 /**
619 * Releases the storage cell referenced by this persistent handle.
620 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000621 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 * cell remain and IsEmpty will still return false.
623 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000624 // TODO(dcarney): deprecate
625 V8_INLINE(void Dispose(Isolate* isolate)) { Dispose(); }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 /**
628 * Make the reference to this object weak. When only weak handles
629 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000630 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631 * it the object reference and the given parameters.
632 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000633 template<typename S, typename P>
634 V8_INLINE(void MakeWeak(
635 P* parameters,
636 typename WeakReferenceCallbacks<S, P>::Revivable callback));
637
638 template<typename P>
639 V8_INLINE(void MakeWeak(
640 P* parameters,
641 typename WeakReferenceCallbacks<T, P>::Revivable callback));
642
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000643 template<typename S, typename P>
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000644 V8_DEPRECATED(void MakeWeak(
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000645 Isolate* isolate,
646 P* parameters,
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000647 typename WeakReferenceCallbacks<S, P>::Revivable callback));
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000648
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000649 template<typename P>
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000650 V8_DEPRECATED(void MakeWeak(
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000651 Isolate* isolate,
652 P* parameters,
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000653 typename WeakReferenceCallbacks<T, P>::Revivable callback));
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000654
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000655 V8_INLINE(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000657 // TODO(dcarney): deprecate
658 V8_INLINE(void ClearWeak(Isolate* isolate)) { ClearWeak(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659
660 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000661 * Marks the reference to this object independent. Garbage collector is free
662 * to ignore any object groups containing this object. Weak callback for an
663 * independent handle should not assume that it will be preceded by a global
664 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000665 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000666 V8_INLINE(void MarkIndependent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000667
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000668 // TODO(dcarney): deprecate
669 V8_INLINE(void MarkIndependent(Isolate* isolate)) { MarkIndependent(); }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000670
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000671 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000672 * Marks the reference to this object partially dependent. Partially dependent
673 * handles only depend on other partially dependent handles and these
674 * dependencies are provided through object groups. It provides a way to build
675 * smaller object groups for young objects that represent only a subset of all
676 * external dependencies. This mark is automatically cleared after each
677 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000678 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000679 V8_INLINE(void MarkPartiallyDependent());
680
681 // TODO(dcarney): deprecate
682 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate)) {
683 MarkPartiallyDependent();
684 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000685
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000686 V8_INLINE(bool IsIndependent() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000687
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000688 // TODO(dcarney): deprecate
689 V8_INLINE(bool IsIndependent(Isolate* isolate) const) {
690 return IsIndependent();
691 }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000692
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000693 /** Checks if the handle holds the only reference to an object. */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000694 V8_INLINE(bool IsNearDeath() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000695
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000696 // TODO(dcarney): deprecate
697 V8_INLINE(bool IsNearDeath(Isolate* isolate) const) { return IsNearDeath(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000699 /** Returns true if the handle's reference is weak. */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000700 V8_INLINE(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000702 // TODO(dcarney): deprecate
703 V8_INLINE(bool IsWeak(Isolate* isolate) const) { return IsWeak(); }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000704
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000705 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000706 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
707 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000708 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000709 V8_INLINE(void SetWrapperClassId(uint16_t class_id));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000710
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000711 // TODO(dcarney): deprecate
712 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id)) {
713 SetWrapperClassId(class_id);
714 }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000715
716 /**
717 * Returns the class ID previously assigned to this handle or 0 if no class ID
718 * was previously assigned.
719 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000720 V8_INLINE(uint16_t WrapperClassId() const);
721
722 // TODO(dcarney): deprecate
723 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const) {
724 return WrapperClassId();
725 }
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000726
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000727 /**
728 * Disposes the current contents of the handle and replaces it.
729 */
730 V8_INLINE(void Reset(Isolate* isolate, const Handle<T>& other));
731
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000732#ifndef V8_USE_UNSAFE_HANDLES
733 V8_INLINE(void Reset(Isolate* isolate, const Persistent<T>& other));
734#endif
735
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000736 /**
737 * Returns the underlying raw pointer and clears the handle. The caller is
738 * responsible of eventually destroying the underlying object (by creating a
739 * Persistent handle which points to it and Disposing it). In the future,
740 * destructing a Persistent will also Dispose it. With this function, the
741 * embedder can let the Persistent go out of scope without it getting
742 * disposed.
743 */
744 V8_INLINE(T* ClearAndLeak());
745
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000746#ifndef V8_USE_UNSAFE_HANDLES
747
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000749 // TODO(dcarney): make unlinkable before cutover
750 V8_INLINE(Persistent(const Persistent& that)) : val_(that.val_) {}
751 // TODO(dcarney): make unlinkable before cutover
752 V8_INLINE(Persistent& operator=(const Persistent& that)) { // NOLINT
753 this->val_ = that.val_;
754 return *this;
755 }
756
757 public:
758#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
759
760 private:
761#endif
762 // TODO(dcarney): remove before cutover
763 template <class S> V8_INLINE(Persistent(S* that)) : val_(that) { }
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000764
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000765 // TODO(dcarney): remove before cutover
766 V8_INLINE(T* operator*() const) { return val_; }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000767
768 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000769 // TODO(dcarney): remove before cutover
770 V8_INLINE(T* operator->() const) { return val_; }
771 public:
772#endif
773
774 private:
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000775 friend class Utils;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000776 template<class F> friend class Handle;
777 template<class F> friend class Local;
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000778 template<class F> friend class Persistent;
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000779 template<class F> friend class ReturnValue;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000780
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000781 V8_INLINE(static T* New(Isolate* isolate, T* that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000782
783#ifndef V8_USE_UNSAFE_HANDLES
784 T* val_;
785#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786};
787
788
v8.team.kasperl727e9952008-09-02 14:56:44 +0000789 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790 * A stack-allocated class that governs a number of local handles.
791 * After a handle scope has been created, all local handles will be
792 * allocated within that handle scope until either the handle scope is
793 * deleted or another handle scope is created. If there is already a
794 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000795 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796 * new handles will again be allocated in the original handle scope.
797 *
798 * After the handle scope of a local handle has been deleted the
799 * garbage collector will no longer track the object stored in the
800 * handle and may deallocate it. The behavior of accessing a handle
801 * for which the handle scope has been deleted is undefined.
802 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000803class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000805 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000806 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000808 HandleScope(Isolate* isolate);
809
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000810 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811
812 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813 * Closes the handle scope and returns the value as a handle in the
814 * previous scope, which is the new current scope after the call.
815 */
816 template <class T> Local<T> Close(Handle<T> value);
817
818 /**
819 * Counts the number of allocated handles.
820 */
821 static int NumberOfHandles();
822
823 /**
824 * Creates a new handle with the given value.
825 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000826 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000827 static internal::Object** CreateHandle(internal::Isolate* isolate,
828 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000829 // Faster version, uses HeapObject to obtain the current Isolate.
830 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831
832 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000833 // Make it hard to create heap-allocated or illegal handle scopes by
834 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835 HandleScope(const HandleScope&);
836 void operator=(const HandleScope&);
837 void* operator new(size_t size);
838 void operator delete(void*, size_t);
839
ager@chromium.org3811b432009-10-28 14:53:37 +0000840 // This Data class is accessible internally as HandleScopeData through a
841 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000842 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000844 internal::Object** next;
845 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000846 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000847 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000849 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850 }
851 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000852
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000853 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000854 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000856 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000857 internal::Object** prev_next_;
858 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000860 // Allow for the active closing of HandleScopes which allows to pass a handle
861 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000863 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 friend class ImplementationUtilities;
866};
867
868
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000869// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870
871
872/**
873 * The superclass of values and API object templates.
874 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000875class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 private:
877 Data();
878};
879
880
881/**
882 * Pre-compilation data that can be associated with a script. This
883 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000884 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000885 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000887class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 public:
889 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000890
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000891 /**
892 * Pre-compiles the specified script (context-independent).
893 *
894 * \param input Pointer to UTF-8 script source code.
895 * \param length Length of UTF-8 script source code.
896 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000899 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000900 * Pre-compiles the specified script (context-independent).
901 *
902 * NOTE: Pre-compilation using this method cannot happen on another thread
903 * without using Lockers.
904 *
905 * \param source Script source code.
906 */
907 static ScriptData* PreCompile(Handle<String> source);
908
909 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000910 * Load previous pre-compilation data.
911 *
912 * \param data Pointer to data returned by a call to Data() of a previous
913 * ScriptData. Ownership is not transferred.
914 * \param length Length of data.
915 */
916 static ScriptData* New(const char* data, int length);
917
918 /**
919 * Returns the length of Data().
920 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000922
923 /**
924 * Returns a serialized representation of this ScriptData that can later be
925 * passed to New(). NOTE: Serialized data is platform-dependent.
926 */
927 virtual const char* Data() = 0;
928
929 /**
930 * Returns true if the source code could not be parsed.
931 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000932 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933};
934
935
936/**
937 * The origin, within a file, of a script.
938 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000939class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000941 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000942 Handle<Value> resource_name,
943 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000944 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 : resource_name_(resource_name),
946 resource_line_offset_(resource_line_offset),
947 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000948 V8_INLINE(Handle<Value> ResourceName() const);
949 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
950 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000952 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 Handle<Integer> resource_line_offset_;
954 Handle<Integer> resource_column_offset_;
955};
956
957
958/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000959 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000961class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000963 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000964 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000965 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000966 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000967 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000968 * when New() returns
969 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
970 * using pre_data speeds compilation if it's done multiple times.
971 * Owned by caller, no references are kept when New() returns.
972 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000973 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000974 * available to compile event handlers.
975 * \return Compiled script object (context independent; when run it
976 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000978 static Local<Script> New(Handle<String> source,
979 ScriptOrigin* origin = NULL,
980 ScriptData* pre_data = NULL,
981 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982
mads.s.agercbaa0602008-08-14 13:41:48 +0000983 /**
984 * Compiles the specified script using the specified file name
985 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000986 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000987 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000988 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000989 * as the script's origin.
990 * \return Compiled script object (context independent; when run it
991 * will use the currently entered context).
992 */
993 static Local<Script> New(Handle<String> source,
994 Handle<Value> file_name);
995
996 /**
997 * Compiles the specified script (bound to current context).
998 *
999 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001000 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +00001001 * when Compile() returns
1002 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1003 * using pre_data speeds compilation if it's done multiple times.
1004 * Owned by caller, no references are kept when Compile() returns.
1005 * \param script_data Arbitrary data associated with script. Using
1006 * this has same effect as calling SetData(), but makes data available
1007 * earlier (i.e. to compile event handlers).
1008 * \return Compiled script object, bound to the context that was active
1009 * when this function was called. When run it will always use this
1010 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +00001011 */
1012 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +00001013 ScriptOrigin* origin = NULL,
1014 ScriptData* pre_data = NULL,
1015 Handle<String> script_data = Handle<String>());
1016
1017 /**
1018 * Compiles the specified script using the specified file name
1019 * object (typically a string) as the script's origin.
1020 *
1021 * \param source Script source code.
1022 * \param file_name File name to use as script's origin
1023 * \param script_data Arbitrary data associated with script. Using
1024 * this has same effect as calling SetData(), but makes data available
1025 * earlier (i.e. to compile event handlers).
1026 * \return Compiled script object, bound to the context that was active
1027 * when this function was called. When run it will always use this
1028 * context.
1029 */
1030 static Local<Script> Compile(Handle<String> source,
1031 Handle<Value> file_name,
1032 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +00001033
v8.team.kasperl727e9952008-09-02 14:56:44 +00001034 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001035 * Runs the script returning the resulting value. If the script is
1036 * context independent (created using ::New) it will be run in the
1037 * currently entered context. If it is context specific (created
1038 * using ::Compile) it will be run in the context in which it was
1039 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001040 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001042
1043 /**
1044 * Returns the script id value.
1045 */
1046 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001047
1048 /**
1049 * Associate an additional data object with the script. This is mainly used
1050 * with the debugger as this data object is only available through the
1051 * debugger API.
1052 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00001053 void SetData(Handle<String> data);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001054
1055 /**
1056 * Returns the name value of one Script.
1057 */
1058 Handle<Value> GetScriptName();
1059
1060 /**
1061 * Returns zero based line number of the code_pos location in the script.
1062 * -1 will be returned if no information available.
1063 */
1064 int GetLineNumber(int code_pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065};
1066
1067
1068/**
1069 * An error message.
1070 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001071class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001073 Local<String> Get() const;
1074 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001076 /**
1077 * Returns the resource name for the script from where the function causing
1078 * the error originates.
1079 */
ager@chromium.org32912102009-01-16 10:38:43 +00001080 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001082 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001083 * Returns the resource data for the script from where the function causing
1084 * the error originates.
1085 */
1086 Handle<Value> GetScriptData() const;
1087
1088 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001089 * Exception stack trace. By default stack traces are not captured for
1090 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1091 * to change this option.
1092 */
1093 Handle<StackTrace> GetStackTrace() const;
1094
1095 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001096 * Returns the number, 1-based, of the line where the error occurred.
1097 */
ager@chromium.org32912102009-01-16 10:38:43 +00001098 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001100 /**
1101 * Returns the index within the script of the first character where
1102 * the error occurred.
1103 */
ager@chromium.org32912102009-01-16 10:38:43 +00001104 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001105
1106 /**
1107 * Returns the index within the script of the last character where
1108 * the error occurred.
1109 */
ager@chromium.org32912102009-01-16 10:38:43 +00001110 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001111
1112 /**
1113 * Returns the index within the line of the first character where
1114 * the error occurred.
1115 */
ager@chromium.org32912102009-01-16 10:38:43 +00001116 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001117
1118 /**
1119 * Returns the index within the line of the last character where
1120 * the error occurred.
1121 */
ager@chromium.org32912102009-01-16 10:38:43 +00001122 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001123
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 // TODO(1245381): Print to a string instead of on a FILE.
1125 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001126
1127 static const int kNoLineNumberInfo = 0;
1128 static const int kNoColumnInfo = 0;
1129};
1130
1131
1132/**
1133 * Representation of a JavaScript stack trace. The information collected is a
1134 * snapshot of the execution stack and the information remains valid after
1135 * execution continues.
1136 */
1137class V8EXPORT StackTrace {
1138 public:
1139 /**
1140 * Flags that determine what information is placed captured for each
1141 * StackFrame when grabbing the current stack trace.
1142 */
1143 enum StackTraceOptions {
1144 kLineNumber = 1,
1145 kColumnOffset = 1 << 1 | kLineNumber,
1146 kScriptName = 1 << 2,
1147 kFunctionName = 1 << 3,
1148 kIsEval = 1 << 4,
1149 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001150 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001151 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001152 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001153 };
1154
1155 /**
1156 * Returns a StackFrame at a particular index.
1157 */
1158 Local<StackFrame> GetFrame(uint32_t index) const;
1159
1160 /**
1161 * Returns the number of StackFrames.
1162 */
1163 int GetFrameCount() const;
1164
1165 /**
1166 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1167 */
1168 Local<Array> AsArray();
1169
1170 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001171 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001172 *
1173 * \param frame_limit The maximum number of stack frames we want to capture.
1174 * \param options Enumerates the set of things we will capture for each
1175 * StackFrame.
1176 */
1177 static Local<StackTrace> CurrentStackTrace(
1178 int frame_limit,
1179 StackTraceOptions options = kOverview);
1180};
1181
1182
1183/**
1184 * A single JavaScript stack frame.
1185 */
1186class V8EXPORT StackFrame {
1187 public:
1188 /**
1189 * Returns the number, 1-based, of the line for the associate function call.
1190 * This method will return Message::kNoLineNumberInfo if it is unable to
1191 * retrieve the line number, or if kLineNumber was not passed as an option
1192 * when capturing the StackTrace.
1193 */
1194 int GetLineNumber() const;
1195
1196 /**
1197 * Returns the 1-based column offset on the line for the associated function
1198 * call.
1199 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1200 * the column number, or if kColumnOffset was not passed as an option when
1201 * capturing the StackTrace.
1202 */
1203 int GetColumn() const;
1204
1205 /**
1206 * Returns the name of the resource that contains the script for the
1207 * function for this StackFrame.
1208 */
1209 Local<String> GetScriptName() const;
1210
1211 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001212 * Returns the name of the resource that contains the script for the
1213 * function for this StackFrame or sourceURL value if the script name
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001214 * is undefined and its source ends with //# sourceURL=... string or
1215 * deprecated //@ sourceURL=... string.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001216 */
1217 Local<String> GetScriptNameOrSourceURL() const;
1218
1219 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001220 * Returns the name of the function associated with this stack frame.
1221 */
1222 Local<String> GetFunctionName() const;
1223
1224 /**
1225 * Returns whether or not the associated function is compiled via a call to
1226 * eval().
1227 */
1228 bool IsEval() const;
1229
1230 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001231 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001232 * constructor via "new".
1233 */
1234 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235};
1236
1237
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001238// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239
1240
1241/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001242 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001244class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 /**
1247 * Returns true if this value is the undefined value. See ECMA-262
1248 * 4.3.10.
1249 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001250 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251
1252 /**
1253 * Returns true if this value is the null value. See ECMA-262
1254 * 4.3.11.
1255 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001256 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257
1258 /**
1259 * Returns true if this value is true.
1260 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001261 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262
1263 /**
1264 * Returns true if this value is false.
1265 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001266 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267
1268 /**
1269 * Returns true if this value is an instance of the String type.
1270 * See ECMA-262 8.4.
1271 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001272 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273
1274 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001275 * Returns true if this value is a symbol.
1276 * This is an experimental feature.
1277 */
1278 bool IsSymbol() const;
1279
1280 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 * Returns true if this value is a function.
1282 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001283 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284
1285 /**
1286 * Returns true if this value is an array.
1287 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001288 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289
v8.team.kasperl727e9952008-09-02 14:56:44 +00001290 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 * Returns true if this value is an object.
1292 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001293 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294
v8.team.kasperl727e9952008-09-02 14:56:44 +00001295 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 * Returns true if this value is boolean.
1297 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001298 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001299
v8.team.kasperl727e9952008-09-02 14:56:44 +00001300 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 * Returns true if this value is a number.
1302 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001303 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304
v8.team.kasperl727e9952008-09-02 14:56:44 +00001305 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 * Returns true if this value is external.
1307 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001308 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309
v8.team.kasperl727e9952008-09-02 14:56:44 +00001310 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 * Returns true if this value is a 32-bit signed integer.
1312 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001313 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001315 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001316 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001317 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001318 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001319
1320 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001321 * Returns true if this value is a Date.
1322 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001323 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001324
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001325 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001326 * Returns true if this value is a Boolean object.
1327 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001328 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001329
1330 /**
1331 * Returns true if this value is a Number object.
1332 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001333 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001334
1335 /**
1336 * Returns true if this value is a String object.
1337 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001338 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001339
1340 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001341 * Returns true if this value is a Symbol object.
1342 * This is an experimental feature.
1343 */
1344 bool IsSymbolObject() const;
1345
1346 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001347 * Returns true if this value is a NativeError.
1348 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001349 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001350
1351 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001352 * Returns true if this value is a RegExp.
1353 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001354 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001355
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001356
1357 /**
1358 * Returns true if this value is an ArrayBuffer.
1359 * This is an experimental feature.
1360 */
1361 bool IsArrayBuffer() const;
1362
1363 /**
1364 * Returns true if this value is one of TypedArrays.
1365 * This is an experimental feature.
1366 */
1367 bool IsTypedArray() const;
1368
1369 /**
1370 * Returns true if this value is an Uint8Array.
1371 * This is an experimental feature.
1372 */
1373 bool IsUint8Array() const;
1374
1375 /**
1376 * Returns true if this value is an Uint8ClampedArray.
1377 * This is an experimental feature.
1378 */
1379 bool IsUint8ClampedArray() const;
1380
1381 /**
1382 * Returns true if this value is an Int8Array.
1383 * This is an experimental feature.
1384 */
1385 bool IsInt8Array() const;
1386
1387 /**
1388 * Returns true if this value is an Uint16Array.
1389 * This is an experimental feature.
1390 */
1391 bool IsUint16Array() const;
1392
1393 /**
1394 * Returns true if this value is an Int16Array.
1395 * This is an experimental feature.
1396 */
1397 bool IsInt16Array() const;
1398
1399 /**
1400 * Returns true if this value is an Uint32Array.
1401 * This is an experimental feature.
1402 */
1403 bool IsUint32Array() const;
1404
1405 /**
1406 * Returns true if this value is an Int32Array.
1407 * This is an experimental feature.
1408 */
1409 bool IsInt32Array() const;
1410
1411 /**
1412 * Returns true if this value is a Float32Array.
1413 * This is an experimental feature.
1414 */
1415 bool IsFloat32Array() const;
1416
1417 /**
1418 * Returns true if this value is a Float64Array.
1419 * This is an experimental feature.
1420 */
1421 bool IsFloat64Array() const;
1422
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001423 Local<Boolean> ToBoolean() const;
1424 Local<Number> ToNumber() const;
1425 Local<String> ToString() const;
1426 Local<String> ToDetailString() const;
1427 Local<Object> ToObject() const;
1428 Local<Integer> ToInteger() const;
1429 Local<Uint32> ToUint32() const;
1430 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431
1432 /**
1433 * Attempts to convert a string to an array index.
1434 * Returns an empty handle if the conversion fails.
1435 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001436 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001438 bool BooleanValue() const;
1439 double NumberValue() const;
1440 int64_t IntegerValue() const;
1441 uint32_t Uint32Value() const;
1442 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443
1444 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001445 bool Equals(Handle<Value> that) const;
1446 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001447
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001448 template <class T> V8_INLINE(static Value* Cast(T* value));
1449
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001450 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001451 V8_INLINE(bool QuickIsUndefined() const);
1452 V8_INLINE(bool QuickIsNull() const);
1453 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001454 bool FullIsUndefined() const;
1455 bool FullIsNull() const;
1456 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457};
1458
1459
1460/**
1461 * The superclass of primitive values. See ECMA-262 4.3.2.
1462 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001463class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464
1465
1466/**
1467 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1468 * or false value.
1469 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001470class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001472 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001473 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474};
1475
1476
1477/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001478 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001480class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001482 enum Encoding {
1483 UNKNOWN_ENCODING = 0x1,
1484 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001485 ASCII_ENCODING = 0x4,
1486 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001487 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001488 /**
1489 * Returns the number of characters in this string.
1490 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001491 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492
v8.team.kasperl727e9952008-09-02 14:56:44 +00001493 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001494 * Returns the number of bytes in the UTF-8 encoded
1495 * representation of this string.
1496 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001497 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001498
1499 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001500 * This function is no longer useful.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001501 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001502 V8_DEPRECATED(V8_INLINE(bool MayContainNonAscii()) const) { return true; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001503
1504 /**
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00001505 * Returns whether this string is known to contain only one byte data.
1506 * Does not read the string.
1507 * False negatives are possible.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001508 */
1509 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001510
1511 /**
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00001512 * Returns whether this string contain only one byte data.
1513 * Will read the entire string in some cases.
1514 */
1515 bool ContainsOnlyOneByte() const;
1516
1517 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001518 * Write the contents of the string to an external buffer.
1519 * If no arguments are given, expects the buffer to be large
1520 * enough to hold the entire string and NULL terminator. Copies
1521 * the contents of the string and the NULL terminator into the
1522 * buffer.
1523 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001524 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1525 * before the end of the buffer.
1526 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001527 * Copies up to length characters into the output buffer.
1528 * Only null-terminates if there is enough space in the buffer.
1529 *
1530 * \param buffer The buffer into which the string will be copied.
1531 * \param start The starting position within the string at which
1532 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001533 * \param length The number of characters to copy from the string. For
1534 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001535 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001536 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001537 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001538 * \return The number of characters copied to the buffer excluding the null
1539 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001540 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001541 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001542 enum WriteOptions {
1543 NO_OPTIONS = 0,
1544 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001545 NO_NULL_TERMINATION = 2,
1546 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001547 };
1548
lrn@chromium.org34e60782011-09-15 07:25:40 +00001549 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001550 int Write(uint16_t* buffer,
1551 int start = 0,
1552 int length = -1,
1553 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001554 // ASCII characters.
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001555 V8_DEPRECATED(int WriteAscii(char* buffer,
1556 int start = 0,
1557 int length = -1,
1558 int options = NO_OPTIONS) const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001559 // One byte characters.
1560 int WriteOneByte(uint8_t* buffer,
1561 int start = 0,
1562 int length = -1,
1563 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001564 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001565 int WriteUtf8(char* buffer,
1566 int length = -1,
1567 int* nchars_ref = NULL,
1568 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569
v8.team.kasperl727e9952008-09-02 14:56:44 +00001570 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001571 * A zero length string.
1572 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001573 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001574 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001575
1576 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001577 * Returns true if the string is external
1578 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001579 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580
v8.team.kasperl727e9952008-09-02 14:56:44 +00001581 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001582 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001583 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001584 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001585
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001586 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001587 public:
1588 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001589
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001590 protected:
1591 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001592
1593 /**
1594 * Internally V8 will call this Dispose method when the external string
1595 * resource is no longer needed. The default implementation will use the
1596 * delete operator. This method can be overridden in subclasses to
1597 * control how allocated external string resources are disposed.
1598 */
1599 virtual void Dispose() { delete this; }
1600
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001601 private:
1602 // Disallow copying and assigning.
1603 ExternalStringResourceBase(const ExternalStringResourceBase&);
1604 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001605
1606 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001607 };
1608
v8.team.kasperl727e9952008-09-02 14:56:44 +00001609 /**
1610 * An ExternalStringResource is a wrapper around a two-byte string
1611 * buffer that resides outside V8's heap. Implement an
1612 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001613 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001614 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001615 class V8EXPORT ExternalStringResource
1616 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 public:
1618 /**
1619 * Override the destructor to manage the life cycle of the underlying
1620 * buffer.
1621 */
1622 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001623
1624 /**
1625 * The string data from the underlying buffer.
1626 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001628
1629 /**
1630 * The length of the string. That is, the number of two-byte characters.
1631 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001633
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 protected:
1635 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 };
1637
1638 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001639 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001640 * string buffer that resides outside V8's heap. Implement an
1641 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001642 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001643 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001644 * UTF-8, which would require special treatment internally in the
1645 * engine and, in the case of UTF-8, do not allow efficient indexing.
1646 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001647 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001649 class V8EXPORT ExternalAsciiStringResource
1650 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 public:
1652 /**
1653 * Override the destructor to manage the life cycle of the underlying
1654 * buffer.
1655 */
1656 virtual ~ExternalAsciiStringResource() {}
1657 /** The string data from the underlying buffer.*/
1658 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001659 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 virtual size_t length() const = 0;
1661 protected:
1662 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 };
1664
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001665 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1666
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001668 * If the string is an external string, return the ExternalStringResourceBase
1669 * regardless of the encoding, otherwise return NULL. The encoding of the
1670 * string is returned in encoding_out.
1671 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001672 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1673 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001674
1675 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001676 * Get the ExternalStringResource for an external string. Returns
1677 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001679 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680
1681 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001682 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001683 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001685 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001687 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001689 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001691 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001692 * The second parameter 'length' gives the buffer length. If omitted,
1693 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001695 V8_INLINE(static Local<String> New(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001697 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001698 /** Allocates a new string from 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001699 V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001701 // TODO(dcarney): deprecate
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001702 /**
1703 * Creates an internalized string (historically called a "symbol",
1704 * not to be confused with ES6 symbols). Returns one if it exists already.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001705 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001706 V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
1707
1708 enum NewStringType {
1709 kNormalString, kInternalizedString, kUndetectableString
1710 };
1711
1712 /** Allocates a new string from UTF-8 data.*/
1713 static Local<String> NewFromUtf8(Isolate* isolate,
1714 const char* data,
1715 NewStringType type = kNormalString,
1716 int length = -1);
1717
1718 /** Allocates a new string from Latin-1 data.*/
1719 static Local<String> NewFromOneByte(
1720 Isolate* isolate,
1721 const uint8_t* data,
1722 NewStringType type = kNormalString,
1723 int length = -1);
1724
1725 /** Allocates a new string from UTF-16 data.*/
1726 static Local<String> NewFromTwoByte(
1727 Isolate* isolate,
1728 const uint16_t* data,
1729 NewStringType type = kNormalString,
1730 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731
v8.team.kasperl727e9952008-09-02 14:56:44 +00001732 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001733 * Creates a new string by concatenating the left and the right strings
1734 * passed in as parameters.
1735 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001736 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001737
1738 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001739 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001740 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001741 * resource will be disposed by calling its Dispose method. The caller of
1742 * this function should not otherwise delete or modify the resource. Neither
1743 * should the underlying buffer be deallocated or modified except through the
1744 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001745 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001746 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001747
ager@chromium.org6f10e412009-02-13 10:11:16 +00001748 /**
1749 * Associate an external string resource with this string by transforming it
1750 * in place so that existing references to this string in the JavaScript heap
1751 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001752 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001753 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001754 * The string is not modified if the operation fails. See NewExternal for
1755 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001756 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001757 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758
v8.team.kasperl727e9952008-09-02 14:56:44 +00001759 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001760 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001761 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001762 * resource will be disposed by calling its Dispose method. The caller of
1763 * this function should not otherwise delete or modify the resource. Neither
1764 * should the underlying buffer be deallocated or modified except through the
1765 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001766 */
1767 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001768
ager@chromium.org6f10e412009-02-13 10:11:16 +00001769 /**
1770 * Associate an external string resource with this string by transforming it
1771 * in place so that existing references to this string in the JavaScript heap
1772 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001773 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001774 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001775 * The string is not modified if the operation fails. See NewExternal for
1776 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001777 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001778 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001780 /**
1781 * Returns true if this string can be made external.
1782 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001783 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001784
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001785 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001786 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001787 V8_INLINE(
1788 static Local<String> NewUndetectable(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001790 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001791 /** Creates an undetectable string from the supplied 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001792 V8_INLINE(static Local<String> NewUndetectable(
1793 const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794
1795 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001796 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001797 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001798 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001799 * then the length() method returns 0 and the * operator returns
1800 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001801 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001802 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001803 public:
1804 explicit Utf8Value(Handle<v8::Value> obj);
1805 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001806 char* operator*() { return str_; }
1807 const char* operator*() const { return str_; }
1808 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001809 private:
1810 char* str_;
1811 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001812
1813 // Disallow copying and assigning.
1814 Utf8Value(const Utf8Value&);
1815 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001816 };
1817
1818 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001819 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001821 * If conversion to a string fails (eg. due to an exception in the toString()
1822 * method of the object) then the length() method returns 0 and the * operator
1823 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001825 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826 public:
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001827 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 explicit AsciiValue(Handle<v8::Value> obj);
1829 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001830 char* operator*() { return str_; }
1831 const char* operator*() const { return str_; }
1832 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833 private:
1834 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001835 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001836
1837 // Disallow copying and assigning.
1838 AsciiValue(const AsciiValue&);
1839 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 };
1841
1842 /**
1843 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001844 * If conversion to a string fails (eg. due to an exception in the toString()
1845 * method of the object) then the length() method returns 0 and the * operator
1846 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001848 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 public:
1850 explicit Value(Handle<v8::Value> obj);
1851 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001852 uint16_t* operator*() { return str_; }
1853 const uint16_t* operator*() const { return str_; }
1854 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 private:
1856 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001857 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001858
1859 // Disallow copying and assigning.
1860 Value(const Value&);
1861 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001863
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001864 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001865 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1866 Encoding encoding) const;
1867 void VerifyExternalStringResource(ExternalStringResource* val) const;
1868 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869};
1870
1871
1872/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001873 * A JavaScript symbol (ECMA-262 edition 6)
1874 *
1875 * This is an experimental feature. Use at your own risk.
1876 */
1877class V8EXPORT Symbol : public Primitive {
1878 public:
1879 // Returns the print name string of the symbol, or undefined if none.
1880 Local<Value> Name() const;
1881
1882 // Create a symbol without a print name.
1883 static Local<Symbol> New(Isolate* isolate);
1884
1885 // Create a symbol with a print name.
1886 static Local<Symbol> New(Isolate *isolate, const char* data, int length = -1);
1887
1888 V8_INLINE(static Symbol* Cast(v8::Value* obj));
1889 private:
1890 Symbol();
1891 static void CheckCast(v8::Value* obj);
1892};
1893
1894
1895/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001896 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001898class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001900 double Value() const;
1901 static Local<Number> New(double value);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001902 static Local<Number> New(Isolate* isolate, double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001903 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001905 Number();
1906 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907};
1908
1909
1910/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001911 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001913class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001915 static Local<Integer> New(int32_t value);
1916 static Local<Integer> NewFromUnsigned(uint32_t value);
1917 static Local<Integer> New(int32_t value, Isolate*);
1918 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1919 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001920 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001922 Integer();
1923 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924};
1925
1926
1927/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001928 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001930class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001932 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001934 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935};
1936
1937
1938/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001939 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001941class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001943 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001945 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946};
1947
1948
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949enum PropertyAttribute {
1950 None = 0,
1951 ReadOnly = 1 << 0,
1952 DontEnum = 1 << 1,
1953 DontDelete = 1 << 2
1954};
1955
ager@chromium.org3811b432009-10-28 14:53:37 +00001956enum ExternalArrayType {
1957 kExternalByteArray = 1,
1958 kExternalUnsignedByteArray,
1959 kExternalShortArray,
1960 kExternalUnsignedShortArray,
1961 kExternalIntArray,
1962 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001963 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001964 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001965 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001966};
1967
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001969 * Accessor[Getter|Setter] are used as callback functions when
1970 * setting|getting a particular property. See Object and ObjectTemplate's
1971 * method SetAccessor.
1972 */
1973typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1974 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001975typedef void (*AccessorGetterCallback)(
1976 Local<String> property,
1977 const PropertyCallbackInfo<Value>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001978
1979
1980typedef void (*AccessorSetter)(Local<String> property,
1981 Local<Value> value,
1982 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001983typedef void (*AccessorSetterCallback)(
1984 Local<String> property,
1985 Local<Value> value,
1986 const PropertyCallbackInfo<void>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001987
1988
1989/**
1990 * Access control specifications.
1991 *
1992 * Some accessors should be accessible across contexts. These
1993 * accessors have an explicit access control parameter which specifies
1994 * the kind of cross-context access that should be allowed.
1995 *
1996 * Additionally, for security, accessors can prohibit overwriting by
1997 * accessors defined in JavaScript. For objects that have such
1998 * accessors either locally or in their prototype chain it is not
1999 * possible to overwrite the accessor by using __defineGetter__ or
2000 * __defineSetter__ from JavaScript code.
2001 */
2002enum AccessControl {
2003 DEFAULT = 0,
2004 ALL_CAN_READ = 1,
2005 ALL_CAN_WRITE = 1 << 1,
2006 PROHIBITS_OVERWRITING = 1 << 2
2007};
2008
2009
2010/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002011 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002013class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002015 bool Set(Handle<Value> key,
2016 Handle<Value> value,
2017 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002018
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002019 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002020
ager@chromium.orge2902be2009-06-08 12:21:35 +00002021 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002022 // overriding accessors or read-only properties.
2023 //
2024 // Note that if the object has an interceptor the property will be set
2025 // locally, but since the interceptor takes precedence the local property
2026 // will only be returned if the interceptor doesn't return a value.
2027 //
2028 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002029 bool ForceSet(Handle<Value> key,
2030 Handle<Value> value,
2031 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002032
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002033 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002035 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002036
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002037 /**
2038 * Gets the property attributes of a property which can be None or
2039 * any combination of ReadOnly, DontEnum and DontDelete. Returns
2040 * None when the property doesn't exist.
2041 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002042 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002043
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002044 bool Has(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002045
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002046 bool Delete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002047
2048 // Delete a property on this object bypassing interceptors and
2049 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002050 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002051
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002052 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002053
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002054 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002056 // TODO(dcarney): deprecate
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002057 bool SetAccessor(Handle<String> name,
2058 AccessorGetter getter,
2059 AccessorSetter setter = 0,
2060 Handle<Value> data = Handle<Value>(),
2061 AccessControl settings = DEFAULT,
2062 PropertyAttribute attribute = None);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002063 bool SetAccessor(Handle<String> name,
2064 AccessorGetterCallback getter,
2065 AccessorSetterCallback setter = 0,
2066 Handle<Value> data = Handle<Value>(),
2067 AccessControl settings = DEFAULT,
2068 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002069
ulan@chromium.org750145a2013-03-07 15:14:13 +00002070 // This function is not yet stable and should not be used at this time.
2071 bool SetAccessor(Handle<String> name,
2072 Handle<DeclaredAccessorDescriptor> descriptor,
2073 AccessControl settings = DEFAULT,
2074 PropertyAttribute attribute = None);
2075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002077 * Returns an array containing the names of the enumerable properties
2078 * of this object, including properties from prototype objects. The
2079 * array returned by this method contains the same values as would
2080 * be enumerated by a for-in statement over this object.
2081 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002082 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002083
2084 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002085 * This function has the same functionality as GetPropertyNames but
2086 * the returned array doesn't contain the names of properties from
2087 * prototype objects.
2088 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002089 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002090
2091 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 * Get the prototype object. This does not skip objects marked to
2093 * be skipped by __proto__ and it does not consult the security
2094 * handler.
2095 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002096 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097
2098 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002099 * Set the prototype object. This does not skip objects marked to
2100 * be skipped by __proto__ and it does not consult the security
2101 * handler.
2102 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002103 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00002104
2105 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002106 * Finds an instance of the given function template in the prototype
2107 * chain.
2108 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002109 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002110
2111 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112 * Call builtin Object.prototype.toString on this object.
2113 * This is different from Value::ToString() that may call
2114 * user-defined toString function. This one does not.
2115 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002116 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002118 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002119 * Returns the function invoked as a constructor for this object.
2120 * May be the null value.
2121 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002122 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002123
2124 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002125 * Returns the name of the function invoked as a constructor for this object.
2126 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002127 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002128
kasper.lund212ac232008-07-16 07:07:30 +00002129 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002130 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002131
2132 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002133 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002134
kasper.lund212ac232008-07-16 07:07:30 +00002135 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002136 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002137
2138 /**
2139 * Gets a 2-byte-aligned native pointer from an internal field. This field
2140 * must have been set by SetAlignedPointerInInternalField, everything else
2141 * leads to undefined behavior.
2142 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002143 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002144
2145 /**
2146 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2147 * a field, GetAlignedPointerFromInternalField must be used, everything else
2148 * leads to undefined behavior.
2149 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002150 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002151
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002153 bool HasOwnProperty(Handle<String> key);
2154 bool HasRealNamedProperty(Handle<String> key);
2155 bool HasRealIndexedProperty(uint32_t index);
2156 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157
2158 /**
2159 * If result.IsEmpty() no real property was located in the prototype chain.
2160 * This means interceptors in the prototype chain are not called.
2161 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002162 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00002163
2164 /**
2165 * If result.IsEmpty() no real property was located on the object or
2166 * in the prototype chain.
2167 * This means interceptors in the prototype chain are not called.
2168 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002169 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170
2171 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002172 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173
kasper.lund212ac232008-07-16 07:07:30 +00002174 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002175 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002177 /**
2178 * Turns on access check on the object if the object is an instance of
2179 * a template that has access check callbacks. If an object has no
2180 * access check info, the object cannot be accessed by anyone.
2181 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002182 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00002183
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002184 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002185 * Returns the identity hash for this object. The current implementation
2186 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002187 *
ager@chromium.org9085a012009-05-11 19:22:57 +00002188 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00002189 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002190 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002191 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00002192
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002193 /**
2194 * Access hidden properties on JavaScript objects. These properties are
2195 * hidden from the executing JavaScript and only accessible through the V8
2196 * C++ API. Hidden properties introduced by V8 internally (for example the
2197 * identity hash) are prefixed with "v8::".
2198 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002199 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
2200 Local<Value> GetHiddenValue(Handle<String> key);
2201 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002202
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002203 /**
2204 * Returns true if this is an instance of an api function (one
2205 * created from a function created from a function template) and has
2206 * been modified since it was created. Note that this method is
2207 * conservative and may return true for objects that haven't actually
2208 * been modified.
2209 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002210 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002211
2212 /**
2213 * Clone this object with a fast but shallow copy. Values will point
2214 * to the same values as the original object.
2215 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002216 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002218 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002219 * Returns the context in which the object was created.
2220 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002221 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002222
2223 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002224 * Set the backing store of the indexed properties to be managed by the
2225 * embedding layer. Access to the indexed properties will follow the rules
2226 * spelled out in CanvasPixelArray.
2227 * Note: The embedding program still owns the data and needs to ensure that
2228 * the backing store is preserved while V8 has a reference.
2229 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002230 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
2231 bool HasIndexedPropertiesInPixelData();
2232 uint8_t* GetIndexedPropertiesPixelData();
2233 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002234
ager@chromium.org3811b432009-10-28 14:53:37 +00002235 /**
2236 * Set the backing store of the indexed properties to be managed by the
2237 * embedding layer. Access to the indexed properties will follow the rules
2238 * spelled out for the CanvasArray subtypes in the WebGL specification.
2239 * Note: The embedding program still owns the data and needs to ensure that
2240 * the backing store is preserved while V8 has a reference.
2241 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002242 void SetIndexedPropertiesToExternalArrayData(void* data,
2243 ExternalArrayType array_type,
2244 int number_of_elements);
2245 bool HasIndexedPropertiesInExternalArrayData();
2246 void* GetIndexedPropertiesExternalArrayData();
2247 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
2248 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00002249
lrn@chromium.org1c092762011-05-09 09:42:16 +00002250 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002251 * Checks whether a callback is set by the
2252 * ObjectTemplate::SetCallAsFunctionHandler method.
2253 * When an Object is callable this method returns true.
2254 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002255 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002256
2257 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002258 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002259 * ObjectTemplate::SetCallAsFunctionHandler method.
2260 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002261 Local<Value> CallAsFunction(Handle<Object> recv,
2262 int argc,
2263 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002264
2265 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002266 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002267 * ObjectTemplate::SetCallAsFunctionHandler method.
2268 * Note: This method behaves like the Function::NewInstance method.
2269 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002270 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002271
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002272 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002273 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002276 Object();
2277 static void CheckCast(Value* obj);
2278 Local<Value> SlowGetInternalField(int index);
2279 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002280};
2281
2282
2283/**
2284 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
2285 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002286class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002287 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002288 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289
ager@chromium.org3e875802009-06-29 08:26:34 +00002290 /**
2291 * Clones an element at index |index|. Returns an empty
2292 * handle if cloning fails (for any reason).
2293 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002294 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00002295
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002296 /**
2297 * Creates a JavaScript array with the given length. If the length
2298 * is negative the returned array will have length 0.
2299 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002300 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002301
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002302 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002304 Array();
2305 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306};
2307
2308
2309/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002310 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002312class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002314 Local<Object> NewInstance() const;
2315 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
2316 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
2317 void SetName(Handle<String> name);
2318 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00002319
2320 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002321 * Name inferred from variable or property assignment of this function.
2322 * Used to facilitate debugging and profiling of JavaScript code written
2323 * in an OO style, where many functions are anonymous but are assigned
2324 * to object properties.
2325 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002326 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002327
2328 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002329 * Returns zero based line number of function body and
2330 * kLineOffsetNotFound if no information available.
2331 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002332 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002333 /**
2334 * Returns zero based column number of function body and
2335 * kLineOffsetNotFound if no information available.
2336 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002337 int GetScriptColumnNumber() const;
2338 Handle<Value> GetScriptId() const;
2339 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002340 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002341 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002342
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002344 Function();
2345 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346};
2347
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002348#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
2349#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
2350#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351
2352/**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002353 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
2354 * This API is experimental and may change significantly.
2355 */
2356class V8EXPORT ArrayBuffer : public Object {
2357 public:
2358 /**
ulan@chromium.org837a67e2013-06-11 15:39:48 +00002359 * Allocator that V8 uses to allocate |ArrayBuffer|'s memory.
2360 * The allocator is a global V8 setting. It should be set with
2361 * V8::SetArrayBufferAllocator prior to creation of a first ArrayBuffer.
2362 *
2363 * This API is experimental and may change significantly.
2364 */
2365 class V8EXPORT Allocator { // NOLINT
2366 public:
2367 virtual ~Allocator() {}
2368
2369 /**
2370 * Allocate |length| bytes. Return NULL if allocation is not successful.
2371 */
2372 virtual void* Allocate(size_t length) = 0;
2373 /**
2374 * Free the memory pointed to |data|. That memory is guaranteed to be
2375 * previously allocated by |Allocate|.
2376 */
2377 virtual void Free(void* data) = 0;
2378 };
2379
2380 /**
2381 * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
2382 * returns an instance of this class, populated, with a pointer to data
2383 * and byte length.
2384 *
2385 * The Data pointer of ArrayBuffer::Contents is always allocated with
2386 * Allocator::Allocate that is set with V8::SetArrayBufferAllocator.
2387 *
2388 * This API is experimental and may change significantly.
2389 */
2390 class V8EXPORT Contents { // NOLINT
2391 public:
2392 Contents() : data_(NULL), byte_length_(0) {}
2393
2394 void* Data() const { return data_; }
2395 size_t ByteLength() const { return byte_length_; }
2396
2397 private:
2398 void* data_;
2399 size_t byte_length_;
2400
2401 friend class ArrayBuffer;
2402 };
2403
2404
2405 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002406 * Data length in bytes.
2407 */
2408 size_t ByteLength() const;
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002409
2410 /**
2411 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
2412 * Allocated memory will be owned by a created ArrayBuffer and
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002413 * will be deallocated when it is garbage-collected,
2414 * unless the object is externalized.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002415 */
2416 static Local<ArrayBuffer> New(size_t byte_length);
2417
2418 /**
2419 * Create a new ArrayBuffer over an existing memory block.
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002420 * The created array buffer is immediately in externalized state.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002421 * The memory block will not be reclaimed when a created ArrayBuffer
2422 * is garbage-collected.
2423 */
2424 static Local<ArrayBuffer> New(void* data, size_t byte_length);
2425
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002426 /**
2427 * Returns true if ArrayBuffer is extrenalized, that is, does not
2428 * own its memory block.
2429 */
2430 bool IsExternal() const;
2431
2432 /**
danno@chromium.org1fd77d52013-06-07 16:01:45 +00002433 * Neuters this ArrayBuffer and all its views (typed arrays).
2434 * Neutering sets the byte length of the buffer and all typed arrays to zero,
2435 * preventing JavaScript from ever accessing underlying backing store.
2436 * ArrayBuffer should have been externalized.
2437 */
2438 void Neuter();
2439
2440 /**
ulan@chromium.org837a67e2013-06-11 15:39:48 +00002441 * Make this ArrayBuffer external. The pointer to underlying memory block
2442 * and byte length are returned as |Contents| structure. After ArrayBuffer
2443 * had been etxrenalized, it does no longer owns the memory block. The caller
2444 * should take steps to free memory when it is no longer needed.
2445 *
2446 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
2447 * that has been set with V8::SetArrayBufferAllocator.
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002448 */
ulan@chromium.org837a67e2013-06-11 15:39:48 +00002449 Contents Externalize();
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002450
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002451 V8_INLINE(static ArrayBuffer* Cast(Value* obj));
2452
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002453 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
2454
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002455 private:
2456 ArrayBuffer();
2457 static void CheckCast(Value* obj);
2458};
2459
2460
2461/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002462 * A base class for an instance of TypedArray series of constructors
2463 * (ES6 draft 15.13.6).
2464 * This API is experimental and may change significantly.
2465 */
2466class V8EXPORT TypedArray : public Object {
2467 public:
2468 /**
2469 * Returns underlying ArrayBuffer.
2470 */
2471 Local<ArrayBuffer> Buffer();
2472 /**
2473 * Byte offset in |Buffer|
2474 */
2475 size_t ByteOffset();
2476 /**
2477 * Numbe of elements in this typed array.
2478 */
2479 size_t Length();
2480 /**
2481 * Size of typed array in bytes (e.g. for Int16Array, 2*|Length|).
2482 */
2483 size_t ByteLength();
2484 /**
2485 * Base address of typed array.
2486 */
2487 void* BaseAddress();
2488
2489 V8_INLINE(static TypedArray* Cast(Value* obj));
2490
2491 private:
2492 TypedArray();
2493 static void CheckCast(Value* obj);
2494};
2495
2496
2497/**
2498 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
2499 * This API is experimental and may change significantly.
2500 */
2501class V8EXPORT Uint8Array : public TypedArray {
2502 public:
2503 static Local<Uint8Array> New(Handle<ArrayBuffer> array_buffer,
2504 size_t byte_offset, size_t length);
2505 V8_INLINE(static Uint8Array* Cast(Value* obj));
2506
2507 private:
2508 Uint8Array();
2509 static void CheckCast(Value* obj);
2510};
2511
2512
2513/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002514 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
2515 * This API is experimental and may change significantly.
2516 */
2517class V8EXPORT Uint8ClampedArray : public TypedArray {
2518 public:
2519 static Local<Uint8ClampedArray> New(Handle<ArrayBuffer> array_buffer,
2520 size_t byte_offset, size_t length);
2521 V8_INLINE(static Uint8ClampedArray* Cast(Value* obj));
2522
2523 private:
2524 Uint8ClampedArray();
2525 static void CheckCast(Value* obj);
2526};
2527
2528/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002529 * An instance of Int8Array constructor (ES6 draft 15.13.6).
2530 * This API is experimental and may change significantly.
2531 */
2532class V8EXPORT Int8Array : public TypedArray {
2533 public:
2534 static Local<Int8Array> New(Handle<ArrayBuffer> array_buffer,
2535 size_t byte_offset, size_t length);
2536 V8_INLINE(static Int8Array* Cast(Value* obj));
2537
2538 private:
2539 Int8Array();
2540 static void CheckCast(Value* obj);
2541};
2542
2543
2544/**
2545 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
2546 * This API is experimental and may change significantly.
2547 */
2548class V8EXPORT Uint16Array : public TypedArray {
2549 public:
2550 static Local<Uint16Array> New(Handle<ArrayBuffer> array_buffer,
2551 size_t byte_offset, size_t length);
2552 V8_INLINE(static Uint16Array* Cast(Value* obj));
2553
2554 private:
2555 Uint16Array();
2556 static void CheckCast(Value* obj);
2557};
2558
2559
2560/**
2561 * An instance of Int16Array constructor (ES6 draft 15.13.6).
2562 * This API is experimental and may change significantly.
2563 */
2564class V8EXPORT Int16Array : public TypedArray {
2565 public:
2566 static Local<Int16Array> New(Handle<ArrayBuffer> array_buffer,
2567 size_t byte_offset, size_t length);
2568 V8_INLINE(static Int16Array* Cast(Value* obj));
2569
2570 private:
2571 Int16Array();
2572 static void CheckCast(Value* obj);
2573};
2574
2575
2576/**
2577 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
2578 * This API is experimental and may change significantly.
2579 */
2580class V8EXPORT Uint32Array : public TypedArray {
2581 public:
2582 static Local<Uint32Array> New(Handle<ArrayBuffer> array_buffer,
2583 size_t byte_offset, size_t length);
2584 V8_INLINE(static Uint32Array* Cast(Value* obj));
2585
2586 private:
2587 Uint32Array();
2588 static void CheckCast(Value* obj);
2589};
2590
2591
2592/**
2593 * An instance of Int32Array constructor (ES6 draft 15.13.6).
2594 * This API is experimental and may change significantly.
2595 */
2596class V8EXPORT Int32Array : public TypedArray {
2597 public:
2598 static Local<Int32Array> New(Handle<ArrayBuffer> array_buffer,
2599 size_t byte_offset, size_t length);
2600 V8_INLINE(static Int32Array* Cast(Value* obj));
2601
2602 private:
2603 Int32Array();
2604 static void CheckCast(Value* obj);
2605};
2606
2607
2608/**
2609 * An instance of Float32Array constructor (ES6 draft 15.13.6).
2610 * This API is experimental and may change significantly.
2611 */
2612class V8EXPORT Float32Array : public TypedArray {
2613 public:
2614 static Local<Float32Array> New(Handle<ArrayBuffer> array_buffer,
2615 size_t byte_offset, size_t length);
2616 V8_INLINE(static Float32Array* Cast(Value* obj));
2617
2618 private:
2619 Float32Array();
2620 static void CheckCast(Value* obj);
2621};
2622
2623
2624/**
2625 * An instance of Float64Array constructor (ES6 draft 15.13.6).
2626 * This API is experimental and may change significantly.
2627 */
2628class V8EXPORT Float64Array : public TypedArray {
2629 public:
2630 static Local<Float64Array> New(Handle<ArrayBuffer> array_buffer,
2631 size_t byte_offset, size_t length);
2632 V8_INLINE(static Float64Array* Cast(Value* obj));
2633
2634 private:
2635 Float64Array();
2636 static void CheckCast(Value* obj);
2637};
2638
2639
2640/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002641 * An instance of the built-in Date constructor (ECMA-262, 15.9).
2642 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002643class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002644 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002645 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002646
2647 /**
2648 * A specialization of Value::NumberValue that is more efficient
2649 * because we know the structure of this object.
2650 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002651 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002652
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002653 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002654
2655 /**
2656 * Notification that the embedder has changed the time zone,
2657 * daylight savings time, or other date / time configuration
2658 * parameters. V8 keeps a cache of various values used for
2659 * date / time computation. This notification will reset
2660 * those cached values for the current context so that date /
2661 * time configuration changes would be reflected in the Date
2662 * object.
2663 *
2664 * This API should not be called more than needed as it will
2665 * negatively impact the performance of date operations.
2666 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002667 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002668
2669 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002670 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002671};
2672
2673
2674/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002675 * A Number object (ECMA-262, 4.3.21).
2676 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002677class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002678 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002679 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002680
2681 /**
2682 * Returns the Number held by the object.
2683 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002684 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002685
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002686 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002687
2688 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002689 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002690};
2691
2692
2693/**
2694 * A Boolean object (ECMA-262, 4.3.15).
2695 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002696class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002697 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002698 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002699
2700 /**
2701 * Returns the Boolean held by the object.
2702 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002703 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002704
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002705 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002706
2707 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002708 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002709};
2710
2711
2712/**
2713 * A String object (ECMA-262, 4.3.18).
2714 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002715class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002716 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002717 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002718
2719 /**
2720 * Returns the String held by the object.
2721 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002722 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002723
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002724 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002725
2726 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002727 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002728};
2729
2730
2731/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002732 * A Symbol object (ECMA-262 edition 6).
2733 *
2734 * This is an experimental feature. Use at your own risk.
2735 */
2736class V8EXPORT SymbolObject : public Object {
2737 public:
2738 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2739
2740 /**
2741 * Returns the Symbol held by the object.
2742 */
2743 Local<Symbol> SymbolValue() const;
2744
2745 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2746
2747 private:
2748 static void CheckCast(v8::Value* obj);
2749};
2750
2751
2752/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002753 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2754 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002755class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002756 public:
2757 /**
2758 * Regular expression flag bits. They can be or'ed to enable a set
2759 * of flags.
2760 */
2761 enum Flags {
2762 kNone = 0,
2763 kGlobal = 1,
2764 kIgnoreCase = 2,
2765 kMultiline = 4
2766 };
2767
2768 /**
2769 * Creates a regular expression from the given pattern string and
2770 * the flags bit field. May throw a JavaScript exception as
2771 * described in ECMA-262, 15.10.4.1.
2772 *
2773 * For example,
2774 * RegExp::New(v8::String::New("foo"),
2775 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2776 * is equivalent to evaluating "/foo/gm".
2777 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002778 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002779
2780 /**
2781 * Returns the value of the source property: a string representing
2782 * the regular expression.
2783 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002784 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002785
2786 /**
2787 * Returns the flags bit field.
2788 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002789 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002790
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002791 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002792
2793 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002794 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002795};
2796
2797
2798/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002799 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2800 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002801 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002802class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002803 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002804 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002805 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002806 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002807 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002808 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809};
2810
2811
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002812// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002813
2814
2815/**
2816 * The superclass of object and function templates.
2817 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002818class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819 public:
2820 /** Adds a property to each instance created by this template.*/
2821 void Set(Handle<String> name, Handle<Data> value,
2822 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002823 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824 private:
2825 Template();
2826
2827 friend class ObjectTemplate;
2828 friend class FunctionTemplate;
2829};
2830
2831
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002832template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002833class ReturnValue {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002834 public:
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00002835 template <class S> V8_INLINE(ReturnValue(const ReturnValue<S>& that))
2836 : value_(that.value_) {
2837 TYPE_CHECK(T, S);
2838 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002839 // Handle setters
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00002840 template <typename S> V8_INLINE(void Set(const Persistent<S>& handle));
2841 template <typename S> V8_INLINE(void Set(const Handle<S> handle));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002842 // Fast primitive setters
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002843 V8_INLINE(void Set(bool value));
2844 V8_INLINE(void Set(double i));
2845 V8_INLINE(void Set(int32_t i));
2846 V8_INLINE(void Set(uint32_t i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002847 // Fast JS primitive setters
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002848 V8_INLINE(void SetNull());
2849 V8_INLINE(void SetUndefined());
ulan@chromium.org837a67e2013-06-11 15:39:48 +00002850 V8_INLINE(void SetEmptyString());
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002851 // Convenience getter for Isolate
2852 V8_INLINE(Isolate* GetIsolate());
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00002853
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002854 private:
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00002855 template<class F> friend class ReturnValue;
2856 template<class F> friend class FunctionCallbackInfo;
2857 template<class F> friend class PropertyCallbackInfo;
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002858 V8_INLINE(internal::Object* GetDefaultValue());
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00002859 V8_INLINE(explicit ReturnValue(internal::Object** slot));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002860 internal::Object** value_;
2861};
2862
2863
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002864/**
2865 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002866 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002867 * including the receiver, the number and values of arguments, and
2868 * the holder of the function.
2869 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002870template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002871class FunctionCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002872 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002873 V8_INLINE(int Length() const);
2874 V8_INLINE(Local<Value> operator[](int i) const);
2875 V8_INLINE(Local<Function> Callee() const);
2876 V8_INLINE(Local<Object> This() const);
2877 V8_INLINE(Local<Object> Holder() const);
2878 V8_INLINE(bool IsConstructCall() const);
2879 V8_INLINE(Local<Value> Data() const);
2880 V8_INLINE(Isolate* GetIsolate() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002881 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2882 // This shouldn't be public, but the arm compiler needs it.
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002883 static const int kArgsLength = 6;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002884
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002885 protected:
2886 friend class internal::FunctionCallbackArguments;
2887 friend class internal::CustomArguments<FunctionCallbackInfo>;
2888 static const int kReturnValueIndex = 0;
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002889 static const int kReturnValueDefaultValueIndex = -1;
2890 static const int kIsolateIndex = -2;
2891 static const int kDataIndex = -3;
2892 static const int kCalleeIndex = -4;
2893 static const int kHolderIndex = -5;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002894
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002895 V8_INLINE(FunctionCallbackInfo(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002896 internal::Object** values,
2897 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002898 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002899 internal::Object** implicit_args_;
2900 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002902 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903};
2904
2905
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002906class V8EXPORT Arguments : public FunctionCallbackInfo<Value> {
2907 private:
2908 friend class internal::FunctionCallbackArguments;
2909 V8_INLINE(Arguments(internal::Object** implicit_args,
2910 internal::Object** values,
2911 int length,
2912 bool is_construct_call));
2913};
2914
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915/**
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002916 * The information passed to a property callback about the context
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917 * of the property access.
2918 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002919template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002920class PropertyCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002921 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002922 V8_INLINE(Isolate* GetIsolate() const);
2923 V8_INLINE(Local<Value> Data() const);
2924 V8_INLINE(Local<Object> This() const);
2925 V8_INLINE(Local<Object> Holder() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002926 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2927 // This shouldn't be public, but the arm compiler needs it.
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002928 static const int kArgsLength = 6;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002929
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002930 protected:
2931 friend class MacroAssembler;
2932 friend class internal::PropertyCallbackArguments;
2933 friend class internal::CustomArguments<PropertyCallbackInfo>;
2934 static const int kThisIndex = 0;
2935 static const int kHolderIndex = -1;
2936 static const int kDataIndex = -2;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002937 static const int kReturnValueIndex = -3;
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00002938 static const int kReturnValueDefaultValueIndex = -4;
2939 static const int kIsolateIndex = -5;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002940
2941 V8_INLINE(PropertyCallbackInfo(internal::Object** args))
2942 : args_(args) { }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002943 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944};
2945
2946
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002947class V8EXPORT AccessorInfo : public PropertyCallbackInfo<Value> {
2948 private:
2949 friend class internal::PropertyCallbackArguments;
2950 V8_INLINE(AccessorInfo(internal::Object** args))
2951 : PropertyCallbackInfo<Value>(args) { }
2952};
2953
2954
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002956typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959 * NamedProperty[Getter|Setter] are used as interceptors on object.
2960 * See ObjectTemplate::SetNamedPropertyHandler.
2961 */
2962typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2963 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002964typedef void (*NamedPropertyGetterCallback)(
2965 Local<String> property,
2966 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967
2968
2969/**
2970 * Returns the value if the setter intercepts the request.
2971 * Otherwise, returns an empty handle.
2972 */
2973typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2974 Local<Value> value,
2975 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002976typedef void (*NamedPropertySetterCallback)(
2977 Local<String> property,
2978 Local<Value> value,
2979 const PropertyCallbackInfo<Value>& info);
2980
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982/**
2983 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002984 * The result is an integer encoding property attributes (like v8::None,
2985 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002986 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002987typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2988 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002989typedef void (*NamedPropertyQueryCallback)(
2990 Local<String> property,
2991 const PropertyCallbackInfo<Integer>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002992
2993
2994/**
2995 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002996 * The return value is true if the property could be deleted and false
2997 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002998 */
2999typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
3000 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003001typedef void (*NamedPropertyDeleterCallback)(
3002 Local<String> property,
3003 const PropertyCallbackInfo<Boolean>& info);
3004
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003005
3006/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003007 * Returns an array containing the names of the properties the named
3008 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003009 */
3010typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003011typedef void (*NamedPropertyEnumeratorCallback)(
3012 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003013
v8.team.kasperl727e9952008-09-02 14:56:44 +00003014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003016 * Returns the value of the property if the getter intercepts the
3017 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003018 */
3019typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
3020 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003021typedef void (*IndexedPropertyGetterCallback)(
3022 uint32_t index,
3023 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003024
3025
3026/**
3027 * Returns the value if the setter intercepts the request.
3028 * Otherwise, returns an empty handle.
3029 */
3030typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
3031 Local<Value> value,
3032 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003033typedef void (*IndexedPropertySetterCallback)(
3034 uint32_t index,
3035 Local<Value> value,
3036 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003037
3038
3039/**
3040 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003041 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003042 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003043typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
3044 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003045typedef void (*IndexedPropertyQueryCallback)(
3046 uint32_t index,
3047 const PropertyCallbackInfo<Integer>& info);
3048
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003049
3050/**
3051 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003052 * The return value is true if the property could be deleted and false
3053 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003054 */
3055typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
3056 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003057typedef void (*IndexedPropertyDeleterCallback)(
3058 uint32_t index,
3059 const PropertyCallbackInfo<Boolean>& info);
3060
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061
v8.team.kasperl727e9952008-09-02 14:56:44 +00003062/**
3063 * Returns an array containing the indices of the properties the
3064 * indexed property getter intercepts.
3065 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003066typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003067typedef void (*IndexedPropertyEnumeratorCallback)(
3068 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069
3070
3071/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003072 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003073 */
3074enum AccessType {
3075 ACCESS_GET,
3076 ACCESS_SET,
3077 ACCESS_HAS,
3078 ACCESS_DELETE,
3079 ACCESS_KEYS
3080};
3081
v8.team.kasperl727e9952008-09-02 14:56:44 +00003082
3083/**
3084 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003085 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003086 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003087typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088 Local<Value> key,
3089 AccessType type,
3090 Local<Value> data);
3091
v8.team.kasperl727e9952008-09-02 14:56:44 +00003092
3093/**
3094 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003095 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003096 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003097typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003098 uint32_t index,
3099 AccessType type,
3100 Local<Value> data);
3101
3102
3103/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003104 * A FunctionTemplate is used to create functions at runtime. There
3105 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003106 * context. The lifetime of the created function is equal to the
3107 * lifetime of the context. So in case the embedder needs to create
3108 * temporary functions that can be collected using Scripts is
3109 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003110 *
3111 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003112 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003113 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003114 * A FunctionTemplate has a corresponding instance template which is
3115 * used to create object instances when the function is used as a
3116 * constructor. Properties added to the instance template are added to
3117 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003118 *
3119 * A FunctionTemplate can have a prototype template. The prototype template
3120 * is used to create the prototype object of the function.
3121 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003122 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003124 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003125 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
3126 * t->Set("func_property", v8::Number::New(1));
3127 *
3128 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
3129 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
3130 * proto_t->Set("proto_const", v8::Number::New(2));
3131 *
3132 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
3133 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
3134 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
3135 * instance_t->Set("instance_property", Number::New(3));
3136 *
3137 * v8::Local<v8::Function> function = t->GetFunction();
3138 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003139 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003140 *
3141 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00003142 * and "instance" for the instance object created above. The function
3143 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003145 * \code
3146 * func_property in function == true;
3147 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003148 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003149 * function.prototype.proto_method() invokes 'InvokeCallback'
3150 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003151 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003152 * instance instanceof function == true;
3153 * instance.instance_accessor calls 'InstanceAccessorCallback'
3154 * instance.instance_property == 3;
3155 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003156 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003157 * A FunctionTemplate can inherit from another one by calling the
3158 * FunctionTemplate::Inherit method. The following graph illustrates
3159 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003160 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003161 * \code
3162 * FunctionTemplate Parent -> Parent() . prototype -> { }
3163 * ^ ^
3164 * | Inherit(Parent) | .__proto__
3165 * | |
3166 * FunctionTemplate Child -> Child() . prototype -> { }
3167 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003168 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003169 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
3170 * object of the Child() function has __proto__ pointing to the
3171 * Parent() function's prototype object. An instance of the Child
3172 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003173 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003174 * Let Parent be the FunctionTemplate initialized in the previous
3175 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003176 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003177 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178 * Local<FunctionTemplate> parent = t;
3179 * Local<FunctionTemplate> child = FunctionTemplate::New();
3180 * child->Inherit(parent);
3181 *
3182 * Local<Function> child_function = child->GetFunction();
3183 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003184 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003185 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003186 * The Child function and Child instance will have the following
3187 * properties:
3188 *
3189 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003190 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003191 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003192 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003193 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003194 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003195class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003196 public:
3197 /** Creates a function template.*/
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003198 // TODO(dcarney): deprecate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003199 static Local<FunctionTemplate> New(
3200 InvocationCallback callback = 0,
3201 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003202 Handle<Signature> signature = Handle<Signature>(),
3203 int length = 0);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003204 static Local<FunctionTemplate> New(
3205 FunctionCallback callback, // TODO(dcarney): add back default param.
3206 Handle<Value> data = Handle<Value>(),
3207 Handle<Signature> signature = Handle<Signature>(),
3208 int length = 0);
3209
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003210 /** Returns the unique function instance in the current execution context.*/
3211 Local<Function> GetFunction();
3212
v8.team.kasperl727e9952008-09-02 14:56:44 +00003213 /**
3214 * Set the call-handler callback for a FunctionTemplate. This
3215 * callback is called whenever the function created from this
3216 * FunctionTemplate is called.
3217 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003218 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003219 void SetCallHandler(InvocationCallback callback,
3220 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003221 void SetCallHandler(FunctionCallback callback,
3222 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003223
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003224 /** Set the predefined length property for the FunctionTemplate. */
3225 void SetLength(int length);
3226
v8.team.kasperl727e9952008-09-02 14:56:44 +00003227 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003228 Local<ObjectTemplate> InstanceTemplate();
3229
3230 /** Causes the function template to inherit from a parent function template.*/
3231 void Inherit(Handle<FunctionTemplate> parent);
3232
3233 /**
3234 * A PrototypeTemplate is the template used to create the prototype object
3235 * of the function created by this template.
3236 */
3237 Local<ObjectTemplate> PrototypeTemplate();
3238
v8.team.kasperl727e9952008-09-02 14:56:44 +00003239 /**
3240 * Set the class name of the FunctionTemplate. This is used for
3241 * printing objects created with the function created from the
3242 * FunctionTemplate as its constructor.
3243 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003244 void SetClassName(Handle<String> name);
3245
3246 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003247 * Determines whether the __proto__ accessor ignores instances of
3248 * the function template. If instances of the function template are
3249 * ignored, __proto__ skips all instances and instead returns the
3250 * next object in the prototype chain.
3251 *
3252 * Call with a value of true to make the __proto__ accessor ignore
3253 * instances of the function template. Call with a value of false
3254 * to make the __proto__ accessor not ignore instances of the
3255 * function template. By default, instances of a function template
3256 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257 */
3258 void SetHiddenPrototype(bool value);
3259
3260 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003261 * Sets the ReadOnly flag in the attributes of the 'prototype' property
3262 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00003263 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003264 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00003265
3266 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003267 * Returns true if the given object is an instance of this function
3268 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003269 */
3270 bool HasInstance(Handle<Value> object);
3271
3272 private:
3273 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274 friend class Context;
3275 friend class ObjectTemplate;
3276};
3277
3278
3279/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003280 * An ObjectTemplate is used to create objects at runtime.
3281 *
3282 * Properties added to an ObjectTemplate are added to each object
3283 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003284 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003285class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003286 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003287 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003289
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003290 /** Creates a new instance of this template.*/
3291 Local<Object> NewInstance();
3292
3293 /**
3294 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003295 *
3296 * Whenever the property with the given name is accessed on objects
3297 * created from this ObjectTemplate the getter and setter callbacks
3298 * are called instead of getting and setting the property directly
3299 * on the JavaScript object.
3300 *
3301 * \param name The name of the property for which an accessor is added.
3302 * \param getter The callback to invoke when getting the property.
3303 * \param setter The callback to invoke when setting the property.
3304 * \param data A piece of data that will be passed to the getter and setter
3305 * callbacks whenever they are invoked.
3306 * \param settings Access control settings for the accessor. This is a bit
3307 * field consisting of one of more of
3308 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
3309 * The default is to not allow cross-context access.
3310 * ALL_CAN_READ means that all cross-context reads are allowed.
3311 * ALL_CAN_WRITE means that all cross-context writes are allowed.
3312 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
3313 * cross-context access.
3314 * \param attribute The attributes of the property for which an accessor
3315 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003316 * \param signature The signature describes valid receivers for the accessor
3317 * and is used to perform implicit instance checks against them. If the
3318 * receiver is incompatible (i.e. is not an instance of the constructor as
3319 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
3320 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003321 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003322 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003323 void SetAccessor(Handle<String> name,
3324 AccessorGetter getter,
3325 AccessorSetter setter = 0,
3326 Handle<Value> data = Handle<Value>(),
3327 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003328 PropertyAttribute attribute = None,
3329 Handle<AccessorSignature> signature =
3330 Handle<AccessorSignature>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003331 void SetAccessor(Handle<String> name,
3332 AccessorGetterCallback getter,
3333 AccessorSetterCallback setter = 0,
3334 Handle<Value> data = Handle<Value>(),
3335 AccessControl settings = DEFAULT,
3336 PropertyAttribute attribute = None,
3337 Handle<AccessorSignature> signature =
3338 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003339
ulan@chromium.org750145a2013-03-07 15:14:13 +00003340 // This function is not yet stable and should not be used at this time.
3341 bool SetAccessor(Handle<String> name,
3342 Handle<DeclaredAccessorDescriptor> descriptor,
3343 AccessControl settings = DEFAULT,
3344 PropertyAttribute attribute = None,
3345 Handle<AccessorSignature> signature =
3346 Handle<AccessorSignature>());
3347
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003348 /**
3349 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003350 *
3351 * Whenever a named property is accessed on objects created from
3352 * this object template, the provided callback is invoked instead of
3353 * accessing the property directly on the JavaScript object.
3354 *
3355 * \param getter The callback to invoke when getting a property.
3356 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003357 * \param query The callback to invoke to check if a property is present,
3358 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003359 * \param deleter The callback to invoke when deleting a property.
3360 * \param enumerator The callback to invoke to enumerate all the named
3361 * properties of an object.
3362 * \param data A piece of data that will be passed to the callbacks
3363 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003364 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003365 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003366 void SetNamedPropertyHandler(NamedPropertyGetter getter,
3367 NamedPropertySetter setter = 0,
3368 NamedPropertyQuery query = 0,
3369 NamedPropertyDeleter deleter = 0,
3370 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003371 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003372 void SetNamedPropertyHandler(
3373 NamedPropertyGetterCallback getter,
3374 NamedPropertySetterCallback setter = 0,
3375 NamedPropertyQueryCallback query = 0,
3376 NamedPropertyDeleterCallback deleter = 0,
3377 NamedPropertyEnumeratorCallback enumerator = 0,
3378 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003379
3380 /**
3381 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003382 *
3383 * Whenever an indexed property is accessed on objects created from
3384 * this object template, the provided callback is invoked instead of
3385 * accessing the property directly on the JavaScript object.
3386 *
3387 * \param getter The callback to invoke when getting a property.
3388 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003389 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003390 * \param deleter The callback to invoke when deleting a property.
3391 * \param enumerator The callback to invoke to enumerate all the indexed
3392 * properties of an object.
3393 * \param data A piece of data that will be passed to the callbacks
3394 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003395 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003396 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003397 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
3398 IndexedPropertySetter setter = 0,
3399 IndexedPropertyQuery query = 0,
3400 IndexedPropertyDeleter deleter = 0,
3401 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003402 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003403 void SetIndexedPropertyHandler(
3404 IndexedPropertyGetterCallback getter,
3405 IndexedPropertySetterCallback setter = 0,
3406 IndexedPropertyQueryCallback query = 0,
3407 IndexedPropertyDeleterCallback deleter = 0,
3408 IndexedPropertyEnumeratorCallback enumerator = 0,
3409 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003410
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003411 /**
3412 * Sets the callback to be used when calling instances created from
3413 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00003414 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003415 * function.
3416 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003417 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003418 void SetCallAsFunctionHandler(InvocationCallback callback,
3419 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003420 void SetCallAsFunctionHandler(FunctionCallback callback,
3421 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003422
v8.team.kasperl727e9952008-09-02 14:56:44 +00003423 /**
3424 * Mark object instances of the template as undetectable.
3425 *
3426 * In many ways, undetectable objects behave as though they are not
3427 * there. They behave like 'undefined' in conditionals and when
3428 * printed. However, properties can be accessed and called as on
3429 * normal objects.
3430 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003431 void MarkAsUndetectable();
3432
v8.team.kasperl727e9952008-09-02 14:56:44 +00003433 /**
3434 * Sets access check callbacks on the object template.
3435 *
3436 * When accessing properties on instances of this object template,
3437 * the access check callback will be called to determine whether or
3438 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003439 * The last parameter specifies whether access checks are turned
3440 * on by default on instances. If access checks are off by default,
3441 * they can be turned on on individual instances by calling
3442 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00003443 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003444 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
3445 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003446 Handle<Value> data = Handle<Value>(),
3447 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003448
kasper.lund212ac232008-07-16 07:07:30 +00003449 /**
3450 * Gets the number of internal fields for objects generated from
3451 * this template.
3452 */
3453 int InternalFieldCount();
3454
3455 /**
3456 * Sets the number of internal fields for objects generated from
3457 * this template.
3458 */
3459 void SetInternalFieldCount(int value);
3460
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003461 private:
3462 ObjectTemplate();
3463 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
3464 friend class FunctionTemplate;
3465};
3466
3467
3468/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003469 * A Signature specifies which receivers and arguments are valid
3470 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003471 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003472class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473 public:
3474 static Local<Signature> New(Handle<FunctionTemplate> receiver =
3475 Handle<FunctionTemplate>(),
3476 int argc = 0,
3477 Handle<FunctionTemplate> argv[] = 0);
3478 private:
3479 Signature();
3480};
3481
3482
3483/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003484 * An AccessorSignature specifies which receivers are valid parameters
3485 * to an accessor callback.
3486 */
3487class V8EXPORT AccessorSignature : public Data {
3488 public:
3489 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
3490 Handle<FunctionTemplate>());
3491 private:
3492 AccessorSignature();
3493};
3494
3495
ulan@chromium.org750145a2013-03-07 15:14:13 +00003496class V8EXPORT DeclaredAccessorDescriptor : public Data {
3497 private:
3498 DeclaredAccessorDescriptor();
3499};
3500
3501
3502class V8EXPORT ObjectOperationDescriptor : public Data {
3503 public:
3504 // This function is not yet stable and should not be used at this time.
3505 static Local<RawOperationDescriptor> NewInternalFieldDereference(
3506 Isolate* isolate,
3507 int internal_field);
3508 private:
3509 ObjectOperationDescriptor();
3510};
3511
3512
3513enum DeclaredAccessorDescriptorDataType {
3514 kDescriptorBoolType,
3515 kDescriptorInt8Type, kDescriptorUint8Type,
3516 kDescriptorInt16Type, kDescriptorUint16Type,
3517 kDescriptorInt32Type, kDescriptorUint32Type,
3518 kDescriptorFloatType, kDescriptorDoubleType
3519};
3520
3521
3522class V8EXPORT RawOperationDescriptor : public Data {
3523 public:
3524 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
3525 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
3526 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
3527 int16_t byte_offset);
3528 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
3529 void* compare_value);
3530 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
3531 Isolate* isolate,
3532 DeclaredAccessorDescriptorDataType data_type,
3533 uint8_t bool_offset = 0);
3534 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
3535 uint8_t bitmask,
3536 uint8_t compare_value);
3537 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
3538 Isolate* isolate,
3539 uint16_t bitmask,
3540 uint16_t compare_value);
3541 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
3542 Isolate* isolate,
3543 uint32_t bitmask,
3544 uint32_t compare_value);
3545
3546 private:
3547 RawOperationDescriptor();
3548};
3549
3550
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003551/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003552 * A utility for determining the type of objects based on the template
3553 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003554 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003555class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003556 public:
3557 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
3558 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
3559 int match(Handle<Value> value);
3560 private:
3561 TypeSwitch();
3562};
3563
3564
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003565// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003566
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003567class V8EXPORT ExternalAsciiStringResourceImpl
3568 : public String::ExternalAsciiStringResource {
3569 public:
3570 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
3571 ExternalAsciiStringResourceImpl(const char* data, size_t length)
3572 : data_(data), length_(length) {}
3573 const char* data() const { return data_; }
3574 size_t length() const { return length_; }
3575
3576 private:
3577 const char* data_;
3578 size_t length_;
3579};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003580
3581/**
3582 * Ignore
3583 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003584class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003585 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003586 // Note that the strings passed into this constructor must live as long
3587 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003588 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003589 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003590 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003591 const char** deps = 0,
3592 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593 virtual ~Extension() { }
3594 virtual v8::Handle<v8::FunctionTemplate>
3595 GetNativeFunction(v8::Handle<v8::String> name) {
3596 return v8::Handle<v8::FunctionTemplate>();
3597 }
3598
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003599 const char* name() const { return name_; }
3600 size_t source_length() const { return source_length_; }
3601 const String::ExternalAsciiStringResource* source() const {
3602 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 int dependency_count() { return dep_count_; }
3604 const char** dependencies() { return deps_; }
3605 void set_auto_enable(bool value) { auto_enable_ = value; }
3606 bool auto_enable() { return auto_enable_; }
3607
3608 private:
3609 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003610 size_t source_length_; // expected to initialize before source_
3611 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003612 int dep_count_;
3613 const char** deps_;
3614 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003615
3616 // Disallow copying and assigning.
3617 Extension(const Extension&);
3618 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003619};
3620
3621
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003622void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003623
3624
3625/**
3626 * Ignore
3627 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003628class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003629 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003630 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003631 RegisterExtension(extension);
3632 }
3633};
3634
3635
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003636// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003637
3638
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003639Handle<Primitive> V8EXPORT Undefined();
3640Handle<Primitive> V8EXPORT Null();
3641Handle<Boolean> V8EXPORT True();
3642Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003643
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003644V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
3645V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
3646V8_INLINE(Handle<Boolean> True(Isolate* isolate));
3647V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003648
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003649
3650/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003651 * A set of constraints that specifies the limits of the runtime's memory use.
3652 * You must set the heap size before initializing the VM - the size cannot be
3653 * adjusted after the VM is initialized.
3654 *
3655 * If you are using threads then you should hold the V8::Locker lock while
3656 * setting the stack limit and you must set a non-default stack limit separately
3657 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003658 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003659class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660 public:
3661 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003662 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003663 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003664 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003666 int max_executable_size() { return max_executable_size_; }
3667 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003668 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003669 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003670 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
3671 private:
3672 int max_young_space_size_;
3673 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003674 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003675 uint32_t* stack_limit_;
3676};
3677
3678
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003679bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003680
3681
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003682// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003683
3684
3685typedef void (*FatalErrorCallback)(const char* location, const char* message);
3686
3687
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003688typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003689
3690
3691/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003692 * Schedules an exception to be thrown when returning to JavaScript. When an
3693 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003694 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00003695 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003696 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003697Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003698
3699/**
3700 * Create new error objects by calling the corresponding error object
3701 * constructor with the message.
3702 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003703class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003704 public:
3705 static Local<Value> RangeError(Handle<String> message);
3706 static Local<Value> ReferenceError(Handle<String> message);
3707 static Local<Value> SyntaxError(Handle<String> message);
3708 static Local<Value> TypeError(Handle<String> message);
3709 static Local<Value> Error(Handle<String> message);
3710};
3711
3712
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003713// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003714
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003715typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003717typedef void* (*CreateHistogramCallback)(const char* name,
3718 int min,
3719 int max,
3720 size_t buckets);
3721
3722typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
3723
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003724// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003725 enum ObjectSpace {
3726 kObjectSpaceNewSpace = 1 << 0,
3727 kObjectSpaceOldPointerSpace = 1 << 1,
3728 kObjectSpaceOldDataSpace = 1 << 2,
3729 kObjectSpaceCodeSpace = 1 << 3,
3730 kObjectSpaceMapSpace = 1 << 4,
3731 kObjectSpaceLoSpace = 1 << 5,
3732
3733 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
3734 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
3735 kObjectSpaceLoSpace
3736 };
3737
3738 enum AllocationAction {
3739 kAllocationActionAllocate = 1 << 0,
3740 kAllocationActionFree = 1 << 1,
3741 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
3742 };
3743
3744typedef void (*MemoryAllocationCallback)(ObjectSpace space,
3745 AllocationAction action,
3746 int size);
3747
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003748// --- Leave Script Callback ---
3749typedef void (*CallCompletedCallback)();
3750
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003751// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003752typedef void (*FailedAccessCheckCallback)(Local<Object> target,
3753 AccessType type,
3754 Local<Value> data);
3755
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003756// --- AllowCodeGenerationFromStrings callbacks ---
3757
3758/**
3759 * Callback to check if code generation from strings is allowed. See
3760 * Context::AllowCodeGenerationFromStrings.
3761 */
3762typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
3763
3764// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003765
3766/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003767 * Applications can register callback functions which will be called
3768 * before and after a garbage collection. Allocations are not
3769 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003770 * objects (set or delete properties for example) since it is possible
3771 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003773enum GCType {
3774 kGCTypeScavenge = 1 << 0,
3775 kGCTypeMarkSweepCompact = 1 << 1,
3776 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
3777};
3778
3779enum GCCallbackFlags {
3780 kNoGCCallbackFlags = 0,
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003781 kGCCallbackFlagCompacted = 1 << 0,
3782 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003783};
3784
3785typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
3786typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
3787
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003788typedef void (*GCCallback)();
3789
3790
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003791/**
ager@chromium.org3811b432009-10-28 14:53:37 +00003792 * Collection of V8 heap information.
3793 *
3794 * Instances of this class can be passed to v8::V8::HeapStatistics to
3795 * get heap statistics from V8.
3796 */
3797class V8EXPORT HeapStatistics {
3798 public:
3799 HeapStatistics();
3800 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003801 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003802 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003803 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003804 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003805
3806 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003807 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003808 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003809 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003810 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003811 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003812
3813 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003814 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003815};
3816
3817
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003818class RetainedObjectInfo;
3819
ager@chromium.org3811b432009-10-28 14:53:37 +00003820/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003821 * Isolate represents an isolated instance of the V8 engine. V8
3822 * isolates have completely separate states. Objects from one isolate
3823 * must not be used in other isolates. When V8 is initialized a
3824 * default isolate is implicitly created and entered. The embedder
3825 * can create additional isolates and use them in parallel in multiple
3826 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003827 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003828 */
3829class V8EXPORT Isolate {
3830 public:
3831 /**
3832 * Stack-allocated class which sets the isolate for all operations
3833 * executed within a local scope.
3834 */
3835 class V8EXPORT Scope {
3836 public:
3837 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3838 isolate->Enter();
3839 }
3840
3841 ~Scope() { isolate_->Exit(); }
3842
3843 private:
3844 Isolate* const isolate_;
3845
3846 // Prevent copying of Scope objects.
3847 Scope(const Scope&);
3848 Scope& operator=(const Scope&);
3849 };
3850
3851 /**
3852 * Creates a new isolate. Does not change the currently entered
3853 * isolate.
3854 *
3855 * When an isolate is no longer used its resources should be freed
3856 * by calling Dispose(). Using the delete operator is not allowed.
3857 */
3858 static Isolate* New();
3859
3860 /**
3861 * Returns the entered isolate for the current thread or NULL in
3862 * case there is no current isolate.
3863 */
3864 static Isolate* GetCurrent();
3865
3866 /**
3867 * Methods below this point require holding a lock (using Locker) in
3868 * a multi-threaded environment.
3869 */
3870
3871 /**
3872 * Sets this isolate as the entered one for the current thread.
3873 * Saves the previously entered one (if any), so that it can be
3874 * restored when exiting. Re-entering an isolate is allowed.
3875 */
3876 void Enter();
3877
3878 /**
3879 * Exits this isolate by restoring the previously entered one in the
3880 * current thread. The isolate may still stay the same, if it was
3881 * entered more than once.
3882 *
3883 * Requires: this == Isolate::GetCurrent().
3884 */
3885 void Exit();
3886
3887 /**
3888 * Disposes the isolate. The isolate must not be entered by any
3889 * thread to be disposable.
3890 */
3891 void Dispose();
3892
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003893 /**
3894 * Associate embedder-specific data with the isolate
3895 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003896 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003897
3898 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003899 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003900 * Returns NULL if SetData has never been called.
3901 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003902 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003903
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003904 /**
3905 * Get statistics about the heap memory usage.
3906 */
3907 void GetHeapStatistics(HeapStatistics* heap_statistics);
3908
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003909 /**
3910 * Adjusts the amount of registered external memory. Used to give V8 an
3911 * indication of the amount of externally allocated memory that is kept alive
3912 * by JavaScript objects. V8 uses this to decide when to perform global
3913 * garbage collections. Registering externally allocated memory will trigger
3914 * global garbage collections more often than it would otherwise in an attempt
3915 * to garbage collect the JavaScript objects that keep the externally
3916 * allocated memory alive.
3917 *
3918 * \param change_in_bytes the change in externally allocated memory that is
3919 * kept alive by JavaScript objects.
3920 * \returns the adjusted value.
3921 */
3922 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3923
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003924 /**
3925 * Returns heap profiler for this isolate. Will return NULL until the isolate
3926 * is initialized.
3927 */
3928 HeapProfiler* GetHeapProfiler();
3929
3930 /**
3931 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3932 * is initialized.
3933 */
3934 CpuProfiler* GetCpuProfiler();
3935
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003936 /** Returns the context that is on the top of the stack. */
3937 Local<Context> GetCurrentContext();
3938
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003939 /**
3940 * Allows the host application to group objects together. If one
3941 * object in the group is alive, all objects in the group are alive.
3942 * After each garbage collection, object groups are removed. It is
3943 * intended to be used in the before-garbage-collection callback
3944 * function, for instance to simulate DOM tree connections among JS
3945 * wrapper objects. Object groups for all dependent handles need to
3946 * be provided for kGCTypeMarkSweepCompact collections, for all other
3947 * garbage collection types it is sufficient to provide object groups
3948 * for partially dependent handles only.
3949 */
3950 void SetObjectGroupId(const Persistent<Value>& object,
3951 UniqueId id);
3952
3953 /**
3954 * Allows the host application to declare implicit references from an object
3955 * group to an object. If the objects of the object group are alive, the child
3956 * object is alive too. After each garbage collection, all implicit references
3957 * are removed. It is intended to be used in the before-garbage-collection
3958 * callback function.
3959 */
3960 void SetReferenceFromGroup(UniqueId id,
3961 const Persistent<Value>& child);
3962
3963 /**
3964 * Allows the host application to declare implicit references from an object
3965 * to another object. If the parent object is alive, the child object is alive
3966 * too. After each garbage collection, all implicit references are removed. It
3967 * is intended to be used in the before-garbage-collection callback function.
3968 */
3969 void SetReference(const Persistent<Object>& parent,
3970 const Persistent<Value>& child);
3971
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003972 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003973 Isolate();
3974 Isolate(const Isolate&);
3975 ~Isolate();
3976 Isolate& operator=(const Isolate&);
3977 void* operator new(size_t size);
3978 void operator delete(void*, size_t);
3979};
3980
3981
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003982class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003983 public:
3984 enum CompressionAlgorithm {
3985 kUncompressed,
3986 kBZip2
3987 };
3988
3989 const char* data;
3990 int compressed_size;
3991 int raw_size;
3992};
3993
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003994
3995/**
3996 * A helper class for driving V8 startup data decompression. It is based on
3997 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3998 * for an embedder to use this class, instead, API functions can be used
3999 * directly.
4000 *
4001 * For an example of the class usage, see the "shell.cc" sample application.
4002 */
4003class V8EXPORT StartupDataDecompressor { // NOLINT
4004 public:
4005 StartupDataDecompressor();
4006 virtual ~StartupDataDecompressor();
4007 int Decompress();
4008
4009 protected:
4010 virtual int DecompressData(char* raw_data,
4011 int* raw_data_size,
4012 const char* compressed_data,
4013 int compressed_data_size) = 0;
4014
4015 private:
4016 char** raw_data;
4017};
4018
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004019
4020/**
4021 * EntropySource is used as a callback function when v8 needs a source
4022 * of entropy.
4023 */
4024typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
4025
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004026
4027/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004028 * ReturnAddressLocationResolver is used as a callback function when v8 is
4029 * resolving the location of a return address on the stack. Profilers that
4030 * change the return address on the stack can use this to resolve the stack
4031 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004032 *
4033 * \param return_addr_location points to a location on stack where a machine
4034 * return address resides.
4035 * \returns either return_addr_location, or else a pointer to the profiler's
4036 * copy of the original return address.
4037 *
4038 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00004039 */
4040typedef uintptr_t (*ReturnAddressLocationResolver)(
4041 uintptr_t return_addr_location);
4042
4043
4044/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004045 * FunctionEntryHook is the type of the profile entry hook called at entry to
4046 * any generated function when function-level profiling is enabled.
4047 *
4048 * \param function the address of the function that's being entered.
4049 * \param return_addr_location points to a location on stack where the machine
4050 * return address resides. This can be used to identify the caller of
4051 * \p function, and/or modified to divert execution when \p function exits.
4052 *
4053 * \note the entry hook must not cause garbage collection.
4054 */
4055typedef void (*FunctionEntryHook)(uintptr_t function,
4056 uintptr_t return_addr_location);
4057
4058
4059/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004060 * A JIT code event is issued each time code is added, moved or removed.
4061 *
4062 * \note removal events are not currently issued.
4063 */
4064struct JitCodeEvent {
4065 enum EventType {
4066 CODE_ADDED,
4067 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004068 CODE_REMOVED,
4069 CODE_ADD_LINE_POS_INFO,
4070 CODE_START_LINE_INFO_RECORDING,
4071 CODE_END_LINE_INFO_RECORDING
4072 };
4073 // Definition of the code position type. The "POSITION" type means the place
4074 // in the source code which are of interest when making stack traces to
4075 // pin-point the source location of a stack frame as close as possible.
4076 // The "STATEMENT_POSITION" means the place at the beginning of each
4077 // statement, and is used to indicate possible break locations.
4078 enum PositionType {
4079 POSITION,
4080 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004081 };
4082
4083 // Type of event.
4084 EventType type;
4085 // Start of the instructions.
4086 void* code_start;
4087 // Size of the instructions.
4088 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004089 // Script info for CODE_ADDED event.
4090 Handle<Script> script;
4091 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
4092 // code line information which is returned from the
4093 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
4094 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
4095 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004096
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004097 struct name_t {
4098 // Name of the object associated with the code, note that the string is not
4099 // zero-terminated.
4100 const char* str;
4101 // Number of chars in str.
4102 size_t len;
4103 };
4104
4105 struct line_info_t {
4106 // PC offset
4107 size_t offset;
4108 // Code postion
4109 size_t pos;
4110 // The position type.
4111 PositionType position_type;
4112 };
4113
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004114 union {
4115 // Only valid for CODE_ADDED.
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004116 struct name_t name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004117
4118 // Only valid for CODE_ADD_LINE_POS_INFO
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004119 struct line_info_t line_info;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004120
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004121 // New location of instructions. Only valid for CODE_MOVED.
4122 void* new_code_start;
4123 };
4124};
4125
4126/**
4127 * Option flags passed to the SetJitCodeEventHandler function.
4128 */
4129enum JitCodeEventOptions {
4130 kJitCodeEventDefault = 0,
4131 // Generate callbacks for already existent code.
4132 kJitCodeEventEnumExisting = 1
4133};
4134
4135
4136/**
4137 * Callback function passed to SetJitCodeEventHandler.
4138 *
4139 * \param event code add, move or removal event.
4140 */
4141typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
4142
4143
4144/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004145 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004146 */
4147class V8EXPORT ExternalResourceVisitor { // NOLINT
4148 public:
4149 virtual ~ExternalResourceVisitor() {}
4150 virtual void VisitExternalString(Handle<String> string) {}
4151};
4152
4153
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004154/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004155 * Interface for iterating through all the persistent handles in the heap.
4156 */
4157class V8EXPORT PersistentHandleVisitor { // NOLINT
4158 public:
4159 virtual ~PersistentHandleVisitor() {}
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004160 virtual void VisitPersistentHandle(Persistent<Value>* value,
4161 uint16_t class_id) {}
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004162};
4163
4164
4165/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004166 * Asserts that no action is performed that could cause a handle's value
4167 * to be modified. Useful when otherwise unsafe handle operations need to
4168 * be performed.
4169 */
4170class V8EXPORT AssertNoGCScope {
4171#ifndef DEBUG
rossberg@chromium.org79e79022013-06-03 15:43:46 +00004172 // TODO(yangguo): remove isolate argument.
4173 V8_INLINE(AssertNoGCScope(Isolate* isolate)) { }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004174#else
4175 AssertNoGCScope(Isolate* isolate);
4176 ~AssertNoGCScope();
4177 private:
rossberg@chromium.org79e79022013-06-03 15:43:46 +00004178 void* disallow_heap_allocation_;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004179#endif
4180};
4181
4182
4183/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004184 * Container class for static utility functions.
4185 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004186class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004187 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00004188 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004189 static void SetFatalErrorHandler(FatalErrorCallback that);
4190
v8.team.kasperl727e9952008-09-02 14:56:44 +00004191 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004192 * Set the callback to invoke to check if code generation from
4193 * strings should be allowed.
4194 */
4195 static void SetAllowCodeGenerationFromStringsCallback(
4196 AllowCodeGenerationFromStringsCallback that);
4197
4198 /**
ulan@chromium.org837a67e2013-06-11 15:39:48 +00004199 * Set allocator to use for ArrayBuffer memory.
4200 * The allocator should be set only once. The allocator should be set
4201 * before any code tha uses ArrayBuffers is executed.
4202 * This allocator is used in all isolates.
4203 */
4204 static void SetArrayBufferAllocator(ArrayBuffer::Allocator* allocator);
4205
4206 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004207 * Ignore out-of-memory exceptions.
4208 *
4209 * V8 running out of memory is treated as a fatal error by default.
4210 * This means that the fatal error handler is called and that V8 is
4211 * terminated.
4212 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004213 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00004214 * out-of-memory situation as a fatal error. This way, the contexts
4215 * that did not cause the out of memory problem might be able to
4216 * continue execution.
4217 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004218 static void IgnoreOutOfMemoryException();
4219
v8.team.kasperl727e9952008-09-02 14:56:44 +00004220 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004221 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00004222 * fatal errors such as out-of-memory situations.
4223 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004224 static bool IsDead();
4225
4226 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004227 * The following 4 functions are to be used when V8 is built with
4228 * the 'compress_startup_data' flag enabled. In this case, the
4229 * embedder must decompress startup data prior to initializing V8.
4230 *
4231 * This is how interaction with V8 should look like:
4232 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
4233 * v8::StartupData* compressed_data =
4234 * new v8::StartupData[compressed_data_count];
4235 * v8::V8::GetCompressedStartupData(compressed_data);
4236 * ... decompress data (compressed_data can be updated in-place) ...
4237 * v8::V8::SetDecompressedStartupData(compressed_data);
4238 * ... now V8 can be initialized
4239 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004240 *
4241 * A helper class StartupDataDecompressor is provided. It implements
4242 * the protocol of the interaction described above, and can be used in
4243 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004244 */
4245 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
4246 static int GetCompressedStartupDataCount();
4247 static void GetCompressedStartupData(StartupData* compressed_data);
4248 static void SetDecompressedStartupData(StartupData* decompressed_data);
4249
4250 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004251 * Adds a message listener.
4252 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004253 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00004254 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004255 *
4256 * If data is specified, it will be passed to the callback when it is called.
4257 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004258 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004259 static bool AddMessageListener(MessageCallback that,
4260 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004261
4262 /**
4263 * Remove all message listeners from the specified callback function.
4264 */
4265 static void RemoveMessageListeners(MessageCallback that);
4266
4267 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004268 * Tells V8 to capture current stack trace when uncaught exception occurs
4269 * and report it to the message listeners. The option is off by default.
4270 */
4271 static void SetCaptureStackTraceForUncaughtExceptions(
4272 bool capture,
4273 int frame_limit = 10,
4274 StackTrace::StackTraceOptions options = StackTrace::kOverview);
4275
4276 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004277 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004278 */
4279 static void SetFlagsFromString(const char* str, int length);
4280
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004281 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004282 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004283 */
4284 static void SetFlagsFromCommandLine(int* argc,
4285 char** argv,
4286 bool remove_flags);
4287
kasper.lund7276f142008-07-30 08:49:36 +00004288 /** Get the version string. */
4289 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004290
4291 /**
4292 * Enables the host application to provide a mechanism for recording
4293 * statistics counters.
4294 */
4295 static void SetCounterFunction(CounterLookupCallback);
4296
4297 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004298 * Enables the host application to provide a mechanism for recording
4299 * histograms. The CreateHistogram function returns a
4300 * histogram which will later be passed to the AddHistogramSample
4301 * function.
4302 */
4303 static void SetCreateHistogramFunction(CreateHistogramCallback);
4304 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
4305
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004306 /** Callback function for reporting failed access checks.*/
4307 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
4308
4309 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004310 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004311 * garbage collection. Allocations are not allowed in the
4312 * callback function, you therefore cannot manipulate objects (set
4313 * or delete properties for example) since it is possible such
4314 * operations will result in the allocation of objects. It is possible
4315 * to specify the GCType filter for your callback. But it is not possible to
4316 * register the same callback function two times with different
4317 * GCType filters.
4318 */
4319 static void AddGCPrologueCallback(
4320 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
4321
4322 /**
4323 * This function removes callback which was installed by
4324 * AddGCPrologueCallback function.
4325 */
4326 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
4327
4328 /**
4329 * The function is deprecated. Please use AddGCPrologueCallback instead.
4330 * Enables the host application to receive a notification before a
4331 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00004332 * callback function, you therefore cannot manipulate objects (set
4333 * or delete properties for example) since it is possible such
4334 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004335 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004336 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004337
4338 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004339 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004340 * garbage collection. Allocations are not allowed in the
4341 * callback function, you therefore cannot manipulate objects (set
4342 * or delete properties for example) since it is possible such
4343 * operations will result in the allocation of objects. It is possible
4344 * to specify the GCType filter for your callback. But it is not possible to
4345 * register the same callback function two times with different
4346 * GCType filters.
4347 */
4348 static void AddGCEpilogueCallback(
4349 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
4350
4351 /**
4352 * This function removes callback which was installed by
4353 * AddGCEpilogueCallback function.
4354 */
4355 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
4356
4357 /**
4358 * The function is deprecated. Please use AddGCEpilogueCallback instead.
4359 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00004360 * major garbage collection. Allocations are not allowed in the
4361 * callback function, you therefore cannot manipulate objects (set
4362 * or delete properties for example) since it is possible such
4363 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004364 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004365 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004366
4367 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004368 * Enables the host application to provide a mechanism to be notified
4369 * and perform custom logging when V8 Allocates Executable Memory.
4370 */
4371 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
4372 ObjectSpace space,
4373 AllocationAction action);
4374
4375 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004376 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004377 */
4378 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
4379
4380 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004381 * Adds a callback to notify the host application when a script finished
4382 * running. If a script re-enters the runtime during executing, the
4383 * CallCompletedCallback is only invoked when the outer-most script
4384 * execution ends. Executing scripts inside the callback do not trigger
4385 * further callbacks.
4386 */
4387 static void AddCallCompletedCallback(CallCompletedCallback callback);
4388
4389 /**
4390 * Removes callback that was installed by AddCallCompletedCallback.
4391 */
4392 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
4393
4394 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004395 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004396 * initialize from scratch. This function is called implicitly if
4397 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004398 */
4399 static bool Initialize();
4400
kasper.lund7276f142008-07-30 08:49:36 +00004401 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004402 * Allows the host application to provide a callback which can be used
4403 * as a source of entropy for random number generators.
4404 */
4405 static void SetEntropySource(EntropySource source);
4406
4407 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004408 * Allows the host application to provide a callback that allows v8 to
4409 * cooperate with a profiler that rewrites return addresses on stack.
4410 */
4411 static void SetReturnAddressLocationResolver(
4412 ReturnAddressLocationResolver return_address_resolver);
4413
4414 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004415 * Allows the host application to provide the address of a function that's
4416 * invoked on entry to every V8-generated function.
4417 * Note that \p entry_hook is invoked at the very start of each
4418 * generated function.
4419 *
4420 * \param entry_hook a function that will be invoked on entry to every
4421 * V8-generated function.
4422 * \returns true on success on supported platforms, false on failure.
4423 * \note Setting a new entry hook function when one is already active will
4424 * fail.
4425 */
4426 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
4427
4428 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004429 * Allows the host application to provide the address of a function that is
4430 * notified each time code is added, moved or removed.
4431 *
4432 * \param options options for the JIT code event handler.
4433 * \param event_handler the JIT code event handler, which will be invoked
4434 * each time code is added, moved or removed.
4435 * \note \p event_handler won't get notified of existent code.
4436 * \note since code removal notifications are not currently issued, the
4437 * \p event_handler may get notifications of code that overlaps earlier
4438 * code notifications. This happens when code areas are reused, and the
4439 * earlier overlapping code areas should therefore be discarded.
4440 * \note the events passed to \p event_handler and the strings they point to
4441 * are not guaranteed to live past each call. The \p event_handler must
4442 * copy strings and other parameters it needs to keep around.
4443 * \note the set of events declared in JitCodeEvent::EventType is expected to
4444 * grow over time, and the JitCodeEvent structure is expected to accrue
4445 * new members. The \p event_handler function must ignore event codes
4446 * it does not recognize to maintain future compatibility.
4447 */
4448 static void SetJitCodeEventHandler(JitCodeEventOptions options,
4449 JitCodeEventHandler event_handler);
4450
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004451 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
4452 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004453 static intptr_t AdjustAmountOfExternalAllocatedMemory(
4454 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00004455
iposva@chromium.org245aa852009-02-10 00:49:54 +00004456 /**
4457 * Suspends recording of tick samples in the profiler.
4458 * When the V8 profiling mode is enabled (usually via command line
4459 * switches) this function suspends recording of tick samples.
4460 * Profiling ticks are discarded until ResumeProfiler() is called.
4461 *
4462 * See also the --prof and --prof_auto command line switches to
4463 * enable V8 profiling.
4464 */
4465 static void PauseProfiler();
4466
4467 /**
4468 * Resumes recording of tick samples in the profiler.
4469 * See also PauseProfiler().
4470 */
4471 static void ResumeProfiler();
4472
ager@chromium.org41826e72009-03-30 13:30:57 +00004473 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004474 * Return whether profiler is currently paused.
4475 */
4476 static bool IsProfilerPaused();
4477
4478 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004479 * Retrieve the V8 thread id of the calling thread.
4480 *
4481 * The thread id for a thread should only be retrieved after the V8
4482 * lock has been acquired with a Locker object with that thread.
4483 */
4484 static int GetCurrentThreadId();
4485
4486 /**
4487 * Forcefully terminate execution of a JavaScript thread. This can
4488 * be used to terminate long-running scripts.
4489 *
4490 * TerminateExecution should only be called when then V8 lock has
4491 * been acquired with a Locker object. Therefore, in order to be
4492 * able to terminate long-running threads, preemption must be
4493 * enabled to allow the user of TerminateExecution to acquire the
4494 * lock.
4495 *
4496 * The termination is achieved by throwing an exception that is
4497 * uncatchable by JavaScript exception handlers. Termination
4498 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004499 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004500 * exception handler that catches an exception should check if that
4501 * exception is a termination exception and immediately return if
4502 * that is the case. Returning immediately in that case will
4503 * continue the propagation of the termination exception if needed.
4504 *
4505 * The thread id passed to TerminateExecution must have been
4506 * obtained by calling GetCurrentThreadId on the thread in question.
4507 *
4508 * \param thread_id The thread id of the thread to terminate.
4509 */
4510 static void TerminateExecution(int thread_id);
4511
4512 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004513 * Forcefully terminate the current thread of JavaScript execution
4514 * in the given isolate. If no isolate is provided, the default
4515 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004516 *
4517 * This method can be used by any thread even if that thread has not
4518 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004519 *
4520 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004521 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004522 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00004523
4524 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004525 * Is V8 terminating JavaScript execution.
4526 *
4527 * Returns true if JavaScript execution is currently terminating
4528 * because of a call to TerminateExecution. In that case there are
4529 * still JavaScript frames on the stack and the termination
4530 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004531 *
4532 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004533 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004534 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004535
4536 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004537 * Resume execution capability in the given isolate, whose execution
4538 * was previously forcefully terminated using TerminateExecution().
4539 *
4540 * When execution is forcefully terminated using TerminateExecution(),
4541 * the isolate can not resume execution until all JavaScript frames
4542 * have propagated the uncatchable exception which is generated. This
4543 * method allows the program embedding the engine to handle the
4544 * termination event and resume execution capability, even if
4545 * JavaScript frames remain on the stack.
4546 *
4547 * This method can be used by any thread even if that thread has not
4548 * acquired the V8 lock with a Locker object.
4549 *
4550 * \param isolate The isolate in which to resume execution capability.
4551 */
4552 static void CancelTerminateExecution(Isolate* isolate);
4553
4554 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00004555 * Releases any resources used by v8 and stops any utility threads
4556 * that may be running. Note that disposing v8 is permanent, it
4557 * cannot be reinitialized.
4558 *
4559 * It should generally not be necessary to dispose v8 before exiting
4560 * a process, this should happen automatically. It is only necessary
4561 * to use if the process needs the resources taken up by v8.
4562 */
4563 static bool Dispose();
4564
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00004565 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
4566 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004567
4568 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004569 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004570 * heap. GC is not invoked prior to iterating, therefore there is no
4571 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004572 */
4573 static void VisitExternalResources(ExternalResourceVisitor* visitor);
4574
4575 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004576 * Iterates through all the persistent handles in the current isolate's heap
4577 * that have class_ids.
4578 */
4579 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
4580
4581 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004582 * Iterates through all the persistent handles in the current isolate's heap
4583 * that have class_ids and are candidates to be marked as partially dependent
4584 * handles. This will visit handles to young objects created since the last
4585 * garbage collection but is free to visit an arbitrary superset of these
4586 * objects.
4587 */
4588 static void VisitHandlesForPartialDependence(
4589 Isolate* isolate, PersistentHandleVisitor* visitor);
4590
4591 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004592 * Optional notification that the embedder is idle.
4593 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004594 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004595 * Returns true if the embedder should stop calling IdleNotification
4596 * until real work has been done. This indicates that V8 has done
4597 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004598 *
4599 * The hint argument specifies the amount of work to be done in the function
4600 * on scale from 1 to 1000. There is no guarantee that the actual work will
4601 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004602 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004603 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004604
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004605 /**
4606 * Optional notification that the system is running low on memory.
4607 * V8 uses these notifications to attempt to free memory.
4608 */
4609 static void LowMemoryNotification();
4610
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004611 /**
4612 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004613 * these notifications to guide the GC heuristic. Returns the number
4614 * of context disposals - including this one - since the last time
4615 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004616 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004617 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004619 private:
4620 V8();
4621
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004622 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
4623 internal::Object** handle);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004624 static void DisposeGlobal(internal::Object** global_handle);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004625 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004626 static void MakeWeak(internal::Object** global_handle,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004627 void* data,
rossberg@chromium.org79e79022013-06-03 15:43:46 +00004628 RevivableCallback weak_reference_callback);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004629 static void ClearWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004630
4631 template <class T> friend class Handle;
4632 template <class T> friend class Local;
4633 template <class T> friend class Persistent;
4634 friend class Context;
4635};
4636
4637
4638/**
4639 * An external exception handler.
4640 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004641class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004642 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004643 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004644 * Creates a new try/catch block and registers it with v8. Note that
4645 * all TryCatch blocks should be stack allocated because the memory
4646 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004647 */
4648 TryCatch();
4649
4650 /**
4651 * Unregisters and deletes this try/catch block.
4652 */
4653 ~TryCatch();
4654
4655 /**
4656 * Returns true if an exception has been caught by this try/catch block.
4657 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004658 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004659
4660 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004661 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004662 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004663 * If CanContinue returns false, the correct action is to perform any C++
4664 * cleanup needed and then return. If CanContinue returns false and
4665 * HasTerminated returns true, it is possible to call
4666 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004667 */
4668 bool CanContinue() const;
4669
4670 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004671 * Returns true if an exception has been caught due to script execution
4672 * being terminated.
4673 *
4674 * There is no JavaScript representation of an execution termination
4675 * exception. Such exceptions are thrown when the TerminateExecution
4676 * methods are called to terminate a long-running script.
4677 *
4678 * If such an exception has been thrown, HasTerminated will return true,
4679 * indicating that it is possible to call CancelTerminateExecution in order
4680 * to continue calling into the engine.
4681 */
4682 bool HasTerminated() const;
4683
4684 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004685 * Throws the exception caught by this TryCatch in a way that avoids
4686 * it being caught again by this same TryCatch. As with ThrowException
4687 * it is illegal to execute any JavaScript operations after calling
4688 * ReThrow; the caller must return immediately to where the exception
4689 * is caught.
4690 */
4691 Handle<Value> ReThrow();
4692
4693 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004694 * Returns the exception caught by this try/catch block. If no exception has
4695 * been caught an empty handle is returned.
4696 *
4697 * The returned handle is valid until this TryCatch block has been destroyed.
4698 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004699 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004700
4701 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004702 * Returns the .stack property of the thrown object. If no .stack
4703 * property is present an empty handle is returned.
4704 */
4705 Local<Value> StackTrace() const;
4706
4707 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004708 * Returns the message associated with this exception. If there is
4709 * no message associated an empty handle is returned.
4710 *
4711 * The returned handle is valid until this TryCatch block has been
4712 * destroyed.
4713 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004714 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004715
4716 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004717 * Clears any exceptions that may have been caught by this try/catch block.
4718 * After this method has been called, HasCaught() will return false.
4719 *
4720 * It is not necessary to clear a try/catch block before using it again; if
4721 * another exception is thrown the previously caught exception will just be
4722 * overwritten. However, it is often a good idea since it makes it easier
4723 * to determine which operation threw a given exception.
4724 */
4725 void Reset();
4726
v8.team.kasperl727e9952008-09-02 14:56:44 +00004727 /**
4728 * Set verbosity of the external exception handler.
4729 *
4730 * By default, exceptions that are caught by an external exception
4731 * handler are not reported. Call SetVerbose with true on an
4732 * external exception handler to have exceptions caught by the
4733 * handler reported as if they were not caught.
4734 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004735 void SetVerbose(bool value);
4736
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004737 /**
4738 * Set whether or not this TryCatch should capture a Message object
4739 * which holds source information about where the exception
4740 * occurred. True by default.
4741 */
4742 void SetCaptureMessage(bool value);
4743
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004744 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004745 // Make it hard to create heap-allocated TryCatch blocks.
4746 TryCatch(const TryCatch&);
4747 void operator=(const TryCatch&);
4748 void* operator new(size_t size);
4749 void operator delete(void*, size_t);
4750
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004751 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004752 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004753 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004754 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004755 bool is_verbose_ : 1;
4756 bool can_continue_ : 1;
4757 bool capture_message_ : 1;
4758 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004759 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004760
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004761 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004762};
4763
4764
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004765// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004766
4767
4768/**
4769 * Ignore
4770 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004771class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004772 public:
4773 ExtensionConfiguration(int name_count, const char* names[])
4774 : name_count_(name_count), names_(names) { }
4775 private:
4776 friend class ImplementationUtilities;
4777 int name_count_;
4778 const char** names_;
4779};
4780
4781
4782/**
4783 * A sandboxed execution context with its own set of built-in objects
4784 * and functions.
4785 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004786class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004787 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004788 /**
4789 * Returns the global proxy object or global object itself for
4790 * detached contexts.
4791 *
4792 * Global proxy object is a thin wrapper whose prototype points to
4793 * actual context's global object with the properties like Object, etc.
4794 * This is done that way for security reasons (for more details see
4795 * https://wiki.mozilla.org/Gecko:SplitWindow).
4796 *
4797 * Please note that changes to global proxy object prototype most probably
4798 * would break VM---v8 expects only global object as a prototype of
4799 * global proxy object.
4800 *
4801 * If DetachGlobal() has been invoked, Global() would return actual global
4802 * object until global is reattached with ReattachGlobal().
4803 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004804 Local<Object> Global();
4805
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004806 /**
4807 * Detaches the global object from its context before
4808 * the global object can be reused to create a new context.
4809 */
4810 void DetachGlobal();
4811
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00004812 /**
4813 * Reattaches a global object to a context. This can be used to
4814 * restore the connection between a global object and a context
4815 * after DetachGlobal has been called.
4816 *
4817 * \param global_object The global object to reattach to the
4818 * context. For this to work, the global object must be the global
4819 * object that was associated with this context before a call to
4820 * DetachGlobal.
4821 */
4822 void ReattachGlobal(Handle<Object> global_object);
4823
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004824 /**
4825 * Creates a new context and returns a handle to the newly allocated
4826 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004827 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004828 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004829 *
4830 * \param extensions An optional extension configuration containing
4831 * the extensions to be installed in the newly created context.
4832 *
4833 * \param global_template An optional object template from which the
4834 * global object for the newly created context will be created.
4835 *
4836 * \param global_object An optional global object to be reused for
4837 * the newly created context. This global object must have been
4838 * created by a previous call to Context::New with the same global
4839 * template. The state of the global object will be completely reset
4840 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004841 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004842 static Local<Context> New(
4843 Isolate* isolate,
4844 ExtensionConfiguration* extensions = NULL,
4845 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4846 Handle<Value> global_object = Handle<Value>());
4847
4848 /** Deprecated. Use Isolate version instead. */
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004849 V8_DEPRECATED(static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004850 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004851 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004852 Handle<Value> global_object = Handle<Value>()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004853
kasper.lund44510672008-07-25 07:37:58 +00004854 /** Returns the last entered context. */
4855 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004856
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004857 // TODO(svenpanne) Actually deprecate this.
4858 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004859 static Local<Context> GetCurrent();
4860
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004861 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004862 * Returns the context of the calling JavaScript code. That is the
4863 * context of the top-most JavaScript frame. If there are no
4864 * JavaScript frames an empty handle is returned.
4865 */
4866 static Local<Context> GetCalling();
4867
4868 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004869 * Sets the security token for the context. To access an object in
4870 * another context, the security tokens must match.
4871 */
4872 void SetSecurityToken(Handle<Value> token);
4873
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004874 /** Restores the security token to the default value. */
4875 void UseDefaultSecurityToken();
4876
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004877 /** Returns the security token of this context.*/
4878 Handle<Value> GetSecurityToken();
4879
v8.team.kasperl727e9952008-09-02 14:56:44 +00004880 /**
4881 * Enter this context. After entering a context, all code compiled
4882 * and run is compiled and run in this context. If another context
4883 * is already entered, this old context is saved so it can be
4884 * restored when the new context is exited.
4885 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004886 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004887
4888 /**
4889 * Exit this context. Exiting the current context restores the
4890 * context that was in place when entering the current context.
4891 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004892 void Exit();
4893
v8.team.kasperl727e9952008-09-02 14:56:44 +00004894 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004895 bool HasOutOfMemoryException();
4896
v8.team.kasperl727e9952008-09-02 14:56:44 +00004897 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004898 static bool InContext();
4899
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004900 /** Returns an isolate associated with a current context. */
4901 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004902
4903 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004904 * Gets the embedder data with the given index, which must have been set by a
4905 * previous call to SetEmbedderData with the same index. Note that index 0
4906 * currently has a special meaning for Chrome's debugger.
4907 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004908 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004909
4910 /**
4911 * Sets the embedder data with the given index, growing the data as
4912 * needed. Note that index 0 currently has a special meaning for Chrome's
4913 * debugger.
4914 */
4915 void SetEmbedderData(int index, Handle<Value> value);
4916
4917 /**
4918 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4919 * index, which must have bees set by a previous call to
4920 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4921 * currently has a special meaning for Chrome's debugger.
4922 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004923 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004924
4925 /**
4926 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4927 * index, growing the data as needed. Note that index 0 currently has a
4928 * special meaning for Chrome's debugger.
4929 */
4930 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004931
4932 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004933 * Control whether code generation from strings is allowed. Calling
4934 * this method with false will disable 'eval' and the 'Function'
4935 * constructor for code running in this context. If 'eval' or the
4936 * 'Function' constructor are used an exception will be thrown.
4937 *
4938 * If code generation from strings is not allowed the
4939 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4940 * set before blocking the call to 'eval' or the 'Function'
4941 * constructor. If that callback returns true, the call will be
4942 * allowed, otherwise an exception will be thrown. If no callback is
4943 * set an exception will be thrown.
4944 */
4945 void AllowCodeGenerationFromStrings(bool allow);
4946
4947 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004948 * Returns true if code generation from strings is allowed for the context.
4949 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4950 */
4951 bool IsCodeGenerationFromStringsAllowed();
4952
4953 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004954 * Sets the error description for the exception that is thrown when
4955 * code generation from strings is not allowed and 'eval' or the 'Function'
4956 * constructor are called.
4957 */
4958 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4959
4960 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004961 * Stack-allocated class which sets the execution context for all
4962 * operations executed within a local scope.
4963 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004964 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004965 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004966 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004967 context_->Enter();
4968 }
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00004969 // TODO(dcarney): deprecate
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004970 V8_INLINE(Scope(Isolate* isolate, Persistent<Context>& context)) // NOLINT
4971#ifndef V8_USE_UNSAFE_HANDLES
4972 : context_(Handle<Context>::New(isolate, context)) {
4973#else
4974 : context_(Local<Context>::New(isolate, context)) {
4975#endif
4976 context_->Enter();
4977 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004978 V8_INLINE(~Scope()) { context_->Exit(); }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004979
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004980 private:
4981 Handle<Context> context_;
4982 };
4983
4984 private:
4985 friend class Value;
4986 friend class Script;
4987 friend class Object;
4988 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004989
4990 Local<Value> SlowGetEmbedderData(int index);
4991 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004992};
4993
4994
4995/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004996 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4997 * to use any given V8 isolate, see the comments in the Isolate class. The
4998 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4999 * object pointers obtained from V8 handles while in the particular V8 isolate.
5000 * It is up to the user of V8 to ensure, perhaps with locking, that this
5001 * constraint is not violated. In addition to any other synchronization
5002 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
5003 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005004 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005005 * v8::Locker is a scoped lock object. While it's active, i.e. between its
5006 * construction and destruction, the current thread is allowed to use the locked
5007 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
5008 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005009 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00005010 * Sample usage:
5011* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005012 * ...
5013 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005014 * v8::Locker locker(isolate);
5015 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005016 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00005017 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005018 * ...
5019 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00005020 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005021 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005022 * If you wish to stop using V8 in a thread A you can do this either by
5023 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
5024 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005025 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00005026 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005027 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005028 * isolate->Exit();
5029 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005030 * ...
5031 * // Code not using V8 goes here while V8 can run in another thread.
5032 * ...
5033 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005034 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005035 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005036 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005037 * The Unlocker object is intended for use in a long-running callback from V8,
5038 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005039 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005040 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
5041 * given thread. This can be useful if you have code that can be called either
5042 * from code that holds the lock or from code that does not. The Unlocker is
5043 * not recursive so you can not have several Unlockers on the stack at once, and
5044 * 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 +00005045 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005046 * An unlocker will unlock several lockers if it has to and reinstate the
5047 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005048 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00005049 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005050 * // V8 not locked.
5051 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005052 * v8::Locker locker(isolate);
5053 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005054 * // V8 locked.
5055 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005056 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005057 * // V8 still locked (2 levels).
5058 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005059 * isolate->Exit();
5060 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005061 * // V8 not locked.
5062 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00005063 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005064 * // V8 locked again (2 levels).
5065 * }
5066 * // V8 still locked (1 level).
5067 * }
5068 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00005069 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005070 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005071class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005072 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005073 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005074 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005075 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005076 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
5077
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005078 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005079 V8_DEPRECATED(Unlocker());
5080
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005081 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00005082 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005083 void Initialize(Isolate* isolate);
5084
lrn@chromium.org1c092762011-05-09 09:42:16 +00005085 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005086};
5087
5088
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005089class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005090 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005091 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005092 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005093 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005094 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
5095
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005096 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005097 V8_DEPRECATED(Locker());
5098
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005099 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005100
5101 /**
5102 * Start preemption.
5103 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005104 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00005105 * that will switch between multiple threads that are in contention
5106 * for the V8 lock.
5107 */
5108 static void StartPreemption(int every_n_ms);
5109
5110 /**
5111 * Stop preemption.
5112 */
5113 static void StopPreemption();
5114
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005115 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005116 * Returns whether or not the locker for a given isolate, is locked by the
5117 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005118 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005119 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005120
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005121 /**
5122 * Returns whether v8::Locker is being used by this V8 instance.
5123 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00005124 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005126 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005127 void Initialize(Isolate* isolate);
5128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005129 bool has_lock_;
5130 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005131 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005132
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005133 static bool active_;
5134
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005135 // Disallow copying and assigning.
5136 Locker(const Locker&);
5137 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005138};
5139
5140
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005141/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005142 * A struct for exporting HeapStats data from V8, using "push" model.
5143 */
5144struct HeapStatsUpdate;
5145
5146
5147/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005148 * An interface for exporting data from V8, using "push" model.
5149 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005150class V8EXPORT OutputStream { // NOLINT
5151 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005152 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005153 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005154 };
5155 enum WriteResult {
5156 kContinue = 0,
5157 kAbort = 1
5158 };
5159 virtual ~OutputStream() {}
5160 /** Notify about the end of stream. */
5161 virtual void EndOfStream() = 0;
5162 /** Get preferred output chunk size. Called only once. */
5163 virtual int GetChunkSize() { return 1024; }
5164 /** Get preferred output encoding. Called only once. */
5165 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
5166 /**
5167 * Writes the next chunk of snapshot data into the stream. Writing
5168 * can be stopped by returning kAbort as function result. EndOfStream
5169 * will not be called in case writing was aborted.
5170 */
5171 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005172 /**
5173 * Writes the next chunk of heap stats data into the stream. Writing
5174 * can be stopped by returning kAbort as function result. EndOfStream
5175 * will not be called in case writing was aborted.
5176 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005177 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00005178 return kAbort;
5179 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005180};
5181
5182
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005183/**
5184 * An interface for reporting progress and controlling long-running
5185 * activities.
5186 */
5187class V8EXPORT ActivityControl { // NOLINT
5188 public:
5189 enum ControlOption {
5190 kContinue = 0,
5191 kAbort = 1
5192 };
5193 virtual ~ActivityControl() {}
5194 /**
5195 * Notify about current progress. The activity can be stopped by
5196 * returning kAbort as the callback result.
5197 */
5198 virtual ControlOption ReportProgressValue(int done, int total) = 0;
5199};
5200
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005201
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00005202// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005203
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005204
5205namespace internal {
5206
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00005207const int kApiPointerSize = sizeof(void*); // NOLINT
5208const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005209
5210// Tag information for HeapObject.
5211const int kHeapObjectTag = 1;
5212const int kHeapObjectTagSize = 2;
5213const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
5214
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005215// Tag information for Smi.
5216const int kSmiTag = 0;
5217const int kSmiTagSize = 1;
5218const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
5219
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005220template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005221
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005222template<int kSmiShiftSize>
5223V8_INLINE(internal::Object* IntToSmi(int value)) {
5224 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
5225 intptr_t tagged_value =
5226 (static_cast<intptr_t>(value) << smi_shift_bits) | kSmiTag;
5227 return reinterpret_cast<internal::Object*>(tagged_value);
5228}
5229
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005230// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005231template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005232 static const int kSmiShiftSize = 0;
5233 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005234 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005235 int shift_bits = kSmiTagSize + kSmiShiftSize;
5236 // Throw away top 32 bits and shift down (requires >> to be sign extending).
5237 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
5238 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005239 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5240 return internal::IntToSmi<kSmiShiftSize>(value);
5241 }
5242 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5243 // To be representable as an tagged small integer, the two
5244 // most-significant bits of 'value' must be either 00 or 11 due to
5245 // sign-extension. To check this we add 01 to the two
5246 // most-significant bits, and check if the most-significant bit is 0
5247 //
5248 // CAUTION: The original code below:
5249 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
5250 // may lead to incorrect results according to the C language spec, and
5251 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
5252 // compiler may produce undefined results in case of signed integer
5253 // overflow. The computation must be done w/ unsigned ints.
5254 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
5255 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005256};
5257
5258// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005259template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005260 static const int kSmiShiftSize = 31;
5261 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005262 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005263 int shift_bits = kSmiTagSize + kSmiShiftSize;
5264 // Shift down and throw away top 32 bits.
5265 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
5266 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005267 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5268 return internal::IntToSmi<kSmiShiftSize>(value);
5269 }
5270 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5271 // To be representable as a long smi, the value must be a 32-bit integer.
5272 return (value == static_cast<int32_t>(value));
5273 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005274};
5275
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005276typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
5277const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
5278const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005279
5280/**
5281 * This class exports constants and functionality from within v8 that
5282 * is necessary to implement inline functions in the v8 api. Don't
5283 * depend on functions and constants defined here.
5284 */
5285class Internals {
5286 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005287 // These values match non-compiler-dependent values defined within
5288 // the implementation of v8.
5289 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005290 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005291 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005292
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005293 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00005294 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005295 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005296 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
5297 static const int kContextHeaderSize = 2 * kApiPointerSize;
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005298 static const int kContextEmbedderDataIndex = 64;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005299 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005300 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00005301 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005302 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005303
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005304 static const int kIsolateStateOffset = 0;
5305 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
5306 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
5307 static const int kUndefinedValueRootIndex = 5;
5308 static const int kNullValueRootIndex = 7;
5309 static const int kTrueValueRootIndex = 8;
5310 static const int kFalseValueRootIndex = 9;
danno@chromium.org41728482013-06-12 22:31:22 +00005311 static const int kEmptyStringRootIndex = 131;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005312
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005313 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
5314 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
5315 static const int kNodeStateMask = 0xf;
5316 static const int kNodeStateIsWeakValue = 2;
5317 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005318 static const int kNodeIsIndependentShift = 4;
5319 static const int kNodeIsPartiallyDependentShift = 5;
5320
danno@chromium.org41728482013-06-12 22:31:22 +00005321 static const int kJSObjectType = 0xb0;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005322 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005323 static const int kOddballType = 0x83;
danno@chromium.org41728482013-06-12 22:31:22 +00005324 static const int kForeignType = 0x88;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005325
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005326 static const int kUndefinedOddballKind = 5;
5327 static const int kNullOddballKind = 3;
5328
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005329 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005330 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
5331 kHeapObjectTag);
5332 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005333
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005334 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005335 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005336 }
5337
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005338 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5339 return PlatformSmiTagging::IntToSmi(value);
5340 }
5341
5342 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5343 return PlatformSmiTagging::IsValidSmi(value);
5344 }
5345
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005346 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005347 typedef internal::Object O;
5348 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
5349 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
5350 }
5351
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005352 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005353 typedef internal::Object O;
5354 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
5355 }
5356
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005357 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005358 int representation = (instance_type & kFullStringRepresentationMask);
5359 return representation == kExternalTwoByteRepresentationTag;
5360 }
5361
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005362 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005363 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
5364 return *reinterpret_cast<int*>(addr) == 1;
5365 }
5366
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005367 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
5368 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5369 return *addr & (1 << shift);
5370 }
5371
5372 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
5373 bool value, int shift)) {
5374 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5375 uint8_t mask = 1 << shift;
5376 *addr = (*addr & ~mask) | (value << shift);
5377 }
5378
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005379 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
5380 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5381 return *addr & kNodeStateMask;
5382 }
5383
5384 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
5385 uint8_t value)) {
5386 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5387 *addr = (*addr & ~kNodeStateMask) | value;
5388 }
5389
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005390 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005391 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5392 kIsolateEmbedderDataOffset;
5393 *reinterpret_cast<void**>(addr) = data;
5394 }
5395
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005396 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005397 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5398 kIsolateEmbedderDataOffset;
5399 return *reinterpret_cast<void**>(addr);
5400 }
5401
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005402 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
5403 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005404 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
5405 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
5406 }
5407
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005408 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005409 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005410 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
5411 return *reinterpret_cast<T*>(addr);
5412 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005413
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005414 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005415 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005416 typedef internal::Object O;
5417 typedef internal::Internals I;
5418 O* ctx = *reinterpret_cast<O**>(context);
5419 int embedder_data_offset = I::kContextHeaderSize +
5420 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
5421 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
5422 int value_offset =
5423 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
5424 return I::ReadField<T>(embedder_data, value_offset);
5425 }
5426
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005427 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
5428 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
5429 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
5430 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
5431 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
5432 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
5433 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005434};
5435
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005436} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005437
5438
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005439template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005440Local<T>::Local() : Handle<T>() { }
5441
5442
5443template <class T>
5444Local<T> Local<T>::New(Handle<T> that) {
5445 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005446 T* that_ptr = *that;
5447 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5448 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
5449 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5450 reinterpret_cast<internal::HeapObject*>(*p))));
5451 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005452 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
5453}
5454
5455
5456template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005457Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005458 return New(isolate, that.val_);
5459}
5460
5461#ifndef V8_USE_UNSAFE_HANDLES
5462template <class T>
5463Local<T> Local<T>::New(Isolate* isolate, const Persistent<T>& that) {
5464 return New(isolate, that.val_);
5465}
5466
5467template <class T>
5468Handle<T> Handle<T>::New(Isolate* isolate, T* that) {
5469 if (that == NULL) return Handle<T>();
5470 T* that_ptr = that;
5471 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5472 return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5473 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5474}
5475#endif
5476
5477
5478template <class T>
5479Local<T> Local<T>::New(Isolate* isolate, T* that) {
5480 if (that == NULL) return Local<T>();
5481 T* that_ptr = that;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005482 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5483 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5484 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5485}
5486
5487
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00005488#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005489template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005490Persistent<T> Persistent<T>::New(Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005491 return New(Isolate::GetCurrent(), that.val_);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005492}
5493
5494
5495template <class T>
5496Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005497 return New(Isolate::GetCurrent(), that.val_);
5498}
5499
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005500template <class T>
5501Persistent<T> Persistent<T>::New(Isolate* isolate, Persistent<T> that) {
5502 return New(Isolate::GetCurrent(), that.val_);
5503}
5504#endif
5505
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00005506
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005507template <class T>
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00005508T* Persistent<T>::New(Isolate* isolate, T* that) {
5509 if (that == NULL) return NULL;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005510 internal::Object** p = reinterpret_cast<internal::Object**>(that);
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00005511 return reinterpret_cast<T*>(
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005512 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00005513 p));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005514}
5515
5516
5517template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005518bool Persistent<T>::IsIndependent() const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005519 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005520 if (this->IsEmpty()) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005521 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005522 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005523}
5524
5525
5526template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005527bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005528 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005529 if (this->IsEmpty()) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005530 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005531 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005532}
5533
5534
5535template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005536bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005537 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005538 if (this->IsEmpty()) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005539 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005540 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005541}
5542
5543
5544template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005545void Persistent<T>::Dispose() {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005546 if (this->IsEmpty()) return;
5547 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
5548#ifndef V8_USE_UNSAFE_HANDLES
5549 val_ = 0;
5550#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005551}
5552
5553
5554template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005555template <typename S, typename P>
5556void Persistent<T>::MakeWeak(
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005557 P* parameters,
5558 typename WeakReferenceCallbacks<S, P>::Revivable callback) {
5559 TYPE_CHECK(S, T);
5560 typedef typename WeakReferenceCallbacks<Value, void>::Revivable Revivable;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005561 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005562 parameters,
rossberg@chromium.org79e79022013-06-03 15:43:46 +00005563 reinterpret_cast<Revivable>(callback));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005564}
5565
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005566
5567template <class T>
5568template <typename P>
5569void Persistent<T>::MakeWeak(
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005570 P* parameters,
5571 typename WeakReferenceCallbacks<T, P>::Revivable callback) {
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005572 MakeWeak<T, P>(parameters, callback);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005573}
5574
5575
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005576template <class T>
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00005577template <typename S, typename P>
5578void Persistent<T>::MakeWeak(
5579 Isolate* isolate,
5580 P* parameters,
5581 typename WeakReferenceCallbacks<S, P>::Revivable callback) {
5582 MakeWeak<S, P>(parameters, callback);
5583}
5584
5585
5586template <class T>
5587template<typename P>
5588void Persistent<T>::MakeWeak(
5589 Isolate* isolate,
5590 P* parameters,
5591 typename WeakReferenceCallbacks<T, P>::Revivable callback) {
5592 MakeWeak<P>(parameters, callback);
5593}
5594
5595
5596template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005597void Persistent<T>::ClearWeak() {
5598 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005599}
5600
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005601
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005602template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005603void Persistent<T>::MarkIndependent() {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005604 typedef internal::Internals I;
5605 if (this->IsEmpty()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005606 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005607 true,
5608 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005609}
5610
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005611
5612template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005613void Persistent<T>::MarkPartiallyDependent() {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005614 typedef internal::Internals I;
5615 if (this->IsEmpty()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005616 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005617 true,
5618 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005619}
5620
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005621
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005622template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005623void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
5624 Dispose(isolate);
5625#ifdef V8_USE_UNSAFE_HANDLES
5626 *this = *New(isolate, other);
5627#else
5628 if (other.IsEmpty()) {
5629 this->val_ = NULL;
5630 return;
5631 }
5632 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5633 this->val_ = reinterpret_cast<T*>(
5634 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5635#endif
5636}
5637
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005638
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005639#ifndef V8_USE_UNSAFE_HANDLES
5640template <class T>
5641void Persistent<T>::Reset(Isolate* isolate, const Persistent<T>& other) {
5642 Dispose(isolate);
5643 if (other.IsEmpty()) {
5644 this->val_ = NULL;
5645 return;
5646 }
5647 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5648 this->val_ = reinterpret_cast<T*>(
5649 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5650}
5651#endif
5652
5653
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005654template <class T>
5655T* Persistent<T>::ClearAndLeak() {
5656 T* old;
5657#ifdef V8_USE_UNSAFE_HANDLES
5658 old = **this;
5659 *this = Persistent<T>();
5660#else
5661 old = val_;
5662 val_ = NULL;
5663#endif
5664 return old;
5665}
5666
5667
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005668template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005669void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005670 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005671 if (this->IsEmpty()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005672 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005673 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5674 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005675}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005676
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005677
5678template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005679uint16_t Persistent<T>::WrapperClassId() const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005680 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005681 if (this->IsEmpty()) return 0;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005682 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005683 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5684 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005685}
5686
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005687
5688template<typename T>
5689ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
5690
5691template<typename T>
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00005692template<typename S>
5693void ReturnValue<T>::Set(const Persistent<S>& handle) {
5694 TYPE_CHECK(T, S);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005695 if (V8_UNLIKELY(handle.IsEmpty())) {
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00005696 *value_ = GetDefaultValue();
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005697 } else {
5698 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5699 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005700}
5701
5702template<typename T>
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00005703template<typename S>
5704void ReturnValue<T>::Set(const Handle<S> handle) {
5705 TYPE_CHECK(T, S);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005706 if (V8_UNLIKELY(handle.IsEmpty())) {
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00005707 *value_ = GetDefaultValue();
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005708 } else {
5709 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5710 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005711}
5712
5713template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005714void ReturnValue<T>::Set(double i) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00005715 TYPE_CHECK(T, Number);
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005716 Set(Number::New(GetIsolate(), i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005717}
5718
5719template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005720void ReturnValue<T>::Set(int32_t i) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00005721 TYPE_CHECK(T, Integer);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005722 typedef internal::Internals I;
5723 if (V8_LIKELY(I::IsValidSmi(i))) {
5724 *value_ = I::IntToSmi(i);
5725 return;
5726 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005727 Set(Integer::New(i, GetIsolate()));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005728}
5729
5730template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005731void ReturnValue<T>::Set(uint32_t i) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00005732 TYPE_CHECK(T, Integer);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005733 typedef internal::Internals I;
danno@chromium.org1fd77d52013-06-07 16:01:45 +00005734 // Can't simply use INT32_MAX here for whatever reason.
5735 bool fits_into_int32_t = (i & (1 << 31)) == 0;
5736 if (V8_LIKELY(fits_into_int32_t)) {
5737 Set(static_cast<int32_t>(i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005738 return;
5739 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005740 Set(Integer::NewFromUnsigned(i, GetIsolate()));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005741}
5742
5743template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005744void ReturnValue<T>::Set(bool value) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00005745 TYPE_CHECK(T, Boolean);
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005746 typedef internal::Internals I;
5747 int root_index;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005748 if (value) {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005749 root_index = I::kTrueValueRootIndex;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005750 } else {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005751 root_index = I::kFalseValueRootIndex;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005752 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005753 *value_ = *I::GetRoot(GetIsolate(), root_index);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005754}
5755
5756template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005757void ReturnValue<T>::SetNull() {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00005758 TYPE_CHECK(T, Primitive);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005759 typedef internal::Internals I;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005760 *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005761}
5762
5763template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005764void ReturnValue<T>::SetUndefined() {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00005765 TYPE_CHECK(T, Primitive);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005766 typedef internal::Internals I;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005767 *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005768}
5769
5770template<typename T>
ulan@chromium.org837a67e2013-06-11 15:39:48 +00005771void ReturnValue<T>::SetEmptyString() {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00005772 TYPE_CHECK(T, String);
ulan@chromium.org837a67e2013-06-11 15:39:48 +00005773 typedef internal::Internals I;
5774 *value_ = *I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex);
5775}
5776
5777template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005778Isolate* ReturnValue<T>::GetIsolate() {
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00005779 // Isolate is always the pointer below the default value on the stack.
5780 return *reinterpret_cast<Isolate**>(&value_[-2]);
5781}
5782
5783template<typename T>
5784internal::Object* ReturnValue<T>::GetDefaultValue() {
5785 // Default value is always the pointer below value_ on the stack.
5786 return value_[-1];
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005787}
5788
5789
5790template<typename T>
5791FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
5792 internal::Object** values,
5793 int length,
5794 bool is_construct_call)
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005795 : implicit_args_(implicit_args),
5796 values_(values),
5797 length_(length),
5798 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005799
5800
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005801Arguments::Arguments(internal::Object** args,
5802 internal::Object** values,
5803 int length,
5804 bool is_construct_call)
5805 : FunctionCallbackInfo<Value>(args, values, length, is_construct_call) { }
5806
5807
5808template<typename T>
5809Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005810 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
5811 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
5812}
5813
5814
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005815template<typename T>
5816Local<Function> FunctionCallbackInfo<T>::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005817 return Local<Function>(reinterpret_cast<Function*>(
5818 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005819}
5820
5821
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005822template<typename T>
5823Local<Object> FunctionCallbackInfo<T>::This() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005824 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
5825}
5826
5827
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005828template<typename T>
5829Local<Object> FunctionCallbackInfo<T>::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005830 return Local<Object>(reinterpret_cast<Object*>(
5831 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005832}
5833
5834
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005835template<typename T>
5836Local<Value> FunctionCallbackInfo<T>::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005837 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005838}
5839
5840
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005841template<typename T>
5842Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005843 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
5844}
5845
5846
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005847template<typename T>
5848ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
5849 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
5850}
5851
5852
5853template<typename T>
5854bool FunctionCallbackInfo<T>::IsConstructCall() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005855 return is_construct_call_;
5856}
5857
5858
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005859template<typename T>
5860int FunctionCallbackInfo<T>::Length() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005861 return length_;
5862}
5863
5864
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005865template <class T>
5866Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005867 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
5868 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005869 return Local<T>(reinterpret_cast<T*>(after));
5870}
5871
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005872Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005873 return resource_name_;
5874}
5875
5876
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005877Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005878 return resource_line_offset_;
5879}
5880
5881
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005882Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005883 return resource_column_offset_;
5884}
5885
5886
5887Handle<Boolean> Boolean::New(bool value) {
5888 return value ? True() : False();
5889}
5890
5891
5892void Template::Set(const char* name, v8::Handle<Data> value) {
5893 Set(v8::String::New(name), value);
5894}
5895
5896
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005897Local<Value> Object::GetInternalField(int index) {
5898#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005899 typedef internal::Object O;
5900 typedef internal::Internals I;
5901 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005902 // Fast path: If the object is a plain JSObject, which is the common case, we
5903 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005904 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005905 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005906 O* value = I::ReadField<O*>(obj, offset);
5907 O** result = HandleScope::CreateHandle(value);
5908 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005909 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005910#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005911 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005912}
5913
5914
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005915void* Object::GetAlignedPointerFromInternalField(int index) {
5916#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005917 typedef internal::Object O;
5918 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005919 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005920 // Fast path: If the object is a plain JSObject, which is the common case, we
5921 // know where to find the internal fields and can return the value directly.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005922 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005923 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005924 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005925 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005926#endif
5927 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005928}
5929
5930
5931String* String::Cast(v8::Value* value) {
5932#ifdef V8_ENABLE_CHECKS
5933 CheckCast(value);
5934#endif
5935 return static_cast<String*>(value);
5936}
5937
5938
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005939Local<String> String::Empty(Isolate* isolate) {
5940 typedef internal::Object* S;
5941 typedef internal::Internals I;
5942 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005943 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005944 return Local<String>(reinterpret_cast<String*>(slot));
5945}
5946
5947
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00005948Local<String> String::New(const char* data, int length) {
5949 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
5950}
5951
5952
5953Local<String> String::New(const uint16_t* data, int length) {
5954 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
5955}
5956
5957
5958Local<String> String::NewSymbol(const char* data, int length) {
5959 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
5960}
5961
5962
5963Local<String> String::NewUndetectable(const char* data, int length) {
5964 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
5965}
5966
5967
5968Local<String> String::NewUndetectable(const uint16_t* data, int length) {
5969 return NewFromTwoByte(
5970 Isolate::GetCurrent(), data, kUndetectableString, length);
5971}
5972
5973
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005974String::ExternalStringResource* String::GetExternalStringResource() const {
5975 typedef internal::Object O;
5976 typedef internal::Internals I;
5977 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005978 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005979 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005980 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5981 result = reinterpret_cast<String::ExternalStringResource*>(value);
5982 } else {
5983 result = NULL;
5984 }
5985#ifdef V8_ENABLE_CHECKS
5986 VerifyExternalStringResource(result);
5987#endif
5988 return result;
5989}
5990
5991
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005992String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
5993 String::Encoding* encoding_out) const {
5994 typedef internal::Object O;
5995 typedef internal::Internals I;
5996 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
5997 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
5998 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5999 ExternalStringResourceBase* resource = NULL;
6000 if (type == I::kExternalAsciiRepresentationTag ||
6001 type == I::kExternalTwoByteRepresentationTag) {
6002 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
6003 resource = static_cast<ExternalStringResourceBase*>(value);
6004 }
6005#ifdef V8_ENABLE_CHECKS
6006 VerifyExternalStringResourceBase(resource, *encoding_out);
6007#endif
6008 return resource;
6009}
6010
6011
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00006012bool Value::IsUndefined() const {
6013#ifdef V8_ENABLE_CHECKS
6014 return FullIsUndefined();
6015#else
6016 return QuickIsUndefined();
6017#endif
6018}
6019
6020bool Value::QuickIsUndefined() const {
6021 typedef internal::Object O;
6022 typedef internal::Internals I;
6023 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6024 if (!I::HasHeapObjectTag(obj)) return false;
6025 if (I::GetInstanceType(obj) != I::kOddballType) return false;
6026 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
6027}
6028
6029
6030bool Value::IsNull() const {
6031#ifdef V8_ENABLE_CHECKS
6032 return FullIsNull();
6033#else
6034 return QuickIsNull();
6035#endif
6036}
6037
6038bool Value::QuickIsNull() const {
6039 typedef internal::Object O;
6040 typedef internal::Internals I;
6041 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6042 if (!I::HasHeapObjectTag(obj)) return false;
6043 if (I::GetInstanceType(obj) != I::kOddballType) return false;
6044 return (I::GetOddballKind(obj) == I::kNullOddballKind);
6045}
6046
6047
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006048bool Value::IsString() const {
6049#ifdef V8_ENABLE_CHECKS
6050 return FullIsString();
6051#else
6052 return QuickIsString();
6053#endif
6054}
6055
6056bool Value::QuickIsString() const {
6057 typedef internal::Object O;
6058 typedef internal::Internals I;
6059 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6060 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006061 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006062}
6063
6064
danno@chromium.org1fd77d52013-06-07 16:01:45 +00006065template <class T> Value* Value::Cast(T* value) {
6066 return static_cast<Value*>(value);
6067}
6068
6069
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006070Symbol* Symbol::Cast(v8::Value* value) {
6071#ifdef V8_ENABLE_CHECKS
6072 CheckCast(value);
6073#endif
6074 return static_cast<Symbol*>(value);
6075}
6076
6077
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006078Number* Number::Cast(v8::Value* value) {
6079#ifdef V8_ENABLE_CHECKS
6080 CheckCast(value);
6081#endif
6082 return static_cast<Number*>(value);
6083}
6084
6085
6086Integer* Integer::Cast(v8::Value* value) {
6087#ifdef V8_ENABLE_CHECKS
6088 CheckCast(value);
6089#endif
6090 return static_cast<Integer*>(value);
6091}
6092
6093
6094Date* Date::Cast(v8::Value* value) {
6095#ifdef V8_ENABLE_CHECKS
6096 CheckCast(value);
6097#endif
6098 return static_cast<Date*>(value);
6099}
6100
6101
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006102StringObject* StringObject::Cast(v8::Value* value) {
6103#ifdef V8_ENABLE_CHECKS
6104 CheckCast(value);
6105#endif
6106 return static_cast<StringObject*>(value);
6107}
6108
6109
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006110SymbolObject* SymbolObject::Cast(v8::Value* value) {
6111#ifdef V8_ENABLE_CHECKS
6112 CheckCast(value);
6113#endif
6114 return static_cast<SymbolObject*>(value);
6115}
6116
6117
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006118NumberObject* NumberObject::Cast(v8::Value* value) {
6119#ifdef V8_ENABLE_CHECKS
6120 CheckCast(value);
6121#endif
6122 return static_cast<NumberObject*>(value);
6123}
6124
6125
6126BooleanObject* BooleanObject::Cast(v8::Value* value) {
6127#ifdef V8_ENABLE_CHECKS
6128 CheckCast(value);
6129#endif
6130 return static_cast<BooleanObject*>(value);
6131}
6132
6133
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00006134RegExp* RegExp::Cast(v8::Value* value) {
6135#ifdef V8_ENABLE_CHECKS
6136 CheckCast(value);
6137#endif
6138 return static_cast<RegExp*>(value);
6139}
6140
6141
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006142Object* Object::Cast(v8::Value* value) {
6143#ifdef V8_ENABLE_CHECKS
6144 CheckCast(value);
6145#endif
6146 return static_cast<Object*>(value);
6147}
6148
6149
6150Array* Array::Cast(v8::Value* value) {
6151#ifdef V8_ENABLE_CHECKS
6152 CheckCast(value);
6153#endif
6154 return static_cast<Array*>(value);
6155}
6156
6157
danno@chromium.orgca29dd82013-04-26 11:59:48 +00006158ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
6159#ifdef V8_ENABLE_CHECKS
6160 CheckCast(value);
6161#endif
6162 return static_cast<ArrayBuffer*>(value);
6163}
6164
6165
danno@chromium.orgf005df62013-04-30 16:36:45 +00006166TypedArray* TypedArray::Cast(v8::Value* value) {
6167#ifdef V8_ENABLE_CHECKS
6168 CheckCast(value);
6169#endif
6170 return static_cast<TypedArray*>(value);
6171}
6172
6173
6174Uint8Array* Uint8Array::Cast(v8::Value* value) {
6175#ifdef V8_ENABLE_CHECKS
6176 CheckCast(value);
6177#endif
6178 return static_cast<Uint8Array*>(value);
6179}
6180
6181
6182Int8Array* Int8Array::Cast(v8::Value* value) {
6183#ifdef V8_ENABLE_CHECKS
6184 CheckCast(value);
6185#endif
6186 return static_cast<Int8Array*>(value);
6187}
6188
6189
6190Uint16Array* Uint16Array::Cast(v8::Value* value) {
6191#ifdef V8_ENABLE_CHECKS
6192 CheckCast(value);
6193#endif
6194 return static_cast<Uint16Array*>(value);
6195}
6196
6197
6198Int16Array* Int16Array::Cast(v8::Value* value) {
6199#ifdef V8_ENABLE_CHECKS
6200 CheckCast(value);
6201#endif
6202 return static_cast<Int16Array*>(value);
6203}
6204
6205
6206Uint32Array* Uint32Array::Cast(v8::Value* value) {
6207#ifdef V8_ENABLE_CHECKS
6208 CheckCast(value);
6209#endif
6210 return static_cast<Uint32Array*>(value);
6211}
6212
6213
6214Int32Array* Int32Array::Cast(v8::Value* value) {
6215#ifdef V8_ENABLE_CHECKS
6216 CheckCast(value);
6217#endif
6218 return static_cast<Int32Array*>(value);
6219}
6220
6221
6222Float32Array* Float32Array::Cast(v8::Value* value) {
6223#ifdef V8_ENABLE_CHECKS
6224 CheckCast(value);
6225#endif
6226 return static_cast<Float32Array*>(value);
6227}
6228
6229
6230Float64Array* Float64Array::Cast(v8::Value* value) {
6231#ifdef V8_ENABLE_CHECKS
6232 CheckCast(value);
6233#endif
6234 return static_cast<Float64Array*>(value);
6235}
6236
6237
danno@chromium.org1fd77d52013-06-07 16:01:45 +00006238Uint8ClampedArray* Uint8ClampedArray::Cast(v8::Value* value) {
6239#ifdef V8_ENABLE_CHECKS
6240 CheckCast(value);
6241#endif
6242 return static_cast<Uint8ClampedArray*>(value);
6243}
6244
6245
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006246Function* Function::Cast(v8::Value* value) {
6247#ifdef V8_ENABLE_CHECKS
6248 CheckCast(value);
6249#endif
6250 return static_cast<Function*>(value);
6251}
6252
6253
6254External* External::Cast(v8::Value* value) {
6255#ifdef V8_ENABLE_CHECKS
6256 CheckCast(value);
6257#endif
6258 return static_cast<External*>(value);
6259}
6260
6261
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006262template<typename T>
6263Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
6264 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00006265}
6266
6267
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006268template<typename T>
6269Local<Value> PropertyCallbackInfo<T>::Data() const {
6270 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006271}
6272
6273
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006274template<typename T>
6275Local<Object> PropertyCallbackInfo<T>::This() const {
6276 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006277}
6278
6279
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006280template<typename T>
6281Local<Object> PropertyCallbackInfo<T>::Holder() const {
6282 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
6283}
6284
6285
6286template<typename T>
6287ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
6288 return ReturnValue<T>(&args_[kReturnValueIndex]);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006289}
6290
6291
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00006292Handle<Primitive> Undefined(Isolate* isolate) {
6293 typedef internal::Object* S;
6294 typedef internal::Internals I;
6295 if (!I::IsInitialized(isolate)) return Undefined();
6296 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
6297 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6298}
6299
6300
6301Handle<Primitive> Null(Isolate* isolate) {
6302 typedef internal::Object* S;
6303 typedef internal::Internals I;
6304 if (!I::IsInitialized(isolate)) return Null();
6305 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
6306 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6307}
6308
6309
6310Handle<Boolean> True(Isolate* isolate) {
6311 typedef internal::Object* S;
6312 typedef internal::Internals I;
6313 if (!I::IsInitialized(isolate)) return True();
6314 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
6315 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6316}
6317
6318
6319Handle<Boolean> False(Isolate* isolate) {
6320 typedef internal::Object* S;
6321 typedef internal::Internals I;
6322 if (!I::IsInitialized(isolate)) return False();
6323 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
6324 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6325}
6326
6327
6328void Isolate::SetData(void* data) {
6329 typedef internal::Internals I;
6330 I::SetEmbedderData(this, data);
6331}
6332
6333
6334void* Isolate::GetData() {
6335 typedef internal::Internals I;
6336 return I::GetEmbedderData(this);
6337}
6338
6339
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00006340Local<Value> Context::GetEmbedderData(int index) {
6341#ifndef V8_ENABLE_CHECKS
6342 typedef internal::Object O;
6343 typedef internal::Internals I;
6344 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
6345 return Local<Value>(reinterpret_cast<Value*>(result));
6346#else
6347 return SlowGetEmbedderData(index);
6348#endif
6349}
6350
6351
6352void* Context::GetAlignedPointerFromEmbedderData(int index) {
6353#ifndef V8_ENABLE_CHECKS
6354 typedef internal::Internals I;
6355 return I::ReadEmbedderData<void*>(this, index);
6356#else
6357 return SlowGetAlignedPointerFromEmbedderData(index);
6358#endif
6359}
6360
6361
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006362/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00006363 * \example shell.cc
6364 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006365 * command-line and executes them.
6366 */
6367
6368
6369/**
6370 * \example process.cc
6371 */
6372
6373
6374} // namespace v8
6375
6376
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006377#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006378#undef TYPE_CHECK
6379
6380
ager@chromium.org9258b6b2008-09-11 09:11:10 +00006381#endif // V8_H_