blob: 3fef9cdd80f4d6597d0a684f56a576f514ee70ae [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000065// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000066#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
67 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070#else
71#define V8EXPORT
72#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000075#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076
77#endif // _WIN32
78
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000079#if defined(__GNUC__) && !defined(DEBUG)
80#define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
81#elif defined(_MSC_VER) && !defined(DEBUG)
82#define V8_INLINE(declarator) __forceinline declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000083#else
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000084#define V8_INLINE(declarator) inline declarator
85#endif
86
87#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
88#define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
89#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
90#define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
91#else
92#define V8_DEPRECATED(declarator) declarator
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000093#endif
94
danno@chromium.orgca29dd82013-04-26 11:59:48 +000095#if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))
96 #define V8_UNLIKELY(condition) __builtin_expect((condition), 0)
97 #define V8_LIKELY(condition) __builtin_expect((condition), 1)
98#else
99 #define V8_UNLIKELY(condition) (condition)
100 #define V8_LIKELY(condition) (condition)
101#endif
102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000104 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 */
106namespace v8 {
107
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000108class AccessorInfo;
109class AccessorSignature;
110class Array;
111class Boolean;
112class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113class Context;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000114class CpuProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000115class Data;
116class Date;
117class DeclaredAccessorDescriptor;
118class External;
119class Function;
120class FunctionTemplate;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000121class HeapProfiler;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000122class ImplementationUtilities;
123class Int32;
124class Integer;
125class Isolate;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000126class LocalContext;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000128class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129class Object;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000130class ObjectOperationDescriptor;
131class ObjectTemplate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132class Primitive;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000133class RawOperationDescriptor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134class Signature;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000135class StackFrame;
136class StackTrace;
137class String;
138class StringObject;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000139class Symbol;
140class SymbolObject;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000141class Uint32;
142class Utils;
143class Value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144template <class T> class Handle;
145template <class T> class Local;
146template <class T> class Persistent;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000147class FunctionTemplate;
148class ObjectTemplate;
149class Data;
150class AccessorInfo;
151template<typename T> class PropertyCallbackInfo;
152class StackTrace;
153class StackFrame;
154class Isolate;
155class DeclaredAccessorDescriptor;
156class ObjectOperationDescriptor;
157class RawOperationDescriptor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000159namespace internal {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000160class Arguments;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000161class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000162class HeapObject;
163class Isolate;
jkummerow@chromium.org4c54a2a2013-03-19 17:51:30 +0000164class Object;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000165template<typename T>
166class CustomArguments;
167class PropertyCallbackArguments;
168class FunctionCallbackArguments;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000169}
170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000172/**
173 * General purpose unique identifier.
174 */
175class UniqueId {
176 public:
177 explicit UniqueId(intptr_t data)
178 : data_(data) {}
179
180 bool operator==(const UniqueId& other) const {
181 return data_ == other.data_;
182 }
183
184 bool operator!=(const UniqueId& other) const {
185 return data_ != other.data_;
186 }
187
188 bool operator<(const UniqueId& other) const {
189 return data_ < other.data_;
190 }
191
192 private:
193 intptr_t data_;
194};
195
196
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000197// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198
199
200/**
201 * A weak reference callback function.
202 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000203 * This callback should either explicitly invoke Dispose on |object| if
204 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
205 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 * \param object the weak global object to be reclaimed by the garbage collector
207 * \param parameter the value passed in when making the weak global object
208 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000209template<typename T, typename P>
210class WeakReferenceCallbacks {
211 public:
212 typedef void (*Revivable)(Isolate* isolate,
213 Persistent<T>* object,
214 P* parameter);
215};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000217// TODO(svenpanne) Temporary definition until Chrome is in sync.
218typedef void (*NearDeathCallback)(Isolate* isolate,
219 Persistent<Value> object,
220 void* parameter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000222// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000224#define TYPE_CHECK(T, S) \
225 while (false) { \
226 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227 }
228
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000229
230#define V8_USE_UNSAFE_HANDLES
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000231#define V8_USE_OLD_STYLE_PERSISTENT_HANDLE_VISITORS
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233/**
234 * An object reference managed by the v8 garbage collector.
235 *
236 * All objects returned from v8 have to be tracked by the garbage
237 * collector so that it knows that the objects are still alive. Also,
238 * because the garbage collector may move objects, it is unsafe to
239 * point directly to an object. Instead, all objects are stored in
240 * handles which are known by the garbage collector and updated
241 * whenever an object moves. Handles should always be passed by value
242 * (except in cases like out-parameters) and they should never be
243 * allocated on the heap.
244 *
245 * There are two types of handles: local and persistent handles.
246 * Local handles are light-weight and transient and typically used in
247 * local operations. They are managed by HandleScopes. Persistent
248 * handles can be used when storing objects across several independent
249 * operations and have to be explicitly deallocated when they're no
250 * longer used.
251 *
252 * It is safe to extract the object stored in the handle by
253 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000254 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 * behind the scenes and the same rules apply to these values as to
256 * their handles.
257 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000258template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 /**
261 * Creates an empty handle.
262 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000263 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000265#ifdef V8_USE_UNSAFE_HANDLES
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 /**
267 * Creates a new handle for the specified value.
268 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000269 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000270#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271
272 /**
273 * Creates a handle for the contents of the specified handle. This
274 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000275 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000277 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 * between compatible handles, for instance assigning a
279 * Handle<String> to a variable declared as Handle<Value>, is legal
280 * because String is a subclass of Value.
281 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000282 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 : val_(reinterpret_cast<T*>(*that)) {
284 /**
285 * This check fails when trying to convert between incompatible
286 * handles. For example, converting from a Handle<String> to a
287 * Handle<Number>.
288 */
289 TYPE_CHECK(T, S);
290 }
291
292 /**
293 * Returns true if the handle is empty.
294 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000295 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 /**
298 * Sets the handle to be empty. IsEmpty() will then return true.
299 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000300 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000301
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000302 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000303
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000304 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305
306 /**
307 * Checks whether two handles are the same.
308 * Returns true if both are empty, or if the objects
309 * to which they refer are identical.
310 * The handles' references are not checked.
311 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000312 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000313 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
314 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 if (a == 0) return b == 0;
316 if (b == 0) return false;
317 return *a == *b;
318 }
319
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000320#ifndef V8_USE_UNSAFE_HANDLES
321 template <class S> V8_INLINE(
322 bool operator==(const Persistent<S>& that) const) {
323 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
324 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
325 if (a == 0) return b == 0;
326 if (b == 0) return false;
327 return *a == *b;
328 }
329#endif
330
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 /**
332 * Checks whether two handles are different.
333 * Returns true if only one of the handles is empty, or if
334 * the objects to which they refer are different.
335 * The handles' references are not checked.
336 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000337 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 return !operator==(that);
339 }
340
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000341 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000342#ifdef V8_ENABLE_CHECKS
343 // If we're going to perform the type check then we have to check
344 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000346#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 return Handle<T>(T::Cast(*that));
348 }
349
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000350 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000351 return Handle<S>::Cast(*this);
352 }
353
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000354#ifndef V8_USE_UNSAFE_HANDLES
355 V8_INLINE(static Handle<T> New(Isolate* isolate, Handle<T> that)) {
356 return New(isolate, that.val_);
357 }
358 // TODO(dcarney): remove before cutover
359 V8_INLINE(static Handle<T> New(Isolate* isolate, const Persistent<T>& that)) {
360 return New(isolate, that.val_);
361 }
362
363#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
364
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000366#endif
367 /**
368 * Creates a new handle for the specified value.
369 */
370 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
371#endif
372
373 private:
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000374 template<class F> friend class Persistent;
375 template<class F> friend class Local;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000376 friend class Arguments;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000377 template<class F> friend class FunctionCallbackInfo;
378 template<class F> friend class PropertyCallbackInfo;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000379 friend class String;
380 friend class Object;
381 friend class AccessorInfo;
382 friend Handle<Primitive> Undefined(Isolate* isolate);
383 friend Handle<Primitive> Null(Isolate* isolate);
384 friend Handle<Boolean> True(Isolate* isolate);
385 friend Handle<Boolean> False(Isolate* isolate);
386 friend class Context;
387 friend class InternalHandleHelper;
388 friend class LocalContext;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000389 friend class HandleScope;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000390
391#ifndef V8_USE_UNSAFE_HANDLES
392 V8_INLINE(static Handle<T> New(Isolate* isolate, T* that));
393#endif
394
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395 T* val_;
396};
397
398
399/**
400 * A light-weight stack-allocated object handle. All operations
401 * that return objects from within v8 return them in local handles. They
402 * are created within HandleScopes, and all local handles allocated within a
403 * handle scope are destroyed when the handle scope is destroyed. Hence it
404 * is not necessary to explicitly deallocate local handles.
405 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000406// TODO(dcarney): deprecate entire class
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000407template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000409 V8_INLINE(Local());
410 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 : Handle<T>(reinterpret_cast<T*>(*that)) {
412 /**
413 * This check fails when trying to convert between incompatible
414 * handles. For example, converting from a Handle<String> to a
415 * Handle<Number>.
416 */
417 TYPE_CHECK(T, S);
418 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000419
420
421#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000422 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000423#endif
424
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000425 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000426#ifdef V8_ENABLE_CHECKS
427 // If we're going to perform the type check then we have to check
428 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000430#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 return Local<T>(T::Cast(*that));
432 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000433#ifndef V8_USE_UNSAFE_HANDLES
434 template <class S> V8_INLINE(Local(Handle<S> that))
435 : Handle<T>(reinterpret_cast<T*>(*that)) {
436 TYPE_CHECK(T, S);
437 }
438#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000440 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000441 return Local<S>::Cast(*this);
442 }
443
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000444 /**
445 * Create a local handle for the content of another handle.
446 * The referee is kept alive by the local handle even when
447 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000449 V8_INLINE(static Local<T> New(Handle<T> that));
450 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000451#ifndef V8_USE_UNSAFE_HANDLES
452 // TODO(dcarney): remove before cutover
453 V8_INLINE(static Local<T> New(Isolate* isolate, const Persistent<T>& that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000455#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
456
457 private:
458#endif
459 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
460#endif
461
462 private:
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000463 template<class F> friend class Persistent;
464 template<class F> friend class Handle;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000465 friend class Arguments;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000466 template<class F> friend class FunctionCallbackInfo;
467 template<class F> friend class PropertyCallbackInfo;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000468 friend class String;
469 friend class Object;
470 friend class AccessorInfo;
471 friend class Context;
472 friend class InternalHandleHelper;
473 friend class LocalContext;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000474 friend class HandleScope;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000475
476 V8_INLINE(static Local<T> New(Isolate* isolate, T* that));
477};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478
479/**
480 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000481 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482 * allocated, a Persistent handle remains valid until it is explicitly
483 * disposed.
484 *
485 * A persistent handle contains a reference to a storage cell within
486 * the v8 engine which holds an object value and which is updated by
487 * the garbage collector whenever the object is moved. A new storage
488 * cell can be created using Persistent::New and existing handles can
489 * be disposed using Persistent::Dispose. Since persistent handles
490 * are passed by value you may have many persistent handle objects
491 * that point to the same storage cell. For instance, if you pass a
492 * persistent handle as an argument to a function you will not get two
493 * different storage cells but rather two references to the same
494 * storage cell.
495 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000496template <class T> class Persistent // NOLINT
497#ifdef V8_USE_UNSAFE_HANDLES
498 : public Handle<T> {
499#else
500 { // NOLINT
501#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 public:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000503#ifndef V8_USE_UNSAFE_HANDLES
504 V8_INLINE(Persistent()) : val_(0) { }
505 V8_INLINE(~Persistent()) {
506 // TODO(dcarney): add this back before cutover.
507 // Dispose();
508 }
509 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
510 // TODO(dcarney): remove somehow before cutover
511 // The handle should either be 0, or a pointer to a live cell.
512 V8_INLINE(void Clear()) { val_ = 0; }
513
514 /**
515 * A constructor that creates a new global cell pointing to that. In contrast
516 * to the copy constructor, this creates a new persistent handle which needs
517 * to be separately disposed.
518 */
519 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
520 : val_(*New(isolate, that)) { }
521
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000522 template <class S> V8_INLINE(Persistent(Isolate* isolate,
523 Persistent<S>& that)) // NOLINT
524 : val_(*New(isolate, that)) { }
525
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000526#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 /**
528 * Creates an empty persistent handle that doesn't point to any
529 * storage cell.
530 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000531 V8_INLINE(Persistent()) : Handle<T>() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532
533 /**
534 * Creates a persistent handle for the same storage cell as the
535 * specified handle. This constructor allows you to pass persistent
536 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000537 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000539 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 * between compatible persistent handles, for instance assigning a
541 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000542 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000544 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 : Handle<T>(reinterpret_cast<T*>(*that)) {
546 /**
547 * This check fails when trying to convert between incompatible
548 * handles. For example, converting from a Handle<String> to a
549 * Handle<Number>.
550 */
551 TYPE_CHECK(T, S);
552 }
553
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000554 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000556 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000557 * A constructor that creates a new global cell pointing to that. In contrast
558 * to the copy constructor, this creates a new persistent handle which needs
559 * to be separately disposed.
560 */
561 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
562 : Handle<T>(New(isolate, that)) { }
563
564 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000565 * "Casts" a plain handle which is known to be a persistent handle
566 * to a persistent handle.
567 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000568 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 : Handle<T>(*that) { }
570
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000571#endif
572
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000573#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000574 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000575#ifdef V8_ENABLE_CHECKS
576 // If we're going to perform the type check then we have to check
577 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000579#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 return Persistent<T>(T::Cast(*that));
581 }
582
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000583 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000584 return Persistent<S>::Cast(*this);
585 }
586
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000587#else
588 template <class S>
589 V8_INLINE(static Persistent<T>& Cast(Persistent<S>& that)) { // NOLINT
590#ifdef V8_ENABLE_CHECKS
591 // If we're going to perform the type check then we have to check
592 // that the handle isn't empty before doing the checked cast.
593 if (!that.IsEmpty()) T::Cast(*that);
594#endif
595 return reinterpret_cast<Persistent<T>&>(that);
596 }
597
598 template <class S> V8_INLINE(Persistent<S>& As()) { // NOLINT
599 return Persistent<S>::Cast(*this);
600 }
601#endif
602
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000603 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
604
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000606 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000608 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000609 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000610#ifndef V8_USE_UNSAFE_HANDLES
611 // TODO(dcarney): remove before cutover
612 V8_INLINE(static Persistent<T> New(Isolate* isolate, Persistent<T> that));
613#endif
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000614
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000615#ifndef V8_USE_UNSAFE_HANDLES
616 template <class S> V8_INLINE(
617 bool operator==(const Persistent<S>& that) const) {
618 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
619 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
620 if (a == 0) return b == 0;
621 if (b == 0) return false;
622 return *a == *b;
623 }
624
625 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
626 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
627 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
628 if (a == 0) return b == 0;
629 if (b == 0) return false;
630 return *a == *b;
631 }
632#endif
633
634 V8_INLINE(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635
636 /**
637 * Releases the storage cell referenced by this persistent handle.
638 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000639 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 * cell remain and IsEmpty will still return false.
641 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000642 // TODO(dcarney): deprecate
643 V8_INLINE(void Dispose(Isolate* isolate)) { Dispose(); }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000644
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645 /**
646 * Make the reference to this object weak. When only weak handles
647 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000648 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 * it the object reference and the given parameters.
650 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000651 template<typename S, typename P>
652 V8_INLINE(void MakeWeak(
653 P* parameters,
654 typename WeakReferenceCallbacks<S, P>::Revivable callback));
655
656 template<typename P>
657 V8_INLINE(void MakeWeak(
658 P* parameters,
659 typename WeakReferenceCallbacks<T, P>::Revivable callback));
660
661 // TODO(dcarney): deprecate
662 template<typename S, typename P>
663 V8_INLINE(void MakeWeak(
664 Isolate* isolate,
665 P* parameters,
666 typename WeakReferenceCallbacks<S, P>::Revivable callback)) {
667 MakeWeak<S, P>(parameters, callback);
668 }
669
670 // TODO(dcarney): deprecate
671 template<typename P>
672 V8_INLINE(void MakeWeak(
673 Isolate* isolate,
674 P* parameters,
675 typename WeakReferenceCallbacks<T, P>::Revivable callback)) {
676 MakeWeak<P>(parameters, callback);
677 }
678
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000679 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000680 V8_INLINE(void MakeWeak(Isolate* isolate,
681 void* parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000682 NearDeathCallback callback));
683
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000684 V8_INLINE(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000686 // TODO(dcarney): deprecate
687 V8_INLINE(void ClearWeak(Isolate* isolate)) { ClearWeak(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688
689 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000690 * Marks the reference to this object independent. Garbage collector is free
691 * to ignore any object groups containing this object. Weak callback for an
692 * independent handle should not assume that it will be preceded by a global
693 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000694 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000695 V8_INLINE(void MarkIndependent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000696
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000697 // TODO(dcarney): deprecate
698 V8_INLINE(void MarkIndependent(Isolate* isolate)) { MarkIndependent(); }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000699
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000700 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000701 * Marks the reference to this object partially dependent. Partially dependent
702 * handles only depend on other partially dependent handles and these
703 * dependencies are provided through object groups. It provides a way to build
704 * smaller object groups for young objects that represent only a subset of all
705 * external dependencies. This mark is automatically cleared after each
706 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000707 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000708 V8_INLINE(void MarkPartiallyDependent());
709
710 // TODO(dcarney): deprecate
711 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate)) {
712 MarkPartiallyDependent();
713 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000714
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000715 V8_INLINE(bool IsIndependent() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000716
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000717 // TODO(dcarney): deprecate
718 V8_INLINE(bool IsIndependent(Isolate* isolate) const) {
719 return IsIndependent();
720 }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000721
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000722 /** Checks if the handle holds the only reference to an object. */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000723 V8_INLINE(bool IsNearDeath() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000724
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000725 // TODO(dcarney): deprecate
726 V8_INLINE(bool IsNearDeath(Isolate* isolate) const) { return IsNearDeath(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000728 /** Returns true if the handle's reference is weak. */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000729 V8_INLINE(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000731 // TODO(dcarney): deprecate
732 V8_INLINE(bool IsWeak(Isolate* isolate) const) { return IsWeak(); }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000733
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000734 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000735 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
736 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000737 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000738 V8_INLINE(void SetWrapperClassId(uint16_t class_id));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000739
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000740 // TODO(dcarney): deprecate
741 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id)) {
742 SetWrapperClassId(class_id);
743 }
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000744
745 /**
746 * Returns the class ID previously assigned to this handle or 0 if no class ID
747 * was previously assigned.
748 */
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000749 V8_INLINE(uint16_t WrapperClassId() const);
750
751 // TODO(dcarney): deprecate
752 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const) {
753 return WrapperClassId();
754 }
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000755
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000756 /**
757 * Disposes the current contents of the handle and replaces it.
758 */
759 V8_INLINE(void Reset(Isolate* isolate, const Handle<T>& other));
760
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000761#ifndef V8_USE_UNSAFE_HANDLES
762 V8_INLINE(void Reset(Isolate* isolate, const Persistent<T>& other));
763#endif
764
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000765 /**
766 * Returns the underlying raw pointer and clears the handle. The caller is
767 * responsible of eventually destroying the underlying object (by creating a
768 * Persistent handle which points to it and Disposing it). In the future,
769 * destructing a Persistent will also Dispose it. With this function, the
770 * embedder can let the Persistent go out of scope without it getting
771 * disposed.
772 */
773 V8_INLINE(T* ClearAndLeak());
774
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000775#ifndef V8_USE_UNSAFE_HANDLES
776
777#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
778
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000780#endif
781 // TODO(dcarney): make unlinkable before cutover
782 V8_INLINE(Persistent(const Persistent& that)) : val_(that.val_) {}
783 // TODO(dcarney): make unlinkable before cutover
784 V8_INLINE(Persistent& operator=(const Persistent& that)) { // NOLINT
785 this->val_ = that.val_;
786 return *this;
787 }
788
789 public:
790#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
791
792 private:
793#endif
794 // TODO(dcarney): remove before cutover
795 template <class S> V8_INLINE(Persistent(S* that)) : val_(that) { }
796 // TODO(dcarney): remove before cutover
797 template <class S> V8_INLINE(Persistent(Persistent<S> that))
798 : val_(*that) {
799 TYPE_CHECK(T, S);
800 }
801 // TODO(dcarney): remove before cutover
802 V8_INLINE(T* operator*() const) { return val_; }
803 public:
804#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW
805
806 private:
807#endif
808 // TODO(dcarney): remove before cutover
809 V8_INLINE(T* operator->() const) { return val_; }
810 public:
811#endif
812
813 private:
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000814 template<class F> friend class Handle;
815 template<class F> friend class Local;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816 friend class ImplementationUtilities;
817 friend class ObjectTemplate;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000818 friend class Context;
819 friend class InternalHandleHelper;
820 friend class LocalContext;
821
822 V8_INLINE(static Persistent<T> New(Isolate* isolate, T* that));
823
824#ifndef V8_USE_UNSAFE_HANDLES
825 T* val_;
826#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827};
828
829
v8.team.kasperl727e9952008-09-02 14:56:44 +0000830 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831 * A stack-allocated class that governs a number of local handles.
832 * After a handle scope has been created, all local handles will be
833 * allocated within that handle scope until either the handle scope is
834 * deleted or another handle scope is created. If there is already a
835 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000836 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837 * new handles will again be allocated in the original handle scope.
838 *
839 * After the handle scope of a local handle has been deleted the
840 * garbage collector will no longer track the object stored in the
841 * handle and may deallocate it. The behavior of accessing a handle
842 * for which the handle scope has been deleted is undefined.
843 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000844class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000846 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000847 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000849 HandleScope(Isolate* isolate);
850
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000851 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852
853 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854 * Closes the handle scope and returns the value as a handle in the
855 * previous scope, which is the new current scope after the call.
856 */
857 template <class T> Local<T> Close(Handle<T> value);
858
859 /**
860 * Counts the number of allocated handles.
861 */
862 static int NumberOfHandles();
863
864 /**
865 * Creates a new handle with the given value.
866 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000867 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000868 static internal::Object** CreateHandle(internal::Isolate* isolate,
869 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000870 // Faster version, uses HeapObject to obtain the current Isolate.
871 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
873 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000874 // Make it hard to create heap-allocated or illegal handle scopes by
875 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 HandleScope(const HandleScope&);
877 void operator=(const HandleScope&);
878 void* operator new(size_t size);
879 void operator delete(void*, size_t);
880
ager@chromium.org3811b432009-10-28 14:53:37 +0000881 // This Data class is accessible internally as HandleScopeData through a
882 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000883 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000885 internal::Object** next;
886 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000887 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000888 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000890 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 }
892 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000893
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000894 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000895 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000897 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000898 internal::Object** prev_next_;
899 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000901 // Allow for the active closing of HandleScopes which allows to pass a handle
902 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000904 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 friend class ImplementationUtilities;
907};
908
909
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000910// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911
912
913/**
914 * The superclass of values and API object templates.
915 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000916class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917 private:
918 Data();
919};
920
921
922/**
923 * Pre-compilation data that can be associated with a script. This
924 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000925 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000926 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000928class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 public:
930 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000931
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000932 /**
933 * Pre-compiles the specified script (context-independent).
934 *
935 * \param input Pointer to UTF-8 script source code.
936 * \param length Length of UTF-8 script source code.
937 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000940 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000941 * Pre-compiles the specified script (context-independent).
942 *
943 * NOTE: Pre-compilation using this method cannot happen on another thread
944 * without using Lockers.
945 *
946 * \param source Script source code.
947 */
948 static ScriptData* PreCompile(Handle<String> source);
949
950 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000951 * Load previous pre-compilation data.
952 *
953 * \param data Pointer to data returned by a call to Data() of a previous
954 * ScriptData. Ownership is not transferred.
955 * \param length Length of data.
956 */
957 static ScriptData* New(const char* data, int length);
958
959 /**
960 * Returns the length of Data().
961 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000963
964 /**
965 * Returns a serialized representation of this ScriptData that can later be
966 * passed to New(). NOTE: Serialized data is platform-dependent.
967 */
968 virtual const char* Data() = 0;
969
970 /**
971 * Returns true if the source code could not be parsed.
972 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000973 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974};
975
976
977/**
978 * The origin, within a file, of a script.
979 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000980class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000982 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000983 Handle<Value> resource_name,
984 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000985 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 : resource_name_(resource_name),
987 resource_line_offset_(resource_line_offset),
988 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000989 V8_INLINE(Handle<Value> ResourceName() const);
990 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
991 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000993 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994 Handle<Integer> resource_line_offset_;
995 Handle<Integer> resource_column_offset_;
996};
997
998
999/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001000 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001002class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001005 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001006 *
ager@chromium.org5c838252010-02-19 08:53:10 +00001007 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001008 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +00001009 * when New() returns
1010 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1011 * using pre_data speeds compilation if it's done multiple times.
1012 * Owned by caller, no references are kept when New() returns.
1013 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001014 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +00001015 * available to compile event handlers.
1016 * \return Compiled script object (context independent; when run it
1017 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018 */
ager@chromium.org5c838252010-02-19 08:53:10 +00001019 static Local<Script> New(Handle<String> source,
1020 ScriptOrigin* origin = NULL,
1021 ScriptData* pre_data = NULL,
1022 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023
mads.s.agercbaa0602008-08-14 13:41:48 +00001024 /**
1025 * Compiles the specified script using the specified file name
1026 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001027 *
ager@chromium.org5c838252010-02-19 08:53:10 +00001028 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001029 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +00001030 * as the script's origin.
1031 * \return Compiled script object (context independent; when run it
1032 * will use the currently entered context).
1033 */
1034 static Local<Script> New(Handle<String> source,
1035 Handle<Value> file_name);
1036
1037 /**
1038 * Compiles the specified script (bound to current context).
1039 *
1040 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001041 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +00001042 * when Compile() returns
1043 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1044 * using pre_data speeds compilation if it's done multiple times.
1045 * Owned by caller, no references are kept when Compile() returns.
1046 * \param script_data Arbitrary data associated with script. Using
1047 * this has same effect as calling SetData(), but makes data available
1048 * earlier (i.e. to compile event handlers).
1049 * \return Compiled script object, bound to the context that was active
1050 * when this function was called. When run it will always use this
1051 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +00001052 */
1053 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +00001054 ScriptOrigin* origin = NULL,
1055 ScriptData* pre_data = NULL,
1056 Handle<String> script_data = Handle<String>());
1057
1058 /**
1059 * Compiles the specified script using the specified file name
1060 * object (typically a string) as the script's origin.
1061 *
1062 * \param source Script source code.
1063 * \param file_name File name to use as script's origin
1064 * \param script_data Arbitrary data associated with script. Using
1065 * this has same effect as calling SetData(), but makes data available
1066 * earlier (i.e. to compile event handlers).
1067 * \return Compiled script object, bound to the context that was active
1068 * when this function was called. When run it will always use this
1069 * context.
1070 */
1071 static Local<Script> Compile(Handle<String> source,
1072 Handle<Value> file_name,
1073 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +00001074
v8.team.kasperl727e9952008-09-02 14:56:44 +00001075 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001076 * Runs the script returning the resulting value. If the script is
1077 * context independent (created using ::New) it will be run in the
1078 * currently entered context. If it is context specific (created
1079 * using ::Compile) it will be run in the context in which it was
1080 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001081 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001083
1084 /**
1085 * Returns the script id value.
1086 */
1087 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001088
1089 /**
1090 * Associate an additional data object with the script. This is mainly used
1091 * with the debugger as this data object is only available through the
1092 * debugger API.
1093 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00001094 void SetData(Handle<String> data);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001095
1096 /**
1097 * Returns the name value of one Script.
1098 */
1099 Handle<Value> GetScriptName();
1100
1101 /**
1102 * Returns zero based line number of the code_pos location in the script.
1103 * -1 will be returned if no information available.
1104 */
1105 int GetLineNumber(int code_pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106};
1107
1108
1109/**
1110 * An error message.
1111 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001112class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001114 Local<String> Get() const;
1115 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001117 /**
1118 * Returns the resource name for the script from where the function causing
1119 * the error originates.
1120 */
ager@chromium.org32912102009-01-16 10:38:43 +00001121 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001123 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001124 * Returns the resource data for the script from where the function causing
1125 * the error originates.
1126 */
1127 Handle<Value> GetScriptData() const;
1128
1129 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001130 * Exception stack trace. By default stack traces are not captured for
1131 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1132 * to change this option.
1133 */
1134 Handle<StackTrace> GetStackTrace() const;
1135
1136 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001137 * Returns the number, 1-based, of the line where the error occurred.
1138 */
ager@chromium.org32912102009-01-16 10:38:43 +00001139 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001141 /**
1142 * Returns the index within the script of the first character where
1143 * the error occurred.
1144 */
ager@chromium.org32912102009-01-16 10:38:43 +00001145 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001146
1147 /**
1148 * Returns the index within the script of the last character where
1149 * the error occurred.
1150 */
ager@chromium.org32912102009-01-16 10:38:43 +00001151 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001152
1153 /**
1154 * Returns the index within the line of the first character where
1155 * the error occurred.
1156 */
ager@chromium.org32912102009-01-16 10:38:43 +00001157 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001158
1159 /**
1160 * Returns the index within the line of the last character where
1161 * the error occurred.
1162 */
ager@chromium.org32912102009-01-16 10:38:43 +00001163 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001164
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165 // TODO(1245381): Print to a string instead of on a FILE.
1166 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001167
1168 static const int kNoLineNumberInfo = 0;
1169 static const int kNoColumnInfo = 0;
1170};
1171
1172
1173/**
1174 * Representation of a JavaScript stack trace. The information collected is a
1175 * snapshot of the execution stack and the information remains valid after
1176 * execution continues.
1177 */
1178class V8EXPORT StackTrace {
1179 public:
1180 /**
1181 * Flags that determine what information is placed captured for each
1182 * StackFrame when grabbing the current stack trace.
1183 */
1184 enum StackTraceOptions {
1185 kLineNumber = 1,
1186 kColumnOffset = 1 << 1 | kLineNumber,
1187 kScriptName = 1 << 2,
1188 kFunctionName = 1 << 3,
1189 kIsEval = 1 << 4,
1190 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001191 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001192 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001193 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001194 };
1195
1196 /**
1197 * Returns a StackFrame at a particular index.
1198 */
1199 Local<StackFrame> GetFrame(uint32_t index) const;
1200
1201 /**
1202 * Returns the number of StackFrames.
1203 */
1204 int GetFrameCount() const;
1205
1206 /**
1207 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1208 */
1209 Local<Array> AsArray();
1210
1211 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001212 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001213 *
1214 * \param frame_limit The maximum number of stack frames we want to capture.
1215 * \param options Enumerates the set of things we will capture for each
1216 * StackFrame.
1217 */
1218 static Local<StackTrace> CurrentStackTrace(
1219 int frame_limit,
1220 StackTraceOptions options = kOverview);
1221};
1222
1223
1224/**
1225 * A single JavaScript stack frame.
1226 */
1227class V8EXPORT StackFrame {
1228 public:
1229 /**
1230 * Returns the number, 1-based, of the line for the associate function call.
1231 * This method will return Message::kNoLineNumberInfo if it is unable to
1232 * retrieve the line number, or if kLineNumber was not passed as an option
1233 * when capturing the StackTrace.
1234 */
1235 int GetLineNumber() const;
1236
1237 /**
1238 * Returns the 1-based column offset on the line for the associated function
1239 * call.
1240 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1241 * the column number, or if kColumnOffset was not passed as an option when
1242 * capturing the StackTrace.
1243 */
1244 int GetColumn() const;
1245
1246 /**
1247 * Returns the name of the resource that contains the script for the
1248 * function for this StackFrame.
1249 */
1250 Local<String> GetScriptName() const;
1251
1252 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001253 * Returns the name of the resource that contains the script for the
1254 * function for this StackFrame or sourceURL value if the script name
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001255 * is undefined and its source ends with //# sourceURL=... string or
1256 * deprecated //@ sourceURL=... string.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001257 */
1258 Local<String> GetScriptNameOrSourceURL() const;
1259
1260 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001261 * Returns the name of the function associated with this stack frame.
1262 */
1263 Local<String> GetFunctionName() const;
1264
1265 /**
1266 * Returns whether or not the associated function is compiled via a call to
1267 * eval().
1268 */
1269 bool IsEval() const;
1270
1271 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001272 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001273 * constructor via "new".
1274 */
1275 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276};
1277
1278
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001279// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280
1281
1282/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001283 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001285class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 /**
1288 * Returns true if this value is the undefined value. See ECMA-262
1289 * 4.3.10.
1290 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001291 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292
1293 /**
1294 * Returns true if this value is the null value. See ECMA-262
1295 * 4.3.11.
1296 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001297 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298
1299 /**
1300 * Returns true if this value is true.
1301 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001302 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303
1304 /**
1305 * Returns true if this value is false.
1306 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001307 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308
1309 /**
1310 * Returns true if this value is an instance of the String type.
1311 * See ECMA-262 8.4.
1312 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001313 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314
1315 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001316 * Returns true if this value is a symbol.
1317 * This is an experimental feature.
1318 */
1319 bool IsSymbol() const;
1320
1321 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 * Returns true if this value is a function.
1323 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001324 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325
1326 /**
1327 * Returns true if this value is an array.
1328 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001329 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330
v8.team.kasperl727e9952008-09-02 14:56:44 +00001331 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 * Returns true if this value is an object.
1333 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001334 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001335
v8.team.kasperl727e9952008-09-02 14:56:44 +00001336 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337 * Returns true if this value is boolean.
1338 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001339 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001340
v8.team.kasperl727e9952008-09-02 14:56:44 +00001341 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 * Returns true if this value is a number.
1343 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001344 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345
v8.team.kasperl727e9952008-09-02 14:56:44 +00001346 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 * Returns true if this value is external.
1348 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001349 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350
v8.team.kasperl727e9952008-09-02 14:56:44 +00001351 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 * Returns true if this value is a 32-bit signed integer.
1353 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001354 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001356 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001357 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001358 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001359 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001360
1361 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001362 * Returns true if this value is a Date.
1363 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001364 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001365
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001366 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001367 * Returns true if this value is a Boolean object.
1368 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001369 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001370
1371 /**
1372 * Returns true if this value is a Number object.
1373 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001374 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001375
1376 /**
1377 * Returns true if this value is a String object.
1378 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001379 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001380
1381 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001382 * Returns true if this value is a Symbol object.
1383 * This is an experimental feature.
1384 */
1385 bool IsSymbolObject() const;
1386
1387 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001388 * Returns true if this value is a NativeError.
1389 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001390 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001391
1392 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001393 * Returns true if this value is a RegExp.
1394 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001395 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001396
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001397
1398 /**
1399 * Returns true if this value is an ArrayBuffer.
1400 * This is an experimental feature.
1401 */
1402 bool IsArrayBuffer() const;
1403
1404 /**
1405 * Returns true if this value is one of TypedArrays.
1406 * This is an experimental feature.
1407 */
1408 bool IsTypedArray() const;
1409
1410 /**
1411 * Returns true if this value is an Uint8Array.
1412 * This is an experimental feature.
1413 */
1414 bool IsUint8Array() const;
1415
1416 /**
1417 * Returns true if this value is an Uint8ClampedArray.
1418 * This is an experimental feature.
1419 */
1420 bool IsUint8ClampedArray() const;
1421
1422 /**
1423 * Returns true if this value is an Int8Array.
1424 * This is an experimental feature.
1425 */
1426 bool IsInt8Array() const;
1427
1428 /**
1429 * Returns true if this value is an Uint16Array.
1430 * This is an experimental feature.
1431 */
1432 bool IsUint16Array() const;
1433
1434 /**
1435 * Returns true if this value is an Int16Array.
1436 * This is an experimental feature.
1437 */
1438 bool IsInt16Array() const;
1439
1440 /**
1441 * Returns true if this value is an Uint32Array.
1442 * This is an experimental feature.
1443 */
1444 bool IsUint32Array() const;
1445
1446 /**
1447 * Returns true if this value is an Int32Array.
1448 * This is an experimental feature.
1449 */
1450 bool IsInt32Array() const;
1451
1452 /**
1453 * Returns true if this value is a Float32Array.
1454 * This is an experimental feature.
1455 */
1456 bool IsFloat32Array() const;
1457
1458 /**
1459 * Returns true if this value is a Float64Array.
1460 * This is an experimental feature.
1461 */
1462 bool IsFloat64Array() const;
1463
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001464 Local<Boolean> ToBoolean() const;
1465 Local<Number> ToNumber() const;
1466 Local<String> ToString() const;
1467 Local<String> ToDetailString() const;
1468 Local<Object> ToObject() const;
1469 Local<Integer> ToInteger() const;
1470 Local<Uint32> ToUint32() const;
1471 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472
1473 /**
1474 * Attempts to convert a string to an array index.
1475 * Returns an empty handle if the conversion fails.
1476 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001477 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001479 bool BooleanValue() const;
1480 double NumberValue() const;
1481 int64_t IntegerValue() const;
1482 uint32_t Uint32Value() const;
1483 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484
1485 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001486 bool Equals(Handle<Value> that) const;
1487 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001488
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001489 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001490 V8_INLINE(bool QuickIsUndefined() const);
1491 V8_INLINE(bool QuickIsNull() const);
1492 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001493 bool FullIsUndefined() const;
1494 bool FullIsNull() const;
1495 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496};
1497
1498
1499/**
1500 * The superclass of primitive values. See ECMA-262 4.3.2.
1501 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001502class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503
1504
1505/**
1506 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1507 * or false value.
1508 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001509class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001510 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001511 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001512 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513};
1514
1515
1516/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001517 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001519class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001521 enum Encoding {
1522 UNKNOWN_ENCODING = 0x1,
1523 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001524 ASCII_ENCODING = 0x4,
1525 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001526 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001527 /**
1528 * Returns the number of characters in this string.
1529 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001530 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531
v8.team.kasperl727e9952008-09-02 14:56:44 +00001532 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001533 * Returns the number of bytes in the UTF-8 encoded
1534 * representation of this string.
1535 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001536 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001537
1538 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001539 * This function is no longer useful.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001540 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001541 V8_DEPRECATED(V8_INLINE(bool MayContainNonAscii()) const) { return true; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001542
1543 /**
1544 * Returns whether this string contains only one byte data.
1545 */
1546 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001547
1548 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001549 * Write the contents of the string to an external buffer.
1550 * If no arguments are given, expects the buffer to be large
1551 * enough to hold the entire string and NULL terminator. Copies
1552 * the contents of the string and the NULL terminator into the
1553 * buffer.
1554 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001555 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1556 * before the end of the buffer.
1557 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001558 * Copies up to length characters into the output buffer.
1559 * Only null-terminates if there is enough space in the buffer.
1560 *
1561 * \param buffer The buffer into which the string will be copied.
1562 * \param start The starting position within the string at which
1563 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001564 * \param length The number of characters to copy from the string. For
1565 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001566 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001567 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001568 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001569 * \return The number of characters copied to the buffer excluding the null
1570 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001571 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001572 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001573 enum WriteOptions {
1574 NO_OPTIONS = 0,
1575 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001576 NO_NULL_TERMINATION = 2,
1577 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001578 };
1579
lrn@chromium.org34e60782011-09-15 07:25:40 +00001580 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001581 int Write(uint16_t* buffer,
1582 int start = 0,
1583 int length = -1,
1584 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001585 // ASCII characters.
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001586 V8_DEPRECATED(int WriteAscii(char* buffer,
1587 int start = 0,
1588 int length = -1,
1589 int options = NO_OPTIONS) const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001590 // One byte characters.
1591 int WriteOneByte(uint8_t* buffer,
1592 int start = 0,
1593 int length = -1,
1594 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001595 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001596 int WriteUtf8(char* buffer,
1597 int length = -1,
1598 int* nchars_ref = NULL,
1599 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600
v8.team.kasperl727e9952008-09-02 14:56:44 +00001601 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001602 * A zero length string.
1603 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001604 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001605 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001606
1607 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001608 * Returns true if the string is external
1609 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001610 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001611
v8.team.kasperl727e9952008-09-02 14:56:44 +00001612 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001613 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001614 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001615 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001616
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001617 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001618 public:
1619 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001620
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001621 protected:
1622 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001623
1624 /**
1625 * Internally V8 will call this Dispose method when the external string
1626 * resource is no longer needed. The default implementation will use the
1627 * delete operator. This method can be overridden in subclasses to
1628 * control how allocated external string resources are disposed.
1629 */
1630 virtual void Dispose() { delete this; }
1631
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001632 private:
1633 // Disallow copying and assigning.
1634 ExternalStringResourceBase(const ExternalStringResourceBase&);
1635 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001636
1637 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001638 };
1639
v8.team.kasperl727e9952008-09-02 14:56:44 +00001640 /**
1641 * An ExternalStringResource is a wrapper around a two-byte string
1642 * buffer that resides outside V8's heap. Implement an
1643 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001644 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001645 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001646 class V8EXPORT ExternalStringResource
1647 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 public:
1649 /**
1650 * Override the destructor to manage the life cycle of the underlying
1651 * buffer.
1652 */
1653 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001654
1655 /**
1656 * The string data from the underlying buffer.
1657 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001659
1660 /**
1661 * The length of the string. That is, the number of two-byte characters.
1662 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001664
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665 protected:
1666 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667 };
1668
1669 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001670 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001671 * string buffer that resides outside V8's heap. Implement an
1672 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001673 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001674 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001675 * UTF-8, which would require special treatment internally in the
1676 * engine and, in the case of UTF-8, do not allow efficient indexing.
1677 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001678 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001680 class V8EXPORT ExternalAsciiStringResource
1681 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 public:
1683 /**
1684 * Override the destructor to manage the life cycle of the underlying
1685 * buffer.
1686 */
1687 virtual ~ExternalAsciiStringResource() {}
1688 /** The string data from the underlying buffer.*/
1689 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001690 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691 virtual size_t length() const = 0;
1692 protected:
1693 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694 };
1695
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001696 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1697
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001699 * If the string is an external string, return the ExternalStringResourceBase
1700 * regardless of the encoding, otherwise return NULL. The encoding of the
1701 * string is returned in encoding_out.
1702 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001703 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1704 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001705
1706 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001707 * Get the ExternalStringResource for an external string. Returns
1708 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001710 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711
1712 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001713 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001714 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001716 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001718 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001720 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001722 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001723 * The second parameter 'length' gives the buffer length. If omitted,
1724 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001726 V8_INLINE(static Local<String> New(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001728 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001729 /** Allocates a new string from 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001730 V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001732 // TODO(dcarney): deprecate
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001733 /**
1734 * Creates an internalized string (historically called a "symbol",
1735 * not to be confused with ES6 symbols). Returns one if it exists already.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001736 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001737 V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
1738
1739 enum NewStringType {
1740 kNormalString, kInternalizedString, kUndetectableString
1741 };
1742
1743 /** Allocates a new string from UTF-8 data.*/
1744 static Local<String> NewFromUtf8(Isolate* isolate,
1745 const char* data,
1746 NewStringType type = kNormalString,
1747 int length = -1);
1748
1749 /** Allocates a new string from Latin-1 data.*/
1750 static Local<String> NewFromOneByte(
1751 Isolate* isolate,
1752 const uint8_t* data,
1753 NewStringType type = kNormalString,
1754 int length = -1);
1755
1756 /** Allocates a new string from UTF-16 data.*/
1757 static Local<String> NewFromTwoByte(
1758 Isolate* isolate,
1759 const uint16_t* data,
1760 NewStringType type = kNormalString,
1761 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762
v8.team.kasperl727e9952008-09-02 14:56:44 +00001763 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001764 * Creates a new string by concatenating the left and the right strings
1765 * passed in as parameters.
1766 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001767 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001768
1769 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001770 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001771 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001772 * resource will be disposed by calling its Dispose method. The caller of
1773 * this function should not otherwise delete or modify the resource. Neither
1774 * should the underlying buffer be deallocated or modified except through the
1775 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001776 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001777 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001778
ager@chromium.org6f10e412009-02-13 10:11:16 +00001779 /**
1780 * Associate an external string resource with this string by transforming it
1781 * in place so that existing references to this string in the JavaScript heap
1782 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001783 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001784 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001785 * The string is not modified if the operation fails. See NewExternal for
1786 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001787 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001788 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789
v8.team.kasperl727e9952008-09-02 14:56:44 +00001790 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001791 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001792 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001793 * resource will be disposed by calling its Dispose method. The caller of
1794 * this function should not otherwise delete or modify the resource. Neither
1795 * should the underlying buffer be deallocated or modified except through the
1796 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001797 */
1798 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001799
ager@chromium.org6f10e412009-02-13 10:11:16 +00001800 /**
1801 * Associate an external string resource with this string by transforming it
1802 * in place so that existing references to this string in the JavaScript heap
1803 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001804 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001805 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001806 * The string is not modified if the operation fails. See NewExternal for
1807 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001808 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001809 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001811 /**
1812 * Returns true if this string can be made external.
1813 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001814 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001815
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001816 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001817 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001818 V8_INLINE(
1819 static Local<String> NewUndetectable(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001821 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001822 /** Creates an undetectable string from the supplied 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001823 V8_INLINE(static Local<String> NewUndetectable(
1824 const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825
1826 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001827 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001828 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001829 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001830 * then the length() method returns 0 and the * operator returns
1831 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001832 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001833 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001834 public:
1835 explicit Utf8Value(Handle<v8::Value> obj);
1836 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001837 char* operator*() { return str_; }
1838 const char* operator*() const { return str_; }
1839 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001840 private:
1841 char* str_;
1842 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001843
1844 // Disallow copying and assigning.
1845 Utf8Value(const Utf8Value&);
1846 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001847 };
1848
1849 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001850 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001852 * If conversion to a string fails (eg. due to an exception in the toString()
1853 * method of the object) then the length() method returns 0 and the * operator
1854 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001856 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 public:
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001858 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 explicit AsciiValue(Handle<v8::Value> obj);
1860 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001861 char* operator*() { return str_; }
1862 const char* operator*() const { return str_; }
1863 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 private:
1865 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001866 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001867
1868 // Disallow copying and assigning.
1869 AsciiValue(const AsciiValue&);
1870 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871 };
1872
1873 /**
1874 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001875 * If conversion to a string fails (eg. due to an exception in the toString()
1876 * method of the object) then the length() method returns 0 and the * operator
1877 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001879 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 public:
1881 explicit Value(Handle<v8::Value> obj);
1882 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001883 uint16_t* operator*() { return str_; }
1884 const uint16_t* operator*() const { return str_; }
1885 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 private:
1887 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001888 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001889
1890 // Disallow copying and assigning.
1891 Value(const Value&);
1892 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001894
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001895 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001896 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1897 Encoding encoding) const;
1898 void VerifyExternalStringResource(ExternalStringResource* val) const;
1899 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900};
1901
1902
1903/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001904 * A JavaScript symbol (ECMA-262 edition 6)
1905 *
1906 * This is an experimental feature. Use at your own risk.
1907 */
1908class V8EXPORT Symbol : public Primitive {
1909 public:
1910 // Returns the print name string of the symbol, or undefined if none.
1911 Local<Value> Name() const;
1912
1913 // Create a symbol without a print name.
1914 static Local<Symbol> New(Isolate* isolate);
1915
1916 // Create a symbol with a print name.
1917 static Local<Symbol> New(Isolate *isolate, const char* data, int length = -1);
1918
1919 V8_INLINE(static Symbol* Cast(v8::Value* obj));
1920 private:
1921 Symbol();
1922 static void CheckCast(v8::Value* obj);
1923};
1924
1925
1926/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001927 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001929class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001931 double Value() const;
1932 static Local<Number> New(double value);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001933 static Local<Number> New(Isolate* isolate, double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001934 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001936 Number();
1937 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938};
1939
1940
1941/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001942 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001944class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001946 static Local<Integer> New(int32_t value);
1947 static Local<Integer> NewFromUnsigned(uint32_t value);
1948 static Local<Integer> New(int32_t value, Isolate*);
1949 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1950 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001951 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001953 Integer();
1954 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955};
1956
1957
1958/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001959 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001961class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001963 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001965 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966};
1967
1968
1969/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001970 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001972class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001974 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001976 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977};
1978
1979
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980enum PropertyAttribute {
1981 None = 0,
1982 ReadOnly = 1 << 0,
1983 DontEnum = 1 << 1,
1984 DontDelete = 1 << 2
1985};
1986
ager@chromium.org3811b432009-10-28 14:53:37 +00001987enum ExternalArrayType {
1988 kExternalByteArray = 1,
1989 kExternalUnsignedByteArray,
1990 kExternalShortArray,
1991 kExternalUnsignedShortArray,
1992 kExternalIntArray,
1993 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001994 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001995 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001996 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001997};
1998
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002000 * Accessor[Getter|Setter] are used as callback functions when
2001 * setting|getting a particular property. See Object and ObjectTemplate's
2002 * method SetAccessor.
2003 */
2004typedef Handle<Value> (*AccessorGetter)(Local<String> property,
2005 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002006typedef void (*AccessorGetterCallback)(
2007 Local<String> property,
2008 const PropertyCallbackInfo<Value>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002009
2010
2011typedef void (*AccessorSetter)(Local<String> property,
2012 Local<Value> value,
2013 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002014typedef void (*AccessorSetterCallback)(
2015 Local<String> property,
2016 Local<Value> value,
2017 const PropertyCallbackInfo<void>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002018
2019
2020/**
2021 * Access control specifications.
2022 *
2023 * Some accessors should be accessible across contexts. These
2024 * accessors have an explicit access control parameter which specifies
2025 * the kind of cross-context access that should be allowed.
2026 *
2027 * Additionally, for security, accessors can prohibit overwriting by
2028 * accessors defined in JavaScript. For objects that have such
2029 * accessors either locally or in their prototype chain it is not
2030 * possible to overwrite the accessor by using __defineGetter__ or
2031 * __defineSetter__ from JavaScript code.
2032 */
2033enum AccessControl {
2034 DEFAULT = 0,
2035 ALL_CAN_READ = 1,
2036 ALL_CAN_WRITE = 1 << 1,
2037 PROHIBITS_OVERWRITING = 1 << 2
2038};
2039
2040
2041/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002042 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002044class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002046 bool Set(Handle<Value> key,
2047 Handle<Value> value,
2048 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002049
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002050 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002051
ager@chromium.orge2902be2009-06-08 12:21:35 +00002052 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002053 // overriding accessors or read-only properties.
2054 //
2055 // Note that if the object has an interceptor the property will be set
2056 // locally, but since the interceptor takes precedence the local property
2057 // will only be returned if the interceptor doesn't return a value.
2058 //
2059 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002060 bool ForceSet(Handle<Value> key,
2061 Handle<Value> value,
2062 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002063
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002064 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002066 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002067
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002068 /**
2069 * Gets the property attributes of a property which can be None or
2070 * any combination of ReadOnly, DontEnum and DontDelete. Returns
2071 * None when the property doesn't exist.
2072 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002073 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002074
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002075 bool Has(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002076
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002077 bool Delete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002078
2079 // Delete a property on this object bypassing interceptors and
2080 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002081 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002082
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002083 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002084
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002085 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002087 // TODO(dcarney): deprecate
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002088 bool SetAccessor(Handle<String> name,
2089 AccessorGetter getter,
2090 AccessorSetter setter = 0,
2091 Handle<Value> data = Handle<Value>(),
2092 AccessControl settings = DEFAULT,
2093 PropertyAttribute attribute = None);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002094 bool SetAccessor(Handle<String> name,
2095 AccessorGetterCallback getter,
2096 AccessorSetterCallback setter = 0,
2097 Handle<Value> data = Handle<Value>(),
2098 AccessControl settings = DEFAULT,
2099 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002100
ulan@chromium.org750145a2013-03-07 15:14:13 +00002101 // This function is not yet stable and should not be used at this time.
2102 bool SetAccessor(Handle<String> name,
2103 Handle<DeclaredAccessorDescriptor> descriptor,
2104 AccessControl settings = DEFAULT,
2105 PropertyAttribute attribute = None);
2106
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002108 * Returns an array containing the names of the enumerable properties
2109 * of this object, including properties from prototype objects. The
2110 * array returned by this method contains the same values as would
2111 * be enumerated by a for-in statement over this object.
2112 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002113 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002114
2115 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002116 * This function has the same functionality as GetPropertyNames but
2117 * the returned array doesn't contain the names of properties from
2118 * prototype objects.
2119 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002120 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002121
2122 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 * Get the prototype object. This does not skip objects marked to
2124 * be skipped by __proto__ and it does not consult the security
2125 * handler.
2126 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002127 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128
2129 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002130 * Set the prototype object. This does not skip objects marked to
2131 * be skipped by __proto__ and it does not consult the security
2132 * handler.
2133 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002134 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00002135
2136 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002137 * Finds an instance of the given function template in the prototype
2138 * chain.
2139 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002140 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002141
2142 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 * Call builtin Object.prototype.toString on this object.
2144 * This is different from Value::ToString() that may call
2145 * user-defined toString function. This one does not.
2146 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002147 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002149 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002150 * Returns the function invoked as a constructor for this object.
2151 * May be the null value.
2152 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002153 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002154
2155 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002156 * Returns the name of the function invoked as a constructor for this object.
2157 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002158 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002159
kasper.lund212ac232008-07-16 07:07:30 +00002160 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002161 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002162
2163 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002164 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002165
kasper.lund212ac232008-07-16 07:07:30 +00002166 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002167 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002168
2169 /**
2170 * Gets a 2-byte-aligned native pointer from an internal field. This field
2171 * must have been set by SetAlignedPointerInInternalField, everything else
2172 * leads to undefined behavior.
2173 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002174 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002175
2176 /**
2177 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2178 * a field, GetAlignedPointerFromInternalField must be used, everything else
2179 * leads to undefined behavior.
2180 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002181 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002184 bool HasOwnProperty(Handle<String> key);
2185 bool HasRealNamedProperty(Handle<String> key);
2186 bool HasRealIndexedProperty(uint32_t index);
2187 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188
2189 /**
2190 * If result.IsEmpty() no real property was located in the prototype chain.
2191 * This means interceptors in the prototype chain are not called.
2192 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002193 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00002194
2195 /**
2196 * If result.IsEmpty() no real property was located on the object or
2197 * in the prototype chain.
2198 * This means interceptors in the prototype chain are not called.
2199 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002200 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201
2202 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002203 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204
kasper.lund212ac232008-07-16 07:07:30 +00002205 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002206 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002208 /**
2209 * Turns on access check on the object if the object is an instance of
2210 * a template that has access check callbacks. If an object has no
2211 * access check info, the object cannot be accessed by anyone.
2212 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002213 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00002214
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002215 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002216 * Returns the identity hash for this object. The current implementation
2217 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002218 *
ager@chromium.org9085a012009-05-11 19:22:57 +00002219 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00002220 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002221 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002222 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00002223
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002224 /**
2225 * Access hidden properties on JavaScript objects. These properties are
2226 * hidden from the executing JavaScript and only accessible through the V8
2227 * C++ API. Hidden properties introduced by V8 internally (for example the
2228 * identity hash) are prefixed with "v8::".
2229 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002230 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
2231 Local<Value> GetHiddenValue(Handle<String> key);
2232 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002233
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002234 /**
2235 * Returns true if this is an instance of an api function (one
2236 * created from a function created from a function template) and has
2237 * been modified since it was created. Note that this method is
2238 * conservative and may return true for objects that haven't actually
2239 * been modified.
2240 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002241 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002242
2243 /**
2244 * Clone this object with a fast but shallow copy. Values will point
2245 * to the same values as the original object.
2246 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002247 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002249 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002250 * Returns the context in which the object was created.
2251 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002252 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002253
2254 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002255 * Set the backing store of the indexed properties to be managed by the
2256 * embedding layer. Access to the indexed properties will follow the rules
2257 * spelled out in CanvasPixelArray.
2258 * Note: The embedding program still owns the data and needs to ensure that
2259 * the backing store is preserved while V8 has a reference.
2260 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002261 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
2262 bool HasIndexedPropertiesInPixelData();
2263 uint8_t* GetIndexedPropertiesPixelData();
2264 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002265
ager@chromium.org3811b432009-10-28 14:53:37 +00002266 /**
2267 * Set the backing store of the indexed properties to be managed by the
2268 * embedding layer. Access to the indexed properties will follow the rules
2269 * spelled out for the CanvasArray subtypes in the WebGL specification.
2270 * Note: The embedding program still owns the data and needs to ensure that
2271 * the backing store is preserved while V8 has a reference.
2272 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002273 void SetIndexedPropertiesToExternalArrayData(void* data,
2274 ExternalArrayType array_type,
2275 int number_of_elements);
2276 bool HasIndexedPropertiesInExternalArrayData();
2277 void* GetIndexedPropertiesExternalArrayData();
2278 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
2279 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00002280
lrn@chromium.org1c092762011-05-09 09:42:16 +00002281 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002282 * Checks whether a callback is set by the
2283 * ObjectTemplate::SetCallAsFunctionHandler method.
2284 * When an Object is callable this method returns true.
2285 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002286 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002287
2288 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002289 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002290 * ObjectTemplate::SetCallAsFunctionHandler method.
2291 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002292 Local<Value> CallAsFunction(Handle<Object> recv,
2293 int argc,
2294 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002295
2296 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002297 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002298 * ObjectTemplate::SetCallAsFunctionHandler method.
2299 * Note: This method behaves like the Function::NewInstance method.
2300 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002301 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002302
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002303 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002304 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002305
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002307 Object();
2308 static void CheckCast(Value* obj);
2309 Local<Value> SlowGetInternalField(int index);
2310 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311};
2312
2313
2314/**
2315 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
2316 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002317class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002319 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320
ager@chromium.org3e875802009-06-29 08:26:34 +00002321 /**
2322 * Clones an element at index |index|. Returns an empty
2323 * handle if cloning fails (for any reason).
2324 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002325 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00002326
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002327 /**
2328 * Creates a JavaScript array with the given length. If the length
2329 * is negative the returned array will have length 0.
2330 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002331 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002332
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002333 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002335 Array();
2336 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337};
2338
2339
2340/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002341 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002343class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002345 Local<Object> NewInstance() const;
2346 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
2347 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
2348 void SetName(Handle<String> name);
2349 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00002350
2351 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002352 * Name inferred from variable or property assignment of this function.
2353 * Used to facilitate debugging and profiling of JavaScript code written
2354 * in an OO style, where many functions are anonymous but are assigned
2355 * to object properties.
2356 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002357 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002358
2359 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002360 * Returns zero based line number of function body and
2361 * kLineOffsetNotFound if no information available.
2362 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002363 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002364 /**
2365 * Returns zero based column number of function body and
2366 * kLineOffsetNotFound if no information available.
2367 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002368 int GetScriptColumnNumber() const;
2369 Handle<Value> GetScriptId() const;
2370 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002371 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002372 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002373
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002375 Function();
2376 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377};
2378
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002379/**
2380 * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
2381 * populates an instance of this class with a pointer to data and byte length.
2382 *
2383 * |ArrayBufferContents| is the owner of its data. When an instance of
2384 * this class is destructed, the |Data| is freed.
2385 *
2386 * This API is experimental and may change significantly.
2387 */
2388class V8EXPORT ArrayBufferContents {
2389 public:
2390 ArrayBufferContents() : data_(NULL), byte_length_(0) {}
2391 ~ArrayBufferContents();
2392
2393 void* Data() const { return data_; }
2394 size_t ByteLength() const { return byte_length_; }
2395
2396 private:
2397 void* data_;
2398 size_t byte_length_;
2399
2400 friend class ArrayBuffer;
2401};
2402
2403#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
2404#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
2405#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406
2407/**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002408 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
2409 * This API is experimental and may change significantly.
2410 */
2411class V8EXPORT ArrayBuffer : public Object {
2412 public:
2413 /**
2414 * Data length in bytes.
2415 */
2416 size_t ByteLength() const;
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002417
2418 /**
2419 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
2420 * Allocated memory will be owned by a created ArrayBuffer and
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002421 * will be deallocated when it is garbage-collected,
2422 * unless the object is externalized.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002423 */
2424 static Local<ArrayBuffer> New(size_t byte_length);
2425
2426 /**
2427 * Create a new ArrayBuffer over an existing memory block.
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002428 * The created array buffer is immediately in externalized state.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002429 * The memory block will not be reclaimed when a created ArrayBuffer
2430 * is garbage-collected.
2431 */
2432 static Local<ArrayBuffer> New(void* data, size_t byte_length);
2433
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002434 /**
2435 * Returns true if ArrayBuffer is extrenalized, that is, does not
2436 * own its memory block.
2437 */
2438 bool IsExternal() const;
2439
2440 /**
2441 * Pass the ownership of this ArrayBuffer's backing store to
2442 * a given ArrayBufferContents.
2443 */
2444 void Externalize(ArrayBufferContents* contents);
2445
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002446 V8_INLINE(static ArrayBuffer* Cast(Value* obj));
2447
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002448
2449 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
2450
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002451 private:
2452 ArrayBuffer();
2453 static void CheckCast(Value* obj);
2454};
2455
2456
2457/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002458 * A base class for an instance of TypedArray series of constructors
2459 * (ES6 draft 15.13.6).
2460 * This API is experimental and may change significantly.
2461 */
2462class V8EXPORT TypedArray : public Object {
2463 public:
2464 /**
2465 * Returns underlying ArrayBuffer.
2466 */
2467 Local<ArrayBuffer> Buffer();
2468 /**
2469 * Byte offset in |Buffer|
2470 */
2471 size_t ByteOffset();
2472 /**
2473 * Numbe of elements in this typed array.
2474 */
2475 size_t Length();
2476 /**
2477 * Size of typed array in bytes (e.g. for Int16Array, 2*|Length|).
2478 */
2479 size_t ByteLength();
2480 /**
2481 * Base address of typed array.
2482 */
2483 void* BaseAddress();
2484
2485 V8_INLINE(static TypedArray* Cast(Value* obj));
2486
2487 private:
2488 TypedArray();
2489 static void CheckCast(Value* obj);
2490};
2491
2492
2493/**
2494 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
2495 * This API is experimental and may change significantly.
2496 */
2497class V8EXPORT Uint8Array : public TypedArray {
2498 public:
2499 static Local<Uint8Array> New(Handle<ArrayBuffer> array_buffer,
2500 size_t byte_offset, size_t length);
2501 V8_INLINE(static Uint8Array* Cast(Value* obj));
2502
2503 private:
2504 Uint8Array();
2505 static void CheckCast(Value* obj);
2506};
2507
2508
2509/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002510 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
2511 * This API is experimental and may change significantly.
2512 */
2513class V8EXPORT Uint8ClampedArray : public TypedArray {
2514 public:
2515 static Local<Uint8ClampedArray> New(Handle<ArrayBuffer> array_buffer,
2516 size_t byte_offset, size_t length);
2517 V8_INLINE(static Uint8ClampedArray* Cast(Value* obj));
2518
2519 private:
2520 Uint8ClampedArray();
2521 static void CheckCast(Value* obj);
2522};
2523
2524/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002525 * An instance of Int8Array constructor (ES6 draft 15.13.6).
2526 * This API is experimental and may change significantly.
2527 */
2528class V8EXPORT Int8Array : public TypedArray {
2529 public:
2530 static Local<Int8Array> New(Handle<ArrayBuffer> array_buffer,
2531 size_t byte_offset, size_t length);
2532 V8_INLINE(static Int8Array* Cast(Value* obj));
2533
2534 private:
2535 Int8Array();
2536 static void CheckCast(Value* obj);
2537};
2538
2539
2540/**
2541 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
2542 * This API is experimental and may change significantly.
2543 */
2544class V8EXPORT Uint16Array : public TypedArray {
2545 public:
2546 static Local<Uint16Array> New(Handle<ArrayBuffer> array_buffer,
2547 size_t byte_offset, size_t length);
2548 V8_INLINE(static Uint16Array* Cast(Value* obj));
2549
2550 private:
2551 Uint16Array();
2552 static void CheckCast(Value* obj);
2553};
2554
2555
2556/**
2557 * An instance of Int16Array constructor (ES6 draft 15.13.6).
2558 * This API is experimental and may change significantly.
2559 */
2560class V8EXPORT Int16Array : public TypedArray {
2561 public:
2562 static Local<Int16Array> New(Handle<ArrayBuffer> array_buffer,
2563 size_t byte_offset, size_t length);
2564 V8_INLINE(static Int16Array* Cast(Value* obj));
2565
2566 private:
2567 Int16Array();
2568 static void CheckCast(Value* obj);
2569};
2570
2571
2572/**
2573 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
2574 * This API is experimental and may change significantly.
2575 */
2576class V8EXPORT Uint32Array : public TypedArray {
2577 public:
2578 static Local<Uint32Array> New(Handle<ArrayBuffer> array_buffer,
2579 size_t byte_offset, size_t length);
2580 V8_INLINE(static Uint32Array* Cast(Value* obj));
2581
2582 private:
2583 Uint32Array();
2584 static void CheckCast(Value* obj);
2585};
2586
2587
2588/**
2589 * An instance of Int32Array constructor (ES6 draft 15.13.6).
2590 * This API is experimental and may change significantly.
2591 */
2592class V8EXPORT Int32Array : public TypedArray {
2593 public:
2594 static Local<Int32Array> New(Handle<ArrayBuffer> array_buffer,
2595 size_t byte_offset, size_t length);
2596 V8_INLINE(static Int32Array* Cast(Value* obj));
2597
2598 private:
2599 Int32Array();
2600 static void CheckCast(Value* obj);
2601};
2602
2603
2604/**
2605 * An instance of Float32Array constructor (ES6 draft 15.13.6).
2606 * This API is experimental and may change significantly.
2607 */
2608class V8EXPORT Float32Array : public TypedArray {
2609 public:
2610 static Local<Float32Array> New(Handle<ArrayBuffer> array_buffer,
2611 size_t byte_offset, size_t length);
2612 V8_INLINE(static Float32Array* Cast(Value* obj));
2613
2614 private:
2615 Float32Array();
2616 static void CheckCast(Value* obj);
2617};
2618
2619
2620/**
2621 * An instance of Float64Array constructor (ES6 draft 15.13.6).
2622 * This API is experimental and may change significantly.
2623 */
2624class V8EXPORT Float64Array : public TypedArray {
2625 public:
2626 static Local<Float64Array> New(Handle<ArrayBuffer> array_buffer,
2627 size_t byte_offset, size_t length);
2628 V8_INLINE(static Float64Array* Cast(Value* obj));
2629
2630 private:
2631 Float64Array();
2632 static void CheckCast(Value* obj);
2633};
2634
2635
2636/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002637 * An instance of the built-in Date constructor (ECMA-262, 15.9).
2638 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002639class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002640 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002641 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002642
2643 /**
2644 * A specialization of Value::NumberValue that is more efficient
2645 * because we know the structure of this object.
2646 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002647 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002648
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002649 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002650
2651 /**
2652 * Notification that the embedder has changed the time zone,
2653 * daylight savings time, or other date / time configuration
2654 * parameters. V8 keeps a cache of various values used for
2655 * date / time computation. This notification will reset
2656 * those cached values for the current context so that date /
2657 * time configuration changes would be reflected in the Date
2658 * object.
2659 *
2660 * This API should not be called more than needed as it will
2661 * negatively impact the performance of date operations.
2662 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002663 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002664
2665 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002666 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002667};
2668
2669
2670/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002671 * A Number object (ECMA-262, 4.3.21).
2672 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002673class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002674 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002675 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002676
2677 /**
2678 * Returns the Number held by the object.
2679 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002680 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002681
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002682 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002683
2684 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002685 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002686};
2687
2688
2689/**
2690 * A Boolean object (ECMA-262, 4.3.15).
2691 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002692class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002693 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002694 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002695
2696 /**
2697 * Returns the Boolean held by the object.
2698 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002699 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002700
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002701 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002702
2703 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002704 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002705};
2706
2707
2708/**
2709 * A String object (ECMA-262, 4.3.18).
2710 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002711class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002712 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002713 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002714
2715 /**
2716 * Returns the String held by the object.
2717 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002718 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002719
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002720 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002721
2722 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002723 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002724};
2725
2726
2727/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002728 * A Symbol object (ECMA-262 edition 6).
2729 *
2730 * This is an experimental feature. Use at your own risk.
2731 */
2732class V8EXPORT SymbolObject : public Object {
2733 public:
2734 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2735
2736 /**
2737 * Returns the Symbol held by the object.
2738 */
2739 Local<Symbol> SymbolValue() const;
2740
2741 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2742
2743 private:
2744 static void CheckCast(v8::Value* obj);
2745};
2746
2747
2748/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002749 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2750 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002751class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002752 public:
2753 /**
2754 * Regular expression flag bits. They can be or'ed to enable a set
2755 * of flags.
2756 */
2757 enum Flags {
2758 kNone = 0,
2759 kGlobal = 1,
2760 kIgnoreCase = 2,
2761 kMultiline = 4
2762 };
2763
2764 /**
2765 * Creates a regular expression from the given pattern string and
2766 * the flags bit field. May throw a JavaScript exception as
2767 * described in ECMA-262, 15.10.4.1.
2768 *
2769 * For example,
2770 * RegExp::New(v8::String::New("foo"),
2771 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2772 * is equivalent to evaluating "/foo/gm".
2773 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002774 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002775
2776 /**
2777 * Returns the value of the source property: a string representing
2778 * the regular expression.
2779 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002780 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002781
2782 /**
2783 * Returns the flags bit field.
2784 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002785 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002786
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002787 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002788
2789 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002790 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002791};
2792
2793
2794/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002795 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2796 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002798class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002800 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002801 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002802 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002803 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002804 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805};
2806
2807
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002808// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809
2810
2811/**
2812 * The superclass of object and function templates.
2813 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002814class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815 public:
2816 /** Adds a property to each instance created by this template.*/
2817 void Set(Handle<String> name, Handle<Data> value,
2818 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002819 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002820 private:
2821 Template();
2822
2823 friend class ObjectTemplate;
2824 friend class FunctionTemplate;
2825};
2826
2827
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002828template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002829class ReturnValue {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002830 public:
2831 V8_INLINE(explicit ReturnValue(internal::Object** slot));
2832 // Handle setters
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00002833 template <typename S> V8_INLINE(void Set(const Persistent<S>& handle));
2834 template <typename S> V8_INLINE(void Set(const Handle<S> handle));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002835 // Fast primitive setters
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002836 V8_INLINE(void Set(bool value));
2837 V8_INLINE(void Set(double i));
2838 V8_INLINE(void Set(int32_t i));
2839 V8_INLINE(void Set(uint32_t i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002840 // Fast JS primitive setters
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002841 V8_INLINE(void SetNull());
2842 V8_INLINE(void SetUndefined());
2843 // Convenience getter for Isolate
2844 V8_INLINE(Isolate* GetIsolate());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002845 private:
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002846 internal::Object** value_;
2847};
2848
2849
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002850/**
2851 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002852 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002853 * including the receiver, the number and values of arguments, and
2854 * the holder of the function.
2855 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002856template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002857class FunctionCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002859 V8_INLINE(int Length() const);
2860 V8_INLINE(Local<Value> operator[](int i) const);
2861 V8_INLINE(Local<Function> Callee() const);
2862 V8_INLINE(Local<Object> This() const);
2863 V8_INLINE(Local<Object> Holder() const);
2864 V8_INLINE(bool IsConstructCall() const);
2865 V8_INLINE(Local<Value> Data() const);
2866 V8_INLINE(Isolate* GetIsolate() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002867 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2868 // This shouldn't be public, but the arm compiler needs it.
2869 static const int kArgsLength = 5;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002870
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002871 protected:
2872 friend class internal::FunctionCallbackArguments;
2873 friend class internal::CustomArguments<FunctionCallbackInfo>;
2874 static const int kReturnValueIndex = 0;
2875 static const int kIsolateIndex = -1;
2876 static const int kDataIndex = -2;
2877 static const int kCalleeIndex = -3;
2878 static const int kHolderIndex = -4;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002879
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002880 V8_INLINE(FunctionCallbackInfo(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002881 internal::Object** values,
2882 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002883 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002884 internal::Object** implicit_args_;
2885 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002886 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002887 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888};
2889
2890
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002891class V8EXPORT Arguments : public FunctionCallbackInfo<Value> {
2892 private:
2893 friend class internal::FunctionCallbackArguments;
2894 V8_INLINE(Arguments(internal::Object** implicit_args,
2895 internal::Object** values,
2896 int length,
2897 bool is_construct_call));
2898};
2899
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900/**
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002901 * The information passed to a property callback about the context
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 * of the property access.
2903 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002904template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002905class PropertyCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002906 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002907 V8_INLINE(Isolate* GetIsolate() const);
2908 V8_INLINE(Local<Value> Data() const);
2909 V8_INLINE(Local<Object> This() const);
2910 V8_INLINE(Local<Object> Holder() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002911 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2912 // This shouldn't be public, but the arm compiler needs it.
2913 static const int kArgsLength = 5;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002914
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002915 protected:
2916 friend class MacroAssembler;
2917 friend class internal::PropertyCallbackArguments;
2918 friend class internal::CustomArguments<PropertyCallbackInfo>;
2919 static const int kThisIndex = 0;
2920 static const int kHolderIndex = -1;
2921 static const int kDataIndex = -2;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002922 static const int kReturnValueIndex = -3;
2923 static const int kIsolateIndex = -4;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002924
2925 V8_INLINE(PropertyCallbackInfo(internal::Object** args))
2926 : args_(args) { }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002927 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002928};
2929
2930
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002931class V8EXPORT AccessorInfo : public PropertyCallbackInfo<Value> {
2932 private:
2933 friend class internal::PropertyCallbackArguments;
2934 V8_INLINE(AccessorInfo(internal::Object** args))
2935 : PropertyCallbackInfo<Value>(args) { }
2936};
2937
2938
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002939typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002940typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002942/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002943 * NamedProperty[Getter|Setter] are used as interceptors on object.
2944 * See ObjectTemplate::SetNamedPropertyHandler.
2945 */
2946typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2947 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002948typedef void (*NamedPropertyGetterCallback)(
2949 Local<String> property,
2950 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951
2952
2953/**
2954 * Returns the value if the setter intercepts the request.
2955 * Otherwise, returns an empty handle.
2956 */
2957typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2958 Local<Value> value,
2959 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002960typedef void (*NamedPropertySetterCallback)(
2961 Local<String> property,
2962 Local<Value> value,
2963 const PropertyCallbackInfo<Value>& info);
2964
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002966/**
2967 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002968 * The result is an integer encoding property attributes (like v8::None,
2969 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002971typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2972 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002973typedef void (*NamedPropertyQueryCallback)(
2974 Local<String> property,
2975 const PropertyCallbackInfo<Integer>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002976
2977
2978/**
2979 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002980 * The return value is true if the property could be deleted and false
2981 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982 */
2983typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2984 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002985typedef void (*NamedPropertyDeleterCallback)(
2986 Local<String> property,
2987 const PropertyCallbackInfo<Boolean>& info);
2988
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002989
2990/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002991 * Returns an array containing the names of the properties the named
2992 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993 */
2994typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002995typedef void (*NamedPropertyEnumeratorCallback)(
2996 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002997
v8.team.kasperl727e9952008-09-02 14:56:44 +00002998
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002999/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003000 * Returns the value of the property if the getter intercepts the
3001 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002 */
3003typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
3004 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003005typedef void (*IndexedPropertyGetterCallback)(
3006 uint32_t index,
3007 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008
3009
3010/**
3011 * Returns the value if the setter intercepts the request.
3012 * Otherwise, returns an empty handle.
3013 */
3014typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
3015 Local<Value> value,
3016 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003017typedef void (*IndexedPropertySetterCallback)(
3018 uint32_t index,
3019 Local<Value> value,
3020 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021
3022
3023/**
3024 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003025 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003026 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003027typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
3028 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003029typedef void (*IndexedPropertyQueryCallback)(
3030 uint32_t index,
3031 const PropertyCallbackInfo<Integer>& info);
3032
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003033
3034/**
3035 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003036 * The return value is true if the property could be deleted and false
3037 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003038 */
3039typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
3040 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003041typedef void (*IndexedPropertyDeleterCallback)(
3042 uint32_t index,
3043 const PropertyCallbackInfo<Boolean>& info);
3044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003045
v8.team.kasperl727e9952008-09-02 14:56:44 +00003046/**
3047 * Returns an array containing the indices of the properties the
3048 * indexed property getter intercepts.
3049 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003051typedef void (*IndexedPropertyEnumeratorCallback)(
3052 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053
3054
3055/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003056 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003057 */
3058enum AccessType {
3059 ACCESS_GET,
3060 ACCESS_SET,
3061 ACCESS_HAS,
3062 ACCESS_DELETE,
3063 ACCESS_KEYS
3064};
3065
v8.team.kasperl727e9952008-09-02 14:56:44 +00003066
3067/**
3068 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003069 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003070 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003071typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003072 Local<Value> key,
3073 AccessType type,
3074 Local<Value> data);
3075
v8.team.kasperl727e9952008-09-02 14:56:44 +00003076
3077/**
3078 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003079 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003080 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003081typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003082 uint32_t index,
3083 AccessType type,
3084 Local<Value> data);
3085
3086
3087/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003088 * A FunctionTemplate is used to create functions at runtime. There
3089 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003090 * context. The lifetime of the created function is equal to the
3091 * lifetime of the context. So in case the embedder needs to create
3092 * temporary functions that can be collected using Scripts is
3093 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003094 *
3095 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003096 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003097 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003098 * A FunctionTemplate has a corresponding instance template which is
3099 * used to create object instances when the function is used as a
3100 * constructor. Properties added to the instance template are added to
3101 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003102 *
3103 * A FunctionTemplate can have a prototype template. The prototype template
3104 * is used to create the prototype object of the function.
3105 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003106 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003107 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003108 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
3110 * t->Set("func_property", v8::Number::New(1));
3111 *
3112 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
3113 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
3114 * proto_t->Set("proto_const", v8::Number::New(2));
3115 *
3116 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
3117 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
3118 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
3119 * instance_t->Set("instance_property", Number::New(3));
3120 *
3121 * v8::Local<v8::Function> function = t->GetFunction();
3122 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003123 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003124 *
3125 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00003126 * and "instance" for the instance object created above. The function
3127 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003128 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003129 * \code
3130 * func_property in function == true;
3131 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003132 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003133 * function.prototype.proto_method() invokes 'InvokeCallback'
3134 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003136 * instance instanceof function == true;
3137 * instance.instance_accessor calls 'InstanceAccessorCallback'
3138 * instance.instance_property == 3;
3139 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003140 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003141 * A FunctionTemplate can inherit from another one by calling the
3142 * FunctionTemplate::Inherit method. The following graph illustrates
3143 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003145 * \code
3146 * FunctionTemplate Parent -> Parent() . prototype -> { }
3147 * ^ ^
3148 * | Inherit(Parent) | .__proto__
3149 * | |
3150 * FunctionTemplate Child -> Child() . prototype -> { }
3151 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003152 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003153 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
3154 * object of the Child() function has __proto__ pointing to the
3155 * Parent() function's prototype object. An instance of the Child
3156 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003157 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003158 * Let Parent be the FunctionTemplate initialized in the previous
3159 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003160 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003161 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003162 * Local<FunctionTemplate> parent = t;
3163 * Local<FunctionTemplate> child = FunctionTemplate::New();
3164 * child->Inherit(parent);
3165 *
3166 * Local<Function> child_function = child->GetFunction();
3167 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003168 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003170 * The Child function and Child instance will have the following
3171 * properties:
3172 *
3173 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003174 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003175 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003176 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003177 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003179class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003180 public:
3181 /** Creates a function template.*/
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003182 // TODO(dcarney): deprecate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003183 static Local<FunctionTemplate> New(
3184 InvocationCallback callback = 0,
3185 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003186 Handle<Signature> signature = Handle<Signature>(),
3187 int length = 0);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003188 static Local<FunctionTemplate> New(
3189 FunctionCallback callback, // TODO(dcarney): add back default param.
3190 Handle<Value> data = Handle<Value>(),
3191 Handle<Signature> signature = Handle<Signature>(),
3192 int length = 0);
3193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003194 /** Returns the unique function instance in the current execution context.*/
3195 Local<Function> GetFunction();
3196
v8.team.kasperl727e9952008-09-02 14:56:44 +00003197 /**
3198 * Set the call-handler callback for a FunctionTemplate. This
3199 * callback is called whenever the function created from this
3200 * FunctionTemplate is called.
3201 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003202 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003203 void SetCallHandler(InvocationCallback callback,
3204 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003205 void SetCallHandler(FunctionCallback callback,
3206 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003208 /** Set the predefined length property for the FunctionTemplate. */
3209 void SetLength(int length);
3210
v8.team.kasperl727e9952008-09-02 14:56:44 +00003211 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003212 Local<ObjectTemplate> InstanceTemplate();
3213
3214 /** Causes the function template to inherit from a parent function template.*/
3215 void Inherit(Handle<FunctionTemplate> parent);
3216
3217 /**
3218 * A PrototypeTemplate is the template used to create the prototype object
3219 * of the function created by this template.
3220 */
3221 Local<ObjectTemplate> PrototypeTemplate();
3222
v8.team.kasperl727e9952008-09-02 14:56:44 +00003223 /**
3224 * Set the class name of the FunctionTemplate. This is used for
3225 * printing objects created with the function created from the
3226 * FunctionTemplate as its constructor.
3227 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003228 void SetClassName(Handle<String> name);
3229
3230 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003231 * Determines whether the __proto__ accessor ignores instances of
3232 * the function template. If instances of the function template are
3233 * ignored, __proto__ skips all instances and instead returns the
3234 * next object in the prototype chain.
3235 *
3236 * Call with a value of true to make the __proto__ accessor ignore
3237 * instances of the function template. Call with a value of false
3238 * to make the __proto__ accessor not ignore instances of the
3239 * function template. By default, instances of a function template
3240 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241 */
3242 void SetHiddenPrototype(bool value);
3243
3244 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003245 * Sets the ReadOnly flag in the attributes of the 'prototype' property
3246 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00003247 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003248 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00003249
3250 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003251 * Returns true if the given object is an instance of this function
3252 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003253 */
3254 bool HasInstance(Handle<Value> object);
3255
3256 private:
3257 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003258 friend class Context;
3259 friend class ObjectTemplate;
3260};
3261
3262
3263/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003264 * An ObjectTemplate is used to create objects at runtime.
3265 *
3266 * Properties added to an ObjectTemplate are added to each object
3267 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003268 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003269class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003270 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003271 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003272 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274 /** Creates a new instance of this template.*/
3275 Local<Object> NewInstance();
3276
3277 /**
3278 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003279 *
3280 * Whenever the property with the given name is accessed on objects
3281 * created from this ObjectTemplate the getter and setter callbacks
3282 * are called instead of getting and setting the property directly
3283 * on the JavaScript object.
3284 *
3285 * \param name The name of the property for which an accessor is added.
3286 * \param getter The callback to invoke when getting the property.
3287 * \param setter The callback to invoke when setting the property.
3288 * \param data A piece of data that will be passed to the getter and setter
3289 * callbacks whenever they are invoked.
3290 * \param settings Access control settings for the accessor. This is a bit
3291 * field consisting of one of more of
3292 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
3293 * The default is to not allow cross-context access.
3294 * ALL_CAN_READ means that all cross-context reads are allowed.
3295 * ALL_CAN_WRITE means that all cross-context writes are allowed.
3296 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
3297 * cross-context access.
3298 * \param attribute The attributes of the property for which an accessor
3299 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003300 * \param signature The signature describes valid receivers for the accessor
3301 * and is used to perform implicit instance checks against them. If the
3302 * receiver is incompatible (i.e. is not an instance of the constructor as
3303 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
3304 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003305 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003306 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307 void SetAccessor(Handle<String> name,
3308 AccessorGetter getter,
3309 AccessorSetter setter = 0,
3310 Handle<Value> data = Handle<Value>(),
3311 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003312 PropertyAttribute attribute = None,
3313 Handle<AccessorSignature> signature =
3314 Handle<AccessorSignature>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003315 void SetAccessor(Handle<String> name,
3316 AccessorGetterCallback getter,
3317 AccessorSetterCallback setter = 0,
3318 Handle<Value> data = Handle<Value>(),
3319 AccessControl settings = DEFAULT,
3320 PropertyAttribute attribute = None,
3321 Handle<AccessorSignature> signature =
3322 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003323
ulan@chromium.org750145a2013-03-07 15:14:13 +00003324 // This function is not yet stable and should not be used at this time.
3325 bool SetAccessor(Handle<String> name,
3326 Handle<DeclaredAccessorDescriptor> descriptor,
3327 AccessControl settings = DEFAULT,
3328 PropertyAttribute attribute = None,
3329 Handle<AccessorSignature> signature =
3330 Handle<AccessorSignature>());
3331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003332 /**
3333 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003334 *
3335 * Whenever a named property is accessed on objects created from
3336 * this object template, the provided callback is invoked instead of
3337 * accessing the property directly on the JavaScript object.
3338 *
3339 * \param getter The callback to invoke when getting a property.
3340 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003341 * \param query The callback to invoke to check if a property is present,
3342 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003343 * \param deleter The callback to invoke when deleting a property.
3344 * \param enumerator The callback to invoke to enumerate all the named
3345 * properties of an object.
3346 * \param data A piece of data that will be passed to the callbacks
3347 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003348 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003349 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003350 void SetNamedPropertyHandler(NamedPropertyGetter getter,
3351 NamedPropertySetter setter = 0,
3352 NamedPropertyQuery query = 0,
3353 NamedPropertyDeleter deleter = 0,
3354 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003355 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003356 void SetNamedPropertyHandler(
3357 NamedPropertyGetterCallback getter,
3358 NamedPropertySetterCallback setter = 0,
3359 NamedPropertyQueryCallback query = 0,
3360 NamedPropertyDeleterCallback deleter = 0,
3361 NamedPropertyEnumeratorCallback enumerator = 0,
3362 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003363
3364 /**
3365 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003366 *
3367 * Whenever an indexed property is accessed on objects created from
3368 * this object template, the provided callback is invoked instead of
3369 * accessing the property directly on the JavaScript object.
3370 *
3371 * \param getter The callback to invoke when getting a property.
3372 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003373 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003374 * \param deleter The callback to invoke when deleting a property.
3375 * \param enumerator The callback to invoke to enumerate all the indexed
3376 * properties of an object.
3377 * \param data A piece of data that will be passed to the callbacks
3378 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003379 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003380 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003381 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
3382 IndexedPropertySetter setter = 0,
3383 IndexedPropertyQuery query = 0,
3384 IndexedPropertyDeleter deleter = 0,
3385 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003386 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003387 void SetIndexedPropertyHandler(
3388 IndexedPropertyGetterCallback getter,
3389 IndexedPropertySetterCallback setter = 0,
3390 IndexedPropertyQueryCallback query = 0,
3391 IndexedPropertyDeleterCallback deleter = 0,
3392 IndexedPropertyEnumeratorCallback enumerator = 0,
3393 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003394
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003395 /**
3396 * Sets the callback to be used when calling instances created from
3397 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00003398 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003399 * function.
3400 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003401 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003402 void SetCallAsFunctionHandler(InvocationCallback callback,
3403 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003404 void SetCallAsFunctionHandler(FunctionCallback callback,
3405 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406
v8.team.kasperl727e9952008-09-02 14:56:44 +00003407 /**
3408 * Mark object instances of the template as undetectable.
3409 *
3410 * In many ways, undetectable objects behave as though they are not
3411 * there. They behave like 'undefined' in conditionals and when
3412 * printed. However, properties can be accessed and called as on
3413 * normal objects.
3414 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003415 void MarkAsUndetectable();
3416
v8.team.kasperl727e9952008-09-02 14:56:44 +00003417 /**
3418 * Sets access check callbacks on the object template.
3419 *
3420 * When accessing properties on instances of this object template,
3421 * the access check callback will be called to determine whether or
3422 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003423 * The last parameter specifies whether access checks are turned
3424 * on by default on instances. If access checks are off by default,
3425 * they can be turned on on individual instances by calling
3426 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00003427 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003428 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
3429 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003430 Handle<Value> data = Handle<Value>(),
3431 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003432
kasper.lund212ac232008-07-16 07:07:30 +00003433 /**
3434 * Gets the number of internal fields for objects generated from
3435 * this template.
3436 */
3437 int InternalFieldCount();
3438
3439 /**
3440 * Sets the number of internal fields for objects generated from
3441 * this template.
3442 */
3443 void SetInternalFieldCount(int value);
3444
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003445 private:
3446 ObjectTemplate();
3447 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
3448 friend class FunctionTemplate;
3449};
3450
3451
3452/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003453 * A Signature specifies which receivers and arguments are valid
3454 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003455 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003456class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003457 public:
3458 static Local<Signature> New(Handle<FunctionTemplate> receiver =
3459 Handle<FunctionTemplate>(),
3460 int argc = 0,
3461 Handle<FunctionTemplate> argv[] = 0);
3462 private:
3463 Signature();
3464};
3465
3466
3467/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003468 * An AccessorSignature specifies which receivers are valid parameters
3469 * to an accessor callback.
3470 */
3471class V8EXPORT AccessorSignature : public Data {
3472 public:
3473 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
3474 Handle<FunctionTemplate>());
3475 private:
3476 AccessorSignature();
3477};
3478
3479
ulan@chromium.org750145a2013-03-07 15:14:13 +00003480class V8EXPORT DeclaredAccessorDescriptor : public Data {
3481 private:
3482 DeclaredAccessorDescriptor();
3483};
3484
3485
3486class V8EXPORT ObjectOperationDescriptor : public Data {
3487 public:
3488 // This function is not yet stable and should not be used at this time.
3489 static Local<RawOperationDescriptor> NewInternalFieldDereference(
3490 Isolate* isolate,
3491 int internal_field);
3492 private:
3493 ObjectOperationDescriptor();
3494};
3495
3496
3497enum DeclaredAccessorDescriptorDataType {
3498 kDescriptorBoolType,
3499 kDescriptorInt8Type, kDescriptorUint8Type,
3500 kDescriptorInt16Type, kDescriptorUint16Type,
3501 kDescriptorInt32Type, kDescriptorUint32Type,
3502 kDescriptorFloatType, kDescriptorDoubleType
3503};
3504
3505
3506class V8EXPORT RawOperationDescriptor : public Data {
3507 public:
3508 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
3509 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
3510 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
3511 int16_t byte_offset);
3512 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
3513 void* compare_value);
3514 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
3515 Isolate* isolate,
3516 DeclaredAccessorDescriptorDataType data_type,
3517 uint8_t bool_offset = 0);
3518 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
3519 uint8_t bitmask,
3520 uint8_t compare_value);
3521 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
3522 Isolate* isolate,
3523 uint16_t bitmask,
3524 uint16_t compare_value);
3525 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
3526 Isolate* isolate,
3527 uint32_t bitmask,
3528 uint32_t compare_value);
3529
3530 private:
3531 RawOperationDescriptor();
3532};
3533
3534
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003535/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003536 * A utility for determining the type of objects based on the template
3537 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003538 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003539class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003540 public:
3541 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
3542 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
3543 int match(Handle<Value> value);
3544 private:
3545 TypeSwitch();
3546};
3547
3548
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003549// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003550
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003551class V8EXPORT ExternalAsciiStringResourceImpl
3552 : public String::ExternalAsciiStringResource {
3553 public:
3554 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
3555 ExternalAsciiStringResourceImpl(const char* data, size_t length)
3556 : data_(data), length_(length) {}
3557 const char* data() const { return data_; }
3558 size_t length() const { return length_; }
3559
3560 private:
3561 const char* data_;
3562 size_t length_;
3563};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003564
3565/**
3566 * Ignore
3567 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003568class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003569 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003570 // Note that the strings passed into this constructor must live as long
3571 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003572 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003573 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003574 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003575 const char** deps = 0,
3576 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003577 virtual ~Extension() { }
3578 virtual v8::Handle<v8::FunctionTemplate>
3579 GetNativeFunction(v8::Handle<v8::String> name) {
3580 return v8::Handle<v8::FunctionTemplate>();
3581 }
3582
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003583 const char* name() const { return name_; }
3584 size_t source_length() const { return source_length_; }
3585 const String::ExternalAsciiStringResource* source() const {
3586 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587 int dependency_count() { return dep_count_; }
3588 const char** dependencies() { return deps_; }
3589 void set_auto_enable(bool value) { auto_enable_ = value; }
3590 bool auto_enable() { return auto_enable_; }
3591
3592 private:
3593 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003594 size_t source_length_; // expected to initialize before source_
3595 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596 int dep_count_;
3597 const char** deps_;
3598 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003599
3600 // Disallow copying and assigning.
3601 Extension(const Extension&);
3602 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603};
3604
3605
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003606void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003607
3608
3609/**
3610 * Ignore
3611 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003612class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003613 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003614 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615 RegisterExtension(extension);
3616 }
3617};
3618
3619
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003620// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003621
3622
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003623Handle<Primitive> V8EXPORT Undefined();
3624Handle<Primitive> V8EXPORT Null();
3625Handle<Boolean> V8EXPORT True();
3626Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003627
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003628V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
3629V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
3630V8_INLINE(Handle<Boolean> True(Isolate* isolate));
3631V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003632
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003633
3634/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003635 * A set of constraints that specifies the limits of the runtime's memory use.
3636 * You must set the heap size before initializing the VM - the size cannot be
3637 * adjusted after the VM is initialized.
3638 *
3639 * If you are using threads then you should hold the V8::Locker lock while
3640 * setting the stack limit and you must set a non-default stack limit separately
3641 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003642 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003643class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003644 public:
3645 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003646 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003647 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003648 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003649 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003650 int max_executable_size() { return max_executable_size_; }
3651 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003652 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003653 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
3655 private:
3656 int max_young_space_size_;
3657 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003658 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003659 uint32_t* stack_limit_;
3660};
3661
3662
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003663bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003664
3665
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003666// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003667
3668
3669typedef void (*FatalErrorCallback)(const char* location, const char* message);
3670
3671
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003672typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003673
3674
3675/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003676 * Schedules an exception to be thrown when returning to JavaScript. When an
3677 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003678 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00003679 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003680 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003681Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003682
3683/**
3684 * Create new error objects by calling the corresponding error object
3685 * constructor with the message.
3686 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003687class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003688 public:
3689 static Local<Value> RangeError(Handle<String> message);
3690 static Local<Value> ReferenceError(Handle<String> message);
3691 static Local<Value> SyntaxError(Handle<String> message);
3692 static Local<Value> TypeError(Handle<String> message);
3693 static Local<Value> Error(Handle<String> message);
3694};
3695
3696
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003697// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003698
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003699typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003700
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003701typedef void* (*CreateHistogramCallback)(const char* name,
3702 int min,
3703 int max,
3704 size_t buckets);
3705
3706typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
3707
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003708// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003709 enum ObjectSpace {
3710 kObjectSpaceNewSpace = 1 << 0,
3711 kObjectSpaceOldPointerSpace = 1 << 1,
3712 kObjectSpaceOldDataSpace = 1 << 2,
3713 kObjectSpaceCodeSpace = 1 << 3,
3714 kObjectSpaceMapSpace = 1 << 4,
3715 kObjectSpaceLoSpace = 1 << 5,
3716
3717 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
3718 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
3719 kObjectSpaceLoSpace
3720 };
3721
3722 enum AllocationAction {
3723 kAllocationActionAllocate = 1 << 0,
3724 kAllocationActionFree = 1 << 1,
3725 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
3726 };
3727
3728typedef void (*MemoryAllocationCallback)(ObjectSpace space,
3729 AllocationAction action,
3730 int size);
3731
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003732// --- Leave Script Callback ---
3733typedef void (*CallCompletedCallback)();
3734
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003735// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003736typedef void (*FailedAccessCheckCallback)(Local<Object> target,
3737 AccessType type,
3738 Local<Value> data);
3739
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003740// --- AllowCodeGenerationFromStrings callbacks ---
3741
3742/**
3743 * Callback to check if code generation from strings is allowed. See
3744 * Context::AllowCodeGenerationFromStrings.
3745 */
3746typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
3747
3748// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003749
3750/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003751 * Applications can register callback functions which will be called
3752 * before and after a garbage collection. Allocations are not
3753 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003754 * objects (set or delete properties for example) since it is possible
3755 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003756 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003757enum GCType {
3758 kGCTypeScavenge = 1 << 0,
3759 kGCTypeMarkSweepCompact = 1 << 1,
3760 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
3761};
3762
3763enum GCCallbackFlags {
3764 kNoGCCallbackFlags = 0,
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003765 kGCCallbackFlagCompacted = 1 << 0,
3766 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003767};
3768
3769typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
3770typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
3771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772typedef void (*GCCallback)();
3773
3774
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003775/**
ager@chromium.org3811b432009-10-28 14:53:37 +00003776 * Collection of V8 heap information.
3777 *
3778 * Instances of this class can be passed to v8::V8::HeapStatistics to
3779 * get heap statistics from V8.
3780 */
3781class V8EXPORT HeapStatistics {
3782 public:
3783 HeapStatistics();
3784 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003785 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003786 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003787 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003788 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003789
3790 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003791 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003792 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003793 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003794 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003795 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003796
3797 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003798 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003799};
3800
3801
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003802class RetainedObjectInfo;
3803
ager@chromium.org3811b432009-10-28 14:53:37 +00003804/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003805 * Isolate represents an isolated instance of the V8 engine. V8
3806 * isolates have completely separate states. Objects from one isolate
3807 * must not be used in other isolates. When V8 is initialized a
3808 * default isolate is implicitly created and entered. The embedder
3809 * can create additional isolates and use them in parallel in multiple
3810 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003811 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003812 */
3813class V8EXPORT Isolate {
3814 public:
3815 /**
3816 * Stack-allocated class which sets the isolate for all operations
3817 * executed within a local scope.
3818 */
3819 class V8EXPORT Scope {
3820 public:
3821 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3822 isolate->Enter();
3823 }
3824
3825 ~Scope() { isolate_->Exit(); }
3826
3827 private:
3828 Isolate* const isolate_;
3829
3830 // Prevent copying of Scope objects.
3831 Scope(const Scope&);
3832 Scope& operator=(const Scope&);
3833 };
3834
3835 /**
3836 * Creates a new isolate. Does not change the currently entered
3837 * isolate.
3838 *
3839 * When an isolate is no longer used its resources should be freed
3840 * by calling Dispose(). Using the delete operator is not allowed.
3841 */
3842 static Isolate* New();
3843
3844 /**
3845 * Returns the entered isolate for the current thread or NULL in
3846 * case there is no current isolate.
3847 */
3848 static Isolate* GetCurrent();
3849
3850 /**
3851 * Methods below this point require holding a lock (using Locker) in
3852 * a multi-threaded environment.
3853 */
3854
3855 /**
3856 * Sets this isolate as the entered one for the current thread.
3857 * Saves the previously entered one (if any), so that it can be
3858 * restored when exiting. Re-entering an isolate is allowed.
3859 */
3860 void Enter();
3861
3862 /**
3863 * Exits this isolate by restoring the previously entered one in the
3864 * current thread. The isolate may still stay the same, if it was
3865 * entered more than once.
3866 *
3867 * Requires: this == Isolate::GetCurrent().
3868 */
3869 void Exit();
3870
3871 /**
3872 * Disposes the isolate. The isolate must not be entered by any
3873 * thread to be disposable.
3874 */
3875 void Dispose();
3876
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003877 /**
3878 * Associate embedder-specific data with the isolate
3879 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003880 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003881
3882 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003883 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003884 * Returns NULL if SetData has never been called.
3885 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003886 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003887
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003888 /**
3889 * Get statistics about the heap memory usage.
3890 */
3891 void GetHeapStatistics(HeapStatistics* heap_statistics);
3892
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003893 /**
3894 * Adjusts the amount of registered external memory. Used to give V8 an
3895 * indication of the amount of externally allocated memory that is kept alive
3896 * by JavaScript objects. V8 uses this to decide when to perform global
3897 * garbage collections. Registering externally allocated memory will trigger
3898 * global garbage collections more often than it would otherwise in an attempt
3899 * to garbage collect the JavaScript objects that keep the externally
3900 * allocated memory alive.
3901 *
3902 * \param change_in_bytes the change in externally allocated memory that is
3903 * kept alive by JavaScript objects.
3904 * \returns the adjusted value.
3905 */
3906 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3907
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003908 /**
3909 * Returns heap profiler for this isolate. Will return NULL until the isolate
3910 * is initialized.
3911 */
3912 HeapProfiler* GetHeapProfiler();
3913
3914 /**
3915 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3916 * is initialized.
3917 */
3918 CpuProfiler* GetCpuProfiler();
3919
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003920 /** Returns the context that is on the top of the stack. */
3921 Local<Context> GetCurrentContext();
3922
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003923 /**
3924 * Allows the host application to group objects together. If one
3925 * object in the group is alive, all objects in the group are alive.
3926 * After each garbage collection, object groups are removed. It is
3927 * intended to be used in the before-garbage-collection callback
3928 * function, for instance to simulate DOM tree connections among JS
3929 * wrapper objects. Object groups for all dependent handles need to
3930 * be provided for kGCTypeMarkSweepCompact collections, for all other
3931 * garbage collection types it is sufficient to provide object groups
3932 * for partially dependent handles only.
3933 */
3934 void SetObjectGroupId(const Persistent<Value>& object,
3935 UniqueId id);
3936
3937 /**
3938 * Allows the host application to declare implicit references from an object
3939 * group to an object. If the objects of the object group are alive, the child
3940 * object is alive too. After each garbage collection, all implicit references
3941 * are removed. It is intended to be used in the before-garbage-collection
3942 * callback function.
3943 */
3944 void SetReferenceFromGroup(UniqueId id,
3945 const Persistent<Value>& child);
3946
3947 /**
3948 * Allows the host application to declare implicit references from an object
3949 * to another object. If the parent object is alive, the child object is alive
3950 * too. After each garbage collection, all implicit references are removed. It
3951 * is intended to be used in the before-garbage-collection callback function.
3952 */
3953 void SetReference(const Persistent<Object>& parent,
3954 const Persistent<Value>& child);
3955
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003956 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003957 Isolate();
3958 Isolate(const Isolate&);
3959 ~Isolate();
3960 Isolate& operator=(const Isolate&);
3961 void* operator new(size_t size);
3962 void operator delete(void*, size_t);
3963};
3964
3965
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003966class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003967 public:
3968 enum CompressionAlgorithm {
3969 kUncompressed,
3970 kBZip2
3971 };
3972
3973 const char* data;
3974 int compressed_size;
3975 int raw_size;
3976};
3977
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003978
3979/**
3980 * A helper class for driving V8 startup data decompression. It is based on
3981 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3982 * for an embedder to use this class, instead, API functions can be used
3983 * directly.
3984 *
3985 * For an example of the class usage, see the "shell.cc" sample application.
3986 */
3987class V8EXPORT StartupDataDecompressor { // NOLINT
3988 public:
3989 StartupDataDecompressor();
3990 virtual ~StartupDataDecompressor();
3991 int Decompress();
3992
3993 protected:
3994 virtual int DecompressData(char* raw_data,
3995 int* raw_data_size,
3996 const char* compressed_data,
3997 int compressed_data_size) = 0;
3998
3999 private:
4000 char** raw_data;
4001};
4002
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004003
4004/**
4005 * EntropySource is used as a callback function when v8 needs a source
4006 * of entropy.
4007 */
4008typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
4009
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004010
4011/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004012 * ReturnAddressLocationResolver is used as a callback function when v8 is
4013 * resolving the location of a return address on the stack. Profilers that
4014 * change the return address on the stack can use this to resolve the stack
4015 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004016 *
4017 * \param return_addr_location points to a location on stack where a machine
4018 * return address resides.
4019 * \returns either return_addr_location, or else a pointer to the profiler's
4020 * copy of the original return address.
4021 *
4022 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00004023 */
4024typedef uintptr_t (*ReturnAddressLocationResolver)(
4025 uintptr_t return_addr_location);
4026
4027
4028/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004029 * FunctionEntryHook is the type of the profile entry hook called at entry to
4030 * any generated function when function-level profiling is enabled.
4031 *
4032 * \param function the address of the function that's being entered.
4033 * \param return_addr_location points to a location on stack where the machine
4034 * return address resides. This can be used to identify the caller of
4035 * \p function, and/or modified to divert execution when \p function exits.
4036 *
4037 * \note the entry hook must not cause garbage collection.
4038 */
4039typedef void (*FunctionEntryHook)(uintptr_t function,
4040 uintptr_t return_addr_location);
4041
4042
4043/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004044 * A JIT code event is issued each time code is added, moved or removed.
4045 *
4046 * \note removal events are not currently issued.
4047 */
4048struct JitCodeEvent {
4049 enum EventType {
4050 CODE_ADDED,
4051 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004052 CODE_REMOVED,
4053 CODE_ADD_LINE_POS_INFO,
4054 CODE_START_LINE_INFO_RECORDING,
4055 CODE_END_LINE_INFO_RECORDING
4056 };
4057 // Definition of the code position type. The "POSITION" type means the place
4058 // in the source code which are of interest when making stack traces to
4059 // pin-point the source location of a stack frame as close as possible.
4060 // The "STATEMENT_POSITION" means the place at the beginning of each
4061 // statement, and is used to indicate possible break locations.
4062 enum PositionType {
4063 POSITION,
4064 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004065 };
4066
4067 // Type of event.
4068 EventType type;
4069 // Start of the instructions.
4070 void* code_start;
4071 // Size of the instructions.
4072 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004073 // Script info for CODE_ADDED event.
4074 Handle<Script> script;
4075 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
4076 // code line information which is returned from the
4077 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
4078 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
4079 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004080
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004081 struct name_t {
4082 // Name of the object associated with the code, note that the string is not
4083 // zero-terminated.
4084 const char* str;
4085 // Number of chars in str.
4086 size_t len;
4087 };
4088
4089 struct line_info_t {
4090 // PC offset
4091 size_t offset;
4092 // Code postion
4093 size_t pos;
4094 // The position type.
4095 PositionType position_type;
4096 };
4097
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004098 union {
4099 // Only valid for CODE_ADDED.
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004100 struct name_t name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004101
4102 // Only valid for CODE_ADD_LINE_POS_INFO
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004103 struct line_info_t line_info;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004104
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004105 // New location of instructions. Only valid for CODE_MOVED.
4106 void* new_code_start;
4107 };
4108};
4109
4110/**
4111 * Option flags passed to the SetJitCodeEventHandler function.
4112 */
4113enum JitCodeEventOptions {
4114 kJitCodeEventDefault = 0,
4115 // Generate callbacks for already existent code.
4116 kJitCodeEventEnumExisting = 1
4117};
4118
4119
4120/**
4121 * Callback function passed to SetJitCodeEventHandler.
4122 *
4123 * \param event code add, move or removal event.
4124 */
4125typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
4126
4127
4128/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004129 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004130 */
4131class V8EXPORT ExternalResourceVisitor { // NOLINT
4132 public:
4133 virtual ~ExternalResourceVisitor() {}
4134 virtual void VisitExternalString(Handle<String> string) {}
4135};
4136
4137
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004138/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004139 * Interface for iterating through all the persistent handles in the heap.
4140 */
4141class V8EXPORT PersistentHandleVisitor { // NOLINT
4142 public:
4143 virtual ~PersistentHandleVisitor() {}
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004144#ifdef V8_USE_OLD_STYLE_PERSISTENT_HANDLE_VISITORS
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004145 virtual void VisitPersistentHandle(Persistent<Value> value,
4146 uint16_t class_id) {}
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004147#else
4148 virtual void VisitPersistentHandle(Persistent<Value>* value,
4149 uint16_t class_id) {}
4150#endif
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004151};
4152
4153
4154/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004155 * Asserts that no action is performed that could cause a handle's value
4156 * to be modified. Useful when otherwise unsafe handle operations need to
4157 * be performed.
4158 */
4159class V8EXPORT AssertNoGCScope {
4160#ifndef DEBUG
4161 V8_INLINE(AssertNoGCScope(Isolate* isolate)) {}
4162#else
4163 AssertNoGCScope(Isolate* isolate);
4164 ~AssertNoGCScope();
4165 private:
4166 Isolate* isolate_;
4167 bool last_state_;
4168#endif
4169};
4170
4171
4172/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004173 * Container class for static utility functions.
4174 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004175class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004176 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00004177 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004178 static void SetFatalErrorHandler(FatalErrorCallback that);
4179
v8.team.kasperl727e9952008-09-02 14:56:44 +00004180 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004181 * Set the callback to invoke to check if code generation from
4182 * strings should be allowed.
4183 */
4184 static void SetAllowCodeGenerationFromStringsCallback(
4185 AllowCodeGenerationFromStringsCallback that);
4186
4187 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004188 * Ignore out-of-memory exceptions.
4189 *
4190 * V8 running out of memory is treated as a fatal error by default.
4191 * This means that the fatal error handler is called and that V8 is
4192 * terminated.
4193 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004194 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00004195 * out-of-memory situation as a fatal error. This way, the contexts
4196 * that did not cause the out of memory problem might be able to
4197 * continue execution.
4198 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004199 static void IgnoreOutOfMemoryException();
4200
v8.team.kasperl727e9952008-09-02 14:56:44 +00004201 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004202 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00004203 * fatal errors such as out-of-memory situations.
4204 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004205 static bool IsDead();
4206
4207 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004208 * The following 4 functions are to be used when V8 is built with
4209 * the 'compress_startup_data' flag enabled. In this case, the
4210 * embedder must decompress startup data prior to initializing V8.
4211 *
4212 * This is how interaction with V8 should look like:
4213 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
4214 * v8::StartupData* compressed_data =
4215 * new v8::StartupData[compressed_data_count];
4216 * v8::V8::GetCompressedStartupData(compressed_data);
4217 * ... decompress data (compressed_data can be updated in-place) ...
4218 * v8::V8::SetDecompressedStartupData(compressed_data);
4219 * ... now V8 can be initialized
4220 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004221 *
4222 * A helper class StartupDataDecompressor is provided. It implements
4223 * the protocol of the interaction described above, and can be used in
4224 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004225 */
4226 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
4227 static int GetCompressedStartupDataCount();
4228 static void GetCompressedStartupData(StartupData* compressed_data);
4229 static void SetDecompressedStartupData(StartupData* decompressed_data);
4230
4231 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004232 * Adds a message listener.
4233 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004234 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00004235 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004236 *
4237 * If data is specified, it will be passed to the callback when it is called.
4238 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004239 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004240 static bool AddMessageListener(MessageCallback that,
4241 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004242
4243 /**
4244 * Remove all message listeners from the specified callback function.
4245 */
4246 static void RemoveMessageListeners(MessageCallback that);
4247
4248 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004249 * Tells V8 to capture current stack trace when uncaught exception occurs
4250 * and report it to the message listeners. The option is off by default.
4251 */
4252 static void SetCaptureStackTraceForUncaughtExceptions(
4253 bool capture,
4254 int frame_limit = 10,
4255 StackTrace::StackTraceOptions options = StackTrace::kOverview);
4256
4257 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004258 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004259 */
4260 static void SetFlagsFromString(const char* str, int length);
4261
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004262 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004263 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004264 */
4265 static void SetFlagsFromCommandLine(int* argc,
4266 char** argv,
4267 bool remove_flags);
4268
kasper.lund7276f142008-07-30 08:49:36 +00004269 /** Get the version string. */
4270 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004271
4272 /**
4273 * Enables the host application to provide a mechanism for recording
4274 * statistics counters.
4275 */
4276 static void SetCounterFunction(CounterLookupCallback);
4277
4278 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004279 * Enables the host application to provide a mechanism for recording
4280 * histograms. The CreateHistogram function returns a
4281 * histogram which will later be passed to the AddHistogramSample
4282 * function.
4283 */
4284 static void SetCreateHistogramFunction(CreateHistogramCallback);
4285 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
4286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004287 /** Callback function for reporting failed access checks.*/
4288 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
4289
4290 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004291 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004292 * garbage collection. Allocations are not allowed in the
4293 * callback function, you therefore cannot manipulate objects (set
4294 * or delete properties for example) since it is possible such
4295 * operations will result in the allocation of objects. It is possible
4296 * to specify the GCType filter for your callback. But it is not possible to
4297 * register the same callback function two times with different
4298 * GCType filters.
4299 */
4300 static void AddGCPrologueCallback(
4301 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
4302
4303 /**
4304 * This function removes callback which was installed by
4305 * AddGCPrologueCallback function.
4306 */
4307 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
4308
4309 /**
4310 * The function is deprecated. Please use AddGCPrologueCallback instead.
4311 * Enables the host application to receive a notification before a
4312 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00004313 * callback function, you therefore cannot manipulate objects (set
4314 * or delete properties for example) since it is possible such
4315 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004316 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004317 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004318
4319 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004320 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004321 * garbage collection. Allocations are not allowed in the
4322 * callback function, you therefore cannot manipulate objects (set
4323 * or delete properties for example) since it is possible such
4324 * operations will result in the allocation of objects. It is possible
4325 * to specify the GCType filter for your callback. But it is not possible to
4326 * register the same callback function two times with different
4327 * GCType filters.
4328 */
4329 static void AddGCEpilogueCallback(
4330 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
4331
4332 /**
4333 * This function removes callback which was installed by
4334 * AddGCEpilogueCallback function.
4335 */
4336 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
4337
4338 /**
4339 * The function is deprecated. Please use AddGCEpilogueCallback instead.
4340 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00004341 * major garbage collection. Allocations are not allowed in the
4342 * callback function, you therefore cannot manipulate objects (set
4343 * or delete properties for example) since it is possible such
4344 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004345 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004346 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004347
4348 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004349 * Enables the host application to provide a mechanism to be notified
4350 * and perform custom logging when V8 Allocates Executable Memory.
4351 */
4352 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
4353 ObjectSpace space,
4354 AllocationAction action);
4355
4356 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004357 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004358 */
4359 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
4360
4361 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004362 * Adds a callback to notify the host application when a script finished
4363 * running. If a script re-enters the runtime during executing, the
4364 * CallCompletedCallback is only invoked when the outer-most script
4365 * execution ends. Executing scripts inside the callback do not trigger
4366 * further callbacks.
4367 */
4368 static void AddCallCompletedCallback(CallCompletedCallback callback);
4369
4370 /**
4371 * Removes callback that was installed by AddCallCompletedCallback.
4372 */
4373 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
4374
4375 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004376 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004377 * initialize from scratch. This function is called implicitly if
4378 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004379 */
4380 static bool Initialize();
4381
kasper.lund7276f142008-07-30 08:49:36 +00004382 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004383 * Allows the host application to provide a callback which can be used
4384 * as a source of entropy for random number generators.
4385 */
4386 static void SetEntropySource(EntropySource source);
4387
4388 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004389 * Allows the host application to provide a callback that allows v8 to
4390 * cooperate with a profiler that rewrites return addresses on stack.
4391 */
4392 static void SetReturnAddressLocationResolver(
4393 ReturnAddressLocationResolver return_address_resolver);
4394
4395 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004396 * Allows the host application to provide the address of a function that's
4397 * invoked on entry to every V8-generated function.
4398 * Note that \p entry_hook is invoked at the very start of each
4399 * generated function.
4400 *
4401 * \param entry_hook a function that will be invoked on entry to every
4402 * V8-generated function.
4403 * \returns true on success on supported platforms, false on failure.
4404 * \note Setting a new entry hook function when one is already active will
4405 * fail.
4406 */
4407 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
4408
4409 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004410 * Allows the host application to provide the address of a function that is
4411 * notified each time code is added, moved or removed.
4412 *
4413 * \param options options for the JIT code event handler.
4414 * \param event_handler the JIT code event handler, which will be invoked
4415 * each time code is added, moved or removed.
4416 * \note \p event_handler won't get notified of existent code.
4417 * \note since code removal notifications are not currently issued, the
4418 * \p event_handler may get notifications of code that overlaps earlier
4419 * code notifications. This happens when code areas are reused, and the
4420 * earlier overlapping code areas should therefore be discarded.
4421 * \note the events passed to \p event_handler and the strings they point to
4422 * are not guaranteed to live past each call. The \p event_handler must
4423 * copy strings and other parameters it needs to keep around.
4424 * \note the set of events declared in JitCodeEvent::EventType is expected to
4425 * grow over time, and the JitCodeEvent structure is expected to accrue
4426 * new members. The \p event_handler function must ignore event codes
4427 * it does not recognize to maintain future compatibility.
4428 */
4429 static void SetJitCodeEventHandler(JitCodeEventOptions options,
4430 JitCodeEventHandler event_handler);
4431
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004432 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
4433 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004434 static intptr_t AdjustAmountOfExternalAllocatedMemory(
4435 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00004436
iposva@chromium.org245aa852009-02-10 00:49:54 +00004437 /**
4438 * Suspends recording of tick samples in the profiler.
4439 * When the V8 profiling mode is enabled (usually via command line
4440 * switches) this function suspends recording of tick samples.
4441 * Profiling ticks are discarded until ResumeProfiler() is called.
4442 *
4443 * See also the --prof and --prof_auto command line switches to
4444 * enable V8 profiling.
4445 */
4446 static void PauseProfiler();
4447
4448 /**
4449 * Resumes recording of tick samples in the profiler.
4450 * See also PauseProfiler().
4451 */
4452 static void ResumeProfiler();
4453
ager@chromium.org41826e72009-03-30 13:30:57 +00004454 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004455 * Return whether profiler is currently paused.
4456 */
4457 static bool IsProfilerPaused();
4458
4459 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004460 * Retrieve the V8 thread id of the calling thread.
4461 *
4462 * The thread id for a thread should only be retrieved after the V8
4463 * lock has been acquired with a Locker object with that thread.
4464 */
4465 static int GetCurrentThreadId();
4466
4467 /**
4468 * Forcefully terminate execution of a JavaScript thread. This can
4469 * be used to terminate long-running scripts.
4470 *
4471 * TerminateExecution should only be called when then V8 lock has
4472 * been acquired with a Locker object. Therefore, in order to be
4473 * able to terminate long-running threads, preemption must be
4474 * enabled to allow the user of TerminateExecution to acquire the
4475 * lock.
4476 *
4477 * The termination is achieved by throwing an exception that is
4478 * uncatchable by JavaScript exception handlers. Termination
4479 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004480 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004481 * exception handler that catches an exception should check if that
4482 * exception is a termination exception and immediately return if
4483 * that is the case. Returning immediately in that case will
4484 * continue the propagation of the termination exception if needed.
4485 *
4486 * The thread id passed to TerminateExecution must have been
4487 * obtained by calling GetCurrentThreadId on the thread in question.
4488 *
4489 * \param thread_id The thread id of the thread to terminate.
4490 */
4491 static void TerminateExecution(int thread_id);
4492
4493 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004494 * Forcefully terminate the current thread of JavaScript execution
4495 * in the given isolate. If no isolate is provided, the default
4496 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004497 *
4498 * This method can be used by any thread even if that thread has not
4499 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004500 *
4501 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004502 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004503 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00004504
4505 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004506 * Is V8 terminating JavaScript execution.
4507 *
4508 * Returns true if JavaScript execution is currently terminating
4509 * because of a call to TerminateExecution. In that case there are
4510 * still JavaScript frames on the stack and the termination
4511 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004512 *
4513 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004514 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004515 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004516
4517 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004518 * Resume execution capability in the given isolate, whose execution
4519 * was previously forcefully terminated using TerminateExecution().
4520 *
4521 * When execution is forcefully terminated using TerminateExecution(),
4522 * the isolate can not resume execution until all JavaScript frames
4523 * have propagated the uncatchable exception which is generated. This
4524 * method allows the program embedding the engine to handle the
4525 * termination event and resume execution capability, even if
4526 * JavaScript frames remain on the stack.
4527 *
4528 * This method can be used by any thread even if that thread has not
4529 * acquired the V8 lock with a Locker object.
4530 *
4531 * \param isolate The isolate in which to resume execution capability.
4532 */
4533 static void CancelTerminateExecution(Isolate* isolate);
4534
4535 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00004536 * Releases any resources used by v8 and stops any utility threads
4537 * that may be running. Note that disposing v8 is permanent, it
4538 * cannot be reinitialized.
4539 *
4540 * It should generally not be necessary to dispose v8 before exiting
4541 * a process, this should happen automatically. It is only necessary
4542 * to use if the process needs the resources taken up by v8.
4543 */
4544 static bool Dispose();
4545
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00004546 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
4547 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004548
4549 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004550 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004551 * heap. GC is not invoked prior to iterating, therefore there is no
4552 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004553 */
4554 static void VisitExternalResources(ExternalResourceVisitor* visitor);
4555
4556 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004557 * Iterates through all the persistent handles in the current isolate's heap
4558 * that have class_ids.
4559 */
4560 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
4561
4562 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004563 * Iterates through all the persistent handles in the current isolate's heap
4564 * that have class_ids and are candidates to be marked as partially dependent
4565 * handles. This will visit handles to young objects created since the last
4566 * garbage collection but is free to visit an arbitrary superset of these
4567 * objects.
4568 */
4569 static void VisitHandlesForPartialDependence(
4570 Isolate* isolate, PersistentHandleVisitor* visitor);
4571
4572 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004573 * Optional notification that the embedder is idle.
4574 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004575 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004576 * Returns true if the embedder should stop calling IdleNotification
4577 * until real work has been done. This indicates that V8 has done
4578 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004579 *
4580 * The hint argument specifies the amount of work to be done in the function
4581 * on scale from 1 to 1000. There is no guarantee that the actual work will
4582 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004583 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004584 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004585
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004586 /**
4587 * Optional notification that the system is running low on memory.
4588 * V8 uses these notifications to attempt to free memory.
4589 */
4590 static void LowMemoryNotification();
4591
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004592 /**
4593 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004594 * these notifications to guide the GC heuristic. Returns the number
4595 * of context disposals - including this one - since the last time
4596 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004597 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004598 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004599
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004600 private:
4601 V8();
4602
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004603 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
4604 internal::Object** handle);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004605 static void DisposeGlobal(internal::Object** global_handle);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004606 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004607 static void MakeWeak(internal::Object** global_handle,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004608 void* data,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004609 RevivableCallback weak_reference_callback,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004610 NearDeathCallback near_death_callback);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00004611 static void ClearWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004612
4613 template <class T> friend class Handle;
4614 template <class T> friend class Local;
4615 template <class T> friend class Persistent;
4616 friend class Context;
4617};
4618
4619
4620/**
4621 * An external exception handler.
4622 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004623class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004624 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004625 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004626 * Creates a new try/catch block and registers it with v8. Note that
4627 * all TryCatch blocks should be stack allocated because the memory
4628 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004629 */
4630 TryCatch();
4631
4632 /**
4633 * Unregisters and deletes this try/catch block.
4634 */
4635 ~TryCatch();
4636
4637 /**
4638 * Returns true if an exception has been caught by this try/catch block.
4639 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004640 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004641
4642 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004643 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004644 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004645 * If CanContinue returns false, the correct action is to perform any C++
4646 * cleanup needed and then return. If CanContinue returns false and
4647 * HasTerminated returns true, it is possible to call
4648 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004649 */
4650 bool CanContinue() const;
4651
4652 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004653 * Returns true if an exception has been caught due to script execution
4654 * being terminated.
4655 *
4656 * There is no JavaScript representation of an execution termination
4657 * exception. Such exceptions are thrown when the TerminateExecution
4658 * methods are called to terminate a long-running script.
4659 *
4660 * If such an exception has been thrown, HasTerminated will return true,
4661 * indicating that it is possible to call CancelTerminateExecution in order
4662 * to continue calling into the engine.
4663 */
4664 bool HasTerminated() const;
4665
4666 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004667 * Throws the exception caught by this TryCatch in a way that avoids
4668 * it being caught again by this same TryCatch. As with ThrowException
4669 * it is illegal to execute any JavaScript operations after calling
4670 * ReThrow; the caller must return immediately to where the exception
4671 * is caught.
4672 */
4673 Handle<Value> ReThrow();
4674
4675 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004676 * Returns the exception caught by this try/catch block. If no exception has
4677 * been caught an empty handle is returned.
4678 *
4679 * The returned handle is valid until this TryCatch block has been destroyed.
4680 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004681 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004682
4683 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004684 * Returns the .stack property of the thrown object. If no .stack
4685 * property is present an empty handle is returned.
4686 */
4687 Local<Value> StackTrace() const;
4688
4689 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004690 * Returns the message associated with this exception. If there is
4691 * no message associated an empty handle is returned.
4692 *
4693 * The returned handle is valid until this TryCatch block has been
4694 * destroyed.
4695 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004696 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004697
4698 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004699 * Clears any exceptions that may have been caught by this try/catch block.
4700 * After this method has been called, HasCaught() will return false.
4701 *
4702 * It is not necessary to clear a try/catch block before using it again; if
4703 * another exception is thrown the previously caught exception will just be
4704 * overwritten. However, it is often a good idea since it makes it easier
4705 * to determine which operation threw a given exception.
4706 */
4707 void Reset();
4708
v8.team.kasperl727e9952008-09-02 14:56:44 +00004709 /**
4710 * Set verbosity of the external exception handler.
4711 *
4712 * By default, exceptions that are caught by an external exception
4713 * handler are not reported. Call SetVerbose with true on an
4714 * external exception handler to have exceptions caught by the
4715 * handler reported as if they were not caught.
4716 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004717 void SetVerbose(bool value);
4718
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004719 /**
4720 * Set whether or not this TryCatch should capture a Message object
4721 * which holds source information about where the exception
4722 * occurred. True by default.
4723 */
4724 void SetCaptureMessage(bool value);
4725
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004726 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004727 // Make it hard to create heap-allocated TryCatch blocks.
4728 TryCatch(const TryCatch&);
4729 void operator=(const TryCatch&);
4730 void* operator new(size_t size);
4731 void operator delete(void*, size_t);
4732
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004733 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004734 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004735 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004736 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004737 bool is_verbose_ : 1;
4738 bool can_continue_ : 1;
4739 bool capture_message_ : 1;
4740 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004741 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004742
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004743 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004744};
4745
4746
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004747// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004748
4749
4750/**
4751 * Ignore
4752 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004753class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004754 public:
4755 ExtensionConfiguration(int name_count, const char* names[])
4756 : name_count_(name_count), names_(names) { }
4757 private:
4758 friend class ImplementationUtilities;
4759 int name_count_;
4760 const char** names_;
4761};
4762
4763
4764/**
4765 * A sandboxed execution context with its own set of built-in objects
4766 * and functions.
4767 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004768class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004769 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004770 /**
4771 * Returns the global proxy object or global object itself for
4772 * detached contexts.
4773 *
4774 * Global proxy object is a thin wrapper whose prototype points to
4775 * actual context's global object with the properties like Object, etc.
4776 * This is done that way for security reasons (for more details see
4777 * https://wiki.mozilla.org/Gecko:SplitWindow).
4778 *
4779 * Please note that changes to global proxy object prototype most probably
4780 * would break VM---v8 expects only global object as a prototype of
4781 * global proxy object.
4782 *
4783 * If DetachGlobal() has been invoked, Global() would return actual global
4784 * object until global is reattached with ReattachGlobal().
4785 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004786 Local<Object> Global();
4787
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004788 /**
4789 * Detaches the global object from its context before
4790 * the global object can be reused to create a new context.
4791 */
4792 void DetachGlobal();
4793
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00004794 /**
4795 * Reattaches a global object to a context. This can be used to
4796 * restore the connection between a global object and a context
4797 * after DetachGlobal has been called.
4798 *
4799 * \param global_object The global object to reattach to the
4800 * context. For this to work, the global object must be the global
4801 * object that was associated with this context before a call to
4802 * DetachGlobal.
4803 */
4804 void ReattachGlobal(Handle<Object> global_object);
4805
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004806 /**
4807 * Creates a new context and returns a handle to the newly allocated
4808 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004809 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004810 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004811 *
4812 * \param extensions An optional extension configuration containing
4813 * the extensions to be installed in the newly created context.
4814 *
4815 * \param global_template An optional object template from which the
4816 * global object for the newly created context will be created.
4817 *
4818 * \param global_object An optional global object to be reused for
4819 * the newly created context. This global object must have been
4820 * created by a previous call to Context::New with the same global
4821 * template. The state of the global object will be completely reset
4822 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004823 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004824 static Local<Context> New(
4825 Isolate* isolate,
4826 ExtensionConfiguration* extensions = NULL,
4827 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4828 Handle<Value> global_object = Handle<Value>());
4829
4830 /** Deprecated. Use Isolate version instead. */
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004831 V8_DEPRECATED(static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004832 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004833 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004834 Handle<Value> global_object = Handle<Value>()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004835
kasper.lund44510672008-07-25 07:37:58 +00004836 /** Returns the last entered context. */
4837 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004838
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004839 // TODO(svenpanne) Actually deprecate this.
4840 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004841 static Local<Context> GetCurrent();
4842
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004843 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004844 * Returns the context of the calling JavaScript code. That is the
4845 * context of the top-most JavaScript frame. If there are no
4846 * JavaScript frames an empty handle is returned.
4847 */
4848 static Local<Context> GetCalling();
4849
4850 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004851 * Sets the security token for the context. To access an object in
4852 * another context, the security tokens must match.
4853 */
4854 void SetSecurityToken(Handle<Value> token);
4855
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004856 /** Restores the security token to the default value. */
4857 void UseDefaultSecurityToken();
4858
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004859 /** Returns the security token of this context.*/
4860 Handle<Value> GetSecurityToken();
4861
v8.team.kasperl727e9952008-09-02 14:56:44 +00004862 /**
4863 * Enter this context. After entering a context, all code compiled
4864 * and run is compiled and run in this context. If another context
4865 * is already entered, this old context is saved so it can be
4866 * restored when the new context is exited.
4867 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004868 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004869
4870 /**
4871 * Exit this context. Exiting the current context restores the
4872 * context that was in place when entering the current context.
4873 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004874 void Exit();
4875
v8.team.kasperl727e9952008-09-02 14:56:44 +00004876 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004877 bool HasOutOfMemoryException();
4878
v8.team.kasperl727e9952008-09-02 14:56:44 +00004879 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004880 static bool InContext();
4881
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004882 /** Returns an isolate associated with a current context. */
4883 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004884
4885 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004886 * Gets the embedder data with the given index, which must have been set by a
4887 * previous call to SetEmbedderData with the same index. Note that index 0
4888 * currently has a special meaning for Chrome's debugger.
4889 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004890 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004891
4892 /**
4893 * Sets the embedder data with the given index, growing the data as
4894 * needed. Note that index 0 currently has a special meaning for Chrome's
4895 * debugger.
4896 */
4897 void SetEmbedderData(int index, Handle<Value> value);
4898
4899 /**
4900 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4901 * index, which must have bees set by a previous call to
4902 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4903 * currently has a special meaning for Chrome's debugger.
4904 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004905 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004906
4907 /**
4908 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4909 * index, growing the data as needed. Note that index 0 currently has a
4910 * special meaning for Chrome's debugger.
4911 */
4912 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004913
4914 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004915 * Control whether code generation from strings is allowed. Calling
4916 * this method with false will disable 'eval' and the 'Function'
4917 * constructor for code running in this context. If 'eval' or the
4918 * 'Function' constructor are used an exception will be thrown.
4919 *
4920 * If code generation from strings is not allowed the
4921 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4922 * set before blocking the call to 'eval' or the 'Function'
4923 * constructor. If that callback returns true, the call will be
4924 * allowed, otherwise an exception will be thrown. If no callback is
4925 * set an exception will be thrown.
4926 */
4927 void AllowCodeGenerationFromStrings(bool allow);
4928
4929 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004930 * Returns true if code generation from strings is allowed for the context.
4931 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4932 */
4933 bool IsCodeGenerationFromStringsAllowed();
4934
4935 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004936 * Sets the error description for the exception that is thrown when
4937 * code generation from strings is not allowed and 'eval' or the 'Function'
4938 * constructor are called.
4939 */
4940 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4941
4942 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004943 * Stack-allocated class which sets the execution context for all
4944 * operations executed within a local scope.
4945 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004946 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004947 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004948 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004949 context_->Enter();
4950 }
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00004951 // TODO(dcarney): deprecate
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004952 V8_INLINE(Scope(Isolate* isolate, Persistent<Context>& context)) // NOLINT
4953#ifndef V8_USE_UNSAFE_HANDLES
4954 : context_(Handle<Context>::New(isolate, context)) {
4955#else
4956 : context_(Local<Context>::New(isolate, context)) {
4957#endif
4958 context_->Enter();
4959 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004960 V8_INLINE(~Scope()) { context_->Exit(); }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004961
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004962 private:
4963 Handle<Context> context_;
4964 };
4965
4966 private:
4967 friend class Value;
4968 friend class Script;
4969 friend class Object;
4970 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004971
4972 Local<Value> SlowGetEmbedderData(int index);
4973 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004974};
4975
4976
4977/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004978 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4979 * to use any given V8 isolate, see the comments in the Isolate class. The
4980 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4981 * object pointers obtained from V8 handles while in the particular V8 isolate.
4982 * It is up to the user of V8 to ensure, perhaps with locking, that this
4983 * constraint is not violated. In addition to any other synchronization
4984 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4985 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004986 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004987 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4988 * construction and destruction, the current thread is allowed to use the locked
4989 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4990 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004991 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004992 * Sample usage:
4993* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004994 * ...
4995 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004996 * v8::Locker locker(isolate);
4997 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004998 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004999 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005000 * ...
5001 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00005002 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005003 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005004 * If you wish to stop using V8 in a thread A you can do this either by
5005 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
5006 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005007 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00005008 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005009 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005010 * isolate->Exit();
5011 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005012 * ...
5013 * // Code not using V8 goes here while V8 can run in another thread.
5014 * ...
5015 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005016 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005017 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005018 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005019 * The Unlocker object is intended for use in a long-running callback from V8,
5020 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005021 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005022 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
5023 * given thread. This can be useful if you have code that can be called either
5024 * from code that holds the lock or from code that does not. The Unlocker is
5025 * not recursive so you can not have several Unlockers on the stack at once, and
5026 * 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 +00005027 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005028 * An unlocker will unlock several lockers if it has to and reinstate the
5029 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005030 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00005031 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005032 * // V8 not locked.
5033 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005034 * v8::Locker locker(isolate);
5035 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005036 * // V8 locked.
5037 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005038 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005039 * // V8 still locked (2 levels).
5040 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005041 * isolate->Exit();
5042 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005043 * // V8 not locked.
5044 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00005045 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005046 * // V8 locked again (2 levels).
5047 * }
5048 * // V8 still locked (1 level).
5049 * }
5050 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00005051 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005052 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005053class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005054 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005055 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005056 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005057 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005058 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
5059
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005060 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005061 V8_DEPRECATED(Unlocker());
5062
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005063 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00005064 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005065 void Initialize(Isolate* isolate);
5066
lrn@chromium.org1c092762011-05-09 09:42:16 +00005067 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005068};
5069
5070
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005071class V8EXPORT Locker {
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 Locker 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 Locker(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(Locker());
5080
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005081 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005082
5083 /**
5084 * Start preemption.
5085 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005086 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00005087 * that will switch between multiple threads that are in contention
5088 * for the V8 lock.
5089 */
5090 static void StartPreemption(int every_n_ms);
5091
5092 /**
5093 * Stop preemption.
5094 */
5095 static void StopPreemption();
5096
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005097 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005098 * Returns whether or not the locker for a given isolate, is locked by the
5099 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005100 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005101 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005102
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005103 /**
5104 * Returns whether v8::Locker is being used by this V8 instance.
5105 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00005106 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005108 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005109 void Initialize(Isolate* isolate);
5110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005111 bool has_lock_;
5112 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005113 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005114
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005115 static bool active_;
5116
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005117 // Disallow copying and assigning.
5118 Locker(const Locker&);
5119 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005120};
5121
5122
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005123/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005124 * A struct for exporting HeapStats data from V8, using "push" model.
5125 */
5126struct HeapStatsUpdate;
5127
5128
5129/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005130 * An interface for exporting data from V8, using "push" model.
5131 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005132class V8EXPORT OutputStream { // NOLINT
5133 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005134 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005135 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005136 };
5137 enum WriteResult {
5138 kContinue = 0,
5139 kAbort = 1
5140 };
5141 virtual ~OutputStream() {}
5142 /** Notify about the end of stream. */
5143 virtual void EndOfStream() = 0;
5144 /** Get preferred output chunk size. Called only once. */
5145 virtual int GetChunkSize() { return 1024; }
5146 /** Get preferred output encoding. Called only once. */
5147 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
5148 /**
5149 * Writes the next chunk of snapshot data into the stream. Writing
5150 * can be stopped by returning kAbort as function result. EndOfStream
5151 * will not be called in case writing was aborted.
5152 */
5153 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005154 /**
5155 * Writes the next chunk of heap stats data into the stream. Writing
5156 * can be stopped by returning kAbort as function result. EndOfStream
5157 * will not be called in case writing was aborted.
5158 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005159 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00005160 return kAbort;
5161 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005162};
5163
5164
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005165/**
5166 * An interface for reporting progress and controlling long-running
5167 * activities.
5168 */
5169class V8EXPORT ActivityControl { // NOLINT
5170 public:
5171 enum ControlOption {
5172 kContinue = 0,
5173 kAbort = 1
5174 };
5175 virtual ~ActivityControl() {}
5176 /**
5177 * Notify about current progress. The activity can be stopped by
5178 * returning kAbort as the callback result.
5179 */
5180 virtual ControlOption ReportProgressValue(int done, int total) = 0;
5181};
5182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005183
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00005184// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005185
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005186
5187namespace internal {
5188
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00005189const int kApiPointerSize = sizeof(void*); // NOLINT
5190const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005191
5192// Tag information for HeapObject.
5193const int kHeapObjectTag = 1;
5194const int kHeapObjectTagSize = 2;
5195const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
5196
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005197// Tag information for Smi.
5198const int kSmiTag = 0;
5199const int kSmiTagSize = 1;
5200const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
5201
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005202template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005203
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005204template<int kSmiShiftSize>
5205V8_INLINE(internal::Object* IntToSmi(int value)) {
5206 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
5207 intptr_t tagged_value =
5208 (static_cast<intptr_t>(value) << smi_shift_bits) | kSmiTag;
5209 return reinterpret_cast<internal::Object*>(tagged_value);
5210}
5211
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005212// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005213template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005214 static const int kSmiShiftSize = 0;
5215 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005216 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005217 int shift_bits = kSmiTagSize + kSmiShiftSize;
5218 // Throw away top 32 bits and shift down (requires >> to be sign extending).
5219 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
5220 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005221 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5222 return internal::IntToSmi<kSmiShiftSize>(value);
5223 }
5224 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5225 // To be representable as an tagged small integer, the two
5226 // most-significant bits of 'value' must be either 00 or 11 due to
5227 // sign-extension. To check this we add 01 to the two
5228 // most-significant bits, and check if the most-significant bit is 0
5229 //
5230 // CAUTION: The original code below:
5231 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
5232 // may lead to incorrect results according to the C language spec, and
5233 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
5234 // compiler may produce undefined results in case of signed integer
5235 // overflow. The computation must be done w/ unsigned ints.
5236 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
5237 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005238};
5239
5240// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005241template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005242 static const int kSmiShiftSize = 31;
5243 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005244 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005245 int shift_bits = kSmiTagSize + kSmiShiftSize;
5246 // Shift down and throw away top 32 bits.
5247 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
5248 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005249 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5250 return internal::IntToSmi<kSmiShiftSize>(value);
5251 }
5252 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5253 // To be representable as a long smi, the value must be a 32-bit integer.
5254 return (value == static_cast<int32_t>(value));
5255 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005256};
5257
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005258typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
5259const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
5260const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005261
5262/**
5263 * This class exports constants and functionality from within v8 that
5264 * is necessary to implement inline functions in the v8 api. Don't
5265 * depend on functions and constants defined here.
5266 */
5267class Internals {
5268 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005269 // These values match non-compiler-dependent values defined within
5270 // the implementation of v8.
5271 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005272 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005273 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005274
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005275 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00005276 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005277 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005278 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
5279 static const int kContextHeaderSize = 2 * kApiPointerSize;
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005280 static const int kContextEmbedderDataIndex = 64;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005281 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005282 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00005283 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005284 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005285
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005286 static const int kIsolateStateOffset = 0;
5287 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
5288 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
5289 static const int kUndefinedValueRootIndex = 5;
5290 static const int kNullValueRootIndex = 7;
5291 static const int kTrueValueRootIndex = 8;
5292 static const int kFalseValueRootIndex = 9;
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005293 static const int kEmptyStringRootIndex = 129;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005294
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005295 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
5296 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
5297 static const int kNodeStateMask = 0xf;
5298 static const int kNodeStateIsWeakValue = 2;
5299 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005300 static const int kNodeIsIndependentShift = 4;
5301 static const int kNodeIsPartiallyDependentShift = 5;
5302
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005303 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005304 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005305 static const int kOddballType = 0x83;
5306 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005307
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005308 static const int kUndefinedOddballKind = 5;
5309 static const int kNullOddballKind = 3;
5310
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005311 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005312 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
5313 kHeapObjectTag);
5314 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005315
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005316 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005317 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005318 }
5319
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005320 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5321 return PlatformSmiTagging::IntToSmi(value);
5322 }
5323
5324 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5325 return PlatformSmiTagging::IsValidSmi(value);
5326 }
5327
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005328 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005329 typedef internal::Object O;
5330 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
5331 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
5332 }
5333
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005334 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005335 typedef internal::Object O;
5336 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
5337 }
5338
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005339 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005340 int representation = (instance_type & kFullStringRepresentationMask);
5341 return representation == kExternalTwoByteRepresentationTag;
5342 }
5343
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005344 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005345 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
5346 return *reinterpret_cast<int*>(addr) == 1;
5347 }
5348
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005349 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
5350 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5351 return *addr & (1 << shift);
5352 }
5353
5354 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
5355 bool value, int shift)) {
5356 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5357 uint8_t mask = 1 << shift;
5358 *addr = (*addr & ~mask) | (value << shift);
5359 }
5360
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005361 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
5362 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5363 return *addr & kNodeStateMask;
5364 }
5365
5366 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
5367 uint8_t value)) {
5368 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5369 *addr = (*addr & ~kNodeStateMask) | value;
5370 }
5371
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005372 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005373 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5374 kIsolateEmbedderDataOffset;
5375 *reinterpret_cast<void**>(addr) = data;
5376 }
5377
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005378 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005379 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5380 kIsolateEmbedderDataOffset;
5381 return *reinterpret_cast<void**>(addr);
5382 }
5383
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005384 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
5385 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005386 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
5387 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
5388 }
5389
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005390 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005391 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005392 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
5393 return *reinterpret_cast<T*>(addr);
5394 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005395
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005396 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005397 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005398 typedef internal::Object O;
5399 typedef internal::Internals I;
5400 O* ctx = *reinterpret_cast<O**>(context);
5401 int embedder_data_offset = I::kContextHeaderSize +
5402 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
5403 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
5404 int value_offset =
5405 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
5406 return I::ReadField<T>(embedder_data, value_offset);
5407 }
5408
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005409 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
5410 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
5411 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
5412 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
5413 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
5414 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
5415 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005416};
5417
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005418} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005419
5420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005421template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005422Local<T>::Local() : Handle<T>() { }
5423
5424
5425template <class T>
5426Local<T> Local<T>::New(Handle<T> that) {
5427 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005428 T* that_ptr = *that;
5429 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5430 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
5431 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5432 reinterpret_cast<internal::HeapObject*>(*p))));
5433 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005434 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
5435}
5436
5437
5438template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005439Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005440 return New(isolate, that.val_);
5441}
5442
5443#ifndef V8_USE_UNSAFE_HANDLES
5444template <class T>
5445Local<T> Local<T>::New(Isolate* isolate, const Persistent<T>& that) {
5446 return New(isolate, that.val_);
5447}
5448
5449template <class T>
5450Handle<T> Handle<T>::New(Isolate* isolate, T* that) {
5451 if (that == NULL) return Handle<T>();
5452 T* that_ptr = that;
5453 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5454 return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5455 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5456}
5457#endif
5458
5459
5460template <class T>
5461Local<T> Local<T>::New(Isolate* isolate, T* that) {
5462 if (that == NULL) return Local<T>();
5463 T* that_ptr = that;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005464 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5465 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5466 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5467}
5468
5469
5470template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005471Persistent<T> Persistent<T>::New(Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005472 return New(Isolate::GetCurrent(), that.val_);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005473}
5474
5475
5476template <class T>
5477Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005478 return New(Isolate::GetCurrent(), that.val_);
5479}
5480
5481#ifndef V8_USE_UNSAFE_HANDLES
5482template <class T>
5483Persistent<T> Persistent<T>::New(Isolate* isolate, Persistent<T> that) {
5484 return New(Isolate::GetCurrent(), that.val_);
5485}
5486#endif
5487
5488template <class T>
5489Persistent<T> Persistent<T>::New(Isolate* isolate, T* that) {
5490 if (that == NULL) return Persistent<T>();
5491 internal::Object** p = reinterpret_cast<internal::Object**>(that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005492 return Persistent<T>(reinterpret_cast<T*>(
5493 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
5494 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005495}
5496
5497
5498template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005499bool Persistent<T>::IsIndependent() const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005500 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005501 if (this->IsEmpty()) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005502 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005503 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005504}
5505
5506
5507template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005508bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005509 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005510 if (this->IsEmpty()) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005511 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005512 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005513}
5514
5515
5516template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005517bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005518 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005519 if (this->IsEmpty()) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005520 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005521 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005522}
5523
5524
5525template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005526void Persistent<T>::Dispose() {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005527 if (this->IsEmpty()) return;
5528 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
5529#ifndef V8_USE_UNSAFE_HANDLES
5530 val_ = 0;
5531#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005532}
5533
5534
5535template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005536template <typename S, typename P>
5537void Persistent<T>::MakeWeak(
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005538 P* parameters,
5539 typename WeakReferenceCallbacks<S, P>::Revivable callback) {
5540 TYPE_CHECK(S, T);
5541 typedef typename WeakReferenceCallbacks<Value, void>::Revivable Revivable;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005542 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005543 parameters,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005544 reinterpret_cast<Revivable>(callback),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005545 NULL);
5546}
5547
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005548
5549template <class T>
5550template <typename P>
5551void Persistent<T>::MakeWeak(
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005552 P* parameters,
5553 typename WeakReferenceCallbacks<T, P>::Revivable callback) {
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005554 MakeWeak<T, P>(parameters, callback);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005555}
5556
5557
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005558template <class T>
5559void Persistent<T>::MakeWeak(Isolate* isolate,
5560 void* parameters,
5561 NearDeathCallback callback) {
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005562 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005563 parameters,
5564 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005565 callback);
5566}
5567
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005568
5569template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005570void Persistent<T>::ClearWeak() {
5571 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005572}
5573
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005574
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005575template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005576void Persistent<T>::MarkIndependent() {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005577 typedef internal::Internals I;
5578 if (this->IsEmpty()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005579 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005580 true,
5581 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005582}
5583
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005584
5585template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005586void Persistent<T>::MarkPartiallyDependent() {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005587 typedef internal::Internals I;
5588 if (this->IsEmpty()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005589 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005590 true,
5591 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005592}
5593
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005594
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005595template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005596void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
5597 Dispose(isolate);
5598#ifdef V8_USE_UNSAFE_HANDLES
5599 *this = *New(isolate, other);
5600#else
5601 if (other.IsEmpty()) {
5602 this->val_ = NULL;
5603 return;
5604 }
5605 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5606 this->val_ = reinterpret_cast<T*>(
5607 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5608#endif
5609}
5610
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005611
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005612#ifndef V8_USE_UNSAFE_HANDLES
5613template <class T>
5614void Persistent<T>::Reset(Isolate* isolate, const Persistent<T>& other) {
5615 Dispose(isolate);
5616 if (other.IsEmpty()) {
5617 this->val_ = NULL;
5618 return;
5619 }
5620 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5621 this->val_ = reinterpret_cast<T*>(
5622 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5623}
5624#endif
5625
5626
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005627template <class T>
5628T* Persistent<T>::ClearAndLeak() {
5629 T* old;
5630#ifdef V8_USE_UNSAFE_HANDLES
5631 old = **this;
5632 *this = Persistent<T>();
5633#else
5634 old = val_;
5635 val_ = NULL;
5636#endif
5637 return old;
5638}
5639
5640
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005641template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005642void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005643 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005644 if (this->IsEmpty()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005645 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005646 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5647 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005648}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005649
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005650
5651template <class T>
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005652uint16_t Persistent<T>::WrapperClassId() const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005653 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005654 if (this->IsEmpty()) return 0;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005655 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005656 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5657 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005658}
5659
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005660
5661template<typename T>
5662ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
5663
5664template<typename T>
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00005665template<typename S>
5666void ReturnValue<T>::Set(const Persistent<S>& handle) {
5667 TYPE_CHECK(T, S);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005668 if (V8_UNLIKELY(handle.IsEmpty())) {
5669 SetUndefined();
5670 } else {
5671 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5672 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005673}
5674
5675template<typename T>
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00005676template<typename S>
5677void ReturnValue<T>::Set(const Handle<S> handle) {
5678 TYPE_CHECK(T, S);
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00005679 if (V8_UNLIKELY(handle.IsEmpty())) {
5680 SetUndefined();
5681 } else {
5682 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5683 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005684}
5685
5686template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005687void ReturnValue<T>::Set(double i) {
5688 Set(Number::New(GetIsolate(), i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005689}
5690
5691template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005692void ReturnValue<T>::Set(int32_t i) {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005693 typedef internal::Internals I;
5694 if (V8_LIKELY(I::IsValidSmi(i))) {
5695 *value_ = I::IntToSmi(i);
5696 return;
5697 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005698 Set(Integer::New(i, GetIsolate()));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005699}
5700
5701template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005702void ReturnValue<T>::Set(uint32_t i) {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005703 typedef internal::Internals I;
5704 if (V8_LIKELY(I::IsValidSmi(i))) {
5705 *value_ = I::IntToSmi(i);
5706 return;
5707 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005708 Set(Integer::NewFromUnsigned(i, GetIsolate()));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005709}
5710
5711template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005712void ReturnValue<T>::Set(bool value) {
5713 typedef internal::Internals I;
5714 int root_index;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005715 if (value) {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005716 root_index = I::kTrueValueRootIndex;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005717 } else {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005718 root_index = I::kFalseValueRootIndex;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005719 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005720 *value_ = *I::GetRoot(GetIsolate(), root_index);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005721}
5722
5723template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005724void ReturnValue<T>::SetNull() {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005725 typedef internal::Internals I;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005726 *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005727}
5728
5729template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005730void ReturnValue<T>::SetUndefined() {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005731 typedef internal::Internals I;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005732 *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005733}
5734
5735template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005736Isolate* ReturnValue<T>::GetIsolate() {
5737 // Isolate is always the pointer below value_ on the stack.
5738 return *reinterpret_cast<Isolate**>(&value_[-1]);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005739}
5740
5741
5742template<typename T>
5743FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
5744 internal::Object** values,
5745 int length,
5746 bool is_construct_call)
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005747 : implicit_args_(implicit_args),
5748 values_(values),
5749 length_(length),
5750 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005751
5752
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005753Arguments::Arguments(internal::Object** args,
5754 internal::Object** values,
5755 int length,
5756 bool is_construct_call)
5757 : FunctionCallbackInfo<Value>(args, values, length, is_construct_call) { }
5758
5759
5760template<typename T>
5761Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005762 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
5763 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
5764}
5765
5766
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005767template<typename T>
5768Local<Function> FunctionCallbackInfo<T>::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005769 return Local<Function>(reinterpret_cast<Function*>(
5770 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005771}
5772
5773
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005774template<typename T>
5775Local<Object> FunctionCallbackInfo<T>::This() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005776 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
5777}
5778
5779
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005780template<typename T>
5781Local<Object> FunctionCallbackInfo<T>::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005782 return Local<Object>(reinterpret_cast<Object*>(
5783 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005784}
5785
5786
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005787template<typename T>
5788Local<Value> FunctionCallbackInfo<T>::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005789 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005790}
5791
5792
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005793template<typename T>
5794Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005795 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
5796}
5797
5798
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005799template<typename T>
5800ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
5801 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
5802}
5803
5804
5805template<typename T>
5806bool FunctionCallbackInfo<T>::IsConstructCall() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005807 return is_construct_call_;
5808}
5809
5810
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005811template<typename T>
5812int FunctionCallbackInfo<T>::Length() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005813 return length_;
5814}
5815
5816
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005817template <class T>
5818Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005819 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
5820 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005821 return Local<T>(reinterpret_cast<T*>(after));
5822}
5823
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005824Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005825 return resource_name_;
5826}
5827
5828
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005829Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005830 return resource_line_offset_;
5831}
5832
5833
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005834Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005835 return resource_column_offset_;
5836}
5837
5838
5839Handle<Boolean> Boolean::New(bool value) {
5840 return value ? True() : False();
5841}
5842
5843
5844void Template::Set(const char* name, v8::Handle<Data> value) {
5845 Set(v8::String::New(name), value);
5846}
5847
5848
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005849Local<Value> Object::GetInternalField(int index) {
5850#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005851 typedef internal::Object O;
5852 typedef internal::Internals I;
5853 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005854 // Fast path: If the object is a plain JSObject, which is the common case, we
5855 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005856 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005857 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005858 O* value = I::ReadField<O*>(obj, offset);
5859 O** result = HandleScope::CreateHandle(value);
5860 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005861 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005862#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005863 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005864}
5865
5866
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005867void* Object::GetAlignedPointerFromInternalField(int index) {
5868#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005869 typedef internal::Object O;
5870 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005871 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005872 // Fast path: If the object is a plain JSObject, which is the common case, we
5873 // know where to find the internal fields and can return the value directly.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005874 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005875 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005876 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005877 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005878#endif
5879 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005880}
5881
5882
5883String* String::Cast(v8::Value* value) {
5884#ifdef V8_ENABLE_CHECKS
5885 CheckCast(value);
5886#endif
5887 return static_cast<String*>(value);
5888}
5889
5890
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005891Local<String> String::Empty(Isolate* isolate) {
5892 typedef internal::Object* S;
5893 typedef internal::Internals I;
5894 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005895 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005896 return Local<String>(reinterpret_cast<String*>(slot));
5897}
5898
5899
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00005900Local<String> String::New(const char* data, int length) {
5901 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
5902}
5903
5904
5905Local<String> String::New(const uint16_t* data, int length) {
5906 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
5907}
5908
5909
5910Local<String> String::NewSymbol(const char* data, int length) {
5911 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
5912}
5913
5914
5915Local<String> String::NewUndetectable(const char* data, int length) {
5916 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
5917}
5918
5919
5920Local<String> String::NewUndetectable(const uint16_t* data, int length) {
5921 return NewFromTwoByte(
5922 Isolate::GetCurrent(), data, kUndetectableString, length);
5923}
5924
5925
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005926String::ExternalStringResource* String::GetExternalStringResource() const {
5927 typedef internal::Object O;
5928 typedef internal::Internals I;
5929 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005930 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005931 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005932 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5933 result = reinterpret_cast<String::ExternalStringResource*>(value);
5934 } else {
5935 result = NULL;
5936 }
5937#ifdef V8_ENABLE_CHECKS
5938 VerifyExternalStringResource(result);
5939#endif
5940 return result;
5941}
5942
5943
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005944String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
5945 String::Encoding* encoding_out) const {
5946 typedef internal::Object O;
5947 typedef internal::Internals I;
5948 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
5949 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
5950 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5951 ExternalStringResourceBase* resource = NULL;
5952 if (type == I::kExternalAsciiRepresentationTag ||
5953 type == I::kExternalTwoByteRepresentationTag) {
5954 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5955 resource = static_cast<ExternalStringResourceBase*>(value);
5956 }
5957#ifdef V8_ENABLE_CHECKS
5958 VerifyExternalStringResourceBase(resource, *encoding_out);
5959#endif
5960 return resource;
5961}
5962
5963
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005964bool Value::IsUndefined() const {
5965#ifdef V8_ENABLE_CHECKS
5966 return FullIsUndefined();
5967#else
5968 return QuickIsUndefined();
5969#endif
5970}
5971
5972bool Value::QuickIsUndefined() const {
5973 typedef internal::Object O;
5974 typedef internal::Internals I;
5975 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5976 if (!I::HasHeapObjectTag(obj)) return false;
5977 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5978 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
5979}
5980
5981
5982bool Value::IsNull() const {
5983#ifdef V8_ENABLE_CHECKS
5984 return FullIsNull();
5985#else
5986 return QuickIsNull();
5987#endif
5988}
5989
5990bool Value::QuickIsNull() const {
5991 typedef internal::Object O;
5992 typedef internal::Internals I;
5993 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5994 if (!I::HasHeapObjectTag(obj)) return false;
5995 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5996 return (I::GetOddballKind(obj) == I::kNullOddballKind);
5997}
5998
5999
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006000bool Value::IsString() const {
6001#ifdef V8_ENABLE_CHECKS
6002 return FullIsString();
6003#else
6004 return QuickIsString();
6005#endif
6006}
6007
6008bool Value::QuickIsString() const {
6009 typedef internal::Object O;
6010 typedef internal::Internals I;
6011 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6012 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006013 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006014}
6015
6016
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006017Symbol* Symbol::Cast(v8::Value* value) {
6018#ifdef V8_ENABLE_CHECKS
6019 CheckCast(value);
6020#endif
6021 return static_cast<Symbol*>(value);
6022}
6023
6024
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006025Number* Number::Cast(v8::Value* value) {
6026#ifdef V8_ENABLE_CHECKS
6027 CheckCast(value);
6028#endif
6029 return static_cast<Number*>(value);
6030}
6031
6032
6033Integer* Integer::Cast(v8::Value* value) {
6034#ifdef V8_ENABLE_CHECKS
6035 CheckCast(value);
6036#endif
6037 return static_cast<Integer*>(value);
6038}
6039
6040
6041Date* Date::Cast(v8::Value* value) {
6042#ifdef V8_ENABLE_CHECKS
6043 CheckCast(value);
6044#endif
6045 return static_cast<Date*>(value);
6046}
6047
6048
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006049StringObject* StringObject::Cast(v8::Value* value) {
6050#ifdef V8_ENABLE_CHECKS
6051 CheckCast(value);
6052#endif
6053 return static_cast<StringObject*>(value);
6054}
6055
6056
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006057SymbolObject* SymbolObject::Cast(v8::Value* value) {
6058#ifdef V8_ENABLE_CHECKS
6059 CheckCast(value);
6060#endif
6061 return static_cast<SymbolObject*>(value);
6062}
6063
6064
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006065NumberObject* NumberObject::Cast(v8::Value* value) {
6066#ifdef V8_ENABLE_CHECKS
6067 CheckCast(value);
6068#endif
6069 return static_cast<NumberObject*>(value);
6070}
6071
6072
6073BooleanObject* BooleanObject::Cast(v8::Value* value) {
6074#ifdef V8_ENABLE_CHECKS
6075 CheckCast(value);
6076#endif
6077 return static_cast<BooleanObject*>(value);
6078}
6079
6080
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00006081RegExp* RegExp::Cast(v8::Value* value) {
6082#ifdef V8_ENABLE_CHECKS
6083 CheckCast(value);
6084#endif
6085 return static_cast<RegExp*>(value);
6086}
6087
6088
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006089Object* Object::Cast(v8::Value* value) {
6090#ifdef V8_ENABLE_CHECKS
6091 CheckCast(value);
6092#endif
6093 return static_cast<Object*>(value);
6094}
6095
6096
6097Array* Array::Cast(v8::Value* value) {
6098#ifdef V8_ENABLE_CHECKS
6099 CheckCast(value);
6100#endif
6101 return static_cast<Array*>(value);
6102}
6103
6104
danno@chromium.orgca29dd82013-04-26 11:59:48 +00006105ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
6106#ifdef V8_ENABLE_CHECKS
6107 CheckCast(value);
6108#endif
6109 return static_cast<ArrayBuffer*>(value);
6110}
6111
6112
danno@chromium.orgf005df62013-04-30 16:36:45 +00006113TypedArray* TypedArray::Cast(v8::Value* value) {
6114#ifdef V8_ENABLE_CHECKS
6115 CheckCast(value);
6116#endif
6117 return static_cast<TypedArray*>(value);
6118}
6119
6120
6121Uint8Array* Uint8Array::Cast(v8::Value* value) {
6122#ifdef V8_ENABLE_CHECKS
6123 CheckCast(value);
6124#endif
6125 return static_cast<Uint8Array*>(value);
6126}
6127
6128
6129Int8Array* Int8Array::Cast(v8::Value* value) {
6130#ifdef V8_ENABLE_CHECKS
6131 CheckCast(value);
6132#endif
6133 return static_cast<Int8Array*>(value);
6134}
6135
6136
6137Uint16Array* Uint16Array::Cast(v8::Value* value) {
6138#ifdef V8_ENABLE_CHECKS
6139 CheckCast(value);
6140#endif
6141 return static_cast<Uint16Array*>(value);
6142}
6143
6144
6145Int16Array* Int16Array::Cast(v8::Value* value) {
6146#ifdef V8_ENABLE_CHECKS
6147 CheckCast(value);
6148#endif
6149 return static_cast<Int16Array*>(value);
6150}
6151
6152
6153Uint32Array* Uint32Array::Cast(v8::Value* value) {
6154#ifdef V8_ENABLE_CHECKS
6155 CheckCast(value);
6156#endif
6157 return static_cast<Uint32Array*>(value);
6158}
6159
6160
6161Int32Array* Int32Array::Cast(v8::Value* value) {
6162#ifdef V8_ENABLE_CHECKS
6163 CheckCast(value);
6164#endif
6165 return static_cast<Int32Array*>(value);
6166}
6167
6168
6169Float32Array* Float32Array::Cast(v8::Value* value) {
6170#ifdef V8_ENABLE_CHECKS
6171 CheckCast(value);
6172#endif
6173 return static_cast<Float32Array*>(value);
6174}
6175
6176
6177Float64Array* Float64Array::Cast(v8::Value* value) {
6178#ifdef V8_ENABLE_CHECKS
6179 CheckCast(value);
6180#endif
6181 return static_cast<Float64Array*>(value);
6182}
6183
6184
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006185Function* Function::Cast(v8::Value* value) {
6186#ifdef V8_ENABLE_CHECKS
6187 CheckCast(value);
6188#endif
6189 return static_cast<Function*>(value);
6190}
6191
6192
6193External* External::Cast(v8::Value* value) {
6194#ifdef V8_ENABLE_CHECKS
6195 CheckCast(value);
6196#endif
6197 return static_cast<External*>(value);
6198}
6199
6200
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006201template<typename T>
6202Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
6203 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00006204}
6205
6206
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006207template<typename T>
6208Local<Value> PropertyCallbackInfo<T>::Data() const {
6209 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006210}
6211
6212
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006213template<typename T>
6214Local<Object> PropertyCallbackInfo<T>::This() const {
6215 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006216}
6217
6218
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006219template<typename T>
6220Local<Object> PropertyCallbackInfo<T>::Holder() const {
6221 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
6222}
6223
6224
6225template<typename T>
6226ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
6227 return ReturnValue<T>(&args_[kReturnValueIndex]);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006228}
6229
6230
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00006231Handle<Primitive> Undefined(Isolate* isolate) {
6232 typedef internal::Object* S;
6233 typedef internal::Internals I;
6234 if (!I::IsInitialized(isolate)) return Undefined();
6235 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
6236 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6237}
6238
6239
6240Handle<Primitive> Null(Isolate* isolate) {
6241 typedef internal::Object* S;
6242 typedef internal::Internals I;
6243 if (!I::IsInitialized(isolate)) return Null();
6244 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
6245 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6246}
6247
6248
6249Handle<Boolean> True(Isolate* isolate) {
6250 typedef internal::Object* S;
6251 typedef internal::Internals I;
6252 if (!I::IsInitialized(isolate)) return True();
6253 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
6254 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6255}
6256
6257
6258Handle<Boolean> False(Isolate* isolate) {
6259 typedef internal::Object* S;
6260 typedef internal::Internals I;
6261 if (!I::IsInitialized(isolate)) return False();
6262 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
6263 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6264}
6265
6266
6267void Isolate::SetData(void* data) {
6268 typedef internal::Internals I;
6269 I::SetEmbedderData(this, data);
6270}
6271
6272
6273void* Isolate::GetData() {
6274 typedef internal::Internals I;
6275 return I::GetEmbedderData(this);
6276}
6277
6278
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00006279Local<Value> Context::GetEmbedderData(int index) {
6280#ifndef V8_ENABLE_CHECKS
6281 typedef internal::Object O;
6282 typedef internal::Internals I;
6283 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
6284 return Local<Value>(reinterpret_cast<Value*>(result));
6285#else
6286 return SlowGetEmbedderData(index);
6287#endif
6288}
6289
6290
6291void* Context::GetAlignedPointerFromEmbedderData(int index) {
6292#ifndef V8_ENABLE_CHECKS
6293 typedef internal::Internals I;
6294 return I::ReadEmbedderData<void*>(this, index);
6295#else
6296 return SlowGetAlignedPointerFromEmbedderData(index);
6297#endif
6298}
6299
6300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006301/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00006302 * \example shell.cc
6303 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006304 * command-line and executes them.
6305 */
6306
6307
6308/**
6309 * \example process.cc
6310 */
6311
6312
6313} // namespace v8
6314
6315
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006316#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006317#undef TYPE_CHECK
6318
6319
ager@chromium.org9258b6b2008-09-11 09:11:10 +00006320#endif // V8_H_