blob: 7dfc9189aa307198416f02c8f804c10d1653adbe [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
231
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232/**
233 * An object reference managed by the v8 garbage collector.
234 *
235 * All objects returned from v8 have to be tracked by the garbage
236 * collector so that it knows that the objects are still alive. Also,
237 * because the garbage collector may move objects, it is unsafe to
238 * point directly to an object. Instead, all objects are stored in
239 * handles which are known by the garbage collector and updated
240 * whenever an object moves. Handles should always be passed by value
241 * (except in cases like out-parameters) and they should never be
242 * allocated on the heap.
243 *
244 * There are two types of handles: local and persistent handles.
245 * Local handles are light-weight and transient and typically used in
246 * local operations. They are managed by HandleScopes. Persistent
247 * handles can be used when storing objects across several independent
248 * operations and have to be explicitly deallocated when they're no
249 * longer used.
250 *
251 * It is safe to extract the object stored in the handle by
252 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000253 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254 * behind the scenes and the same rules apply to these values as to
255 * their handles.
256 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000257template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 /**
260 * Creates an empty handle.
261 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000262 V8_INLINE(Handle()) : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000264#ifdef V8_USE_UNSAFE_HANDLES
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 /**
266 * Creates a new handle for the specified value.
267 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000268 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000269#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
271 /**
272 * Creates a handle for the contents of the specified handle. This
273 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000274 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000276 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 * between compatible handles, for instance assigning a
278 * Handle<String> to a variable declared as Handle<Value>, is legal
279 * because String is a subclass of Value.
280 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000281 template <class S> V8_INLINE(Handle(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282 : val_(reinterpret_cast<T*>(*that)) {
283 /**
284 * This check fails when trying to convert between incompatible
285 * handles. For example, converting from a Handle<String> to a
286 * Handle<Number>.
287 */
288 TYPE_CHECK(T, S);
289 }
290
291 /**
292 * Returns true if the handle is empty.
293 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000294 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 /**
297 * Sets the handle to be empty. IsEmpty() will then return true.
298 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000299 V8_INLINE(void Clear()) { val_ = 0; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000300
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000301 V8_INLINE(T* operator->() const) { return val_; }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000302
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000303 V8_INLINE(T* operator*() const) { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304
305 /**
306 * Checks whether two handles are the same.
307 * Returns true if both are empty, or if the objects
308 * to which they refer are identical.
309 * The handles' references are not checked.
310 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000311 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000312 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
313 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 if (a == 0) return b == 0;
315 if (b == 0) return false;
316 return *a == *b;
317 }
318
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000319#ifndef V8_USE_UNSAFE_HANDLES
320 template <class S> V8_INLINE(
321 bool operator==(const Persistent<S>& that) const) {
322 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
323 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
324 if (a == 0) return b == 0;
325 if (b == 0) return false;
326 return *a == *b;
327 }
328#endif
329
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 /**
331 * Checks whether two handles are different.
332 * Returns true if only one of the handles is empty, or if
333 * the objects to which they refer are different.
334 * The handles' references are not checked.
335 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000336 template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 return !operator==(that);
338 }
339
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000340 template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000341#ifdef V8_ENABLE_CHECKS
342 // If we're going to perform the type check then we have to check
343 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000345#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 return Handle<T>(T::Cast(*that));
347 }
348
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000349 template <class S> V8_INLINE(Handle<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000350 return Handle<S>::Cast(*this);
351 }
352
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000353#ifndef V8_USE_UNSAFE_HANDLES
354 V8_INLINE(static Handle<T> New(Isolate* isolate, Handle<T> that)) {
355 return New(isolate, that.val_);
356 }
357 // TODO(dcarney): remove before cutover
358 V8_INLINE(static Handle<T> New(Isolate* isolate, const Persistent<T>& that)) {
359 return New(isolate, that.val_);
360 }
361
362#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
363
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000365#endif
366 /**
367 * Creates a new handle for the specified value.
368 */
369 V8_INLINE(explicit Handle(T* val)) : val_(val) {}
370#endif
371
372 private:
373 template<class F>
374 friend class Persistent;
375 template<class F>
376 friend class Local;
377 friend class Arguments;
378 friend class String;
379 friend class Object;
380 friend class AccessorInfo;
381 friend Handle<Primitive> Undefined(Isolate* isolate);
382 friend Handle<Primitive> Null(Isolate* isolate);
383 friend Handle<Boolean> True(Isolate* isolate);
384 friend Handle<Boolean> False(Isolate* isolate);
385 friend class Context;
386 friend class InternalHandleHelper;
387 friend class LocalContext;
388
389#ifndef V8_USE_UNSAFE_HANDLES
390 V8_INLINE(static Handle<T> New(Isolate* isolate, T* that));
391#endif
392
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 T* val_;
394};
395
396
397/**
398 * A light-weight stack-allocated object handle. All operations
399 * that return objects from within v8 return them in local handles. They
400 * are created within HandleScopes, and all local handles allocated within a
401 * handle scope are destroyed when the handle scope is destroyed. Hence it
402 * is not necessary to explicitly deallocate local handles.
403 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000404// TODO(dcarney): deprecate entire class
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000405template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000407 V8_INLINE(Local());
408 template <class S> V8_INLINE(Local(Local<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409 : Handle<T>(reinterpret_cast<T*>(*that)) {
410 /**
411 * This check fails when trying to convert between incompatible
412 * handles. For example, converting from a Handle<String> to a
413 * Handle<Number>.
414 */
415 TYPE_CHECK(T, S);
416 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000417
418
419#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000420 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000421#endif
422
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000423 template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000424#ifdef V8_ENABLE_CHECKS
425 // If we're going to perform the type check then we have to check
426 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000428#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 return Local<T>(T::Cast(*that));
430 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000431#ifndef V8_USE_UNSAFE_HANDLES
432 template <class S> V8_INLINE(Local(Handle<S> that))
433 : Handle<T>(reinterpret_cast<T*>(*that)) {
434 TYPE_CHECK(T, S);
435 }
436#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000438 template <class S> V8_INLINE(Local<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000439 return Local<S>::Cast(*this);
440 }
441
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000442 /**
443 * Create a local handle for the content of another handle.
444 * The referee is kept alive by the local handle even when
445 * the original handle is destroyed/disposed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000447 V8_INLINE(static Local<T> New(Handle<T> that));
448 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000449#ifndef V8_USE_UNSAFE_HANDLES
450 // TODO(dcarney): remove before cutover
451 V8_INLINE(static Local<T> New(Isolate* isolate, const Persistent<T>& that));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000453#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
454
455 private:
456#endif
457 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
458#endif
459
460 private:
461 template<class F>
462 friend class Persistent;
463 template<class F>
464 friend class Handle;
465 friend class Arguments;
466 friend class String;
467 friend class Object;
468 friend class AccessorInfo;
469 friend class Context;
470 friend class InternalHandleHelper;
471 friend class LocalContext;
472
473 V8_INLINE(static Local<T> New(Isolate* isolate, T* that));
474};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
476/**
477 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000478 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 * allocated, a Persistent handle remains valid until it is explicitly
480 * disposed.
481 *
482 * A persistent handle contains a reference to a storage cell within
483 * the v8 engine which holds an object value and which is updated by
484 * the garbage collector whenever the object is moved. A new storage
485 * cell can be created using Persistent::New and existing handles can
486 * be disposed using Persistent::Dispose. Since persistent handles
487 * are passed by value you may have many persistent handle objects
488 * that point to the same storage cell. For instance, if you pass a
489 * persistent handle as an argument to a function you will not get two
490 * different storage cells but rather two references to the same
491 * storage cell.
492 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000493template <class T> class Persistent // NOLINT
494#ifdef V8_USE_UNSAFE_HANDLES
495 : public Handle<T> {
496#else
497 { // NOLINT
498#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 public:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000500#ifndef V8_USE_UNSAFE_HANDLES
501 V8_INLINE(Persistent()) : val_(0) { }
502 V8_INLINE(~Persistent()) {
503 // TODO(dcarney): add this back before cutover.
504 // Dispose();
505 }
506 V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
507 // TODO(dcarney): remove somehow before cutover
508 // The handle should either be 0, or a pointer to a live cell.
509 V8_INLINE(void Clear()) { val_ = 0; }
510
511 /**
512 * A constructor that creates a new global cell pointing to that. In contrast
513 * to the copy constructor, this creates a new persistent handle which needs
514 * to be separately disposed.
515 */
516 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
517 : val_(*New(isolate, that)) { }
518
519#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 /**
521 * Creates an empty persistent handle that doesn't point to any
522 * storage cell.
523 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000524 V8_INLINE(Persistent()) : Handle<T>() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525
526 /**
527 * Creates a persistent handle for the same storage cell as the
528 * specified handle. This constructor allows you to pass persistent
529 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000530 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000532 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 * between compatible persistent handles, for instance assigning a
534 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000535 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000537 template <class S> V8_INLINE(Persistent(Persistent<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 : Handle<T>(reinterpret_cast<T*>(*that)) {
539 /**
540 * This check fails when trying to convert between incompatible
541 * handles. For example, converting from a Handle<String> to a
542 * Handle<Number>.
543 */
544 TYPE_CHECK(T, S);
545 }
546
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000547 template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000549 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000550 * A constructor that creates a new global cell pointing to that. In contrast
551 * to the copy constructor, this creates a new persistent handle which needs
552 * to be separately disposed.
553 */
554 template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
555 : Handle<T>(New(isolate, that)) { }
556
557 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000558 * "Casts" a plain handle which is known to be a persistent handle
559 * to a persistent handle.
560 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000561 template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 : Handle<T>(*that) { }
563
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000564#endif
565
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000566 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000567#ifdef V8_ENABLE_CHECKS
568 // If we're going to perform the type check then we have to check
569 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000571#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 return Persistent<T>(T::Cast(*that));
573 }
574
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000575 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000576 return Persistent<S>::Cast(*this);
577 }
578
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000579 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
580
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000582 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000584 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000585 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000586#ifndef V8_USE_UNSAFE_HANDLES
587 // TODO(dcarney): remove before cutover
588 V8_INLINE(static Persistent<T> New(Isolate* isolate, Persistent<T> that));
589#endif
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000590
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000591#ifndef V8_USE_UNSAFE_HANDLES
592 template <class S> V8_INLINE(
593 bool operator==(const Persistent<S>& that) const) {
594 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
595 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
596 if (a == 0) return b == 0;
597 if (b == 0) return false;
598 return *a == *b;
599 }
600
601 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
602 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
603 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
604 if (a == 0) return b == 0;
605 if (b == 0) return false;
606 return *a == *b;
607 }
608#endif
609
610 V8_INLINE(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611
612 /**
613 * Releases the storage cell referenced by this persistent handle.
614 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000615 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616 * cell remain and IsEmpty will still return false.
617 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000618 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000619 V8_INLINE(void Dispose(Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000621 template<typename S, typename P>
622 V8_INLINE(void MakeWeak(
623 Isolate* isolate,
624 P* parameters,
625 typename WeakReferenceCallbacks<S, P>::Revivable callback));
626
627 template<typename P>
628 V8_INLINE(void MakeWeak(
629 Isolate* isolate,
630 P* parameters,
631 typename WeakReferenceCallbacks<T, P>::Revivable callback));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000632
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633 /**
634 * Make the reference to this object weak. When only weak handles
635 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000636 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 * it the object reference and the given parameters.
638 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000639 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000640 V8_INLINE(void MakeWeak(Isolate* isolate,
641 void* parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000642 NearDeathCallback callback));
643
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000644 V8_INLINE(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000646 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000647 V8_INLINE(void ClearWeak(Isolate* isolate));
648
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000649 V8_INLINE(void MarkIndependent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650
651 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000652 * Marks the reference to this object independent. Garbage collector is free
653 * to ignore any object groups containing this object. Weak callback for an
654 * independent handle should not assume that it will be preceded by a global
655 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000656 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000657 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000658 V8_INLINE(void MarkIndependent(Isolate* isolate));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000659
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000660 V8_INLINE(void MarkPartiallyDependent());
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000661
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000662 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000663 * Marks the reference to this object partially dependent. Partially dependent
664 * handles only depend on other partially dependent handles and these
665 * dependencies are provided through object groups. It provides a way to build
666 * smaller object groups for young objects that represent only a subset of all
667 * external dependencies. This mark is automatically cleared after each
668 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000669 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000670 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000671 V8_INLINE(void MarkPartiallyDependent(Isolate* isolate));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000672
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000673 V8_INLINE(bool IsIndependent() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000674
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000675 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000676 V8_INLINE(bool IsIndependent(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000677
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000678 V8_INLINE(bool IsNearDeath() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000679
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000680 /** Checks if the handle holds the only reference to an object. */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000681 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000682 V8_INLINE(bool IsNearDeath(Isolate* isolate) const);
683
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000684 V8_INLINE(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000686 /** Returns true if the handle's reference is weak. */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000687 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000688 V8_INLINE(bool IsWeak(Isolate* isolate) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000690 V8_INLINE(void SetWrapperClassId(uint16_t class_id));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000691
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000692 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000693 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
694 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000695 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000696 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000697 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id));
698
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000699 V8_INLINE(uint16_t WrapperClassId() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000700
701 /**
702 * Returns the class ID previously assigned to this handle or 0 if no class ID
703 * was previously assigned.
704 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000705 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000706 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000707
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000708 /**
709 * Disposes the current contents of the handle and replaces it.
710 */
711 V8_INLINE(void Reset(Isolate* isolate, const Handle<T>& other));
712
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000713 /**
714 * Returns the underlying raw pointer and clears the handle. The caller is
715 * responsible of eventually destroying the underlying object (by creating a
716 * Persistent handle which points to it and Disposing it). In the future,
717 * destructing a Persistent will also Dispose it. With this function, the
718 * embedder can let the Persistent go out of scope without it getting
719 * disposed.
720 */
721 V8_INLINE(T* ClearAndLeak());
722
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000723#ifndef V8_USE_UNSAFE_HANDLES
724
725#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
726
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000728#endif
729 // TODO(dcarney): make unlinkable before cutover
730 V8_INLINE(Persistent(const Persistent& that)) : val_(that.val_) {}
731 // TODO(dcarney): make unlinkable before cutover
732 V8_INLINE(Persistent& operator=(const Persistent& that)) { // NOLINT
733 this->val_ = that.val_;
734 return *this;
735 }
736
737 public:
738#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
739
740 private:
741#endif
742 // TODO(dcarney): remove before cutover
743 template <class S> V8_INLINE(Persistent(S* that)) : val_(that) { }
744 // TODO(dcarney): remove before cutover
745 template <class S> V8_INLINE(Persistent(Persistent<S> that))
746 : val_(*that) {
747 TYPE_CHECK(T, S);
748 }
749 // TODO(dcarney): remove before cutover
750 V8_INLINE(T* operator*() const) { return val_; }
751 public:
752#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW
753
754 private:
755#endif
756 // TODO(dcarney): remove before cutover
757 V8_INLINE(T* operator->() const) { return val_; }
758 public:
759#endif
760
761 private:
762 template<class F>
763 friend class Handle;
764 template<class F>
765 friend class Local;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766 friend class ImplementationUtilities;
767 friend class ObjectTemplate;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000768 friend class Context;
769 friend class InternalHandleHelper;
770 friend class LocalContext;
771
772 V8_INLINE(static Persistent<T> New(Isolate* isolate, T* that));
773
774#ifndef V8_USE_UNSAFE_HANDLES
775 T* val_;
776#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777};
778
779
v8.team.kasperl727e9952008-09-02 14:56:44 +0000780 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781 * A stack-allocated class that governs a number of local handles.
782 * After a handle scope has been created, all local handles will be
783 * allocated within that handle scope until either the handle scope is
784 * deleted or another handle scope is created. If there is already a
785 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000786 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787 * new handles will again be allocated in the original handle scope.
788 *
789 * After the handle scope of a local handle has been deleted the
790 * garbage collector will no longer track the object stored in the
791 * handle and may deallocate it. The behavior of accessing a handle
792 * for which the handle scope has been deleted is undefined.
793 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000794class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000796 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000797 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000799 HandleScope(Isolate* isolate);
800
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000801 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802
803 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804 * Closes the handle scope and returns the value as a handle in the
805 * previous scope, which is the new current scope after the call.
806 */
807 template <class T> Local<T> Close(Handle<T> value);
808
809 /**
810 * Counts the number of allocated handles.
811 */
812 static int NumberOfHandles();
813
814 /**
815 * Creates a new handle with the given value.
816 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000817 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000818 static internal::Object** CreateHandle(internal::Isolate* isolate,
819 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000820 // Faster version, uses HeapObject to obtain the current Isolate.
821 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822
823 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000824 // Make it hard to create heap-allocated or illegal handle scopes by
825 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 HandleScope(const HandleScope&);
827 void operator=(const HandleScope&);
828 void* operator new(size_t size);
829 void operator delete(void*, size_t);
830
ager@chromium.org3811b432009-10-28 14:53:37 +0000831 // This Data class is accessible internally as HandleScopeData through a
832 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000833 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000835 internal::Object** next;
836 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000837 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000838 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000839 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000840 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841 }
842 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000843
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000844 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000845 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000847 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000848 internal::Object** prev_next_;
849 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000851 // Allow for the active closing of HandleScopes which allows to pass a handle
852 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000854 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 friend class ImplementationUtilities;
857};
858
859
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000860// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861
862
863/**
864 * The superclass of values and API object templates.
865 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000866class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867 private:
868 Data();
869};
870
871
872/**
873 * Pre-compilation data that can be associated with a script. This
874 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000875 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000876 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000878class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879 public:
880 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000881
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000882 /**
883 * Pre-compiles the specified script (context-independent).
884 *
885 * \param input Pointer to UTF-8 script source code.
886 * \param length Length of UTF-8 script source code.
887 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000890 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000891 * Pre-compiles the specified script (context-independent).
892 *
893 * NOTE: Pre-compilation using this method cannot happen on another thread
894 * without using Lockers.
895 *
896 * \param source Script source code.
897 */
898 static ScriptData* PreCompile(Handle<String> source);
899
900 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000901 * Load previous pre-compilation data.
902 *
903 * \param data Pointer to data returned by a call to Data() of a previous
904 * ScriptData. Ownership is not transferred.
905 * \param length Length of data.
906 */
907 static ScriptData* New(const char* data, int length);
908
909 /**
910 * Returns the length of Data().
911 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000913
914 /**
915 * Returns a serialized representation of this ScriptData that can later be
916 * passed to New(). NOTE: Serialized data is platform-dependent.
917 */
918 virtual const char* Data() = 0;
919
920 /**
921 * Returns true if the source code could not be parsed.
922 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000923 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924};
925
926
927/**
928 * The origin, within a file, of a script.
929 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000930class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000932 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000933 Handle<Value> resource_name,
934 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000935 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 : resource_name_(resource_name),
937 resource_line_offset_(resource_line_offset),
938 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000939 V8_INLINE(Handle<Value> ResourceName() const);
940 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
941 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000943 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 Handle<Integer> resource_line_offset_;
945 Handle<Integer> resource_column_offset_;
946};
947
948
949/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000950 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000952class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000955 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000956 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000957 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000958 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000959 * when New() returns
960 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
961 * using pre_data speeds compilation if it's done multiple times.
962 * Owned by caller, no references are kept when New() returns.
963 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000964 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000965 * available to compile event handlers.
966 * \return Compiled script object (context independent; when run it
967 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000969 static Local<Script> New(Handle<String> source,
970 ScriptOrigin* origin = NULL,
971 ScriptData* pre_data = NULL,
972 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973
mads.s.agercbaa0602008-08-14 13:41:48 +0000974 /**
975 * Compiles the specified script using the specified file name
976 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000977 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000978 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000979 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000980 * as the script's origin.
981 * \return Compiled script object (context independent; when run it
982 * will use the currently entered context).
983 */
984 static Local<Script> New(Handle<String> source,
985 Handle<Value> file_name);
986
987 /**
988 * Compiles the specified script (bound to current context).
989 *
990 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000991 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000992 * when Compile() returns
993 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
994 * using pre_data speeds compilation if it's done multiple times.
995 * Owned by caller, no references are kept when Compile() returns.
996 * \param script_data Arbitrary data associated with script. Using
997 * this has same effect as calling SetData(), but makes data available
998 * earlier (i.e. to compile event handlers).
999 * \return Compiled script object, bound to the context that was active
1000 * when this function was called. When run it will always use this
1001 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +00001002 */
1003 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +00001004 ScriptOrigin* origin = NULL,
1005 ScriptData* pre_data = NULL,
1006 Handle<String> script_data = Handle<String>());
1007
1008 /**
1009 * Compiles the specified script using the specified file name
1010 * object (typically a string) as the script's origin.
1011 *
1012 * \param source Script source code.
1013 * \param file_name File name to use as script's origin
1014 * \param script_data Arbitrary data associated with script. Using
1015 * this has same effect as calling SetData(), but makes data available
1016 * earlier (i.e. to compile event handlers).
1017 * \return Compiled script object, bound to the context that was active
1018 * when this function was called. When run it will always use this
1019 * context.
1020 */
1021 static Local<Script> Compile(Handle<String> source,
1022 Handle<Value> file_name,
1023 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +00001024
v8.team.kasperl727e9952008-09-02 14:56:44 +00001025 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001026 * Runs the script returning the resulting value. If the script is
1027 * context independent (created using ::New) it will be run in the
1028 * currently entered context. If it is context specific (created
1029 * using ::Compile) it will be run in the context in which it was
1030 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001031 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001033
1034 /**
1035 * Returns the script id value.
1036 */
1037 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001038
1039 /**
1040 * Associate an additional data object with the script. This is mainly used
1041 * with the debugger as this data object is only available through the
1042 * debugger API.
1043 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00001044 void SetData(Handle<String> data);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001045
1046 /**
1047 * Returns the name value of one Script.
1048 */
1049 Handle<Value> GetScriptName();
1050
1051 /**
1052 * Returns zero based line number of the code_pos location in the script.
1053 * -1 will be returned if no information available.
1054 */
1055 int GetLineNumber(int code_pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056};
1057
1058
1059/**
1060 * An error message.
1061 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001062class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001064 Local<String> Get() const;
1065 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001067 /**
1068 * Returns the resource name for the script from where the function causing
1069 * the error originates.
1070 */
ager@chromium.org32912102009-01-16 10:38:43 +00001071 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001073 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001074 * Returns the resource data for the script from where the function causing
1075 * the error originates.
1076 */
1077 Handle<Value> GetScriptData() const;
1078
1079 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001080 * Exception stack trace. By default stack traces are not captured for
1081 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1082 * to change this option.
1083 */
1084 Handle<StackTrace> GetStackTrace() const;
1085
1086 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001087 * Returns the number, 1-based, of the line where the error occurred.
1088 */
ager@chromium.org32912102009-01-16 10:38:43 +00001089 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001091 /**
1092 * Returns the index within the script of the first character where
1093 * the error occurred.
1094 */
ager@chromium.org32912102009-01-16 10:38:43 +00001095 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001096
1097 /**
1098 * Returns the index within the script of the last character where
1099 * the error occurred.
1100 */
ager@chromium.org32912102009-01-16 10:38:43 +00001101 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001102
1103 /**
1104 * Returns the index within the line of the first character where
1105 * the error occurred.
1106 */
ager@chromium.org32912102009-01-16 10:38:43 +00001107 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001108
1109 /**
1110 * Returns the index within the line of the last character where
1111 * the error occurred.
1112 */
ager@chromium.org32912102009-01-16 10:38:43 +00001113 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001114
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 // TODO(1245381): Print to a string instead of on a FILE.
1116 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001117
1118 static const int kNoLineNumberInfo = 0;
1119 static const int kNoColumnInfo = 0;
1120};
1121
1122
1123/**
1124 * Representation of a JavaScript stack trace. The information collected is a
1125 * snapshot of the execution stack and the information remains valid after
1126 * execution continues.
1127 */
1128class V8EXPORT StackTrace {
1129 public:
1130 /**
1131 * Flags that determine what information is placed captured for each
1132 * StackFrame when grabbing the current stack trace.
1133 */
1134 enum StackTraceOptions {
1135 kLineNumber = 1,
1136 kColumnOffset = 1 << 1 | kLineNumber,
1137 kScriptName = 1 << 2,
1138 kFunctionName = 1 << 3,
1139 kIsEval = 1 << 4,
1140 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001141 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001142 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001143 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001144 };
1145
1146 /**
1147 * Returns a StackFrame at a particular index.
1148 */
1149 Local<StackFrame> GetFrame(uint32_t index) const;
1150
1151 /**
1152 * Returns the number of StackFrames.
1153 */
1154 int GetFrameCount() const;
1155
1156 /**
1157 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1158 */
1159 Local<Array> AsArray();
1160
1161 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001162 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001163 *
1164 * \param frame_limit The maximum number of stack frames we want to capture.
1165 * \param options Enumerates the set of things we will capture for each
1166 * StackFrame.
1167 */
1168 static Local<StackTrace> CurrentStackTrace(
1169 int frame_limit,
1170 StackTraceOptions options = kOverview);
1171};
1172
1173
1174/**
1175 * A single JavaScript stack frame.
1176 */
1177class V8EXPORT StackFrame {
1178 public:
1179 /**
1180 * Returns the number, 1-based, of the line for the associate function call.
1181 * This method will return Message::kNoLineNumberInfo if it is unable to
1182 * retrieve the line number, or if kLineNumber was not passed as an option
1183 * when capturing the StackTrace.
1184 */
1185 int GetLineNumber() const;
1186
1187 /**
1188 * Returns the 1-based column offset on the line for the associated function
1189 * call.
1190 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1191 * the column number, or if kColumnOffset was not passed as an option when
1192 * capturing the StackTrace.
1193 */
1194 int GetColumn() const;
1195
1196 /**
1197 * Returns the name of the resource that contains the script for the
1198 * function for this StackFrame.
1199 */
1200 Local<String> GetScriptName() const;
1201
1202 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001203 * Returns the name of the resource that contains the script for the
1204 * function for this StackFrame or sourceURL value if the script name
1205 * is undefined and its source ends with //@ sourceURL=... string.
1206 */
1207 Local<String> GetScriptNameOrSourceURL() const;
1208
1209 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001210 * Returns the name of the function associated with this stack frame.
1211 */
1212 Local<String> GetFunctionName() const;
1213
1214 /**
1215 * Returns whether or not the associated function is compiled via a call to
1216 * eval().
1217 */
1218 bool IsEval() const;
1219
1220 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001221 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001222 * constructor via "new".
1223 */
1224 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225};
1226
1227
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001228// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229
1230
1231/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001232 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001233 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001234class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 /**
1237 * Returns true if this value is the undefined value. See ECMA-262
1238 * 4.3.10.
1239 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001240 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241
1242 /**
1243 * Returns true if this value is the null value. See ECMA-262
1244 * 4.3.11.
1245 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001246 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247
1248 /**
1249 * Returns true if this value is true.
1250 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001251 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001252
1253 /**
1254 * Returns true if this value is false.
1255 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001256 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257
1258 /**
1259 * Returns true if this value is an instance of the String type.
1260 * See ECMA-262 8.4.
1261 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001262 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263
1264 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001265 * Returns true if this value is a symbol.
1266 * This is an experimental feature.
1267 */
1268 bool IsSymbol() const;
1269
1270 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 * Returns true if this value is a function.
1272 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001273 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274
1275 /**
1276 * Returns true if this value is an array.
1277 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001278 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279
v8.team.kasperl727e9952008-09-02 14:56:44 +00001280 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 * Returns true if this value is an object.
1282 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001283 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284
v8.team.kasperl727e9952008-09-02 14:56:44 +00001285 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 * Returns true if this value is boolean.
1287 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001288 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001289
v8.team.kasperl727e9952008-09-02 14:56:44 +00001290 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 * Returns true if this value is a number.
1292 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001293 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294
v8.team.kasperl727e9952008-09-02 14:56:44 +00001295 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 * Returns true if this value is external.
1297 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001298 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299
v8.team.kasperl727e9952008-09-02 14:56:44 +00001300 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 * Returns true if this value is a 32-bit signed integer.
1302 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001303 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001305 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001306 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001307 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001308 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001309
1310 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001311 * Returns true if this value is a Date.
1312 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001313 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001314
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001315 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001316 * Returns true if this value is a Boolean object.
1317 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001318 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001319
1320 /**
1321 * Returns true if this value is a Number object.
1322 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001323 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001324
1325 /**
1326 * Returns true if this value is a String object.
1327 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001328 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001329
1330 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001331 * Returns true if this value is a Symbol object.
1332 * This is an experimental feature.
1333 */
1334 bool IsSymbolObject() const;
1335
1336 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001337 * Returns true if this value is a NativeError.
1338 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001339 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001340
1341 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001342 * Returns true if this value is a RegExp.
1343 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001344 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001345
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001346
1347 /**
1348 * Returns true if this value is an ArrayBuffer.
1349 * This is an experimental feature.
1350 */
1351 bool IsArrayBuffer() const;
1352
1353 /**
1354 * Returns true if this value is one of TypedArrays.
1355 * This is an experimental feature.
1356 */
1357 bool IsTypedArray() const;
1358
1359 /**
1360 * Returns true if this value is an Uint8Array.
1361 * This is an experimental feature.
1362 */
1363 bool IsUint8Array() const;
1364
1365 /**
1366 * Returns true if this value is an Uint8ClampedArray.
1367 * This is an experimental feature.
1368 */
1369 bool IsUint8ClampedArray() const;
1370
1371 /**
1372 * Returns true if this value is an Int8Array.
1373 * This is an experimental feature.
1374 */
1375 bool IsInt8Array() const;
1376
1377 /**
1378 * Returns true if this value is an Uint16Array.
1379 * This is an experimental feature.
1380 */
1381 bool IsUint16Array() const;
1382
1383 /**
1384 * Returns true if this value is an Int16Array.
1385 * This is an experimental feature.
1386 */
1387 bool IsInt16Array() const;
1388
1389 /**
1390 * Returns true if this value is an Uint32Array.
1391 * This is an experimental feature.
1392 */
1393 bool IsUint32Array() const;
1394
1395 /**
1396 * Returns true if this value is an Int32Array.
1397 * This is an experimental feature.
1398 */
1399 bool IsInt32Array() const;
1400
1401 /**
1402 * Returns true if this value is a Float32Array.
1403 * This is an experimental feature.
1404 */
1405 bool IsFloat32Array() const;
1406
1407 /**
1408 * Returns true if this value is a Float64Array.
1409 * This is an experimental feature.
1410 */
1411 bool IsFloat64Array() const;
1412
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001413 Local<Boolean> ToBoolean() const;
1414 Local<Number> ToNumber() const;
1415 Local<String> ToString() const;
1416 Local<String> ToDetailString() const;
1417 Local<Object> ToObject() const;
1418 Local<Integer> ToInteger() const;
1419 Local<Uint32> ToUint32() const;
1420 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421
1422 /**
1423 * Attempts to convert a string to an array index.
1424 * Returns an empty handle if the conversion fails.
1425 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001426 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001428 bool BooleanValue() const;
1429 double NumberValue() const;
1430 int64_t IntegerValue() const;
1431 uint32_t Uint32Value() const;
1432 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433
1434 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001435 bool Equals(Handle<Value> that) const;
1436 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001437
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001438 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001439 V8_INLINE(bool QuickIsUndefined() const);
1440 V8_INLINE(bool QuickIsNull() const);
1441 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001442 bool FullIsUndefined() const;
1443 bool FullIsNull() const;
1444 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001445};
1446
1447
1448/**
1449 * The superclass of primitive values. See ECMA-262 4.3.2.
1450 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001451class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452
1453
1454/**
1455 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1456 * or false value.
1457 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001458class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001460 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001461 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462};
1463
1464
1465/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001466 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001468class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001470 enum Encoding {
1471 UNKNOWN_ENCODING = 0x1,
1472 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001473 ASCII_ENCODING = 0x4,
1474 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001475 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001476 /**
1477 * Returns the number of characters in this string.
1478 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001479 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480
v8.team.kasperl727e9952008-09-02 14:56:44 +00001481 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001482 * Returns the number of bytes in the UTF-8 encoded
1483 * representation of this string.
1484 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001485 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001486
1487 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001488 * This function is no longer useful.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001489 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001490 V8_DEPRECATED(V8_INLINE(bool MayContainNonAscii()) const) { return true; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001491
1492 /**
1493 * Returns whether this string contains only one byte data.
1494 */
1495 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001496
1497 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001498 * Write the contents of the string to an external buffer.
1499 * If no arguments are given, expects the buffer to be large
1500 * enough to hold the entire string and NULL terminator. Copies
1501 * the contents of the string and the NULL terminator into the
1502 * buffer.
1503 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001504 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1505 * before the end of the buffer.
1506 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001507 * Copies up to length characters into the output buffer.
1508 * Only null-terminates if there is enough space in the buffer.
1509 *
1510 * \param buffer The buffer into which the string will be copied.
1511 * \param start The starting position within the string at which
1512 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001513 * \param length The number of characters to copy from the string. For
1514 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001515 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001516 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001517 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001518 * \return The number of characters copied to the buffer excluding the null
1519 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001520 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001521 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001522 enum WriteOptions {
1523 NO_OPTIONS = 0,
1524 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001525 NO_NULL_TERMINATION = 2,
1526 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001527 };
1528
lrn@chromium.org34e60782011-09-15 07:25:40 +00001529 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001530 int Write(uint16_t* buffer,
1531 int start = 0,
1532 int length = -1,
1533 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001534 // ASCII characters.
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001535 V8_DEPRECATED(int WriteAscii(char* buffer,
1536 int start = 0,
1537 int length = -1,
1538 int options = NO_OPTIONS) const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001539 // One byte characters.
1540 int WriteOneByte(uint8_t* buffer,
1541 int start = 0,
1542 int length = -1,
1543 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001544 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001545 int WriteUtf8(char* buffer,
1546 int length = -1,
1547 int* nchars_ref = NULL,
1548 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549
v8.team.kasperl727e9952008-09-02 14:56:44 +00001550 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001551 * A zero length string.
1552 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001553 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001554 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001555
1556 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001557 * Returns true if the string is external
1558 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001559 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560
v8.team.kasperl727e9952008-09-02 14:56:44 +00001561 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001562 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001563 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001564 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001565
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001566 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001567 public:
1568 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001569
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001570 protected:
1571 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001572
1573 /**
1574 * Internally V8 will call this Dispose method when the external string
1575 * resource is no longer needed. The default implementation will use the
1576 * delete operator. This method can be overridden in subclasses to
1577 * control how allocated external string resources are disposed.
1578 */
1579 virtual void Dispose() { delete this; }
1580
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001581 private:
1582 // Disallow copying and assigning.
1583 ExternalStringResourceBase(const ExternalStringResourceBase&);
1584 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001585
1586 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001587 };
1588
v8.team.kasperl727e9952008-09-02 14:56:44 +00001589 /**
1590 * An ExternalStringResource is a wrapper around a two-byte string
1591 * buffer that resides outside V8's heap. Implement an
1592 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001593 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001594 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001595 class V8EXPORT ExternalStringResource
1596 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597 public:
1598 /**
1599 * Override the destructor to manage the life cycle of the underlying
1600 * buffer.
1601 */
1602 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001603
1604 /**
1605 * The string data from the underlying buffer.
1606 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001608
1609 /**
1610 * The length of the string. That is, the number of two-byte characters.
1611 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001613
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 protected:
1615 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 };
1617
1618 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001619 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001620 * string buffer that resides outside V8's heap. Implement an
1621 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001622 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001623 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001624 * UTF-8, which would require special treatment internally in the
1625 * engine and, in the case of UTF-8, do not allow efficient indexing.
1626 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001627 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001629 class V8EXPORT ExternalAsciiStringResource
1630 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631 public:
1632 /**
1633 * Override the destructor to manage the life cycle of the underlying
1634 * buffer.
1635 */
1636 virtual ~ExternalAsciiStringResource() {}
1637 /** The string data from the underlying buffer.*/
1638 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001639 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 virtual size_t length() const = 0;
1641 protected:
1642 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 };
1644
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001645 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1646
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001647 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001648 * If the string is an external string, return the ExternalStringResourceBase
1649 * regardless of the encoding, otherwise return NULL. The encoding of the
1650 * string is returned in encoding_out.
1651 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001652 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1653 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001654
1655 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001656 * Get the ExternalStringResource for an external string. Returns
1657 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001659 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660
1661 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001662 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001663 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001665 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001667 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001669 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001671 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001672 * The second parameter 'length' gives the buffer length. If omitted,
1673 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001674 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001675 V8_INLINE(static Local<String> New(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001677 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001678 /** Allocates a new string from 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001679 V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001681 // TODO(dcarney): deprecate
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001682 /**
1683 * Creates an internalized string (historically called a "symbol",
1684 * not to be confused with ES6 symbols). Returns one if it exists already.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001685 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001686 V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
1687
1688 enum NewStringType {
1689 kNormalString, kInternalizedString, kUndetectableString
1690 };
1691
1692 /** Allocates a new string from UTF-8 data.*/
1693 static Local<String> NewFromUtf8(Isolate* isolate,
1694 const char* data,
1695 NewStringType type = kNormalString,
1696 int length = -1);
1697
1698 /** Allocates a new string from Latin-1 data.*/
1699 static Local<String> NewFromOneByte(
1700 Isolate* isolate,
1701 const uint8_t* data,
1702 NewStringType type = kNormalString,
1703 int length = -1);
1704
1705 /** Allocates a new string from UTF-16 data.*/
1706 static Local<String> NewFromTwoByte(
1707 Isolate* isolate,
1708 const uint16_t* data,
1709 NewStringType type = kNormalString,
1710 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711
v8.team.kasperl727e9952008-09-02 14:56:44 +00001712 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001713 * Creates a new string by concatenating the left and the right strings
1714 * passed in as parameters.
1715 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001716 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001717
1718 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001719 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001720 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001721 * resource will be disposed by calling its Dispose method. The caller of
1722 * this function should not otherwise delete or modify the resource. Neither
1723 * should the underlying buffer be deallocated or modified except through the
1724 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001725 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001726 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001727
ager@chromium.org6f10e412009-02-13 10:11:16 +00001728 /**
1729 * Associate an external string resource with this string by transforming it
1730 * in place so that existing references to this string in the JavaScript heap
1731 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001732 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001733 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001734 * The string is not modified if the operation fails. See NewExternal for
1735 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001736 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001737 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738
v8.team.kasperl727e9952008-09-02 14:56:44 +00001739 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001740 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001741 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001742 * resource will be disposed by calling its Dispose method. The caller of
1743 * this function should not otherwise delete or modify the resource. Neither
1744 * should the underlying buffer be deallocated or modified except through the
1745 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001746 */
1747 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001748
ager@chromium.org6f10e412009-02-13 10:11:16 +00001749 /**
1750 * Associate an external string resource with this string by transforming it
1751 * in place so that existing references to this string in the JavaScript heap
1752 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001753 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001754 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001755 * The string is not modified if the operation fails. See NewExternal for
1756 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001757 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001758 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001760 /**
1761 * Returns true if this string can be made external.
1762 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001763 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001764
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001765 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001766 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001767 V8_INLINE(
1768 static Local<String> NewUndetectable(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001770 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001771 /** Creates an undetectable string from the supplied 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001772 V8_INLINE(static Local<String> NewUndetectable(
1773 const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774
1775 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001776 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001777 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001778 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001779 * then the length() method returns 0 and the * operator returns
1780 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001781 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001782 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001783 public:
1784 explicit Utf8Value(Handle<v8::Value> obj);
1785 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001786 char* operator*() { return str_; }
1787 const char* operator*() const { return str_; }
1788 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001789 private:
1790 char* str_;
1791 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001792
1793 // Disallow copying and assigning.
1794 Utf8Value(const Utf8Value&);
1795 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001796 };
1797
1798 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001799 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001801 * If conversion to a string fails (eg. due to an exception in the toString()
1802 * method of the object) then the length() method returns 0 and the * operator
1803 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001805 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 public:
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001807 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 explicit AsciiValue(Handle<v8::Value> obj);
1809 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001810 char* operator*() { return str_; }
1811 const char* operator*() const { return str_; }
1812 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 private:
1814 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001815 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001816
1817 // Disallow copying and assigning.
1818 AsciiValue(const AsciiValue&);
1819 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 };
1821
1822 /**
1823 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001824 * If conversion to a string fails (eg. due to an exception in the toString()
1825 * method of the object) then the length() method returns 0 and the * operator
1826 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001828 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 public:
1830 explicit Value(Handle<v8::Value> obj);
1831 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001832 uint16_t* operator*() { return str_; }
1833 const uint16_t* operator*() const { return str_; }
1834 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 private:
1836 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001837 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001838
1839 // Disallow copying and assigning.
1840 Value(const Value&);
1841 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001843
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001844 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001845 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1846 Encoding encoding) const;
1847 void VerifyExternalStringResource(ExternalStringResource* val) const;
1848 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849};
1850
1851
1852/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001853 * A JavaScript symbol (ECMA-262 edition 6)
1854 *
1855 * This is an experimental feature. Use at your own risk.
1856 */
1857class V8EXPORT Symbol : public Primitive {
1858 public:
1859 // Returns the print name string of the symbol, or undefined if none.
1860 Local<Value> Name() const;
1861
1862 // Create a symbol without a print name.
1863 static Local<Symbol> New(Isolate* isolate);
1864
1865 // Create a symbol with a print name.
1866 static Local<Symbol> New(Isolate *isolate, const char* data, int length = -1);
1867
1868 V8_INLINE(static Symbol* Cast(v8::Value* obj));
1869 private:
1870 Symbol();
1871 static void CheckCast(v8::Value* obj);
1872};
1873
1874
1875/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001876 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001878class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001880 double Value() const;
1881 static Local<Number> New(double value);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001882 static Local<Number> New(Isolate* isolate, double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001883 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001885 Number();
1886 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887};
1888
1889
1890/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001891 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001893class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001895 static Local<Integer> New(int32_t value);
1896 static Local<Integer> NewFromUnsigned(uint32_t value);
1897 static Local<Integer> New(int32_t value, Isolate*);
1898 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1899 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001900 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001901 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001902 Integer();
1903 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904};
1905
1906
1907/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001908 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001910class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001912 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001914 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915};
1916
1917
1918/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001919 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001921class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001923 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001925 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926};
1927
1928
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929enum PropertyAttribute {
1930 None = 0,
1931 ReadOnly = 1 << 0,
1932 DontEnum = 1 << 1,
1933 DontDelete = 1 << 2
1934};
1935
ager@chromium.org3811b432009-10-28 14:53:37 +00001936enum ExternalArrayType {
1937 kExternalByteArray = 1,
1938 kExternalUnsignedByteArray,
1939 kExternalShortArray,
1940 kExternalUnsignedShortArray,
1941 kExternalIntArray,
1942 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001943 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001944 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001945 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001946};
1947
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001949 * Accessor[Getter|Setter] are used as callback functions when
1950 * setting|getting a particular property. See Object and ObjectTemplate's
1951 * method SetAccessor.
1952 */
1953typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1954 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001955typedef void (*AccessorGetterCallback)(
1956 Local<String> property,
1957 const PropertyCallbackInfo<Value>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001958
1959
1960typedef void (*AccessorSetter)(Local<String> property,
1961 Local<Value> value,
1962 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001963typedef void (*AccessorSetterCallback)(
1964 Local<String> property,
1965 Local<Value> value,
1966 const PropertyCallbackInfo<void>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001967
1968
1969/**
1970 * Access control specifications.
1971 *
1972 * Some accessors should be accessible across contexts. These
1973 * accessors have an explicit access control parameter which specifies
1974 * the kind of cross-context access that should be allowed.
1975 *
1976 * Additionally, for security, accessors can prohibit overwriting by
1977 * accessors defined in JavaScript. For objects that have such
1978 * accessors either locally or in their prototype chain it is not
1979 * possible to overwrite the accessor by using __defineGetter__ or
1980 * __defineSetter__ from JavaScript code.
1981 */
1982enum AccessControl {
1983 DEFAULT = 0,
1984 ALL_CAN_READ = 1,
1985 ALL_CAN_WRITE = 1 << 1,
1986 PROHIBITS_OVERWRITING = 1 << 2
1987};
1988
1989
1990/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001991 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001993class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001995 bool Set(Handle<Value> key,
1996 Handle<Value> value,
1997 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001998
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001999 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002000
ager@chromium.orge2902be2009-06-08 12:21:35 +00002001 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002002 // overriding accessors or read-only properties.
2003 //
2004 // Note that if the object has an interceptor the property will be set
2005 // locally, but since the interceptor takes precedence the local property
2006 // will only be returned if the interceptor doesn't return a value.
2007 //
2008 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002009 bool ForceSet(Handle<Value> key,
2010 Handle<Value> value,
2011 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002012
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002013 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002015 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002016
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002017 /**
2018 * Gets the property attributes of a property which can be None or
2019 * any combination of ReadOnly, DontEnum and DontDelete. Returns
2020 * None when the property doesn't exist.
2021 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002022 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002023
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002024 bool Has(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002025
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002026 bool Delete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002027
2028 // Delete a property on this object bypassing interceptors and
2029 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002030 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002031
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002032 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002033
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002034 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002036 // TODO(dcarney): deprecate
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002037 bool SetAccessor(Handle<String> name,
2038 AccessorGetter getter,
2039 AccessorSetter setter = 0,
2040 Handle<Value> data = Handle<Value>(),
2041 AccessControl settings = DEFAULT,
2042 PropertyAttribute attribute = None);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002043 bool SetAccessor(Handle<String> name,
2044 AccessorGetterCallback getter,
2045 AccessorSetterCallback setter = 0,
2046 Handle<Value> data = Handle<Value>(),
2047 AccessControl settings = DEFAULT,
2048 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002049
ulan@chromium.org750145a2013-03-07 15:14:13 +00002050 // This function is not yet stable and should not be used at this time.
2051 bool SetAccessor(Handle<String> name,
2052 Handle<DeclaredAccessorDescriptor> descriptor,
2053 AccessControl settings = DEFAULT,
2054 PropertyAttribute attribute = None);
2055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002057 * Returns an array containing the names of the enumerable properties
2058 * of this object, including properties from prototype objects. The
2059 * array returned by this method contains the same values as would
2060 * be enumerated by a for-in statement over this object.
2061 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002062 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002063
2064 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002065 * This function has the same functionality as GetPropertyNames but
2066 * the returned array doesn't contain the names of properties from
2067 * prototype objects.
2068 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002069 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002070
2071 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 * Get the prototype object. This does not skip objects marked to
2073 * be skipped by __proto__ and it does not consult the security
2074 * handler.
2075 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002076 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077
2078 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002079 * Set the prototype object. This does not skip objects marked to
2080 * be skipped by __proto__ and it does not consult the security
2081 * handler.
2082 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002083 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00002084
2085 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002086 * Finds an instance of the given function template in the prototype
2087 * chain.
2088 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002089 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002090
2091 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 * Call builtin Object.prototype.toString on this object.
2093 * This is different from Value::ToString() that may call
2094 * user-defined toString function. This one does not.
2095 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002096 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002098 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002099 * Returns the function invoked as a constructor for this object.
2100 * May be the null value.
2101 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002102 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002103
2104 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002105 * Returns the name of the function invoked as a constructor for this object.
2106 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002107 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002108
kasper.lund212ac232008-07-16 07:07:30 +00002109 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002110 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002111
2112 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002113 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002114
kasper.lund212ac232008-07-16 07:07:30 +00002115 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002116 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002117
2118 /**
2119 * Gets a 2-byte-aligned native pointer from an internal field. This field
2120 * must have been set by SetAlignedPointerInInternalField, everything else
2121 * leads to undefined behavior.
2122 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002123 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002124
2125 /**
2126 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2127 * a field, GetAlignedPointerFromInternalField must be used, everything else
2128 * leads to undefined behavior.
2129 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002130 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002131
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002133 bool HasOwnProperty(Handle<String> key);
2134 bool HasRealNamedProperty(Handle<String> key);
2135 bool HasRealIndexedProperty(uint32_t index);
2136 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137
2138 /**
2139 * If result.IsEmpty() no real property was located in the prototype chain.
2140 * This means interceptors in the prototype chain are not called.
2141 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002142 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00002143
2144 /**
2145 * If result.IsEmpty() no real property was located on the object or
2146 * in the prototype chain.
2147 * This means interceptors in the prototype chain are not called.
2148 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002149 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150
2151 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002152 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153
kasper.lund212ac232008-07-16 07:07:30 +00002154 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002155 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002157 /**
2158 * Turns on access check on the object if the object is an instance of
2159 * a template that has access check callbacks. If an object has no
2160 * access check info, the object cannot be accessed by anyone.
2161 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002162 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00002163
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002164 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002165 * Returns the identity hash for this object. The current implementation
2166 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002167 *
ager@chromium.org9085a012009-05-11 19:22:57 +00002168 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00002169 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002170 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002171 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00002172
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002173 /**
2174 * Access hidden properties on JavaScript objects. These properties are
2175 * hidden from the executing JavaScript and only accessible through the V8
2176 * C++ API. Hidden properties introduced by V8 internally (for example the
2177 * identity hash) are prefixed with "v8::".
2178 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002179 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
2180 Local<Value> GetHiddenValue(Handle<String> key);
2181 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002182
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002183 /**
2184 * Returns true if this is an instance of an api function (one
2185 * created from a function created from a function template) and has
2186 * been modified since it was created. Note that this method is
2187 * conservative and may return true for objects that haven't actually
2188 * been modified.
2189 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002190 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002191
2192 /**
2193 * Clone this object with a fast but shallow copy. Values will point
2194 * to the same values as the original object.
2195 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002196 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002198 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002199 * Returns the context in which the object was created.
2200 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002201 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002202
2203 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002204 * Set the backing store of the indexed properties to be managed by the
2205 * embedding layer. Access to the indexed properties will follow the rules
2206 * spelled out in CanvasPixelArray.
2207 * Note: The embedding program still owns the data and needs to ensure that
2208 * the backing store is preserved while V8 has a reference.
2209 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002210 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
2211 bool HasIndexedPropertiesInPixelData();
2212 uint8_t* GetIndexedPropertiesPixelData();
2213 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002214
ager@chromium.org3811b432009-10-28 14:53:37 +00002215 /**
2216 * Set the backing store of the indexed properties to be managed by the
2217 * embedding layer. Access to the indexed properties will follow the rules
2218 * spelled out for the CanvasArray subtypes in the WebGL specification.
2219 * Note: The embedding program still owns the data and needs to ensure that
2220 * the backing store is preserved while V8 has a reference.
2221 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002222 void SetIndexedPropertiesToExternalArrayData(void* data,
2223 ExternalArrayType array_type,
2224 int number_of_elements);
2225 bool HasIndexedPropertiesInExternalArrayData();
2226 void* GetIndexedPropertiesExternalArrayData();
2227 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
2228 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00002229
lrn@chromium.org1c092762011-05-09 09:42:16 +00002230 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002231 * Checks whether a callback is set by the
2232 * ObjectTemplate::SetCallAsFunctionHandler method.
2233 * When an Object is callable this method returns true.
2234 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002235 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002236
2237 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002238 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002239 * ObjectTemplate::SetCallAsFunctionHandler method.
2240 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002241 Local<Value> CallAsFunction(Handle<Object> recv,
2242 int argc,
2243 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002244
2245 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002246 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002247 * ObjectTemplate::SetCallAsFunctionHandler method.
2248 * Note: This method behaves like the Function::NewInstance method.
2249 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002250 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002251
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002252 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002253 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002256 Object();
2257 static void CheckCast(Value* obj);
2258 Local<Value> SlowGetInternalField(int index);
2259 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260};
2261
2262
2263/**
2264 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
2265 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002266class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002268 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269
ager@chromium.org3e875802009-06-29 08:26:34 +00002270 /**
2271 * Clones an element at index |index|. Returns an empty
2272 * handle if cloning fails (for any reason).
2273 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002274 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00002275
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002276 /**
2277 * Creates a JavaScript array with the given length. If the length
2278 * is negative the returned array will have length 0.
2279 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002280 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002281
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002282 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002284 Array();
2285 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286};
2287
2288
2289/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002290 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002292class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002294 Local<Object> NewInstance() const;
2295 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
2296 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
2297 void SetName(Handle<String> name);
2298 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00002299
2300 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002301 * Name inferred from variable or property assignment of this function.
2302 * Used to facilitate debugging and profiling of JavaScript code written
2303 * in an OO style, where many functions are anonymous but are assigned
2304 * to object properties.
2305 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002306 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002307
2308 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002309 * Returns zero based line number of function body and
2310 * kLineOffsetNotFound if no information available.
2311 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002312 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002313 /**
2314 * Returns zero based column number of function body and
2315 * kLineOffsetNotFound if no information available.
2316 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002317 int GetScriptColumnNumber() const;
2318 Handle<Value> GetScriptId() const;
2319 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002320 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002321 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002322
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002324 Function();
2325 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326};
2327
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002328/**
2329 * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
2330 * populates an instance of this class with a pointer to data and byte length.
2331 *
2332 * |ArrayBufferContents| is the owner of its data. When an instance of
2333 * this class is destructed, the |Data| is freed.
2334 *
2335 * This API is experimental and may change significantly.
2336 */
2337class V8EXPORT ArrayBufferContents {
2338 public:
2339 ArrayBufferContents() : data_(NULL), byte_length_(0) {}
2340 ~ArrayBufferContents();
2341
2342 void* Data() const { return data_; }
2343 size_t ByteLength() const { return byte_length_; }
2344
2345 private:
2346 void* data_;
2347 size_t byte_length_;
2348
2349 friend class ArrayBuffer;
2350};
2351
2352#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
2353#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
2354#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355
2356/**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002357 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
2358 * This API is experimental and may change significantly.
2359 */
2360class V8EXPORT ArrayBuffer : public Object {
2361 public:
2362 /**
2363 * Data length in bytes.
2364 */
2365 size_t ByteLength() const;
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002366
2367 /**
2368 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
2369 * Allocated memory will be owned by a created ArrayBuffer and
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002370 * will be deallocated when it is garbage-collected,
2371 * unless the object is externalized.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002372 */
2373 static Local<ArrayBuffer> New(size_t byte_length);
2374
2375 /**
2376 * Create a new ArrayBuffer over an existing memory block.
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002377 * The created array buffer is immediately in externalized state.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002378 * The memory block will not be reclaimed when a created ArrayBuffer
2379 * is garbage-collected.
2380 */
2381 static Local<ArrayBuffer> New(void* data, size_t byte_length);
2382
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002383 /**
2384 * Returns true if ArrayBuffer is extrenalized, that is, does not
2385 * own its memory block.
2386 */
2387 bool IsExternal() const;
2388
2389 /**
2390 * Pass the ownership of this ArrayBuffer's backing store to
2391 * a given ArrayBufferContents.
2392 */
2393 void Externalize(ArrayBufferContents* contents);
2394
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002395 V8_INLINE(static ArrayBuffer* Cast(Value* obj));
2396
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002397
2398 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
2399
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002400 private:
2401 ArrayBuffer();
2402 static void CheckCast(Value* obj);
2403};
2404
2405
2406/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002407 * A base class for an instance of TypedArray series of constructors
2408 * (ES6 draft 15.13.6).
2409 * This API is experimental and may change significantly.
2410 */
2411class V8EXPORT TypedArray : public Object {
2412 public:
2413 /**
2414 * Returns underlying ArrayBuffer.
2415 */
2416 Local<ArrayBuffer> Buffer();
2417 /**
2418 * Byte offset in |Buffer|
2419 */
2420 size_t ByteOffset();
2421 /**
2422 * Numbe of elements in this typed array.
2423 */
2424 size_t Length();
2425 /**
2426 * Size of typed array in bytes (e.g. for Int16Array, 2*|Length|).
2427 */
2428 size_t ByteLength();
2429 /**
2430 * Base address of typed array.
2431 */
2432 void* BaseAddress();
2433
2434 V8_INLINE(static TypedArray* Cast(Value* obj));
2435
2436 private:
2437 TypedArray();
2438 static void CheckCast(Value* obj);
2439};
2440
2441
2442/**
2443 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
2444 * This API is experimental and may change significantly.
2445 */
2446class V8EXPORT Uint8Array : public TypedArray {
2447 public:
2448 static Local<Uint8Array> New(Handle<ArrayBuffer> array_buffer,
2449 size_t byte_offset, size_t length);
2450 V8_INLINE(static Uint8Array* Cast(Value* obj));
2451
2452 private:
2453 Uint8Array();
2454 static void CheckCast(Value* obj);
2455};
2456
2457
2458/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002459 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
2460 * This API is experimental and may change significantly.
2461 */
2462class V8EXPORT Uint8ClampedArray : public TypedArray {
2463 public:
2464 static Local<Uint8ClampedArray> New(Handle<ArrayBuffer> array_buffer,
2465 size_t byte_offset, size_t length);
2466 V8_INLINE(static Uint8ClampedArray* Cast(Value* obj));
2467
2468 private:
2469 Uint8ClampedArray();
2470 static void CheckCast(Value* obj);
2471};
2472
2473/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002474 * An instance of Int8Array constructor (ES6 draft 15.13.6).
2475 * This API is experimental and may change significantly.
2476 */
2477class V8EXPORT Int8Array : public TypedArray {
2478 public:
2479 static Local<Int8Array> New(Handle<ArrayBuffer> array_buffer,
2480 size_t byte_offset, size_t length);
2481 V8_INLINE(static Int8Array* Cast(Value* obj));
2482
2483 private:
2484 Int8Array();
2485 static void CheckCast(Value* obj);
2486};
2487
2488
2489/**
2490 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
2491 * This API is experimental and may change significantly.
2492 */
2493class V8EXPORT Uint16Array : public TypedArray {
2494 public:
2495 static Local<Uint16Array> New(Handle<ArrayBuffer> array_buffer,
2496 size_t byte_offset, size_t length);
2497 V8_INLINE(static Uint16Array* Cast(Value* obj));
2498
2499 private:
2500 Uint16Array();
2501 static void CheckCast(Value* obj);
2502};
2503
2504
2505/**
2506 * An instance of Int16Array constructor (ES6 draft 15.13.6).
2507 * This API is experimental and may change significantly.
2508 */
2509class V8EXPORT Int16Array : public TypedArray {
2510 public:
2511 static Local<Int16Array> New(Handle<ArrayBuffer> array_buffer,
2512 size_t byte_offset, size_t length);
2513 V8_INLINE(static Int16Array* Cast(Value* obj));
2514
2515 private:
2516 Int16Array();
2517 static void CheckCast(Value* obj);
2518};
2519
2520
2521/**
2522 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
2523 * This API is experimental and may change significantly.
2524 */
2525class V8EXPORT Uint32Array : public TypedArray {
2526 public:
2527 static Local<Uint32Array> New(Handle<ArrayBuffer> array_buffer,
2528 size_t byte_offset, size_t length);
2529 V8_INLINE(static Uint32Array* Cast(Value* obj));
2530
2531 private:
2532 Uint32Array();
2533 static void CheckCast(Value* obj);
2534};
2535
2536
2537/**
2538 * An instance of Int32Array constructor (ES6 draft 15.13.6).
2539 * This API is experimental and may change significantly.
2540 */
2541class V8EXPORT Int32Array : public TypedArray {
2542 public:
2543 static Local<Int32Array> New(Handle<ArrayBuffer> array_buffer,
2544 size_t byte_offset, size_t length);
2545 V8_INLINE(static Int32Array* Cast(Value* obj));
2546
2547 private:
2548 Int32Array();
2549 static void CheckCast(Value* obj);
2550};
2551
2552
2553/**
2554 * An instance of Float32Array constructor (ES6 draft 15.13.6).
2555 * This API is experimental and may change significantly.
2556 */
2557class V8EXPORT Float32Array : public TypedArray {
2558 public:
2559 static Local<Float32Array> New(Handle<ArrayBuffer> array_buffer,
2560 size_t byte_offset, size_t length);
2561 V8_INLINE(static Float32Array* Cast(Value* obj));
2562
2563 private:
2564 Float32Array();
2565 static void CheckCast(Value* obj);
2566};
2567
2568
2569/**
2570 * An instance of Float64Array constructor (ES6 draft 15.13.6).
2571 * This API is experimental and may change significantly.
2572 */
2573class V8EXPORT Float64Array : public TypedArray {
2574 public:
2575 static Local<Float64Array> New(Handle<ArrayBuffer> array_buffer,
2576 size_t byte_offset, size_t length);
2577 V8_INLINE(static Float64Array* Cast(Value* obj));
2578
2579 private:
2580 Float64Array();
2581 static void CheckCast(Value* obj);
2582};
2583
2584
2585/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002586 * An instance of the built-in Date constructor (ECMA-262, 15.9).
2587 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002588class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002589 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002590 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002591
2592 /**
2593 * A specialization of Value::NumberValue that is more efficient
2594 * because we know the structure of this object.
2595 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002596 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002597
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002598 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002599
2600 /**
2601 * Notification that the embedder has changed the time zone,
2602 * daylight savings time, or other date / time configuration
2603 * parameters. V8 keeps a cache of various values used for
2604 * date / time computation. This notification will reset
2605 * those cached values for the current context so that date /
2606 * time configuration changes would be reflected in the Date
2607 * object.
2608 *
2609 * This API should not be called more than needed as it will
2610 * negatively impact the performance of date operations.
2611 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002612 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002613
2614 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002615 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002616};
2617
2618
2619/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002620 * A Number object (ECMA-262, 4.3.21).
2621 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002622class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002623 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002624 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002625
2626 /**
2627 * Returns the Number held by the object.
2628 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002629 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002630
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002631 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002632
2633 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002634 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002635};
2636
2637
2638/**
2639 * A Boolean object (ECMA-262, 4.3.15).
2640 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002641class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002642 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002643 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002644
2645 /**
2646 * Returns the Boolean held by the object.
2647 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002648 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002649
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002650 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002651
2652 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002653 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002654};
2655
2656
2657/**
2658 * A String object (ECMA-262, 4.3.18).
2659 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002660class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002661 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002662 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002663
2664 /**
2665 * Returns the String held by the object.
2666 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002667 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002668
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002669 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002670
2671 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002672 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002673};
2674
2675
2676/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002677 * A Symbol object (ECMA-262 edition 6).
2678 *
2679 * This is an experimental feature. Use at your own risk.
2680 */
2681class V8EXPORT SymbolObject : public Object {
2682 public:
2683 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2684
2685 /**
2686 * Returns the Symbol held by the object.
2687 */
2688 Local<Symbol> SymbolValue() const;
2689
2690 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2691
2692 private:
2693 static void CheckCast(v8::Value* obj);
2694};
2695
2696
2697/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002698 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2699 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002700class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002701 public:
2702 /**
2703 * Regular expression flag bits. They can be or'ed to enable a set
2704 * of flags.
2705 */
2706 enum Flags {
2707 kNone = 0,
2708 kGlobal = 1,
2709 kIgnoreCase = 2,
2710 kMultiline = 4
2711 };
2712
2713 /**
2714 * Creates a regular expression from the given pattern string and
2715 * the flags bit field. May throw a JavaScript exception as
2716 * described in ECMA-262, 15.10.4.1.
2717 *
2718 * For example,
2719 * RegExp::New(v8::String::New("foo"),
2720 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2721 * is equivalent to evaluating "/foo/gm".
2722 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002723 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002724
2725 /**
2726 * Returns the value of the source property: a string representing
2727 * the regular expression.
2728 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002729 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002730
2731 /**
2732 * Returns the flags bit field.
2733 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002734 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002735
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002736 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002737
2738 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002739 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002740};
2741
2742
2743/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002744 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2745 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002747class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002749 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002750 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002751 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002753 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002754};
2755
2756
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002757// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758
2759
2760/**
2761 * The superclass of object and function templates.
2762 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002763class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764 public:
2765 /** Adds a property to each instance created by this template.*/
2766 void Set(Handle<String> name, Handle<Data> value,
2767 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002768 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002769 private:
2770 Template();
2771
2772 friend class ObjectTemplate;
2773 friend class FunctionTemplate;
2774};
2775
2776
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002777template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002778class ReturnValue {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002779 public:
2780 V8_INLINE(explicit ReturnValue(internal::Object** slot));
2781 // Handle setters
2782 V8_INLINE(void Set(const Persistent<T>& handle));
2783 V8_INLINE(void Set(const Handle<T> handle));
2784 // Fast primitive setters
2785 V8_INLINE(void Set(Isolate* isolate, bool value));
2786 V8_INLINE(void Set(Isolate* isolate, double i));
2787 V8_INLINE(void Set(Isolate* isolate, int32_t i));
2788 V8_INLINE(void Set(Isolate* isolate, uint32_t i));
2789 // Fast JS primitive setters
2790 V8_INLINE(void SetNull(Isolate* isolate));
2791 V8_INLINE(void SetUndefined(Isolate* isolate));
2792 private:
2793 V8_INLINE(void SetTrue(Isolate* isolate));
2794 V8_INLINE(void SetFalse(Isolate* isolate));
2795 internal::Object** value_;
2796};
2797
2798
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799/**
2800 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002801 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802 * including the receiver, the number and values of arguments, and
2803 * the holder of the function.
2804 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002805template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002806class FunctionCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002807 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002808 V8_INLINE(int Length() const);
2809 V8_INLINE(Local<Value> operator[](int i) const);
2810 V8_INLINE(Local<Function> Callee() const);
2811 V8_INLINE(Local<Object> This() const);
2812 V8_INLINE(Local<Object> Holder() const);
2813 V8_INLINE(bool IsConstructCall() const);
2814 V8_INLINE(Local<Value> Data() const);
2815 V8_INLINE(Isolate* GetIsolate() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002816 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2817 // This shouldn't be public, but the arm compiler needs it.
2818 static const int kArgsLength = 5;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002819
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002820 protected:
2821 friend class internal::FunctionCallbackArguments;
2822 friend class internal::CustomArguments<FunctionCallbackInfo>;
2823 static const int kReturnValueIndex = 0;
2824 static const int kIsolateIndex = -1;
2825 static const int kDataIndex = -2;
2826 static const int kCalleeIndex = -3;
2827 static const int kHolderIndex = -4;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002828
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002829 V8_INLINE(FunctionCallbackInfo(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002830 internal::Object** values,
2831 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002832 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002833 internal::Object** implicit_args_;
2834 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002836 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837};
2838
2839
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002840class V8EXPORT Arguments : public FunctionCallbackInfo<Value> {
2841 private:
2842 friend class internal::FunctionCallbackArguments;
2843 V8_INLINE(Arguments(internal::Object** implicit_args,
2844 internal::Object** values,
2845 int length,
2846 bool is_construct_call));
2847};
2848
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002849/**
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002850 * The information passed to a property callback about the context
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851 * of the property access.
2852 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002853template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002854class PropertyCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002855 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002856 V8_INLINE(Isolate* GetIsolate() const);
2857 V8_INLINE(Local<Value> Data() const);
2858 V8_INLINE(Local<Object> This() const);
2859 V8_INLINE(Local<Object> Holder() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002860 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2861 // This shouldn't be public, but the arm compiler needs it.
2862 static const int kArgsLength = 5;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002863
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002864 protected:
2865 friend class MacroAssembler;
2866 friend class internal::PropertyCallbackArguments;
2867 friend class internal::CustomArguments<PropertyCallbackInfo>;
2868 static const int kThisIndex = 0;
2869 static const int kHolderIndex = -1;
2870 static const int kDataIndex = -2;
2871 static const int kIsolateIndex = -3;
2872 static const int kReturnValueIndex = -4;
2873
2874 V8_INLINE(PropertyCallbackInfo(internal::Object** args))
2875 : args_(args) { }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002876 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877};
2878
2879
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002880class V8EXPORT AccessorInfo : public PropertyCallbackInfo<Value> {
2881 private:
2882 friend class internal::PropertyCallbackArguments;
2883 V8_INLINE(AccessorInfo(internal::Object** args))
2884 : PropertyCallbackInfo<Value>(args) { }
2885};
2886
2887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002889typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892 * NamedProperty[Getter|Setter] are used as interceptors on object.
2893 * See ObjectTemplate::SetNamedPropertyHandler.
2894 */
2895typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2896 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002897typedef void (*NamedPropertyGetterCallback)(
2898 Local<String> property,
2899 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900
2901
2902/**
2903 * Returns the value if the setter intercepts the request.
2904 * Otherwise, returns an empty handle.
2905 */
2906typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2907 Local<Value> value,
2908 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002909typedef void (*NamedPropertySetterCallback)(
2910 Local<String> property,
2911 Local<Value> value,
2912 const PropertyCallbackInfo<Value>& info);
2913
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002914
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915/**
2916 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002917 * The result is an integer encoding property attributes (like v8::None,
2918 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002919 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002920typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2921 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002922typedef void (*NamedPropertyQueryCallback)(
2923 Local<String> property,
2924 const PropertyCallbackInfo<Integer>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002925
2926
2927/**
2928 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002929 * The return value is true if the property could be deleted and false
2930 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002931 */
2932typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2933 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002934typedef void (*NamedPropertyDeleterCallback)(
2935 Local<String> property,
2936 const PropertyCallbackInfo<Boolean>& info);
2937
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002938
2939/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002940 * Returns an array containing the names of the properties the named
2941 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002942 */
2943typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002944typedef void (*NamedPropertyEnumeratorCallback)(
2945 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002946
v8.team.kasperl727e9952008-09-02 14:56:44 +00002947
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002948/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002949 * Returns the value of the property if the getter intercepts the
2950 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951 */
2952typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2953 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002954typedef void (*IndexedPropertyGetterCallback)(
2955 uint32_t index,
2956 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957
2958
2959/**
2960 * Returns the value if the setter intercepts the request.
2961 * Otherwise, returns an empty handle.
2962 */
2963typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2964 Local<Value> value,
2965 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002966typedef void (*IndexedPropertySetterCallback)(
2967 uint32_t index,
2968 Local<Value> value,
2969 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970
2971
2972/**
2973 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002974 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002975 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002976typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2977 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002978typedef void (*IndexedPropertyQueryCallback)(
2979 uint32_t index,
2980 const PropertyCallbackInfo<Integer>& info);
2981
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982
2983/**
2984 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002985 * The return value is true if the property could be deleted and false
2986 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002987 */
2988typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2989 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002990typedef void (*IndexedPropertyDeleterCallback)(
2991 uint32_t index,
2992 const PropertyCallbackInfo<Boolean>& info);
2993
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002994
v8.team.kasperl727e9952008-09-02 14:56:44 +00002995/**
2996 * Returns an array containing the indices of the properties the
2997 * indexed property getter intercepts.
2998 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002999typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003000typedef void (*IndexedPropertyEnumeratorCallback)(
3001 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002
3003
3004/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003005 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003006 */
3007enum AccessType {
3008 ACCESS_GET,
3009 ACCESS_SET,
3010 ACCESS_HAS,
3011 ACCESS_DELETE,
3012 ACCESS_KEYS
3013};
3014
v8.team.kasperl727e9952008-09-02 14:56:44 +00003015
3016/**
3017 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003018 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003019 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003020typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021 Local<Value> key,
3022 AccessType type,
3023 Local<Value> data);
3024
v8.team.kasperl727e9952008-09-02 14:56:44 +00003025
3026/**
3027 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003028 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003029 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003030typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003031 uint32_t index,
3032 AccessType type,
3033 Local<Value> data);
3034
3035
3036/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003037 * A FunctionTemplate is used to create functions at runtime. There
3038 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003039 * context. The lifetime of the created function is equal to the
3040 * lifetime of the context. So in case the embedder needs to create
3041 * temporary functions that can be collected using Scripts is
3042 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003043 *
3044 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003045 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003047 * A FunctionTemplate has a corresponding instance template which is
3048 * used to create object instances when the function is used as a
3049 * constructor. Properties added to the instance template are added to
3050 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003051 *
3052 * A FunctionTemplate can have a prototype template. The prototype template
3053 * is used to create the prototype object of the function.
3054 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003055 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003057 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003058 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
3059 * t->Set("func_property", v8::Number::New(1));
3060 *
3061 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
3062 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
3063 * proto_t->Set("proto_const", v8::Number::New(2));
3064 *
3065 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
3066 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
3067 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
3068 * instance_t->Set("instance_property", Number::New(3));
3069 *
3070 * v8::Local<v8::Function> function = t->GetFunction();
3071 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003072 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003073 *
3074 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00003075 * and "instance" for the instance object created above. The function
3076 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003078 * \code
3079 * func_property in function == true;
3080 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003081 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003082 * function.prototype.proto_method() invokes 'InvokeCallback'
3083 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003085 * instance instanceof function == true;
3086 * instance.instance_accessor calls 'InstanceAccessorCallback'
3087 * instance.instance_property == 3;
3088 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003089 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003090 * A FunctionTemplate can inherit from another one by calling the
3091 * FunctionTemplate::Inherit method. The following graph illustrates
3092 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003093 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003094 * \code
3095 * FunctionTemplate Parent -> Parent() . prototype -> { }
3096 * ^ ^
3097 * | Inherit(Parent) | .__proto__
3098 * | |
3099 * FunctionTemplate Child -> Child() . prototype -> { }
3100 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003102 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
3103 * object of the Child() function has __proto__ pointing to the
3104 * Parent() function's prototype object. An instance of the Child
3105 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003106 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003107 * Let Parent be the FunctionTemplate initialized in the previous
3108 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003110 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003111 * Local<FunctionTemplate> parent = t;
3112 * Local<FunctionTemplate> child = FunctionTemplate::New();
3113 * child->Inherit(parent);
3114 *
3115 * Local<Function> child_function = child->GetFunction();
3116 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003117 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003118 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003119 * The Child function and Child instance will have the following
3120 * properties:
3121 *
3122 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003124 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003125 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003126 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003127 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003128class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129 public:
3130 /** Creates a function template.*/
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003131 // TODO(dcarney): deprecate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003132 static Local<FunctionTemplate> New(
3133 InvocationCallback callback = 0,
3134 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003135 Handle<Signature> signature = Handle<Signature>(),
3136 int length = 0);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003137 static Local<FunctionTemplate> New(
3138 FunctionCallback callback, // TODO(dcarney): add back default param.
3139 Handle<Value> data = Handle<Value>(),
3140 Handle<Signature> signature = Handle<Signature>(),
3141 int length = 0);
3142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003143 /** Returns the unique function instance in the current execution context.*/
3144 Local<Function> GetFunction();
3145
v8.team.kasperl727e9952008-09-02 14:56:44 +00003146 /**
3147 * Set the call-handler callback for a FunctionTemplate. This
3148 * callback is called whenever the function created from this
3149 * FunctionTemplate is called.
3150 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003151 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003152 void SetCallHandler(InvocationCallback callback,
3153 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003154 void SetCallHandler(FunctionCallback callback,
3155 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003156
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003157 /** Set the predefined length property for the FunctionTemplate. */
3158 void SetLength(int length);
3159
v8.team.kasperl727e9952008-09-02 14:56:44 +00003160 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003161 Local<ObjectTemplate> InstanceTemplate();
3162
3163 /** Causes the function template to inherit from a parent function template.*/
3164 void Inherit(Handle<FunctionTemplate> parent);
3165
3166 /**
3167 * A PrototypeTemplate is the template used to create the prototype object
3168 * of the function created by this template.
3169 */
3170 Local<ObjectTemplate> PrototypeTemplate();
3171
v8.team.kasperl727e9952008-09-02 14:56:44 +00003172 /**
3173 * Set the class name of the FunctionTemplate. This is used for
3174 * printing objects created with the function created from the
3175 * FunctionTemplate as its constructor.
3176 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003177 void SetClassName(Handle<String> name);
3178
3179 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003180 * Determines whether the __proto__ accessor ignores instances of
3181 * the function template. If instances of the function template are
3182 * ignored, __proto__ skips all instances and instead returns the
3183 * next object in the prototype chain.
3184 *
3185 * Call with a value of true to make the __proto__ accessor ignore
3186 * instances of the function template. Call with a value of false
3187 * to make the __proto__ accessor not ignore instances of the
3188 * function template. By default, instances of a function template
3189 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003190 */
3191 void SetHiddenPrototype(bool value);
3192
3193 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003194 * Sets the ReadOnly flag in the attributes of the 'prototype' property
3195 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00003196 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003197 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00003198
3199 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003200 * Returns true if the given object is an instance of this function
3201 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003202 */
3203 bool HasInstance(Handle<Value> object);
3204
3205 private:
3206 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207 friend class Context;
3208 friend class ObjectTemplate;
3209};
3210
3211
3212/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003213 * An ObjectTemplate is used to create objects at runtime.
3214 *
3215 * Properties added to an ObjectTemplate are added to each object
3216 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003217 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003218class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003219 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003220 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003221 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003222
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003223 /** Creates a new instance of this template.*/
3224 Local<Object> NewInstance();
3225
3226 /**
3227 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003228 *
3229 * Whenever the property with the given name is accessed on objects
3230 * created from this ObjectTemplate the getter and setter callbacks
3231 * are called instead of getting and setting the property directly
3232 * on the JavaScript object.
3233 *
3234 * \param name The name of the property for which an accessor is added.
3235 * \param getter The callback to invoke when getting the property.
3236 * \param setter The callback to invoke when setting the property.
3237 * \param data A piece of data that will be passed to the getter and setter
3238 * callbacks whenever they are invoked.
3239 * \param settings Access control settings for the accessor. This is a bit
3240 * field consisting of one of more of
3241 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
3242 * The default is to not allow cross-context access.
3243 * ALL_CAN_READ means that all cross-context reads are allowed.
3244 * ALL_CAN_WRITE means that all cross-context writes are allowed.
3245 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
3246 * cross-context access.
3247 * \param attribute The attributes of the property for which an accessor
3248 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003249 * \param signature The signature describes valid receivers for the accessor
3250 * and is used to perform implicit instance checks against them. If the
3251 * receiver is incompatible (i.e. is not an instance of the constructor as
3252 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
3253 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003254 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003255 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003256 void SetAccessor(Handle<String> name,
3257 AccessorGetter getter,
3258 AccessorSetter setter = 0,
3259 Handle<Value> data = Handle<Value>(),
3260 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003261 PropertyAttribute attribute = None,
3262 Handle<AccessorSignature> signature =
3263 Handle<AccessorSignature>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003264 void SetAccessor(Handle<String> name,
3265 AccessorGetterCallback getter,
3266 AccessorSetterCallback setter = 0,
3267 Handle<Value> data = Handle<Value>(),
3268 AccessControl settings = DEFAULT,
3269 PropertyAttribute attribute = None,
3270 Handle<AccessorSignature> signature =
3271 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003272
ulan@chromium.org750145a2013-03-07 15:14:13 +00003273 // This function is not yet stable and should not be used at this time.
3274 bool SetAccessor(Handle<String> name,
3275 Handle<DeclaredAccessorDescriptor> descriptor,
3276 AccessControl settings = DEFAULT,
3277 PropertyAttribute attribute = None,
3278 Handle<AccessorSignature> signature =
3279 Handle<AccessorSignature>());
3280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281 /**
3282 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003283 *
3284 * Whenever a named property is accessed on objects created from
3285 * this object template, the provided callback is invoked instead of
3286 * accessing the property directly on the JavaScript object.
3287 *
3288 * \param getter The callback to invoke when getting a property.
3289 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003290 * \param query The callback to invoke to check if a property is present,
3291 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003292 * \param deleter The callback to invoke when deleting a property.
3293 * \param enumerator The callback to invoke to enumerate all the named
3294 * properties of an object.
3295 * \param data A piece of data that will be passed to the callbacks
3296 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003297 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003298 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003299 void SetNamedPropertyHandler(NamedPropertyGetter getter,
3300 NamedPropertySetter setter = 0,
3301 NamedPropertyQuery query = 0,
3302 NamedPropertyDeleter deleter = 0,
3303 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003304 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003305 void SetNamedPropertyHandler(
3306 NamedPropertyGetterCallback getter,
3307 NamedPropertySetterCallback setter = 0,
3308 NamedPropertyQueryCallback query = 0,
3309 NamedPropertyDeleterCallback deleter = 0,
3310 NamedPropertyEnumeratorCallback enumerator = 0,
3311 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003312
3313 /**
3314 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003315 *
3316 * Whenever an indexed property is accessed on objects created from
3317 * this object template, the provided callback is invoked instead of
3318 * accessing the property directly on the JavaScript object.
3319 *
3320 * \param getter The callback to invoke when getting a property.
3321 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003322 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003323 * \param deleter The callback to invoke when deleting a property.
3324 * \param enumerator The callback to invoke to enumerate all the indexed
3325 * properties of an object.
3326 * \param data A piece of data that will be passed to the callbacks
3327 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003328 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003329 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003330 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
3331 IndexedPropertySetter setter = 0,
3332 IndexedPropertyQuery query = 0,
3333 IndexedPropertyDeleter deleter = 0,
3334 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003335 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003336 void SetIndexedPropertyHandler(
3337 IndexedPropertyGetterCallback getter,
3338 IndexedPropertySetterCallback setter = 0,
3339 IndexedPropertyQueryCallback query = 0,
3340 IndexedPropertyDeleterCallback deleter = 0,
3341 IndexedPropertyEnumeratorCallback enumerator = 0,
3342 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003343
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003344 /**
3345 * Sets the callback to be used when calling instances created from
3346 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00003347 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003348 * function.
3349 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003350 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003351 void SetCallAsFunctionHandler(InvocationCallback callback,
3352 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003353 void SetCallAsFunctionHandler(FunctionCallback callback,
3354 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003355
v8.team.kasperl727e9952008-09-02 14:56:44 +00003356 /**
3357 * Mark object instances of the template as undetectable.
3358 *
3359 * In many ways, undetectable objects behave as though they are not
3360 * there. They behave like 'undefined' in conditionals and when
3361 * printed. However, properties can be accessed and called as on
3362 * normal objects.
3363 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003364 void MarkAsUndetectable();
3365
v8.team.kasperl727e9952008-09-02 14:56:44 +00003366 /**
3367 * Sets access check callbacks on the object template.
3368 *
3369 * When accessing properties on instances of this object template,
3370 * the access check callback will be called to determine whether or
3371 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003372 * The last parameter specifies whether access checks are turned
3373 * on by default on instances. If access checks are off by default,
3374 * they can be turned on on individual instances by calling
3375 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00003376 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003377 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
3378 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003379 Handle<Value> data = Handle<Value>(),
3380 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003381
kasper.lund212ac232008-07-16 07:07:30 +00003382 /**
3383 * Gets the number of internal fields for objects generated from
3384 * this template.
3385 */
3386 int InternalFieldCount();
3387
3388 /**
3389 * Sets the number of internal fields for objects generated from
3390 * this template.
3391 */
3392 void SetInternalFieldCount(int value);
3393
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 private:
3395 ObjectTemplate();
3396 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
3397 friend class FunctionTemplate;
3398};
3399
3400
3401/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003402 * A Signature specifies which receivers and arguments are valid
3403 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003404 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003405class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 public:
3407 static Local<Signature> New(Handle<FunctionTemplate> receiver =
3408 Handle<FunctionTemplate>(),
3409 int argc = 0,
3410 Handle<FunctionTemplate> argv[] = 0);
3411 private:
3412 Signature();
3413};
3414
3415
3416/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003417 * An AccessorSignature specifies which receivers are valid parameters
3418 * to an accessor callback.
3419 */
3420class V8EXPORT AccessorSignature : public Data {
3421 public:
3422 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
3423 Handle<FunctionTemplate>());
3424 private:
3425 AccessorSignature();
3426};
3427
3428
ulan@chromium.org750145a2013-03-07 15:14:13 +00003429class V8EXPORT DeclaredAccessorDescriptor : public Data {
3430 private:
3431 DeclaredAccessorDescriptor();
3432};
3433
3434
3435class V8EXPORT ObjectOperationDescriptor : public Data {
3436 public:
3437 // This function is not yet stable and should not be used at this time.
3438 static Local<RawOperationDescriptor> NewInternalFieldDereference(
3439 Isolate* isolate,
3440 int internal_field);
3441 private:
3442 ObjectOperationDescriptor();
3443};
3444
3445
3446enum DeclaredAccessorDescriptorDataType {
3447 kDescriptorBoolType,
3448 kDescriptorInt8Type, kDescriptorUint8Type,
3449 kDescriptorInt16Type, kDescriptorUint16Type,
3450 kDescriptorInt32Type, kDescriptorUint32Type,
3451 kDescriptorFloatType, kDescriptorDoubleType
3452};
3453
3454
3455class V8EXPORT RawOperationDescriptor : public Data {
3456 public:
3457 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
3458 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
3459 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
3460 int16_t byte_offset);
3461 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
3462 void* compare_value);
3463 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
3464 Isolate* isolate,
3465 DeclaredAccessorDescriptorDataType data_type,
3466 uint8_t bool_offset = 0);
3467 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
3468 uint8_t bitmask,
3469 uint8_t compare_value);
3470 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
3471 Isolate* isolate,
3472 uint16_t bitmask,
3473 uint16_t compare_value);
3474 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
3475 Isolate* isolate,
3476 uint32_t bitmask,
3477 uint32_t compare_value);
3478
3479 private:
3480 RawOperationDescriptor();
3481};
3482
3483
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003484/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003485 * A utility for determining the type of objects based on the template
3486 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003487 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003488class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003489 public:
3490 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
3491 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
3492 int match(Handle<Value> value);
3493 private:
3494 TypeSwitch();
3495};
3496
3497
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003498// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003500class V8EXPORT ExternalAsciiStringResourceImpl
3501 : public String::ExternalAsciiStringResource {
3502 public:
3503 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
3504 ExternalAsciiStringResourceImpl(const char* data, size_t length)
3505 : data_(data), length_(length) {}
3506 const char* data() const { return data_; }
3507 size_t length() const { return length_; }
3508
3509 private:
3510 const char* data_;
3511 size_t length_;
3512};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003513
3514/**
3515 * Ignore
3516 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003517class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003519 // Note that the strings passed into this constructor must live as long
3520 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003522 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003523 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003524 const char** deps = 0,
3525 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003526 virtual ~Extension() { }
3527 virtual v8::Handle<v8::FunctionTemplate>
3528 GetNativeFunction(v8::Handle<v8::String> name) {
3529 return v8::Handle<v8::FunctionTemplate>();
3530 }
3531
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003532 const char* name() const { return name_; }
3533 size_t source_length() const { return source_length_; }
3534 const String::ExternalAsciiStringResource* source() const {
3535 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003536 int dependency_count() { return dep_count_; }
3537 const char** dependencies() { return deps_; }
3538 void set_auto_enable(bool value) { auto_enable_ = value; }
3539 bool auto_enable() { return auto_enable_; }
3540
3541 private:
3542 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003543 size_t source_length_; // expected to initialize before source_
3544 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003545 int dep_count_;
3546 const char** deps_;
3547 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003548
3549 // Disallow copying and assigning.
3550 Extension(const Extension&);
3551 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003552};
3553
3554
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003555void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003556
3557
3558/**
3559 * Ignore
3560 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003561class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003562 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003563 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003564 RegisterExtension(extension);
3565 }
3566};
3567
3568
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003569// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570
3571
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003572Handle<Primitive> V8EXPORT Undefined();
3573Handle<Primitive> V8EXPORT Null();
3574Handle<Boolean> V8EXPORT True();
3575Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003576
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003577V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
3578V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
3579V8_INLINE(Handle<Boolean> True(Isolate* isolate));
3580V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003581
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003582
3583/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003584 * A set of constraints that specifies the limits of the runtime's memory use.
3585 * You must set the heap size before initializing the VM - the size cannot be
3586 * adjusted after the VM is initialized.
3587 *
3588 * If you are using threads then you should hold the V8::Locker lock while
3589 * setting the stack limit and you must set a non-default stack limit separately
3590 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003592class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593 public:
3594 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003595 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003597 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003598 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003599 int max_executable_size() { return max_executable_size_; }
3600 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003601 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003602 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
3604 private:
3605 int max_young_space_size_;
3606 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003607 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608 uint32_t* stack_limit_;
3609};
3610
3611
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003612bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003613
3614
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003615// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003616
3617
3618typedef void (*FatalErrorCallback)(const char* location, const char* message);
3619
3620
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003621typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003622
3623
3624/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003625 * Schedules an exception to be thrown when returning to JavaScript. When an
3626 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003627 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00003628 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003629 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003630Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003631
3632/**
3633 * Create new error objects by calling the corresponding error object
3634 * constructor with the message.
3635 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003636class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003637 public:
3638 static Local<Value> RangeError(Handle<String> message);
3639 static Local<Value> ReferenceError(Handle<String> message);
3640 static Local<Value> SyntaxError(Handle<String> message);
3641 static Local<Value> TypeError(Handle<String> message);
3642 static Local<Value> Error(Handle<String> message);
3643};
3644
3645
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003646// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003647
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003648typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003649
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003650typedef void* (*CreateHistogramCallback)(const char* name,
3651 int min,
3652 int max,
3653 size_t buckets);
3654
3655typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
3656
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003657// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003658 enum ObjectSpace {
3659 kObjectSpaceNewSpace = 1 << 0,
3660 kObjectSpaceOldPointerSpace = 1 << 1,
3661 kObjectSpaceOldDataSpace = 1 << 2,
3662 kObjectSpaceCodeSpace = 1 << 3,
3663 kObjectSpaceMapSpace = 1 << 4,
3664 kObjectSpaceLoSpace = 1 << 5,
3665
3666 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
3667 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
3668 kObjectSpaceLoSpace
3669 };
3670
3671 enum AllocationAction {
3672 kAllocationActionAllocate = 1 << 0,
3673 kAllocationActionFree = 1 << 1,
3674 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
3675 };
3676
3677typedef void (*MemoryAllocationCallback)(ObjectSpace space,
3678 AllocationAction action,
3679 int size);
3680
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003681// --- Leave Script Callback ---
3682typedef void (*CallCompletedCallback)();
3683
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003684// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003685typedef void (*FailedAccessCheckCallback)(Local<Object> target,
3686 AccessType type,
3687 Local<Value> data);
3688
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003689// --- AllowCodeGenerationFromStrings callbacks ---
3690
3691/**
3692 * Callback to check if code generation from strings is allowed. See
3693 * Context::AllowCodeGenerationFromStrings.
3694 */
3695typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
3696
3697// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003698
3699/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003700 * Applications can register callback functions which will be called
3701 * before and after a garbage collection. Allocations are not
3702 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003703 * objects (set or delete properties for example) since it is possible
3704 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003705 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003706enum GCType {
3707 kGCTypeScavenge = 1 << 0,
3708 kGCTypeMarkSweepCompact = 1 << 1,
3709 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
3710};
3711
3712enum GCCallbackFlags {
3713 kNoGCCallbackFlags = 0,
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003714 kGCCallbackFlagCompacted = 1 << 0,
3715 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003716};
3717
3718typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
3719typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
3720
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003721typedef void (*GCCallback)();
3722
3723
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724/**
ager@chromium.org3811b432009-10-28 14:53:37 +00003725 * Collection of V8 heap information.
3726 *
3727 * Instances of this class can be passed to v8::V8::HeapStatistics to
3728 * get heap statistics from V8.
3729 */
3730class V8EXPORT HeapStatistics {
3731 public:
3732 HeapStatistics();
3733 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003734 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003735 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003736 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003737 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003738
3739 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003740 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003741 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003742 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003743 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003744 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003745
3746 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003747 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003748};
3749
3750
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003751class RetainedObjectInfo;
3752
ager@chromium.org3811b432009-10-28 14:53:37 +00003753/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003754 * Isolate represents an isolated instance of the V8 engine. V8
3755 * isolates have completely separate states. Objects from one isolate
3756 * must not be used in other isolates. When V8 is initialized a
3757 * default isolate is implicitly created and entered. The embedder
3758 * can create additional isolates and use them in parallel in multiple
3759 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003760 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003761 */
3762class V8EXPORT Isolate {
3763 public:
3764 /**
3765 * Stack-allocated class which sets the isolate for all operations
3766 * executed within a local scope.
3767 */
3768 class V8EXPORT Scope {
3769 public:
3770 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3771 isolate->Enter();
3772 }
3773
3774 ~Scope() { isolate_->Exit(); }
3775
3776 private:
3777 Isolate* const isolate_;
3778
3779 // Prevent copying of Scope objects.
3780 Scope(const Scope&);
3781 Scope& operator=(const Scope&);
3782 };
3783
3784 /**
3785 * Creates a new isolate. Does not change the currently entered
3786 * isolate.
3787 *
3788 * When an isolate is no longer used its resources should be freed
3789 * by calling Dispose(). Using the delete operator is not allowed.
3790 */
3791 static Isolate* New();
3792
3793 /**
3794 * Returns the entered isolate for the current thread or NULL in
3795 * case there is no current isolate.
3796 */
3797 static Isolate* GetCurrent();
3798
3799 /**
3800 * Methods below this point require holding a lock (using Locker) in
3801 * a multi-threaded environment.
3802 */
3803
3804 /**
3805 * Sets this isolate as the entered one for the current thread.
3806 * Saves the previously entered one (if any), so that it can be
3807 * restored when exiting. Re-entering an isolate is allowed.
3808 */
3809 void Enter();
3810
3811 /**
3812 * Exits this isolate by restoring the previously entered one in the
3813 * current thread. The isolate may still stay the same, if it was
3814 * entered more than once.
3815 *
3816 * Requires: this == Isolate::GetCurrent().
3817 */
3818 void Exit();
3819
3820 /**
3821 * Disposes the isolate. The isolate must not be entered by any
3822 * thread to be disposable.
3823 */
3824 void Dispose();
3825
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003826 /**
3827 * Associate embedder-specific data with the isolate
3828 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003829 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003830
3831 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003832 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003833 * Returns NULL if SetData has never been called.
3834 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003835 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003836
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003837 /**
3838 * Get statistics about the heap memory usage.
3839 */
3840 void GetHeapStatistics(HeapStatistics* heap_statistics);
3841
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003842 /**
3843 * Adjusts the amount of registered external memory. Used to give V8 an
3844 * indication of the amount of externally allocated memory that is kept alive
3845 * by JavaScript objects. V8 uses this to decide when to perform global
3846 * garbage collections. Registering externally allocated memory will trigger
3847 * global garbage collections more often than it would otherwise in an attempt
3848 * to garbage collect the JavaScript objects that keep the externally
3849 * allocated memory alive.
3850 *
3851 * \param change_in_bytes the change in externally allocated memory that is
3852 * kept alive by JavaScript objects.
3853 * \returns the adjusted value.
3854 */
3855 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3856
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003857 /**
3858 * Returns heap profiler for this isolate. Will return NULL until the isolate
3859 * is initialized.
3860 */
3861 HeapProfiler* GetHeapProfiler();
3862
3863 /**
3864 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3865 * is initialized.
3866 */
3867 CpuProfiler* GetCpuProfiler();
3868
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003869 /** Returns the context that is on the top of the stack. */
3870 Local<Context> GetCurrentContext();
3871
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003872 /**
3873 * Allows the host application to group objects together. If one
3874 * object in the group is alive, all objects in the group are alive.
3875 * After each garbage collection, object groups are removed. It is
3876 * intended to be used in the before-garbage-collection callback
3877 * function, for instance to simulate DOM tree connections among JS
3878 * wrapper objects. Object groups for all dependent handles need to
3879 * be provided for kGCTypeMarkSweepCompact collections, for all other
3880 * garbage collection types it is sufficient to provide object groups
3881 * for partially dependent handles only.
3882 */
3883 void SetObjectGroupId(const Persistent<Value>& object,
3884 UniqueId id);
3885
3886 /**
3887 * Allows the host application to declare implicit references from an object
3888 * group to an object. If the objects of the object group are alive, the child
3889 * object is alive too. After each garbage collection, all implicit references
3890 * are removed. It is intended to be used in the before-garbage-collection
3891 * callback function.
3892 */
3893 void SetReferenceFromGroup(UniqueId id,
3894 const Persistent<Value>& child);
3895
3896 /**
3897 * Allows the host application to declare implicit references from an object
3898 * to another object. If the parent object is alive, the child object is alive
3899 * too. After each garbage collection, all implicit references are removed. It
3900 * is intended to be used in the before-garbage-collection callback function.
3901 */
3902 void SetReference(const Persistent<Object>& parent,
3903 const Persistent<Value>& child);
3904
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003905 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003906 Isolate();
3907 Isolate(const Isolate&);
3908 ~Isolate();
3909 Isolate& operator=(const Isolate&);
3910 void* operator new(size_t size);
3911 void operator delete(void*, size_t);
3912};
3913
3914
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003915class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003916 public:
3917 enum CompressionAlgorithm {
3918 kUncompressed,
3919 kBZip2
3920 };
3921
3922 const char* data;
3923 int compressed_size;
3924 int raw_size;
3925};
3926
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003927
3928/**
3929 * A helper class for driving V8 startup data decompression. It is based on
3930 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3931 * for an embedder to use this class, instead, API functions can be used
3932 * directly.
3933 *
3934 * For an example of the class usage, see the "shell.cc" sample application.
3935 */
3936class V8EXPORT StartupDataDecompressor { // NOLINT
3937 public:
3938 StartupDataDecompressor();
3939 virtual ~StartupDataDecompressor();
3940 int Decompress();
3941
3942 protected:
3943 virtual int DecompressData(char* raw_data,
3944 int* raw_data_size,
3945 const char* compressed_data,
3946 int compressed_data_size) = 0;
3947
3948 private:
3949 char** raw_data;
3950};
3951
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003952
3953/**
3954 * EntropySource is used as a callback function when v8 needs a source
3955 * of entropy.
3956 */
3957typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3958
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003959
3960/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003961 * ReturnAddressLocationResolver is used as a callback function when v8 is
3962 * resolving the location of a return address on the stack. Profilers that
3963 * change the return address on the stack can use this to resolve the stack
3964 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003965 *
3966 * \param return_addr_location points to a location on stack where a machine
3967 * return address resides.
3968 * \returns either return_addr_location, or else a pointer to the profiler's
3969 * copy of the original return address.
3970 *
3971 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003972 */
3973typedef uintptr_t (*ReturnAddressLocationResolver)(
3974 uintptr_t return_addr_location);
3975
3976
3977/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003978 * FunctionEntryHook is the type of the profile entry hook called at entry to
3979 * any generated function when function-level profiling is enabled.
3980 *
3981 * \param function the address of the function that's being entered.
3982 * \param return_addr_location points to a location on stack where the machine
3983 * return address resides. This can be used to identify the caller of
3984 * \p function, and/or modified to divert execution when \p function exits.
3985 *
3986 * \note the entry hook must not cause garbage collection.
3987 */
3988typedef void (*FunctionEntryHook)(uintptr_t function,
3989 uintptr_t return_addr_location);
3990
3991
3992/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003993 * A JIT code event is issued each time code is added, moved or removed.
3994 *
3995 * \note removal events are not currently issued.
3996 */
3997struct JitCodeEvent {
3998 enum EventType {
3999 CODE_ADDED,
4000 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004001 CODE_REMOVED,
4002 CODE_ADD_LINE_POS_INFO,
4003 CODE_START_LINE_INFO_RECORDING,
4004 CODE_END_LINE_INFO_RECORDING
4005 };
4006 // Definition of the code position type. The "POSITION" type means the place
4007 // in the source code which are of interest when making stack traces to
4008 // pin-point the source location of a stack frame as close as possible.
4009 // The "STATEMENT_POSITION" means the place at the beginning of each
4010 // statement, and is used to indicate possible break locations.
4011 enum PositionType {
4012 POSITION,
4013 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004014 };
4015
4016 // Type of event.
4017 EventType type;
4018 // Start of the instructions.
4019 void* code_start;
4020 // Size of the instructions.
4021 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004022 // Script info for CODE_ADDED event.
4023 Handle<Script> script;
4024 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
4025 // code line information which is returned from the
4026 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
4027 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
4028 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004029
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004030 struct name_t {
4031 // Name of the object associated with the code, note that the string is not
4032 // zero-terminated.
4033 const char* str;
4034 // Number of chars in str.
4035 size_t len;
4036 };
4037
4038 struct line_info_t {
4039 // PC offset
4040 size_t offset;
4041 // Code postion
4042 size_t pos;
4043 // The position type.
4044 PositionType position_type;
4045 };
4046
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004047 union {
4048 // Only valid for CODE_ADDED.
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004049 struct name_t name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004050
4051 // Only valid for CODE_ADD_LINE_POS_INFO
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004052 struct line_info_t line_info;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004053
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004054 // New location of instructions. Only valid for CODE_MOVED.
4055 void* new_code_start;
4056 };
4057};
4058
4059/**
4060 * Option flags passed to the SetJitCodeEventHandler function.
4061 */
4062enum JitCodeEventOptions {
4063 kJitCodeEventDefault = 0,
4064 // Generate callbacks for already existent code.
4065 kJitCodeEventEnumExisting = 1
4066};
4067
4068
4069/**
4070 * Callback function passed to SetJitCodeEventHandler.
4071 *
4072 * \param event code add, move or removal event.
4073 */
4074typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
4075
4076
4077/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004078 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004079 */
4080class V8EXPORT ExternalResourceVisitor { // NOLINT
4081 public:
4082 virtual ~ExternalResourceVisitor() {}
4083 virtual void VisitExternalString(Handle<String> string) {}
4084};
4085
4086
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004087/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004088 * Interface for iterating through all the persistent handles in the heap.
4089 */
4090class V8EXPORT PersistentHandleVisitor { // NOLINT
4091 public:
4092 virtual ~PersistentHandleVisitor() {}
4093 virtual void VisitPersistentHandle(Persistent<Value> value,
4094 uint16_t class_id) {}
4095};
4096
4097
4098/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004099 * Asserts that no action is performed that could cause a handle's value
4100 * to be modified. Useful when otherwise unsafe handle operations need to
4101 * be performed.
4102 */
4103class V8EXPORT AssertNoGCScope {
4104#ifndef DEBUG
4105 V8_INLINE(AssertNoGCScope(Isolate* isolate)) {}
4106#else
4107 AssertNoGCScope(Isolate* isolate);
4108 ~AssertNoGCScope();
4109 private:
4110 Isolate* isolate_;
4111 bool last_state_;
4112#endif
4113};
4114
4115
4116/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004117 * Container class for static utility functions.
4118 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004119class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004120 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00004121 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004122 static void SetFatalErrorHandler(FatalErrorCallback that);
4123
v8.team.kasperl727e9952008-09-02 14:56:44 +00004124 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004125 * Set the callback to invoke to check if code generation from
4126 * strings should be allowed.
4127 */
4128 static void SetAllowCodeGenerationFromStringsCallback(
4129 AllowCodeGenerationFromStringsCallback that);
4130
4131 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004132 * Ignore out-of-memory exceptions.
4133 *
4134 * V8 running out of memory is treated as a fatal error by default.
4135 * This means that the fatal error handler is called and that V8 is
4136 * terminated.
4137 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004138 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00004139 * out-of-memory situation as a fatal error. This way, the contexts
4140 * that did not cause the out of memory problem might be able to
4141 * continue execution.
4142 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004143 static void IgnoreOutOfMemoryException();
4144
v8.team.kasperl727e9952008-09-02 14:56:44 +00004145 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004146 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00004147 * fatal errors such as out-of-memory situations.
4148 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004149 static bool IsDead();
4150
4151 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004152 * The following 4 functions are to be used when V8 is built with
4153 * the 'compress_startup_data' flag enabled. In this case, the
4154 * embedder must decompress startup data prior to initializing V8.
4155 *
4156 * This is how interaction with V8 should look like:
4157 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
4158 * v8::StartupData* compressed_data =
4159 * new v8::StartupData[compressed_data_count];
4160 * v8::V8::GetCompressedStartupData(compressed_data);
4161 * ... decompress data (compressed_data can be updated in-place) ...
4162 * v8::V8::SetDecompressedStartupData(compressed_data);
4163 * ... now V8 can be initialized
4164 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004165 *
4166 * A helper class StartupDataDecompressor is provided. It implements
4167 * the protocol of the interaction described above, and can be used in
4168 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004169 */
4170 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
4171 static int GetCompressedStartupDataCount();
4172 static void GetCompressedStartupData(StartupData* compressed_data);
4173 static void SetDecompressedStartupData(StartupData* decompressed_data);
4174
4175 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004176 * Adds a message listener.
4177 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004178 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00004179 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004180 *
4181 * If data is specified, it will be passed to the callback when it is called.
4182 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004183 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004184 static bool AddMessageListener(MessageCallback that,
4185 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004186
4187 /**
4188 * Remove all message listeners from the specified callback function.
4189 */
4190 static void RemoveMessageListeners(MessageCallback that);
4191
4192 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004193 * Tells V8 to capture current stack trace when uncaught exception occurs
4194 * and report it to the message listeners. The option is off by default.
4195 */
4196 static void SetCaptureStackTraceForUncaughtExceptions(
4197 bool capture,
4198 int frame_limit = 10,
4199 StackTrace::StackTraceOptions options = StackTrace::kOverview);
4200
4201 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004202 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004203 */
4204 static void SetFlagsFromString(const char* str, int length);
4205
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004206 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004207 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004208 */
4209 static void SetFlagsFromCommandLine(int* argc,
4210 char** argv,
4211 bool remove_flags);
4212
kasper.lund7276f142008-07-30 08:49:36 +00004213 /** Get the version string. */
4214 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004215
4216 /**
4217 * Enables the host application to provide a mechanism for recording
4218 * statistics counters.
4219 */
4220 static void SetCounterFunction(CounterLookupCallback);
4221
4222 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004223 * Enables the host application to provide a mechanism for recording
4224 * histograms. The CreateHistogram function returns a
4225 * histogram which will later be passed to the AddHistogramSample
4226 * function.
4227 */
4228 static void SetCreateHistogramFunction(CreateHistogramCallback);
4229 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
4230
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004231 /** Callback function for reporting failed access checks.*/
4232 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
4233
4234 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004235 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004236 * garbage collection. Allocations are not allowed in the
4237 * callback function, you therefore cannot manipulate objects (set
4238 * or delete properties for example) since it is possible such
4239 * operations will result in the allocation of objects. It is possible
4240 * to specify the GCType filter for your callback. But it is not possible to
4241 * register the same callback function two times with different
4242 * GCType filters.
4243 */
4244 static void AddGCPrologueCallback(
4245 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
4246
4247 /**
4248 * This function removes callback which was installed by
4249 * AddGCPrologueCallback function.
4250 */
4251 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
4252
4253 /**
4254 * The function is deprecated. Please use AddGCPrologueCallback instead.
4255 * Enables the host application to receive a notification before a
4256 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00004257 * callback function, you therefore cannot manipulate objects (set
4258 * or delete properties for example) since it is possible such
4259 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004260 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004261 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004262
4263 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004264 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004265 * garbage collection. Allocations are not allowed in the
4266 * callback function, you therefore cannot manipulate objects (set
4267 * or delete properties for example) since it is possible such
4268 * operations will result in the allocation of objects. It is possible
4269 * to specify the GCType filter for your callback. But it is not possible to
4270 * register the same callback function two times with different
4271 * GCType filters.
4272 */
4273 static void AddGCEpilogueCallback(
4274 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
4275
4276 /**
4277 * This function removes callback which was installed by
4278 * AddGCEpilogueCallback function.
4279 */
4280 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
4281
4282 /**
4283 * The function is deprecated. Please use AddGCEpilogueCallback instead.
4284 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00004285 * major garbage collection. Allocations are not allowed in the
4286 * callback function, you therefore cannot manipulate objects (set
4287 * or delete properties for example) since it is possible such
4288 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004289 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004290 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004291
4292 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004293 * Enables the host application to provide a mechanism to be notified
4294 * and perform custom logging when V8 Allocates Executable Memory.
4295 */
4296 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
4297 ObjectSpace space,
4298 AllocationAction action);
4299
4300 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004301 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004302 */
4303 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
4304
4305 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004306 * Adds a callback to notify the host application when a script finished
4307 * running. If a script re-enters the runtime during executing, the
4308 * CallCompletedCallback is only invoked when the outer-most script
4309 * execution ends. Executing scripts inside the callback do not trigger
4310 * further callbacks.
4311 */
4312 static void AddCallCompletedCallback(CallCompletedCallback callback);
4313
4314 /**
4315 * Removes callback that was installed by AddCallCompletedCallback.
4316 */
4317 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
4318
4319 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004320 * Allows the host application to group objects together. If one
4321 * object in the group is alive, all objects in the group are alive.
4322 * After each garbage collection, object groups are removed. It is
4323 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00004324 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004325 * wrapper objects. Object groups for all dependent handles need to
4326 * be provided for kGCTypeMarkSweepCompact collections, for all other
4327 * garbage collection types it is sufficient to provide object groups
4328 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004329 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004330 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004331 // TODO(marja): deprecate AddObjectGroup. Use Isolate::SetObjectGroupId and
4332 // HeapProfiler::SetRetainedObjectInfo instead.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004333 static void AddObjectGroup(Persistent<Value>* objects,
4334 size_t length,
4335 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004336 static void AddObjectGroup(Isolate* isolate,
4337 Persistent<Value>* objects,
4338 size_t length,
4339 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004340
4341 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004342 * Allows the host application to declare implicit references between
4343 * the objects: if |parent| is alive, all |children| are alive too.
4344 * After each garbage collection, all implicit references
4345 * are removed. It is intended to be used in the before-garbage-collection
4346 * callback function.
4347 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004348 // TODO(marja): Deprecate AddImplicitReferences. Use
4349 // Isolate::SetReferenceFromGroup instead.
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004350 static void AddImplicitReferences(Persistent<Object> parent,
4351 Persistent<Value>* children,
4352 size_t length);
4353
4354 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004355 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004356 * initialize from scratch. This function is called implicitly if
4357 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004358 */
4359 static bool Initialize();
4360
kasper.lund7276f142008-07-30 08:49:36 +00004361 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004362 * Allows the host application to provide a callback which can be used
4363 * as a source of entropy for random number generators.
4364 */
4365 static void SetEntropySource(EntropySource source);
4366
4367 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004368 * Allows the host application to provide a callback that allows v8 to
4369 * cooperate with a profiler that rewrites return addresses on stack.
4370 */
4371 static void SetReturnAddressLocationResolver(
4372 ReturnAddressLocationResolver return_address_resolver);
4373
4374 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004375 * Allows the host application to provide the address of a function that's
4376 * invoked on entry to every V8-generated function.
4377 * Note that \p entry_hook is invoked at the very start of each
4378 * generated function.
4379 *
4380 * \param entry_hook a function that will be invoked on entry to every
4381 * V8-generated function.
4382 * \returns true on success on supported platforms, false on failure.
4383 * \note Setting a new entry hook function when one is already active will
4384 * fail.
4385 */
4386 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
4387
4388 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004389 * Allows the host application to provide the address of a function that is
4390 * notified each time code is added, moved or removed.
4391 *
4392 * \param options options for the JIT code event handler.
4393 * \param event_handler the JIT code event handler, which will be invoked
4394 * each time code is added, moved or removed.
4395 * \note \p event_handler won't get notified of existent code.
4396 * \note since code removal notifications are not currently issued, the
4397 * \p event_handler may get notifications of code that overlaps earlier
4398 * code notifications. This happens when code areas are reused, and the
4399 * earlier overlapping code areas should therefore be discarded.
4400 * \note the events passed to \p event_handler and the strings they point to
4401 * are not guaranteed to live past each call. The \p event_handler must
4402 * copy strings and other parameters it needs to keep around.
4403 * \note the set of events declared in JitCodeEvent::EventType is expected to
4404 * grow over time, and the JitCodeEvent structure is expected to accrue
4405 * new members. The \p event_handler function must ignore event codes
4406 * it does not recognize to maintain future compatibility.
4407 */
4408 static void SetJitCodeEventHandler(JitCodeEventOptions options,
4409 JitCodeEventHandler event_handler);
4410
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004411 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
4412 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004413 static intptr_t AdjustAmountOfExternalAllocatedMemory(
4414 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00004415
iposva@chromium.org245aa852009-02-10 00:49:54 +00004416 /**
4417 * Suspends recording of tick samples in the profiler.
4418 * When the V8 profiling mode is enabled (usually via command line
4419 * switches) this function suspends recording of tick samples.
4420 * Profiling ticks are discarded until ResumeProfiler() is called.
4421 *
4422 * See also the --prof and --prof_auto command line switches to
4423 * enable V8 profiling.
4424 */
4425 static void PauseProfiler();
4426
4427 /**
4428 * Resumes recording of tick samples in the profiler.
4429 * See also PauseProfiler().
4430 */
4431 static void ResumeProfiler();
4432
ager@chromium.org41826e72009-03-30 13:30:57 +00004433 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004434 * Return whether profiler is currently paused.
4435 */
4436 static bool IsProfilerPaused();
4437
4438 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004439 * Retrieve the V8 thread id of the calling thread.
4440 *
4441 * The thread id for a thread should only be retrieved after the V8
4442 * lock has been acquired with a Locker object with that thread.
4443 */
4444 static int GetCurrentThreadId();
4445
4446 /**
4447 * Forcefully terminate execution of a JavaScript thread. This can
4448 * be used to terminate long-running scripts.
4449 *
4450 * TerminateExecution should only be called when then V8 lock has
4451 * been acquired with a Locker object. Therefore, in order to be
4452 * able to terminate long-running threads, preemption must be
4453 * enabled to allow the user of TerminateExecution to acquire the
4454 * lock.
4455 *
4456 * The termination is achieved by throwing an exception that is
4457 * uncatchable by JavaScript exception handlers. Termination
4458 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004459 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004460 * exception handler that catches an exception should check if that
4461 * exception is a termination exception and immediately return if
4462 * that is the case. Returning immediately in that case will
4463 * continue the propagation of the termination exception if needed.
4464 *
4465 * The thread id passed to TerminateExecution must have been
4466 * obtained by calling GetCurrentThreadId on the thread in question.
4467 *
4468 * \param thread_id The thread id of the thread to terminate.
4469 */
4470 static void TerminateExecution(int thread_id);
4471
4472 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004473 * Forcefully terminate the current thread of JavaScript execution
4474 * in the given isolate. If no isolate is provided, the default
4475 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004476 *
4477 * This method can be used by any thread even if that thread has not
4478 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004479 *
4480 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004481 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004482 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00004483
4484 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004485 * Is V8 terminating JavaScript execution.
4486 *
4487 * Returns true if JavaScript execution is currently terminating
4488 * because of a call to TerminateExecution. In that case there are
4489 * still JavaScript frames on the stack and the termination
4490 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004491 *
4492 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004493 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004494 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004495
4496 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004497 * Resume execution capability in the given isolate, whose execution
4498 * was previously forcefully terminated using TerminateExecution().
4499 *
4500 * When execution is forcefully terminated using TerminateExecution(),
4501 * the isolate can not resume execution until all JavaScript frames
4502 * have propagated the uncatchable exception which is generated. This
4503 * method allows the program embedding the engine to handle the
4504 * termination event and resume execution capability, even if
4505 * JavaScript frames remain on the stack.
4506 *
4507 * This method can be used by any thread even if that thread has not
4508 * acquired the V8 lock with a Locker object.
4509 *
4510 * \param isolate The isolate in which to resume execution capability.
4511 */
4512 static void CancelTerminateExecution(Isolate* isolate);
4513
4514 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00004515 * Releases any resources used by v8 and stops any utility threads
4516 * that may be running. Note that disposing v8 is permanent, it
4517 * cannot be reinitialized.
4518 *
4519 * It should generally not be necessary to dispose v8 before exiting
4520 * a process, this should happen automatically. It is only necessary
4521 * to use if the process needs the resources taken up by v8.
4522 */
4523 static bool Dispose();
4524
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00004525 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
4526 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004527
4528 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004529 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004530 * heap. GC is not invoked prior to iterating, therefore there is no
4531 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004532 */
4533 static void VisitExternalResources(ExternalResourceVisitor* visitor);
4534
4535 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004536 * Iterates through all the persistent handles in the current isolate's heap
4537 * that have class_ids.
4538 */
4539 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
4540
4541 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004542 * Iterates through all the persistent handles in the current isolate's heap
4543 * that have class_ids and are candidates to be marked as partially dependent
4544 * handles. This will visit handles to young objects created since the last
4545 * garbage collection but is free to visit an arbitrary superset of these
4546 * objects.
4547 */
4548 static void VisitHandlesForPartialDependence(
4549 Isolate* isolate, PersistentHandleVisitor* visitor);
4550
4551 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004552 * Optional notification that the embedder is idle.
4553 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004554 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004555 * Returns true if the embedder should stop calling IdleNotification
4556 * until real work has been done. This indicates that V8 has done
4557 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004558 *
4559 * The hint argument specifies the amount of work to be done in the function
4560 * on scale from 1 to 1000. There is no guarantee that the actual work will
4561 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004562 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004563 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004564
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004565 /**
4566 * Optional notification that the system is running low on memory.
4567 * V8 uses these notifications to attempt to free memory.
4568 */
4569 static void LowMemoryNotification();
4570
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004571 /**
4572 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004573 * these notifications to guide the GC heuristic. Returns the number
4574 * of context disposals - including this one - since the last time
4575 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004576 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004577 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004578
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004579 private:
4580 V8();
4581
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004582 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
4583 internal::Object** handle);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004584 static void DisposeGlobal(internal::Object** global_handle);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004585 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004586 static void MakeWeak(internal::Isolate* isolate,
4587 internal::Object** global_handle,
4588 void* data,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004589 RevivableCallback weak_reference_callback,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004590 NearDeathCallback near_death_callback);
4591 static void ClearWeak(internal::Isolate* isolate,
4592 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004593
4594 template <class T> friend class Handle;
4595 template <class T> friend class Local;
4596 template <class T> friend class Persistent;
4597 friend class Context;
4598};
4599
4600
4601/**
4602 * An external exception handler.
4603 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004604class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004605 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004606 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004607 * Creates a new try/catch block and registers it with v8. Note that
4608 * all TryCatch blocks should be stack allocated because the memory
4609 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004610 */
4611 TryCatch();
4612
4613 /**
4614 * Unregisters and deletes this try/catch block.
4615 */
4616 ~TryCatch();
4617
4618 /**
4619 * Returns true if an exception has been caught by this try/catch block.
4620 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004621 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004622
4623 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004624 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004625 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004626 * If CanContinue returns false, the correct action is to perform any C++
4627 * cleanup needed and then return. If CanContinue returns false and
4628 * HasTerminated returns true, it is possible to call
4629 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004630 */
4631 bool CanContinue() const;
4632
4633 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004634 * Returns true if an exception has been caught due to script execution
4635 * being terminated.
4636 *
4637 * There is no JavaScript representation of an execution termination
4638 * exception. Such exceptions are thrown when the TerminateExecution
4639 * methods are called to terminate a long-running script.
4640 *
4641 * If such an exception has been thrown, HasTerminated will return true,
4642 * indicating that it is possible to call CancelTerminateExecution in order
4643 * to continue calling into the engine.
4644 */
4645 bool HasTerminated() const;
4646
4647 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004648 * Throws the exception caught by this TryCatch in a way that avoids
4649 * it being caught again by this same TryCatch. As with ThrowException
4650 * it is illegal to execute any JavaScript operations after calling
4651 * ReThrow; the caller must return immediately to where the exception
4652 * is caught.
4653 */
4654 Handle<Value> ReThrow();
4655
4656 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004657 * Returns the exception caught by this try/catch block. If no exception has
4658 * been caught an empty handle is returned.
4659 *
4660 * The returned handle is valid until this TryCatch block has been destroyed.
4661 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004662 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004663
4664 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004665 * Returns the .stack property of the thrown object. If no .stack
4666 * property is present an empty handle is returned.
4667 */
4668 Local<Value> StackTrace() const;
4669
4670 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004671 * Returns the message associated with this exception. If there is
4672 * no message associated an empty handle is returned.
4673 *
4674 * The returned handle is valid until this TryCatch block has been
4675 * destroyed.
4676 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004677 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004678
4679 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004680 * Clears any exceptions that may have been caught by this try/catch block.
4681 * After this method has been called, HasCaught() will return false.
4682 *
4683 * It is not necessary to clear a try/catch block before using it again; if
4684 * another exception is thrown the previously caught exception will just be
4685 * overwritten. However, it is often a good idea since it makes it easier
4686 * to determine which operation threw a given exception.
4687 */
4688 void Reset();
4689
v8.team.kasperl727e9952008-09-02 14:56:44 +00004690 /**
4691 * Set verbosity of the external exception handler.
4692 *
4693 * By default, exceptions that are caught by an external exception
4694 * handler are not reported. Call SetVerbose with true on an
4695 * external exception handler to have exceptions caught by the
4696 * handler reported as if they were not caught.
4697 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004698 void SetVerbose(bool value);
4699
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004700 /**
4701 * Set whether or not this TryCatch should capture a Message object
4702 * which holds source information about where the exception
4703 * occurred. True by default.
4704 */
4705 void SetCaptureMessage(bool value);
4706
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004707 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004708 // Make it hard to create heap-allocated TryCatch blocks.
4709 TryCatch(const TryCatch&);
4710 void operator=(const TryCatch&);
4711 void* operator new(size_t size);
4712 void operator delete(void*, size_t);
4713
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004714 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004715 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004716 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004717 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004718 bool is_verbose_ : 1;
4719 bool can_continue_ : 1;
4720 bool capture_message_ : 1;
4721 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004722 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004723
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004724 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004725};
4726
4727
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004728// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004729
4730
4731/**
4732 * Ignore
4733 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004734class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004735 public:
4736 ExtensionConfiguration(int name_count, const char* names[])
4737 : name_count_(name_count), names_(names) { }
4738 private:
4739 friend class ImplementationUtilities;
4740 int name_count_;
4741 const char** names_;
4742};
4743
4744
4745/**
4746 * A sandboxed execution context with its own set of built-in objects
4747 * and functions.
4748 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004749class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004750 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004751 /**
4752 * Returns the global proxy object or global object itself for
4753 * detached contexts.
4754 *
4755 * Global proxy object is a thin wrapper whose prototype points to
4756 * actual context's global object with the properties like Object, etc.
4757 * This is done that way for security reasons (for more details see
4758 * https://wiki.mozilla.org/Gecko:SplitWindow).
4759 *
4760 * Please note that changes to global proxy object prototype most probably
4761 * would break VM---v8 expects only global object as a prototype of
4762 * global proxy object.
4763 *
4764 * If DetachGlobal() has been invoked, Global() would return actual global
4765 * object until global is reattached with ReattachGlobal().
4766 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004767 Local<Object> Global();
4768
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004769 /**
4770 * Detaches the global object from its context before
4771 * the global object can be reused to create a new context.
4772 */
4773 void DetachGlobal();
4774
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00004775 /**
4776 * Reattaches a global object to a context. This can be used to
4777 * restore the connection between a global object and a context
4778 * after DetachGlobal has been called.
4779 *
4780 * \param global_object The global object to reattach to the
4781 * context. For this to work, the global object must be the global
4782 * object that was associated with this context before a call to
4783 * DetachGlobal.
4784 */
4785 void ReattachGlobal(Handle<Object> global_object);
4786
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004787 /**
4788 * Creates a new context and returns a handle to the newly allocated
4789 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004790 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004791 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004792 *
4793 * \param extensions An optional extension configuration containing
4794 * the extensions to be installed in the newly created context.
4795 *
4796 * \param global_template An optional object template from which the
4797 * global object for the newly created context will be created.
4798 *
4799 * \param global_object An optional global object to be reused for
4800 * the newly created context. This global object must have been
4801 * created by a previous call to Context::New with the same global
4802 * template. The state of the global object will be completely reset
4803 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004804 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004805 static Local<Context> New(
4806 Isolate* isolate,
4807 ExtensionConfiguration* extensions = NULL,
4808 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4809 Handle<Value> global_object = Handle<Value>());
4810
4811 /** Deprecated. Use Isolate version instead. */
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004812 V8_DEPRECATED(static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004813 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004814 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004815 Handle<Value> global_object = Handle<Value>()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004816
kasper.lund44510672008-07-25 07:37:58 +00004817 /** Returns the last entered context. */
4818 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004819
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004820 // TODO(svenpanne) Actually deprecate this.
4821 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004822 static Local<Context> GetCurrent();
4823
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004824 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004825 * Returns the context of the calling JavaScript code. That is the
4826 * context of the top-most JavaScript frame. If there are no
4827 * JavaScript frames an empty handle is returned.
4828 */
4829 static Local<Context> GetCalling();
4830
4831 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004832 * Sets the security token for the context. To access an object in
4833 * another context, the security tokens must match.
4834 */
4835 void SetSecurityToken(Handle<Value> token);
4836
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004837 /** Restores the security token to the default value. */
4838 void UseDefaultSecurityToken();
4839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004840 /** Returns the security token of this context.*/
4841 Handle<Value> GetSecurityToken();
4842
v8.team.kasperl727e9952008-09-02 14:56:44 +00004843 /**
4844 * Enter this context. After entering a context, all code compiled
4845 * and run is compiled and run in this context. If another context
4846 * is already entered, this old context is saved so it can be
4847 * restored when the new context is exited.
4848 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004849 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004850
4851 /**
4852 * Exit this context. Exiting the current context restores the
4853 * context that was in place when entering the current context.
4854 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004855 void Exit();
4856
v8.team.kasperl727e9952008-09-02 14:56:44 +00004857 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004858 bool HasOutOfMemoryException();
4859
v8.team.kasperl727e9952008-09-02 14:56:44 +00004860 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004861 static bool InContext();
4862
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004863 /** Returns an isolate associated with a current context. */
4864 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004865
4866 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004867 * Gets the embedder data with the given index, which must have been set by a
4868 * previous call to SetEmbedderData with the same index. Note that index 0
4869 * currently has a special meaning for Chrome's debugger.
4870 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004871 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004872
4873 /**
4874 * Sets the embedder data with the given index, growing the data as
4875 * needed. Note that index 0 currently has a special meaning for Chrome's
4876 * debugger.
4877 */
4878 void SetEmbedderData(int index, Handle<Value> value);
4879
4880 /**
4881 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4882 * index, which must have bees set by a previous call to
4883 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4884 * currently has a special meaning for Chrome's debugger.
4885 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004886 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004887
4888 /**
4889 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4890 * index, growing the data as needed. Note that index 0 currently has a
4891 * special meaning for Chrome's debugger.
4892 */
4893 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004894
4895 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004896 * Control whether code generation from strings is allowed. Calling
4897 * this method with false will disable 'eval' and the 'Function'
4898 * constructor for code running in this context. If 'eval' or the
4899 * 'Function' constructor are used an exception will be thrown.
4900 *
4901 * If code generation from strings is not allowed the
4902 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4903 * set before blocking the call to 'eval' or the 'Function'
4904 * constructor. If that callback returns true, the call will be
4905 * allowed, otherwise an exception will be thrown. If no callback is
4906 * set an exception will be thrown.
4907 */
4908 void AllowCodeGenerationFromStrings(bool allow);
4909
4910 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004911 * Returns true if code generation from strings is allowed for the context.
4912 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4913 */
4914 bool IsCodeGenerationFromStringsAllowed();
4915
4916 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004917 * Sets the error description for the exception that is thrown when
4918 * code generation from strings is not allowed and 'eval' or the 'Function'
4919 * constructor are called.
4920 */
4921 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4922
4923 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004924 * Stack-allocated class which sets the execution context for all
4925 * operations executed within a local scope.
4926 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004927 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004928 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004929 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004930 context_->Enter();
4931 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004932 V8_INLINE(Scope(Isolate* isolate, Persistent<Context>& context)) // NOLINT
4933#ifndef V8_USE_UNSAFE_HANDLES
4934 : context_(Handle<Context>::New(isolate, context)) {
4935#else
4936 : context_(Local<Context>::New(isolate, context)) {
4937#endif
4938 context_->Enter();
4939 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004940 V8_INLINE(~Scope()) { context_->Exit(); }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004941
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004942 private:
4943 Handle<Context> context_;
4944 };
4945
4946 private:
4947 friend class Value;
4948 friend class Script;
4949 friend class Object;
4950 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004951
4952 Local<Value> SlowGetEmbedderData(int index);
4953 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004954};
4955
4956
4957/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004958 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4959 * to use any given V8 isolate, see the comments in the Isolate class. The
4960 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4961 * object pointers obtained from V8 handles while in the particular V8 isolate.
4962 * It is up to the user of V8 to ensure, perhaps with locking, that this
4963 * constraint is not violated. In addition to any other synchronization
4964 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4965 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004966 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004967 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4968 * construction and destruction, the current thread is allowed to use the locked
4969 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4970 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004971 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004972 * Sample usage:
4973* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004974 * ...
4975 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004976 * v8::Locker locker(isolate);
4977 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004978 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004979 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004980 * ...
4981 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00004982 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004983 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004984 * If you wish to stop using V8 in a thread A you can do this either by
4985 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
4986 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004987 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004988 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004989 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004990 * isolate->Exit();
4991 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004992 * ...
4993 * // Code not using V8 goes here while V8 can run in another thread.
4994 * ...
4995 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004996 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004997 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004998 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004999 * The Unlocker object is intended for use in a long-running callback from V8,
5000 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005001 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005002 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
5003 * given thread. This can be useful if you have code that can be called either
5004 * from code that holds the lock or from code that does not. The Unlocker is
5005 * not recursive so you can not have several Unlockers on the stack at once, and
5006 * 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 +00005007 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005008 * An unlocker will unlock several lockers if it has to and reinstate the
5009 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005010 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00005011 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005012 * // V8 not locked.
5013 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005014 * v8::Locker locker(isolate);
5015 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005016 * // V8 locked.
5017 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005018 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005019 * // V8 still locked (2 levels).
5020 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005021 * isolate->Exit();
5022 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005023 * // V8 not locked.
5024 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00005025 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005026 * // V8 locked again (2 levels).
5027 * }
5028 * // V8 still locked (1 level).
5029 * }
5030 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00005031 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005032 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005033class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005034 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005035 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005036 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005037 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005038 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
5039
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005040 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005041 V8_DEPRECATED(Unlocker());
5042
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005043 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00005044 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005045 void Initialize(Isolate* isolate);
5046
lrn@chromium.org1c092762011-05-09 09:42:16 +00005047 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005048};
5049
5050
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005051class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005052 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005053 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005054 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005055 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005056 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
5057
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005058 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005059 V8_DEPRECATED(Locker());
5060
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005061 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005062
5063 /**
5064 * Start preemption.
5065 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005066 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00005067 * that will switch between multiple threads that are in contention
5068 * for the V8 lock.
5069 */
5070 static void StartPreemption(int every_n_ms);
5071
5072 /**
5073 * Stop preemption.
5074 */
5075 static void StopPreemption();
5076
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005077 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005078 * Returns whether or not the locker for a given isolate, is locked by the
5079 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005080 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005081 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005082
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005083 /**
5084 * Returns whether v8::Locker is being used by this V8 instance.
5085 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00005086 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005087
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005088 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005089 void Initialize(Isolate* isolate);
5090
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005091 bool has_lock_;
5092 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005093 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005094
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005095 static bool active_;
5096
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005097 // Disallow copying and assigning.
5098 Locker(const Locker&);
5099 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005100};
5101
5102
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005103/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005104 * A struct for exporting HeapStats data from V8, using "push" model.
5105 */
5106struct HeapStatsUpdate;
5107
5108
5109/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005110 * An interface for exporting data from V8, using "push" model.
5111 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005112class V8EXPORT OutputStream { // NOLINT
5113 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005114 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005115 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005116 };
5117 enum WriteResult {
5118 kContinue = 0,
5119 kAbort = 1
5120 };
5121 virtual ~OutputStream() {}
5122 /** Notify about the end of stream. */
5123 virtual void EndOfStream() = 0;
5124 /** Get preferred output chunk size. Called only once. */
5125 virtual int GetChunkSize() { return 1024; }
5126 /** Get preferred output encoding. Called only once. */
5127 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
5128 /**
5129 * Writes the next chunk of snapshot data into the stream. Writing
5130 * can be stopped by returning kAbort as function result. EndOfStream
5131 * will not be called in case writing was aborted.
5132 */
5133 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005134 /**
5135 * Writes the next chunk of heap stats data into the stream. Writing
5136 * can be stopped by returning kAbort as function result. EndOfStream
5137 * will not be called in case writing was aborted.
5138 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005139 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00005140 return kAbort;
5141 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005142};
5143
5144
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005145/**
5146 * An interface for reporting progress and controlling long-running
5147 * activities.
5148 */
5149class V8EXPORT ActivityControl { // NOLINT
5150 public:
5151 enum ControlOption {
5152 kContinue = 0,
5153 kAbort = 1
5154 };
5155 virtual ~ActivityControl() {}
5156 /**
5157 * Notify about current progress. The activity can be stopped by
5158 * returning kAbort as the callback result.
5159 */
5160 virtual ControlOption ReportProgressValue(int done, int total) = 0;
5161};
5162
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005163
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00005164// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005165
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005166
5167namespace internal {
5168
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00005169const int kApiPointerSize = sizeof(void*); // NOLINT
5170const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005171
5172// Tag information for HeapObject.
5173const int kHeapObjectTag = 1;
5174const int kHeapObjectTagSize = 2;
5175const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
5176
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005177// Tag information for Smi.
5178const int kSmiTag = 0;
5179const int kSmiTagSize = 1;
5180const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
5181
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005182template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005183
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005184template<int kSmiShiftSize>
5185V8_INLINE(internal::Object* IntToSmi(int value)) {
5186 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
5187 intptr_t tagged_value =
5188 (static_cast<intptr_t>(value) << smi_shift_bits) | kSmiTag;
5189 return reinterpret_cast<internal::Object*>(tagged_value);
5190}
5191
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005192// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005193template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005194 static const int kSmiShiftSize = 0;
5195 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005196 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005197 int shift_bits = kSmiTagSize + kSmiShiftSize;
5198 // Throw away top 32 bits and shift down (requires >> to be sign extending).
5199 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
5200 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005201 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5202 return internal::IntToSmi<kSmiShiftSize>(value);
5203 }
5204 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5205 // To be representable as an tagged small integer, the two
5206 // most-significant bits of 'value' must be either 00 or 11 due to
5207 // sign-extension. To check this we add 01 to the two
5208 // most-significant bits, and check if the most-significant bit is 0
5209 //
5210 // CAUTION: The original code below:
5211 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
5212 // may lead to incorrect results according to the C language spec, and
5213 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
5214 // compiler may produce undefined results in case of signed integer
5215 // overflow. The computation must be done w/ unsigned ints.
5216 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
5217 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005218};
5219
5220// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005221template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005222 static const int kSmiShiftSize = 31;
5223 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005224 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005225 int shift_bits = kSmiTagSize + kSmiShiftSize;
5226 // Shift down and throw away top 32 bits.
5227 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
5228 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005229 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5230 return internal::IntToSmi<kSmiShiftSize>(value);
5231 }
5232 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5233 // To be representable as a long smi, the value must be a 32-bit integer.
5234 return (value == static_cast<int32_t>(value));
5235 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005236};
5237
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005238typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
5239const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
5240const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005241
5242/**
5243 * This class exports constants and functionality from within v8 that
5244 * is necessary to implement inline functions in the v8 api. Don't
5245 * depend on functions and constants defined here.
5246 */
5247class Internals {
5248 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005249 // These values match non-compiler-dependent values defined within
5250 // the implementation of v8.
5251 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005252 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005253 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005254
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005255 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00005256 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005257 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005258 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
5259 static const int kContextHeaderSize = 2 * kApiPointerSize;
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005260 static const int kContextEmbedderDataIndex = 64;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005261 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005262 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00005263 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005264 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005265
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005266 static const int kIsolateStateOffset = 0;
5267 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
5268 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
5269 static const int kUndefinedValueRootIndex = 5;
5270 static const int kNullValueRootIndex = 7;
5271 static const int kTrueValueRootIndex = 8;
5272 static const int kFalseValueRootIndex = 9;
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005273 static const int kEmptyStringRootIndex = 129;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005274
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005275 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
5276 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
5277 static const int kNodeStateMask = 0xf;
5278 static const int kNodeStateIsWeakValue = 2;
5279 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005280 static const int kNodeIsIndependentShift = 4;
5281 static const int kNodeIsPartiallyDependentShift = 5;
5282
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005283 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005284 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005285 static const int kOddballType = 0x83;
5286 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005287
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005288 static const int kUndefinedOddballKind = 5;
5289 static const int kNullOddballKind = 3;
5290
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005291 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005292 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
5293 kHeapObjectTag);
5294 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005295
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005296 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005297 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005298 }
5299
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005300 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5301 return PlatformSmiTagging::IntToSmi(value);
5302 }
5303
5304 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5305 return PlatformSmiTagging::IsValidSmi(value);
5306 }
5307
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005308 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005309 typedef internal::Object O;
5310 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
5311 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
5312 }
5313
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005314 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005315 typedef internal::Object O;
5316 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
5317 }
5318
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005319 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005320 int representation = (instance_type & kFullStringRepresentationMask);
5321 return representation == kExternalTwoByteRepresentationTag;
5322 }
5323
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005324 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005325 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
5326 return *reinterpret_cast<int*>(addr) == 1;
5327 }
5328
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005329 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
5330 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5331 return *addr & (1 << shift);
5332 }
5333
5334 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
5335 bool value, int shift)) {
5336 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5337 uint8_t mask = 1 << shift;
5338 *addr = (*addr & ~mask) | (value << shift);
5339 }
5340
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005341 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
5342 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5343 return *addr & kNodeStateMask;
5344 }
5345
5346 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
5347 uint8_t value)) {
5348 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5349 *addr = (*addr & ~kNodeStateMask) | value;
5350 }
5351
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005352 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005353 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5354 kIsolateEmbedderDataOffset;
5355 *reinterpret_cast<void**>(addr) = data;
5356 }
5357
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005358 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005359 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5360 kIsolateEmbedderDataOffset;
5361 return *reinterpret_cast<void**>(addr);
5362 }
5363
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005364 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
5365 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005366 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
5367 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
5368 }
5369
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005370 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005371 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005372 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
5373 return *reinterpret_cast<T*>(addr);
5374 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005375
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005376 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005377 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005378 typedef internal::Object O;
5379 typedef internal::Internals I;
5380 O* ctx = *reinterpret_cast<O**>(context);
5381 int embedder_data_offset = I::kContextHeaderSize +
5382 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
5383 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
5384 int value_offset =
5385 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
5386 return I::ReadField<T>(embedder_data, value_offset);
5387 }
5388
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005389 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
5390 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
5391 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
5392 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
5393 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
5394 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
5395 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005396};
5397
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005398} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005399
5400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005401template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005402Local<T>::Local() : Handle<T>() { }
5403
5404
5405template <class T>
5406Local<T> Local<T>::New(Handle<T> that) {
5407 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005408 T* that_ptr = *that;
5409 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5410 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
5411 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5412 reinterpret_cast<internal::HeapObject*>(*p))));
5413 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005414 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
5415}
5416
5417
5418template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005419Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005420 return New(isolate, that.val_);
5421}
5422
5423#ifndef V8_USE_UNSAFE_HANDLES
5424template <class T>
5425Local<T> Local<T>::New(Isolate* isolate, const Persistent<T>& that) {
5426 return New(isolate, that.val_);
5427}
5428
5429template <class T>
5430Handle<T> Handle<T>::New(Isolate* isolate, T* that) {
5431 if (that == NULL) return Handle<T>();
5432 T* that_ptr = that;
5433 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5434 return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5435 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5436}
5437#endif
5438
5439
5440template <class T>
5441Local<T> Local<T>::New(Isolate* isolate, T* that) {
5442 if (that == NULL) return Local<T>();
5443 T* that_ptr = that;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005444 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5445 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5446 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5447}
5448
5449
5450template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005451Persistent<T> Persistent<T>::New(Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005452 return New(Isolate::GetCurrent(), that.val_);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005453}
5454
5455
5456template <class T>
5457Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005458 return New(Isolate::GetCurrent(), that.val_);
5459}
5460
5461#ifndef V8_USE_UNSAFE_HANDLES
5462template <class T>
5463Persistent<T> Persistent<T>::New(Isolate* isolate, Persistent<T> that) {
5464 return New(Isolate::GetCurrent(), that.val_);
5465}
5466#endif
5467
5468template <class T>
5469Persistent<T> Persistent<T>::New(Isolate* isolate, T* that) {
5470 if (that == NULL) return Persistent<T>();
5471 internal::Object** p = reinterpret_cast<internal::Object**>(that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005472 return Persistent<T>(reinterpret_cast<T*>(
5473 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
5474 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005475}
5476
5477
5478template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005479bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005480 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005481}
5482
5483
5484template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005485bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005486 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005487 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005488 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005489 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005490 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005491}
5492
5493
5494template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005495bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005496 return IsNearDeath(Isolate::GetCurrent());
5497}
5498
5499
5500template <class T>
5501bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
5502 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005503 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005504 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005505 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005506 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005507}
5508
5509
5510template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005511bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005512 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005513}
5514
5515
5516template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005517bool Persistent<T>::IsWeak(Isolate* isolate) 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;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005520 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005521 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005522 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005523}
5524
5525
5526template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005527void Persistent<T>::Dispose() {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005528 if (this->IsEmpty()) return;
5529 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
5530#ifndef V8_USE_UNSAFE_HANDLES
5531 val_ = 0;
5532#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005533}
5534
5535
5536template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005537void Persistent<T>::Dispose(Isolate* isolate) {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005538 Dispose();
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005539}
5540
5541
5542template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005543template <typename S, typename P>
5544void Persistent<T>::MakeWeak(
5545 Isolate* isolate,
5546 P* parameters,
5547 typename WeakReferenceCallbacks<S, P>::Revivable callback) {
5548 TYPE_CHECK(S, T);
5549 typedef typename WeakReferenceCallbacks<Value, void>::Revivable Revivable;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005550 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005551 reinterpret_cast<internal::Object**>(this->val_),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005552 parameters,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005553 reinterpret_cast<Revivable>(callback),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005554 NULL);
5555}
5556
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005557
5558template <class T>
5559template <typename P>
5560void Persistent<T>::MakeWeak(
5561 Isolate* isolate,
5562 P* parameters,
5563 typename WeakReferenceCallbacks<T, P>::Revivable callback) {
5564 MakeWeak<T, P>(isolate, parameters, callback);
5565}
5566
5567
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005568template <class T>
5569void Persistent<T>::MakeWeak(Isolate* isolate,
5570 void* parameters,
5571 NearDeathCallback callback) {
5572 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005573 reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005574 parameters,
5575 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005576 callback);
5577}
5578
5579template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005580void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005581 ClearWeak(Isolate::GetCurrent());
5582}
5583
5584template <class T>
5585void Persistent<T>::ClearWeak(Isolate* isolate) {
5586 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005587 reinterpret_cast<internal::Object**>(this->val_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005588}
5589
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005590template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005591void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005592 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005593}
5594
5595template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005596void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005597 typedef internal::Internals I;
5598 if (this->IsEmpty()) return;
5599 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005600 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005601 true,
5602 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005603}
5604
5605template <class T>
5606void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005607 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005608}
5609
5610template <class T>
5611void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005612 typedef internal::Internals I;
5613 if (this->IsEmpty()) return;
5614 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005615 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005616 true,
5617 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005618}
5619
5620template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005621void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005622 SetWrapperClassId(Isolate::GetCurrent(), class_id);
5623}
5624
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005625
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005626template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005627void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
5628 Dispose(isolate);
5629#ifdef V8_USE_UNSAFE_HANDLES
5630 *this = *New(isolate, other);
5631#else
5632 if (other.IsEmpty()) {
5633 this->val_ = NULL;
5634 return;
5635 }
5636 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5637 this->val_ = reinterpret_cast<T*>(
5638 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5639#endif
5640}
5641
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005642
5643template <class T>
5644T* Persistent<T>::ClearAndLeak() {
5645 T* old;
5646#ifdef V8_USE_UNSAFE_HANDLES
5647 old = **this;
5648 *this = Persistent<T>();
5649#else
5650 old = val_;
5651 val_ = NULL;
5652#endif
5653 return old;
5654}
5655
5656
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005657template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005658void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005659 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005660 if (this->IsEmpty()) return;
5661 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005662 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005663 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5664 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005665}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005666
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005667template <class T>
5668uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005669 return WrapperClassId(Isolate::GetCurrent());
5670}
5671
5672template <class T>
5673uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005674 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005675 if (this->IsEmpty()) return 0;
5676 if (!I::IsInitialized(isolate)) return 0;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005677 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005678 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5679 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005680}
5681
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005682
5683template<typename T>
5684ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
5685
5686template<typename T>
5687void ReturnValue<T>::Set(const Persistent<T>& handle) {
5688 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5689}
5690
5691template<typename T>
5692void ReturnValue<T>::Set(const Handle<T> handle) {
5693 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5694}
5695
5696template<typename T>
5697void ReturnValue<T>::Set(Isolate* isolate, double i) {
5698 Set(Number::New(isolate, i));
5699}
5700
5701template<typename T>
5702void ReturnValue<T>::Set(Isolate* isolate, int32_t i) {
5703 typedef internal::Internals I;
5704 if (V8_LIKELY(I::IsValidSmi(i))) {
5705 *value_ = I::IntToSmi(i);
5706 return;
5707 }
5708 Set(Integer::New(i, isolate));
5709}
5710
5711template<typename T>
5712void ReturnValue<T>::Set(Isolate* isolate, uint32_t i) {
5713 typedef internal::Internals I;
5714 if (V8_LIKELY(I::IsValidSmi(i))) {
5715 *value_ = I::IntToSmi(i);
5716 return;
5717 }
5718 Set(Integer::NewFromUnsigned(i, isolate));
5719}
5720
5721template<typename T>
5722void ReturnValue<T>::Set(Isolate* isolate, bool value) {
5723 if (value) {
5724 SetTrue(isolate);
5725 } else {
5726 SetFalse(isolate);
5727 }
5728}
5729
5730template<typename T>
5731void ReturnValue<T>::SetTrue(Isolate* isolate) {
5732 typedef internal::Internals I;
5733 *value_ = *I::GetRoot(isolate, I::kTrueValueRootIndex);
5734}
5735
5736template<typename T>
5737void ReturnValue<T>::SetFalse(Isolate* isolate) {
5738 typedef internal::Internals I;
5739 *value_ = *I::GetRoot(isolate, I::kFalseValueRootIndex);
5740}
5741
5742template<typename T>
5743void ReturnValue<T>::SetNull(Isolate* isolate) {
5744 typedef internal::Internals I;
5745 *value_ = *I::GetRoot(isolate, I::kNullValueRootIndex);
5746}
5747
5748template<typename T>
5749void ReturnValue<T>::SetUndefined(Isolate* isolate) {
5750 typedef internal::Internals I;
5751 *value_ = *I::GetRoot(isolate, I::kUndefinedValueRootIndex);
5752}
5753
5754
5755template<typename T>
5756FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
5757 internal::Object** values,
5758 int length,
5759 bool is_construct_call)
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005760 : implicit_args_(implicit_args),
5761 values_(values),
5762 length_(length),
5763 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005764
5765
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005766Arguments::Arguments(internal::Object** args,
5767 internal::Object** values,
5768 int length,
5769 bool is_construct_call)
5770 : FunctionCallbackInfo<Value>(args, values, length, is_construct_call) { }
5771
5772
5773template<typename T>
5774Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005775 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
5776 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
5777}
5778
5779
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005780template<typename T>
5781Local<Function> FunctionCallbackInfo<T>::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005782 return Local<Function>(reinterpret_cast<Function*>(
5783 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005784}
5785
5786
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005787template<typename T>
5788Local<Object> FunctionCallbackInfo<T>::This() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005789 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
5790}
5791
5792
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005793template<typename T>
5794Local<Object> FunctionCallbackInfo<T>::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005795 return Local<Object>(reinterpret_cast<Object*>(
5796 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005797}
5798
5799
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005800template<typename T>
5801Local<Value> FunctionCallbackInfo<T>::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005802 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005803}
5804
5805
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005806template<typename T>
5807Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005808 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
5809}
5810
5811
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005812template<typename T>
5813ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
5814 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
5815}
5816
5817
5818template<typename T>
5819bool FunctionCallbackInfo<T>::IsConstructCall() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005820 return is_construct_call_;
5821}
5822
5823
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005824template<typename T>
5825int FunctionCallbackInfo<T>::Length() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005826 return length_;
5827}
5828
5829
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005830template <class T>
5831Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005832 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
5833 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005834 return Local<T>(reinterpret_cast<T*>(after));
5835}
5836
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005837Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005838 return resource_name_;
5839}
5840
5841
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005842Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005843 return resource_line_offset_;
5844}
5845
5846
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005847Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005848 return resource_column_offset_;
5849}
5850
5851
5852Handle<Boolean> Boolean::New(bool value) {
5853 return value ? True() : False();
5854}
5855
5856
5857void Template::Set(const char* name, v8::Handle<Data> value) {
5858 Set(v8::String::New(name), value);
5859}
5860
5861
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005862Local<Value> Object::GetInternalField(int index) {
5863#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005864 typedef internal::Object O;
5865 typedef internal::Internals I;
5866 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005867 // Fast path: If the object is a plain JSObject, which is the common case, we
5868 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005869 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005870 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005871 O* value = I::ReadField<O*>(obj, offset);
5872 O** result = HandleScope::CreateHandle(value);
5873 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005874 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005875#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005876 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005877}
5878
5879
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005880void* Object::GetAlignedPointerFromInternalField(int index) {
5881#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005882 typedef internal::Object O;
5883 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005884 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005885 // Fast path: If the object is a plain JSObject, which is the common case, we
5886 // know where to find the internal fields and can return the value directly.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005887 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005888 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005889 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005890 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005891#endif
5892 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005893}
5894
5895
5896String* String::Cast(v8::Value* value) {
5897#ifdef V8_ENABLE_CHECKS
5898 CheckCast(value);
5899#endif
5900 return static_cast<String*>(value);
5901}
5902
5903
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005904Local<String> String::Empty(Isolate* isolate) {
5905 typedef internal::Object* S;
5906 typedef internal::Internals I;
5907 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005908 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005909 return Local<String>(reinterpret_cast<String*>(slot));
5910}
5911
5912
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00005913Local<String> String::New(const char* data, int length) {
5914 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
5915}
5916
5917
5918Local<String> String::New(const uint16_t* data, int length) {
5919 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
5920}
5921
5922
5923Local<String> String::NewSymbol(const char* data, int length) {
5924 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
5925}
5926
5927
5928Local<String> String::NewUndetectable(const char* data, int length) {
5929 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
5930}
5931
5932
5933Local<String> String::NewUndetectable(const uint16_t* data, int length) {
5934 return NewFromTwoByte(
5935 Isolate::GetCurrent(), data, kUndetectableString, length);
5936}
5937
5938
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005939String::ExternalStringResource* String::GetExternalStringResource() const {
5940 typedef internal::Object O;
5941 typedef internal::Internals I;
5942 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005943 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005944 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005945 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5946 result = reinterpret_cast<String::ExternalStringResource*>(value);
5947 } else {
5948 result = NULL;
5949 }
5950#ifdef V8_ENABLE_CHECKS
5951 VerifyExternalStringResource(result);
5952#endif
5953 return result;
5954}
5955
5956
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005957String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
5958 String::Encoding* encoding_out) const {
5959 typedef internal::Object O;
5960 typedef internal::Internals I;
5961 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
5962 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
5963 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5964 ExternalStringResourceBase* resource = NULL;
5965 if (type == I::kExternalAsciiRepresentationTag ||
5966 type == I::kExternalTwoByteRepresentationTag) {
5967 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5968 resource = static_cast<ExternalStringResourceBase*>(value);
5969 }
5970#ifdef V8_ENABLE_CHECKS
5971 VerifyExternalStringResourceBase(resource, *encoding_out);
5972#endif
5973 return resource;
5974}
5975
5976
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005977bool Value::IsUndefined() const {
5978#ifdef V8_ENABLE_CHECKS
5979 return FullIsUndefined();
5980#else
5981 return QuickIsUndefined();
5982#endif
5983}
5984
5985bool Value::QuickIsUndefined() const {
5986 typedef internal::Object O;
5987 typedef internal::Internals I;
5988 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5989 if (!I::HasHeapObjectTag(obj)) return false;
5990 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5991 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
5992}
5993
5994
5995bool Value::IsNull() const {
5996#ifdef V8_ENABLE_CHECKS
5997 return FullIsNull();
5998#else
5999 return QuickIsNull();
6000#endif
6001}
6002
6003bool Value::QuickIsNull() const {
6004 typedef internal::Object O;
6005 typedef internal::Internals I;
6006 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6007 if (!I::HasHeapObjectTag(obj)) return false;
6008 if (I::GetInstanceType(obj) != I::kOddballType) return false;
6009 return (I::GetOddballKind(obj) == I::kNullOddballKind);
6010}
6011
6012
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006013bool Value::IsString() const {
6014#ifdef V8_ENABLE_CHECKS
6015 return FullIsString();
6016#else
6017 return QuickIsString();
6018#endif
6019}
6020
6021bool Value::QuickIsString() const {
6022 typedef internal::Object O;
6023 typedef internal::Internals I;
6024 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6025 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006026 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006027}
6028
6029
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006030Symbol* Symbol::Cast(v8::Value* value) {
6031#ifdef V8_ENABLE_CHECKS
6032 CheckCast(value);
6033#endif
6034 return static_cast<Symbol*>(value);
6035}
6036
6037
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006038Number* Number::Cast(v8::Value* value) {
6039#ifdef V8_ENABLE_CHECKS
6040 CheckCast(value);
6041#endif
6042 return static_cast<Number*>(value);
6043}
6044
6045
6046Integer* Integer::Cast(v8::Value* value) {
6047#ifdef V8_ENABLE_CHECKS
6048 CheckCast(value);
6049#endif
6050 return static_cast<Integer*>(value);
6051}
6052
6053
6054Date* Date::Cast(v8::Value* value) {
6055#ifdef V8_ENABLE_CHECKS
6056 CheckCast(value);
6057#endif
6058 return static_cast<Date*>(value);
6059}
6060
6061
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006062StringObject* StringObject::Cast(v8::Value* value) {
6063#ifdef V8_ENABLE_CHECKS
6064 CheckCast(value);
6065#endif
6066 return static_cast<StringObject*>(value);
6067}
6068
6069
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006070SymbolObject* SymbolObject::Cast(v8::Value* value) {
6071#ifdef V8_ENABLE_CHECKS
6072 CheckCast(value);
6073#endif
6074 return static_cast<SymbolObject*>(value);
6075}
6076
6077
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006078NumberObject* NumberObject::Cast(v8::Value* value) {
6079#ifdef V8_ENABLE_CHECKS
6080 CheckCast(value);
6081#endif
6082 return static_cast<NumberObject*>(value);
6083}
6084
6085
6086BooleanObject* BooleanObject::Cast(v8::Value* value) {
6087#ifdef V8_ENABLE_CHECKS
6088 CheckCast(value);
6089#endif
6090 return static_cast<BooleanObject*>(value);
6091}
6092
6093
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00006094RegExp* RegExp::Cast(v8::Value* value) {
6095#ifdef V8_ENABLE_CHECKS
6096 CheckCast(value);
6097#endif
6098 return static_cast<RegExp*>(value);
6099}
6100
6101
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006102Object* Object::Cast(v8::Value* value) {
6103#ifdef V8_ENABLE_CHECKS
6104 CheckCast(value);
6105#endif
6106 return static_cast<Object*>(value);
6107}
6108
6109
6110Array* Array::Cast(v8::Value* value) {
6111#ifdef V8_ENABLE_CHECKS
6112 CheckCast(value);
6113#endif
6114 return static_cast<Array*>(value);
6115}
6116
6117
danno@chromium.orgca29dd82013-04-26 11:59:48 +00006118ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
6119#ifdef V8_ENABLE_CHECKS
6120 CheckCast(value);
6121#endif
6122 return static_cast<ArrayBuffer*>(value);
6123}
6124
6125
danno@chromium.orgf005df62013-04-30 16:36:45 +00006126TypedArray* TypedArray::Cast(v8::Value* value) {
6127#ifdef V8_ENABLE_CHECKS
6128 CheckCast(value);
6129#endif
6130 return static_cast<TypedArray*>(value);
6131}
6132
6133
6134Uint8Array* Uint8Array::Cast(v8::Value* value) {
6135#ifdef V8_ENABLE_CHECKS
6136 CheckCast(value);
6137#endif
6138 return static_cast<Uint8Array*>(value);
6139}
6140
6141
6142Int8Array* Int8Array::Cast(v8::Value* value) {
6143#ifdef V8_ENABLE_CHECKS
6144 CheckCast(value);
6145#endif
6146 return static_cast<Int8Array*>(value);
6147}
6148
6149
6150Uint16Array* Uint16Array::Cast(v8::Value* value) {
6151#ifdef V8_ENABLE_CHECKS
6152 CheckCast(value);
6153#endif
6154 return static_cast<Uint16Array*>(value);
6155}
6156
6157
6158Int16Array* Int16Array::Cast(v8::Value* value) {
6159#ifdef V8_ENABLE_CHECKS
6160 CheckCast(value);
6161#endif
6162 return static_cast<Int16Array*>(value);
6163}
6164
6165
6166Uint32Array* Uint32Array::Cast(v8::Value* value) {
6167#ifdef V8_ENABLE_CHECKS
6168 CheckCast(value);
6169#endif
6170 return static_cast<Uint32Array*>(value);
6171}
6172
6173
6174Int32Array* Int32Array::Cast(v8::Value* value) {
6175#ifdef V8_ENABLE_CHECKS
6176 CheckCast(value);
6177#endif
6178 return static_cast<Int32Array*>(value);
6179}
6180
6181
6182Float32Array* Float32Array::Cast(v8::Value* value) {
6183#ifdef V8_ENABLE_CHECKS
6184 CheckCast(value);
6185#endif
6186 return static_cast<Float32Array*>(value);
6187}
6188
6189
6190Float64Array* Float64Array::Cast(v8::Value* value) {
6191#ifdef V8_ENABLE_CHECKS
6192 CheckCast(value);
6193#endif
6194 return static_cast<Float64Array*>(value);
6195}
6196
6197
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006198Function* Function::Cast(v8::Value* value) {
6199#ifdef V8_ENABLE_CHECKS
6200 CheckCast(value);
6201#endif
6202 return static_cast<Function*>(value);
6203}
6204
6205
6206External* External::Cast(v8::Value* value) {
6207#ifdef V8_ENABLE_CHECKS
6208 CheckCast(value);
6209#endif
6210 return static_cast<External*>(value);
6211}
6212
6213
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006214template<typename T>
6215Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
6216 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00006217}
6218
6219
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006220template<typename T>
6221Local<Value> PropertyCallbackInfo<T>::Data() const {
6222 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006223}
6224
6225
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006226template<typename T>
6227Local<Object> PropertyCallbackInfo<T>::This() const {
6228 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006229}
6230
6231
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006232template<typename T>
6233Local<Object> PropertyCallbackInfo<T>::Holder() const {
6234 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
6235}
6236
6237
6238template<typename T>
6239ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
6240 return ReturnValue<T>(&args_[kReturnValueIndex]);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006241}
6242
6243
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00006244Handle<Primitive> Undefined(Isolate* isolate) {
6245 typedef internal::Object* S;
6246 typedef internal::Internals I;
6247 if (!I::IsInitialized(isolate)) return Undefined();
6248 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
6249 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6250}
6251
6252
6253Handle<Primitive> Null(Isolate* isolate) {
6254 typedef internal::Object* S;
6255 typedef internal::Internals I;
6256 if (!I::IsInitialized(isolate)) return Null();
6257 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
6258 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6259}
6260
6261
6262Handle<Boolean> True(Isolate* isolate) {
6263 typedef internal::Object* S;
6264 typedef internal::Internals I;
6265 if (!I::IsInitialized(isolate)) return True();
6266 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
6267 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6268}
6269
6270
6271Handle<Boolean> False(Isolate* isolate) {
6272 typedef internal::Object* S;
6273 typedef internal::Internals I;
6274 if (!I::IsInitialized(isolate)) return False();
6275 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
6276 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6277}
6278
6279
6280void Isolate::SetData(void* data) {
6281 typedef internal::Internals I;
6282 I::SetEmbedderData(this, data);
6283}
6284
6285
6286void* Isolate::GetData() {
6287 typedef internal::Internals I;
6288 return I::GetEmbedderData(this);
6289}
6290
6291
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00006292Local<Value> Context::GetEmbedderData(int index) {
6293#ifndef V8_ENABLE_CHECKS
6294 typedef internal::Object O;
6295 typedef internal::Internals I;
6296 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
6297 return Local<Value>(reinterpret_cast<Value*>(result));
6298#else
6299 return SlowGetEmbedderData(index);
6300#endif
6301}
6302
6303
6304void* Context::GetAlignedPointerFromEmbedderData(int index) {
6305#ifndef V8_ENABLE_CHECKS
6306 typedef internal::Internals I;
6307 return I::ReadEmbedderData<void*>(this, index);
6308#else
6309 return SlowGetAlignedPointerFromEmbedderData(index);
6310#endif
6311}
6312
6313
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006314/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00006315 * \example shell.cc
6316 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006317 * command-line and executes them.
6318 */
6319
6320
6321/**
6322 * \example process.cc
6323 */
6324
6325
6326} // namespace v8
6327
6328
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006329#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006330#undef TYPE_CHECK
6331
6332
ager@chromium.org9258b6b2008-09-11 09:11:10 +00006333#endif // V8_H_