blob: b3dff3fee107a6e3992e925b488d8ba760723e50 [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
2328
2329/**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002330 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
2331 * This API is experimental and may change significantly.
2332 */
2333class V8EXPORT ArrayBuffer : public Object {
2334 public:
2335 /**
2336 * Data length in bytes.
2337 */
2338 size_t ByteLength() const;
2339 /**
2340 * Raw pointer to the array buffer data
2341 */
2342 void* Data() const;
2343
2344 /**
2345 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
2346 * Allocated memory will be owned by a created ArrayBuffer and
2347 * will be deallocated when it is garbage-collected.
2348 */
2349 static Local<ArrayBuffer> New(size_t byte_length);
2350
2351 /**
2352 * Create a new ArrayBuffer over an existing memory block.
2353 * The memory block will not be reclaimed when a created ArrayBuffer
2354 * is garbage-collected.
2355 */
2356 static Local<ArrayBuffer> New(void* data, size_t byte_length);
2357
2358 V8_INLINE(static ArrayBuffer* Cast(Value* obj));
2359
2360 private:
2361 ArrayBuffer();
2362 static void CheckCast(Value* obj);
2363};
2364
2365
2366/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002367 * A base class for an instance of TypedArray series of constructors
2368 * (ES6 draft 15.13.6).
2369 * This API is experimental and may change significantly.
2370 */
2371class V8EXPORT TypedArray : public Object {
2372 public:
2373 /**
2374 * Returns underlying ArrayBuffer.
2375 */
2376 Local<ArrayBuffer> Buffer();
2377 /**
2378 * Byte offset in |Buffer|
2379 */
2380 size_t ByteOffset();
2381 /**
2382 * Numbe of elements in this typed array.
2383 */
2384 size_t Length();
2385 /**
2386 * Size of typed array in bytes (e.g. for Int16Array, 2*|Length|).
2387 */
2388 size_t ByteLength();
2389 /**
2390 * Base address of typed array.
2391 */
2392 void* BaseAddress();
2393
2394 V8_INLINE(static TypedArray* Cast(Value* obj));
2395
2396 private:
2397 TypedArray();
2398 static void CheckCast(Value* obj);
2399};
2400
2401
2402/**
2403 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
2404 * This API is experimental and may change significantly.
2405 */
2406class V8EXPORT Uint8Array : public TypedArray {
2407 public:
2408 static Local<Uint8Array> New(Handle<ArrayBuffer> array_buffer,
2409 size_t byte_offset, size_t length);
2410 V8_INLINE(static Uint8Array* Cast(Value* obj));
2411
2412 private:
2413 Uint8Array();
2414 static void CheckCast(Value* obj);
2415};
2416
2417
2418/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002419 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
2420 * This API is experimental and may change significantly.
2421 */
2422class V8EXPORT Uint8ClampedArray : public TypedArray {
2423 public:
2424 static Local<Uint8ClampedArray> New(Handle<ArrayBuffer> array_buffer,
2425 size_t byte_offset, size_t length);
2426 V8_INLINE(static Uint8ClampedArray* Cast(Value* obj));
2427
2428 private:
2429 Uint8ClampedArray();
2430 static void CheckCast(Value* obj);
2431};
2432
2433/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002434 * An instance of Int8Array constructor (ES6 draft 15.13.6).
2435 * This API is experimental and may change significantly.
2436 */
2437class V8EXPORT Int8Array : public TypedArray {
2438 public:
2439 static Local<Int8Array> New(Handle<ArrayBuffer> array_buffer,
2440 size_t byte_offset, size_t length);
2441 V8_INLINE(static Int8Array* Cast(Value* obj));
2442
2443 private:
2444 Int8Array();
2445 static void CheckCast(Value* obj);
2446};
2447
2448
2449/**
2450 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
2451 * This API is experimental and may change significantly.
2452 */
2453class V8EXPORT Uint16Array : public TypedArray {
2454 public:
2455 static Local<Uint16Array> New(Handle<ArrayBuffer> array_buffer,
2456 size_t byte_offset, size_t length);
2457 V8_INLINE(static Uint16Array* Cast(Value* obj));
2458
2459 private:
2460 Uint16Array();
2461 static void CheckCast(Value* obj);
2462};
2463
2464
2465/**
2466 * An instance of Int16Array constructor (ES6 draft 15.13.6).
2467 * This API is experimental and may change significantly.
2468 */
2469class V8EXPORT Int16Array : public TypedArray {
2470 public:
2471 static Local<Int16Array> New(Handle<ArrayBuffer> array_buffer,
2472 size_t byte_offset, size_t length);
2473 V8_INLINE(static Int16Array* Cast(Value* obj));
2474
2475 private:
2476 Int16Array();
2477 static void CheckCast(Value* obj);
2478};
2479
2480
2481/**
2482 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
2483 * This API is experimental and may change significantly.
2484 */
2485class V8EXPORT Uint32Array : public TypedArray {
2486 public:
2487 static Local<Uint32Array> New(Handle<ArrayBuffer> array_buffer,
2488 size_t byte_offset, size_t length);
2489 V8_INLINE(static Uint32Array* Cast(Value* obj));
2490
2491 private:
2492 Uint32Array();
2493 static void CheckCast(Value* obj);
2494};
2495
2496
2497/**
2498 * An instance of Int32Array constructor (ES6 draft 15.13.6).
2499 * This API is experimental and may change significantly.
2500 */
2501class V8EXPORT Int32Array : public TypedArray {
2502 public:
2503 static Local<Int32Array> New(Handle<ArrayBuffer> array_buffer,
2504 size_t byte_offset, size_t length);
2505 V8_INLINE(static Int32Array* Cast(Value* obj));
2506
2507 private:
2508 Int32Array();
2509 static void CheckCast(Value* obj);
2510};
2511
2512
2513/**
2514 * An instance of Float32Array constructor (ES6 draft 15.13.6).
2515 * This API is experimental and may change significantly.
2516 */
2517class V8EXPORT Float32Array : public TypedArray {
2518 public:
2519 static Local<Float32Array> New(Handle<ArrayBuffer> array_buffer,
2520 size_t byte_offset, size_t length);
2521 V8_INLINE(static Float32Array* Cast(Value* obj));
2522
2523 private:
2524 Float32Array();
2525 static void CheckCast(Value* obj);
2526};
2527
2528
2529/**
2530 * An instance of Float64Array constructor (ES6 draft 15.13.6).
2531 * This API is experimental and may change significantly.
2532 */
2533class V8EXPORT Float64Array : public TypedArray {
2534 public:
2535 static Local<Float64Array> New(Handle<ArrayBuffer> array_buffer,
2536 size_t byte_offset, size_t length);
2537 V8_INLINE(static Float64Array* Cast(Value* obj));
2538
2539 private:
2540 Float64Array();
2541 static void CheckCast(Value* obj);
2542};
2543
2544
2545/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002546 * An instance of the built-in Date constructor (ECMA-262, 15.9).
2547 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002548class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002549 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002550 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002551
2552 /**
2553 * A specialization of Value::NumberValue that is more efficient
2554 * because we know the structure of this object.
2555 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002556 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002557
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002558 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002559
2560 /**
2561 * Notification that the embedder has changed the time zone,
2562 * daylight savings time, or other date / time configuration
2563 * parameters. V8 keeps a cache of various values used for
2564 * date / time computation. This notification will reset
2565 * those cached values for the current context so that date /
2566 * time configuration changes would be reflected in the Date
2567 * object.
2568 *
2569 * This API should not be called more than needed as it will
2570 * negatively impact the performance of date operations.
2571 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002572 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002573
2574 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002575 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002576};
2577
2578
2579/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002580 * A Number object (ECMA-262, 4.3.21).
2581 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002582class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002583 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002584 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002585
2586 /**
2587 * Returns the Number held by the object.
2588 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002589 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002590
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002591 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002592
2593 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002594 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002595};
2596
2597
2598/**
2599 * A Boolean object (ECMA-262, 4.3.15).
2600 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002601class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002602 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002603 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002604
2605 /**
2606 * Returns the Boolean held by the object.
2607 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002608 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002609
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002610 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002611
2612 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002613 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002614};
2615
2616
2617/**
2618 * A String object (ECMA-262, 4.3.18).
2619 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002620class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002621 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002622 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002623
2624 /**
2625 * Returns the String held by the object.
2626 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002627 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002628
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002629 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002630
2631 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002632 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002633};
2634
2635
2636/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002637 * A Symbol object (ECMA-262 edition 6).
2638 *
2639 * This is an experimental feature. Use at your own risk.
2640 */
2641class V8EXPORT SymbolObject : public Object {
2642 public:
2643 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2644
2645 /**
2646 * Returns the Symbol held by the object.
2647 */
2648 Local<Symbol> SymbolValue() const;
2649
2650 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2651
2652 private:
2653 static void CheckCast(v8::Value* obj);
2654};
2655
2656
2657/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002658 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2659 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002660class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002661 public:
2662 /**
2663 * Regular expression flag bits. They can be or'ed to enable a set
2664 * of flags.
2665 */
2666 enum Flags {
2667 kNone = 0,
2668 kGlobal = 1,
2669 kIgnoreCase = 2,
2670 kMultiline = 4
2671 };
2672
2673 /**
2674 * Creates a regular expression from the given pattern string and
2675 * the flags bit field. May throw a JavaScript exception as
2676 * described in ECMA-262, 15.10.4.1.
2677 *
2678 * For example,
2679 * RegExp::New(v8::String::New("foo"),
2680 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2681 * is equivalent to evaluating "/foo/gm".
2682 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002683 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002684
2685 /**
2686 * Returns the value of the source property: a string representing
2687 * the regular expression.
2688 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002689 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002690
2691 /**
2692 * Returns the flags bit field.
2693 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002694 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002695
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002696 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002697
2698 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002699 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002700};
2701
2702
2703/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002704 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2705 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002707class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002709 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002710 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002711 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002712 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002713 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714};
2715
2716
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002717// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718
2719
2720/**
2721 * The superclass of object and function templates.
2722 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002723class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 public:
2725 /** Adds a property to each instance created by this template.*/
2726 void Set(Handle<String> name, Handle<Data> value,
2727 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002728 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729 private:
2730 Template();
2731
2732 friend class ObjectTemplate;
2733 friend class FunctionTemplate;
2734};
2735
2736
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002737template<typename T>
2738class V8EXPORT ReturnValue {
2739 public:
2740 V8_INLINE(explicit ReturnValue(internal::Object** slot));
2741 // Handle setters
2742 V8_INLINE(void Set(const Persistent<T>& handle));
2743 V8_INLINE(void Set(const Handle<T> handle));
2744 // Fast primitive setters
2745 V8_INLINE(void Set(Isolate* isolate, bool value));
2746 V8_INLINE(void Set(Isolate* isolate, double i));
2747 V8_INLINE(void Set(Isolate* isolate, int32_t i));
2748 V8_INLINE(void Set(Isolate* isolate, uint32_t i));
2749 // Fast JS primitive setters
2750 V8_INLINE(void SetNull(Isolate* isolate));
2751 V8_INLINE(void SetUndefined(Isolate* isolate));
2752 private:
2753 V8_INLINE(void SetTrue(Isolate* isolate));
2754 V8_INLINE(void SetFalse(Isolate* isolate));
2755 internal::Object** value_;
2756};
2757
2758
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002759/**
2760 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002761 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762 * including the receiver, the number and values of arguments, and
2763 * the holder of the function.
2764 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002765template<typename T>
2766class V8EXPORT FunctionCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002768 V8_INLINE(int Length() const);
2769 V8_INLINE(Local<Value> operator[](int i) const);
2770 V8_INLINE(Local<Function> Callee() const);
2771 V8_INLINE(Local<Object> This() const);
2772 V8_INLINE(Local<Object> Holder() const);
2773 V8_INLINE(bool IsConstructCall() const);
2774 V8_INLINE(Local<Value> Data() const);
2775 V8_INLINE(Isolate* GetIsolate() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002776 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2777 // This shouldn't be public, but the arm compiler needs it.
2778 static const int kArgsLength = 5;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002779
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002780 protected:
2781 friend class internal::FunctionCallbackArguments;
2782 friend class internal::CustomArguments<FunctionCallbackInfo>;
2783 static const int kReturnValueIndex = 0;
2784 static const int kIsolateIndex = -1;
2785 static const int kDataIndex = -2;
2786 static const int kCalleeIndex = -3;
2787 static const int kHolderIndex = -4;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002788
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002789 V8_INLINE(FunctionCallbackInfo(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002790 internal::Object** values,
2791 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002792 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002793 internal::Object** implicit_args_;
2794 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002795 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002796 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797};
2798
2799
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002800class V8EXPORT Arguments : public FunctionCallbackInfo<Value> {
2801 private:
2802 friend class internal::FunctionCallbackArguments;
2803 V8_INLINE(Arguments(internal::Object** implicit_args,
2804 internal::Object** values,
2805 int length,
2806 bool is_construct_call));
2807};
2808
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809/**
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002810 * The information passed to a property callback about the context
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811 * of the property access.
2812 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002813template<typename T>
2814class V8EXPORT PropertyCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002816 V8_INLINE(Isolate* GetIsolate() const);
2817 V8_INLINE(Local<Value> Data() const);
2818 V8_INLINE(Local<Object> This() const);
2819 V8_INLINE(Local<Object> Holder() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002820 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2821 // This shouldn't be public, but the arm compiler needs it.
2822 static const int kArgsLength = 5;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002823
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002824 protected:
2825 friend class MacroAssembler;
2826 friend class internal::PropertyCallbackArguments;
2827 friend class internal::CustomArguments<PropertyCallbackInfo>;
2828 static const int kThisIndex = 0;
2829 static const int kHolderIndex = -1;
2830 static const int kDataIndex = -2;
2831 static const int kIsolateIndex = -3;
2832 static const int kReturnValueIndex = -4;
2833
2834 V8_INLINE(PropertyCallbackInfo(internal::Object** args))
2835 : args_(args) { }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002836 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837};
2838
2839
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002840class V8EXPORT AccessorInfo : public PropertyCallbackInfo<Value> {
2841 private:
2842 friend class internal::PropertyCallbackArguments;
2843 V8_INLINE(AccessorInfo(internal::Object** args))
2844 : PropertyCallbackInfo<Value>(args) { }
2845};
2846
2847
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002848typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002849typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002850
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002852 * NamedProperty[Getter|Setter] are used as interceptors on object.
2853 * See ObjectTemplate::SetNamedPropertyHandler.
2854 */
2855typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2856 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002857typedef void (*NamedPropertyGetterCallback)(
2858 Local<String> property,
2859 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002860
2861
2862/**
2863 * Returns the value if the setter intercepts the request.
2864 * Otherwise, returns an empty handle.
2865 */
2866typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2867 Local<Value> value,
2868 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002869typedef void (*NamedPropertySetterCallback)(
2870 Local<String> property,
2871 Local<Value> value,
2872 const PropertyCallbackInfo<Value>& info);
2873
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875/**
2876 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002877 * The result is an integer encoding property attributes (like v8::None,
2878 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002880typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2881 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002882typedef void (*NamedPropertyQueryCallback)(
2883 Local<String> property,
2884 const PropertyCallbackInfo<Integer>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002885
2886
2887/**
2888 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002889 * The return value is true if the property could be deleted and false
2890 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891 */
2892typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2893 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002894typedef void (*NamedPropertyDeleterCallback)(
2895 Local<String> property,
2896 const PropertyCallbackInfo<Boolean>& info);
2897
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002898
2899/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002900 * Returns an array containing the names of the properties the named
2901 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 */
2903typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002904typedef void (*NamedPropertyEnumeratorCallback)(
2905 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002906
v8.team.kasperl727e9952008-09-02 14:56:44 +00002907
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002909 * Returns the value of the property if the getter intercepts the
2910 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911 */
2912typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2913 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002914typedef void (*IndexedPropertyGetterCallback)(
2915 uint32_t index,
2916 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917
2918
2919/**
2920 * Returns the value if the setter intercepts the request.
2921 * Otherwise, returns an empty handle.
2922 */
2923typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2924 Local<Value> value,
2925 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002926typedef void (*IndexedPropertySetterCallback)(
2927 uint32_t index,
2928 Local<Value> value,
2929 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002930
2931
2932/**
2933 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002934 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002936typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2937 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002938typedef void (*IndexedPropertyQueryCallback)(
2939 uint32_t index,
2940 const PropertyCallbackInfo<Integer>& info);
2941
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002942
2943/**
2944 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002945 * The return value is true if the property could be deleted and false
2946 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947 */
2948typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2949 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002950typedef void (*IndexedPropertyDeleterCallback)(
2951 uint32_t index,
2952 const PropertyCallbackInfo<Boolean>& info);
2953
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002954
v8.team.kasperl727e9952008-09-02 14:56:44 +00002955/**
2956 * Returns an array containing the indices of the properties the
2957 * indexed property getter intercepts.
2958 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002960typedef void (*IndexedPropertyEnumeratorCallback)(
2961 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002962
2963
2964/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002965 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002966 */
2967enum AccessType {
2968 ACCESS_GET,
2969 ACCESS_SET,
2970 ACCESS_HAS,
2971 ACCESS_DELETE,
2972 ACCESS_KEYS
2973};
2974
v8.team.kasperl727e9952008-09-02 14:56:44 +00002975
2976/**
2977 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002978 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002979 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002980typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981 Local<Value> key,
2982 AccessType type,
2983 Local<Value> data);
2984
v8.team.kasperl727e9952008-09-02 14:56:44 +00002985
2986/**
2987 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002988 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002989 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002990typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002991 uint32_t index,
2992 AccessType type,
2993 Local<Value> data);
2994
2995
2996/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002997 * A FunctionTemplate is used to create functions at runtime. There
2998 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002999 * context. The lifetime of the created function is equal to the
3000 * lifetime of the context. So in case the embedder needs to create
3001 * temporary functions that can be collected using Scripts is
3002 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003 *
3004 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003005 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003006 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003007 * A FunctionTemplate has a corresponding instance template which is
3008 * used to create object instances when the function is used as a
3009 * constructor. Properties added to the instance template are added to
3010 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011 *
3012 * A FunctionTemplate can have a prototype template. The prototype template
3013 * is used to create the prototype object of the function.
3014 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003015 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003016 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003017 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003018 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
3019 * t->Set("func_property", v8::Number::New(1));
3020 *
3021 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
3022 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
3023 * proto_t->Set("proto_const", v8::Number::New(2));
3024 *
3025 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
3026 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
3027 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
3028 * instance_t->Set("instance_property", Number::New(3));
3029 *
3030 * v8::Local<v8::Function> function = t->GetFunction();
3031 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003032 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003033 *
3034 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00003035 * and "instance" for the instance object created above. The function
3036 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003037 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003038 * \code
3039 * func_property in function == true;
3040 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003041 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003042 * function.prototype.proto_method() invokes 'InvokeCallback'
3043 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003044 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003045 * instance instanceof function == true;
3046 * instance.instance_accessor calls 'InstanceAccessorCallback'
3047 * instance.instance_property == 3;
3048 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003049 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003050 * A FunctionTemplate can inherit from another one by calling the
3051 * FunctionTemplate::Inherit method. The following graph illustrates
3052 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003054 * \code
3055 * FunctionTemplate Parent -> Parent() . prototype -> { }
3056 * ^ ^
3057 * | Inherit(Parent) | .__proto__
3058 * | |
3059 * FunctionTemplate Child -> Child() . prototype -> { }
3060 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003062 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
3063 * object of the Child() function has __proto__ pointing to the
3064 * Parent() function's prototype object. An instance of the Child
3065 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003066 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003067 * Let Parent be the FunctionTemplate initialized in the previous
3068 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003070 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071 * Local<FunctionTemplate> parent = t;
3072 * Local<FunctionTemplate> child = FunctionTemplate::New();
3073 * child->Inherit(parent);
3074 *
3075 * Local<Function> child_function = child->GetFunction();
3076 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003077 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003078 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003079 * The Child function and Child instance will have the following
3080 * properties:
3081 *
3082 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003083 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003084 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003086 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003087 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003088class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003089 public:
3090 /** Creates a function template.*/
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003091 // TODO(dcarney): deprecate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003092 static Local<FunctionTemplate> New(
3093 InvocationCallback callback = 0,
3094 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003095 Handle<Signature> signature = Handle<Signature>(),
3096 int length = 0);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003097 static Local<FunctionTemplate> New(
3098 FunctionCallback callback, // TODO(dcarney): add back default param.
3099 Handle<Value> data = Handle<Value>(),
3100 Handle<Signature> signature = Handle<Signature>(),
3101 int length = 0);
3102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003103 /** Returns the unique function instance in the current execution context.*/
3104 Local<Function> GetFunction();
3105
v8.team.kasperl727e9952008-09-02 14:56:44 +00003106 /**
3107 * Set the call-handler callback for a FunctionTemplate. This
3108 * callback is called whenever the function created from this
3109 * FunctionTemplate is called.
3110 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003111 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003112 void SetCallHandler(InvocationCallback callback,
3113 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003114 void SetCallHandler(FunctionCallback callback,
3115 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003116
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003117 /** Set the predefined length property for the FunctionTemplate. */
3118 void SetLength(int length);
3119
v8.team.kasperl727e9952008-09-02 14:56:44 +00003120 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003121 Local<ObjectTemplate> InstanceTemplate();
3122
3123 /** Causes the function template to inherit from a parent function template.*/
3124 void Inherit(Handle<FunctionTemplate> parent);
3125
3126 /**
3127 * A PrototypeTemplate is the template used to create the prototype object
3128 * of the function created by this template.
3129 */
3130 Local<ObjectTemplate> PrototypeTemplate();
3131
v8.team.kasperl727e9952008-09-02 14:56:44 +00003132 /**
3133 * Set the class name of the FunctionTemplate. This is used for
3134 * printing objects created with the function created from the
3135 * FunctionTemplate as its constructor.
3136 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003137 void SetClassName(Handle<String> name);
3138
3139 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003140 * Determines whether the __proto__ accessor ignores instances of
3141 * the function template. If instances of the function template are
3142 * ignored, __proto__ skips all instances and instead returns the
3143 * next object in the prototype chain.
3144 *
3145 * Call with a value of true to make the __proto__ accessor ignore
3146 * instances of the function template. Call with a value of false
3147 * to make the __proto__ accessor not ignore instances of the
3148 * function template. By default, instances of a function template
3149 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003150 */
3151 void SetHiddenPrototype(bool value);
3152
3153 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003154 * Sets the ReadOnly flag in the attributes of the 'prototype' property
3155 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00003156 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003157 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00003158
3159 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003160 * Returns true if the given object is an instance of this function
3161 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003162 */
3163 bool HasInstance(Handle<Value> object);
3164
3165 private:
3166 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003167 friend class Context;
3168 friend class ObjectTemplate;
3169};
3170
3171
3172/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003173 * An ObjectTemplate is used to create objects at runtime.
3174 *
3175 * Properties added to an ObjectTemplate are added to each object
3176 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003177 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003178class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003179 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003180 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003181 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003183 /** Creates a new instance of this template.*/
3184 Local<Object> NewInstance();
3185
3186 /**
3187 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003188 *
3189 * Whenever the property with the given name is accessed on objects
3190 * created from this ObjectTemplate the getter and setter callbacks
3191 * are called instead of getting and setting the property directly
3192 * on the JavaScript object.
3193 *
3194 * \param name The name of the property for which an accessor is added.
3195 * \param getter The callback to invoke when getting the property.
3196 * \param setter The callback to invoke when setting the property.
3197 * \param data A piece of data that will be passed to the getter and setter
3198 * callbacks whenever they are invoked.
3199 * \param settings Access control settings for the accessor. This is a bit
3200 * field consisting of one of more of
3201 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
3202 * The default is to not allow cross-context access.
3203 * ALL_CAN_READ means that all cross-context reads are allowed.
3204 * ALL_CAN_WRITE means that all cross-context writes are allowed.
3205 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
3206 * cross-context access.
3207 * \param attribute The attributes of the property for which an accessor
3208 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003209 * \param signature The signature describes valid receivers for the accessor
3210 * and is used to perform implicit instance checks against them. If the
3211 * receiver is incompatible (i.e. is not an instance of the constructor as
3212 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
3213 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003214 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003215 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003216 void SetAccessor(Handle<String> name,
3217 AccessorGetter getter,
3218 AccessorSetter setter = 0,
3219 Handle<Value> data = Handle<Value>(),
3220 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003221 PropertyAttribute attribute = None,
3222 Handle<AccessorSignature> signature =
3223 Handle<AccessorSignature>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003224 void SetAccessor(Handle<String> name,
3225 AccessorGetterCallback getter,
3226 AccessorSetterCallback setter = 0,
3227 Handle<Value> data = Handle<Value>(),
3228 AccessControl settings = DEFAULT,
3229 PropertyAttribute attribute = None,
3230 Handle<AccessorSignature> signature =
3231 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232
ulan@chromium.org750145a2013-03-07 15:14:13 +00003233 // This function is not yet stable and should not be used at this time.
3234 bool SetAccessor(Handle<String> name,
3235 Handle<DeclaredAccessorDescriptor> descriptor,
3236 AccessControl settings = DEFAULT,
3237 PropertyAttribute attribute = None,
3238 Handle<AccessorSignature> signature =
3239 Handle<AccessorSignature>());
3240
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241 /**
3242 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003243 *
3244 * Whenever a named property is accessed on objects created from
3245 * this object template, the provided callback is invoked instead of
3246 * accessing the property directly on the JavaScript object.
3247 *
3248 * \param getter The callback to invoke when getting a property.
3249 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003250 * \param query The callback to invoke to check if a property is present,
3251 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003252 * \param deleter The callback to invoke when deleting a property.
3253 * \param enumerator The callback to invoke to enumerate all the named
3254 * properties of an object.
3255 * \param data A piece of data that will be passed to the callbacks
3256 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003258 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003259 void SetNamedPropertyHandler(NamedPropertyGetter getter,
3260 NamedPropertySetter setter = 0,
3261 NamedPropertyQuery query = 0,
3262 NamedPropertyDeleter deleter = 0,
3263 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003264 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003265 void SetNamedPropertyHandler(
3266 NamedPropertyGetterCallback getter,
3267 NamedPropertySetterCallback setter = 0,
3268 NamedPropertyQueryCallback query = 0,
3269 NamedPropertyDeleterCallback deleter = 0,
3270 NamedPropertyEnumeratorCallback enumerator = 0,
3271 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003272
3273 /**
3274 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003275 *
3276 * Whenever an indexed property is accessed on objects created from
3277 * this object template, the provided callback is invoked instead of
3278 * accessing the property directly on the JavaScript object.
3279 *
3280 * \param getter The callback to invoke when getting a property.
3281 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003282 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003283 * \param deleter The callback to invoke when deleting a property.
3284 * \param enumerator The callback to invoke to enumerate all the indexed
3285 * properties of an object.
3286 * \param data A piece of data that will be passed to the callbacks
3287 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003289 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003290 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
3291 IndexedPropertySetter setter = 0,
3292 IndexedPropertyQuery query = 0,
3293 IndexedPropertyDeleter deleter = 0,
3294 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003295 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003296 void SetIndexedPropertyHandler(
3297 IndexedPropertyGetterCallback getter,
3298 IndexedPropertySetterCallback setter = 0,
3299 IndexedPropertyQueryCallback query = 0,
3300 IndexedPropertyDeleterCallback deleter = 0,
3301 IndexedPropertyEnumeratorCallback enumerator = 0,
3302 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003303
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003304 /**
3305 * Sets the callback to be used when calling instances created from
3306 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00003307 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003308 * function.
3309 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003310 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003311 void SetCallAsFunctionHandler(InvocationCallback callback,
3312 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003313 void SetCallAsFunctionHandler(FunctionCallback callback,
3314 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003315
v8.team.kasperl727e9952008-09-02 14:56:44 +00003316 /**
3317 * Mark object instances of the template as undetectable.
3318 *
3319 * In many ways, undetectable objects behave as though they are not
3320 * there. They behave like 'undefined' in conditionals and when
3321 * printed. However, properties can be accessed and called as on
3322 * normal objects.
3323 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003324 void MarkAsUndetectable();
3325
v8.team.kasperl727e9952008-09-02 14:56:44 +00003326 /**
3327 * Sets access check callbacks on the object template.
3328 *
3329 * When accessing properties on instances of this object template,
3330 * the access check callback will be called to determine whether or
3331 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003332 * The last parameter specifies whether access checks are turned
3333 * on by default on instances. If access checks are off by default,
3334 * they can be turned on on individual instances by calling
3335 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00003336 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003337 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
3338 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003339 Handle<Value> data = Handle<Value>(),
3340 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003341
kasper.lund212ac232008-07-16 07:07:30 +00003342 /**
3343 * Gets the number of internal fields for objects generated from
3344 * this template.
3345 */
3346 int InternalFieldCount();
3347
3348 /**
3349 * Sets the number of internal fields for objects generated from
3350 * this template.
3351 */
3352 void SetInternalFieldCount(int value);
3353
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003354 private:
3355 ObjectTemplate();
3356 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
3357 friend class FunctionTemplate;
3358};
3359
3360
3361/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003362 * A Signature specifies which receivers and arguments are valid
3363 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003364 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003365class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003366 public:
3367 static Local<Signature> New(Handle<FunctionTemplate> receiver =
3368 Handle<FunctionTemplate>(),
3369 int argc = 0,
3370 Handle<FunctionTemplate> argv[] = 0);
3371 private:
3372 Signature();
3373};
3374
3375
3376/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003377 * An AccessorSignature specifies which receivers are valid parameters
3378 * to an accessor callback.
3379 */
3380class V8EXPORT AccessorSignature : public Data {
3381 public:
3382 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
3383 Handle<FunctionTemplate>());
3384 private:
3385 AccessorSignature();
3386};
3387
3388
ulan@chromium.org750145a2013-03-07 15:14:13 +00003389class V8EXPORT DeclaredAccessorDescriptor : public Data {
3390 private:
3391 DeclaredAccessorDescriptor();
3392};
3393
3394
3395class V8EXPORT ObjectOperationDescriptor : public Data {
3396 public:
3397 // This function is not yet stable and should not be used at this time.
3398 static Local<RawOperationDescriptor> NewInternalFieldDereference(
3399 Isolate* isolate,
3400 int internal_field);
3401 private:
3402 ObjectOperationDescriptor();
3403};
3404
3405
3406enum DeclaredAccessorDescriptorDataType {
3407 kDescriptorBoolType,
3408 kDescriptorInt8Type, kDescriptorUint8Type,
3409 kDescriptorInt16Type, kDescriptorUint16Type,
3410 kDescriptorInt32Type, kDescriptorUint32Type,
3411 kDescriptorFloatType, kDescriptorDoubleType
3412};
3413
3414
3415class V8EXPORT RawOperationDescriptor : public Data {
3416 public:
3417 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
3418 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
3419 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
3420 int16_t byte_offset);
3421 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
3422 void* compare_value);
3423 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
3424 Isolate* isolate,
3425 DeclaredAccessorDescriptorDataType data_type,
3426 uint8_t bool_offset = 0);
3427 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
3428 uint8_t bitmask,
3429 uint8_t compare_value);
3430 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
3431 Isolate* isolate,
3432 uint16_t bitmask,
3433 uint16_t compare_value);
3434 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
3435 Isolate* isolate,
3436 uint32_t bitmask,
3437 uint32_t compare_value);
3438
3439 private:
3440 RawOperationDescriptor();
3441};
3442
3443
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003444/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003445 * A utility for determining the type of objects based on the template
3446 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003447 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003448class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003449 public:
3450 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
3451 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
3452 int match(Handle<Value> value);
3453 private:
3454 TypeSwitch();
3455};
3456
3457
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003458// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003460class V8EXPORT ExternalAsciiStringResourceImpl
3461 : public String::ExternalAsciiStringResource {
3462 public:
3463 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
3464 ExternalAsciiStringResourceImpl(const char* data, size_t length)
3465 : data_(data), length_(length) {}
3466 const char* data() const { return data_; }
3467 size_t length() const { return length_; }
3468
3469 private:
3470 const char* data_;
3471 size_t length_;
3472};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473
3474/**
3475 * Ignore
3476 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003477class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003478 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003479 // Note that the strings passed into this constructor must live as long
3480 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003481 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003482 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003483 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003484 const char** deps = 0,
3485 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003486 virtual ~Extension() { }
3487 virtual v8::Handle<v8::FunctionTemplate>
3488 GetNativeFunction(v8::Handle<v8::String> name) {
3489 return v8::Handle<v8::FunctionTemplate>();
3490 }
3491
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003492 const char* name() const { return name_; }
3493 size_t source_length() const { return source_length_; }
3494 const String::ExternalAsciiStringResource* source() const {
3495 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003496 int dependency_count() { return dep_count_; }
3497 const char** dependencies() { return deps_; }
3498 void set_auto_enable(bool value) { auto_enable_ = value; }
3499 bool auto_enable() { return auto_enable_; }
3500
3501 private:
3502 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003503 size_t source_length_; // expected to initialize before source_
3504 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003505 int dep_count_;
3506 const char** deps_;
3507 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003508
3509 // Disallow copying and assigning.
3510 Extension(const Extension&);
3511 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003512};
3513
3514
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003515void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003516
3517
3518/**
3519 * Ignore
3520 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003521class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003523 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003524 RegisterExtension(extension);
3525 }
3526};
3527
3528
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003529// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003530
3531
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003532Handle<Primitive> V8EXPORT Undefined();
3533Handle<Primitive> V8EXPORT Null();
3534Handle<Boolean> V8EXPORT True();
3535Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003536
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003537V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
3538V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
3539V8_INLINE(Handle<Boolean> True(Isolate* isolate));
3540V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003541
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003542
3543/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003544 * A set of constraints that specifies the limits of the runtime's memory use.
3545 * You must set the heap size before initializing the VM - the size cannot be
3546 * adjusted after the VM is initialized.
3547 *
3548 * If you are using threads then you should hold the V8::Locker lock while
3549 * setting the stack limit and you must set a non-default stack limit separately
3550 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003552class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553 public:
3554 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003555 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003556 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003557 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003558 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003559 int max_executable_size() { return max_executable_size_; }
3560 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003561 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003562 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003563 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
3564 private:
3565 int max_young_space_size_;
3566 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003567 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003568 uint32_t* stack_limit_;
3569};
3570
3571
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003572bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003573
3574
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003575// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003576
3577
3578typedef void (*FatalErrorCallback)(const char* location, const char* message);
3579
3580
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003581typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003582
3583
3584/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003585 * Schedules an exception to be thrown when returning to JavaScript. When an
3586 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00003588 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003589 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003590Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591
3592/**
3593 * Create new error objects by calling the corresponding error object
3594 * constructor with the message.
3595 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003596class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003597 public:
3598 static Local<Value> RangeError(Handle<String> message);
3599 static Local<Value> ReferenceError(Handle<String> message);
3600 static Local<Value> SyntaxError(Handle<String> message);
3601 static Local<Value> TypeError(Handle<String> message);
3602 static Local<Value> Error(Handle<String> message);
3603};
3604
3605
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003606// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003607
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003608typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003609
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003610typedef void* (*CreateHistogramCallback)(const char* name,
3611 int min,
3612 int max,
3613 size_t buckets);
3614
3615typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
3616
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003617// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003618 enum ObjectSpace {
3619 kObjectSpaceNewSpace = 1 << 0,
3620 kObjectSpaceOldPointerSpace = 1 << 1,
3621 kObjectSpaceOldDataSpace = 1 << 2,
3622 kObjectSpaceCodeSpace = 1 << 3,
3623 kObjectSpaceMapSpace = 1 << 4,
3624 kObjectSpaceLoSpace = 1 << 5,
3625
3626 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
3627 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
3628 kObjectSpaceLoSpace
3629 };
3630
3631 enum AllocationAction {
3632 kAllocationActionAllocate = 1 << 0,
3633 kAllocationActionFree = 1 << 1,
3634 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
3635 };
3636
3637typedef void (*MemoryAllocationCallback)(ObjectSpace space,
3638 AllocationAction action,
3639 int size);
3640
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003641// --- Leave Script Callback ---
3642typedef void (*CallCompletedCallback)();
3643
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003644// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003645typedef void (*FailedAccessCheckCallback)(Local<Object> target,
3646 AccessType type,
3647 Local<Value> data);
3648
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003649// --- AllowCodeGenerationFromStrings callbacks ---
3650
3651/**
3652 * Callback to check if code generation from strings is allowed. See
3653 * Context::AllowCodeGenerationFromStrings.
3654 */
3655typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
3656
3657// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003658
3659/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003660 * Applications can register callback functions which will be called
3661 * before and after a garbage collection. Allocations are not
3662 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003663 * objects (set or delete properties for example) since it is possible
3664 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003666enum GCType {
3667 kGCTypeScavenge = 1 << 0,
3668 kGCTypeMarkSweepCompact = 1 << 1,
3669 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
3670};
3671
3672enum GCCallbackFlags {
3673 kNoGCCallbackFlags = 0,
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003674 kGCCallbackFlagCompacted = 1 << 0,
3675 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003676};
3677
3678typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
3679typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
3680
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003681typedef void (*GCCallback)();
3682
3683
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003684/**
ager@chromium.org3811b432009-10-28 14:53:37 +00003685 * Collection of V8 heap information.
3686 *
3687 * Instances of this class can be passed to v8::V8::HeapStatistics to
3688 * get heap statistics from V8.
3689 */
3690class V8EXPORT HeapStatistics {
3691 public:
3692 HeapStatistics();
3693 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003694 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003695 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003696 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003697 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003698
3699 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003700 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003701 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003702 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003703 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003704 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003705
3706 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003707 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003708};
3709
3710
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003711class RetainedObjectInfo;
3712
ager@chromium.org3811b432009-10-28 14:53:37 +00003713/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003714 * Isolate represents an isolated instance of the V8 engine. V8
3715 * isolates have completely separate states. Objects from one isolate
3716 * must not be used in other isolates. When V8 is initialized a
3717 * default isolate is implicitly created and entered. The embedder
3718 * can create additional isolates and use them in parallel in multiple
3719 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003720 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003721 */
3722class V8EXPORT Isolate {
3723 public:
3724 /**
3725 * Stack-allocated class which sets the isolate for all operations
3726 * executed within a local scope.
3727 */
3728 class V8EXPORT Scope {
3729 public:
3730 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3731 isolate->Enter();
3732 }
3733
3734 ~Scope() { isolate_->Exit(); }
3735
3736 private:
3737 Isolate* const isolate_;
3738
3739 // Prevent copying of Scope objects.
3740 Scope(const Scope&);
3741 Scope& operator=(const Scope&);
3742 };
3743
3744 /**
3745 * Creates a new isolate. Does not change the currently entered
3746 * isolate.
3747 *
3748 * When an isolate is no longer used its resources should be freed
3749 * by calling Dispose(). Using the delete operator is not allowed.
3750 */
3751 static Isolate* New();
3752
3753 /**
3754 * Returns the entered isolate for the current thread or NULL in
3755 * case there is no current isolate.
3756 */
3757 static Isolate* GetCurrent();
3758
3759 /**
3760 * Methods below this point require holding a lock (using Locker) in
3761 * a multi-threaded environment.
3762 */
3763
3764 /**
3765 * Sets this isolate as the entered one for the current thread.
3766 * Saves the previously entered one (if any), so that it can be
3767 * restored when exiting. Re-entering an isolate is allowed.
3768 */
3769 void Enter();
3770
3771 /**
3772 * Exits this isolate by restoring the previously entered one in the
3773 * current thread. The isolate may still stay the same, if it was
3774 * entered more than once.
3775 *
3776 * Requires: this == Isolate::GetCurrent().
3777 */
3778 void Exit();
3779
3780 /**
3781 * Disposes the isolate. The isolate must not be entered by any
3782 * thread to be disposable.
3783 */
3784 void Dispose();
3785
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003786 /**
3787 * Associate embedder-specific data with the isolate
3788 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003789 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003790
3791 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003792 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003793 * Returns NULL if SetData has never been called.
3794 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003795 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003796
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003797 /**
3798 * Get statistics about the heap memory usage.
3799 */
3800 void GetHeapStatistics(HeapStatistics* heap_statistics);
3801
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003802 /**
3803 * Adjusts the amount of registered external memory. Used to give V8 an
3804 * indication of the amount of externally allocated memory that is kept alive
3805 * by JavaScript objects. V8 uses this to decide when to perform global
3806 * garbage collections. Registering externally allocated memory will trigger
3807 * global garbage collections more often than it would otherwise in an attempt
3808 * to garbage collect the JavaScript objects that keep the externally
3809 * allocated memory alive.
3810 *
3811 * \param change_in_bytes the change in externally allocated memory that is
3812 * kept alive by JavaScript objects.
3813 * \returns the adjusted value.
3814 */
3815 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3816
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003817 /**
3818 * Returns heap profiler for this isolate. Will return NULL until the isolate
3819 * is initialized.
3820 */
3821 HeapProfiler* GetHeapProfiler();
3822
3823 /**
3824 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3825 * is initialized.
3826 */
3827 CpuProfiler* GetCpuProfiler();
3828
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003829 /** Returns the context that is on the top of the stack. */
3830 Local<Context> GetCurrentContext();
3831
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003832 /**
3833 * Allows the host application to group objects together. If one
3834 * object in the group is alive, all objects in the group are alive.
3835 * After each garbage collection, object groups are removed. It is
3836 * intended to be used in the before-garbage-collection callback
3837 * function, for instance to simulate DOM tree connections among JS
3838 * wrapper objects. Object groups for all dependent handles need to
3839 * be provided for kGCTypeMarkSweepCompact collections, for all other
3840 * garbage collection types it is sufficient to provide object groups
3841 * for partially dependent handles only.
3842 */
3843 void SetObjectGroupId(const Persistent<Value>& object,
3844 UniqueId id);
3845
3846 /**
3847 * Allows the host application to declare implicit references from an object
3848 * group to an object. If the objects of the object group are alive, the child
3849 * object is alive too. After each garbage collection, all implicit references
3850 * are removed. It is intended to be used in the before-garbage-collection
3851 * callback function.
3852 */
3853 void SetReferenceFromGroup(UniqueId id,
3854 const Persistent<Value>& child);
3855
3856 /**
3857 * Allows the host application to declare implicit references from an object
3858 * to another object. If the parent object is alive, the child object is alive
3859 * too. After each garbage collection, all implicit references are removed. It
3860 * is intended to be used in the before-garbage-collection callback function.
3861 */
3862 void SetReference(const Persistent<Object>& parent,
3863 const Persistent<Value>& child);
3864
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003865 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003866 Isolate();
3867 Isolate(const Isolate&);
3868 ~Isolate();
3869 Isolate& operator=(const Isolate&);
3870 void* operator new(size_t size);
3871 void operator delete(void*, size_t);
3872};
3873
3874
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003875class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003876 public:
3877 enum CompressionAlgorithm {
3878 kUncompressed,
3879 kBZip2
3880 };
3881
3882 const char* data;
3883 int compressed_size;
3884 int raw_size;
3885};
3886
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003887
3888/**
3889 * A helper class for driving V8 startup data decompression. It is based on
3890 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3891 * for an embedder to use this class, instead, API functions can be used
3892 * directly.
3893 *
3894 * For an example of the class usage, see the "shell.cc" sample application.
3895 */
3896class V8EXPORT StartupDataDecompressor { // NOLINT
3897 public:
3898 StartupDataDecompressor();
3899 virtual ~StartupDataDecompressor();
3900 int Decompress();
3901
3902 protected:
3903 virtual int DecompressData(char* raw_data,
3904 int* raw_data_size,
3905 const char* compressed_data,
3906 int compressed_data_size) = 0;
3907
3908 private:
3909 char** raw_data;
3910};
3911
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003912
3913/**
3914 * EntropySource is used as a callback function when v8 needs a source
3915 * of entropy.
3916 */
3917typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3918
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003919
3920/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003921 * ReturnAddressLocationResolver is used as a callback function when v8 is
3922 * resolving the location of a return address on the stack. Profilers that
3923 * change the return address on the stack can use this to resolve the stack
3924 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003925 *
3926 * \param return_addr_location points to a location on stack where a machine
3927 * return address resides.
3928 * \returns either return_addr_location, or else a pointer to the profiler's
3929 * copy of the original return address.
3930 *
3931 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003932 */
3933typedef uintptr_t (*ReturnAddressLocationResolver)(
3934 uintptr_t return_addr_location);
3935
3936
3937/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003938 * FunctionEntryHook is the type of the profile entry hook called at entry to
3939 * any generated function when function-level profiling is enabled.
3940 *
3941 * \param function the address of the function that's being entered.
3942 * \param return_addr_location points to a location on stack where the machine
3943 * return address resides. This can be used to identify the caller of
3944 * \p function, and/or modified to divert execution when \p function exits.
3945 *
3946 * \note the entry hook must not cause garbage collection.
3947 */
3948typedef void (*FunctionEntryHook)(uintptr_t function,
3949 uintptr_t return_addr_location);
3950
3951
3952/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003953 * A JIT code event is issued each time code is added, moved or removed.
3954 *
3955 * \note removal events are not currently issued.
3956 */
3957struct JitCodeEvent {
3958 enum EventType {
3959 CODE_ADDED,
3960 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003961 CODE_REMOVED,
3962 CODE_ADD_LINE_POS_INFO,
3963 CODE_START_LINE_INFO_RECORDING,
3964 CODE_END_LINE_INFO_RECORDING
3965 };
3966 // Definition of the code position type. The "POSITION" type means the place
3967 // in the source code which are of interest when making stack traces to
3968 // pin-point the source location of a stack frame as close as possible.
3969 // The "STATEMENT_POSITION" means the place at the beginning of each
3970 // statement, and is used to indicate possible break locations.
3971 enum PositionType {
3972 POSITION,
3973 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003974 };
3975
3976 // Type of event.
3977 EventType type;
3978 // Start of the instructions.
3979 void* code_start;
3980 // Size of the instructions.
3981 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00003982 // Script info for CODE_ADDED event.
3983 Handle<Script> script;
3984 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
3985 // code line information which is returned from the
3986 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
3987 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
3988 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003989
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00003990 struct name_t {
3991 // Name of the object associated with the code, note that the string is not
3992 // zero-terminated.
3993 const char* str;
3994 // Number of chars in str.
3995 size_t len;
3996 };
3997
3998 struct line_info_t {
3999 // PC offset
4000 size_t offset;
4001 // Code postion
4002 size_t pos;
4003 // The position type.
4004 PositionType position_type;
4005 };
4006
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004007 union {
4008 // Only valid for CODE_ADDED.
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004009 struct name_t name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004010
4011 // Only valid for CODE_ADD_LINE_POS_INFO
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004012 struct line_info_t line_info;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004013
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004014 // New location of instructions. Only valid for CODE_MOVED.
4015 void* new_code_start;
4016 };
4017};
4018
4019/**
4020 * Option flags passed to the SetJitCodeEventHandler function.
4021 */
4022enum JitCodeEventOptions {
4023 kJitCodeEventDefault = 0,
4024 // Generate callbacks for already existent code.
4025 kJitCodeEventEnumExisting = 1
4026};
4027
4028
4029/**
4030 * Callback function passed to SetJitCodeEventHandler.
4031 *
4032 * \param event code add, move or removal event.
4033 */
4034typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
4035
4036
4037/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004038 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004039 */
4040class V8EXPORT ExternalResourceVisitor { // NOLINT
4041 public:
4042 virtual ~ExternalResourceVisitor() {}
4043 virtual void VisitExternalString(Handle<String> string) {}
4044};
4045
4046
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004047/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004048 * Interface for iterating through all the persistent handles in the heap.
4049 */
4050class V8EXPORT PersistentHandleVisitor { // NOLINT
4051 public:
4052 virtual ~PersistentHandleVisitor() {}
4053 virtual void VisitPersistentHandle(Persistent<Value> value,
4054 uint16_t class_id) {}
4055};
4056
4057
4058/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004059 * Asserts that no action is performed that could cause a handle's value
4060 * to be modified. Useful when otherwise unsafe handle operations need to
4061 * be performed.
4062 */
4063class V8EXPORT AssertNoGCScope {
4064#ifndef DEBUG
4065 V8_INLINE(AssertNoGCScope(Isolate* isolate)) {}
4066#else
4067 AssertNoGCScope(Isolate* isolate);
4068 ~AssertNoGCScope();
4069 private:
4070 Isolate* isolate_;
4071 bool last_state_;
4072#endif
4073};
4074
4075
4076/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004077 * Container class for static utility functions.
4078 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004079class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004080 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00004081 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004082 static void SetFatalErrorHandler(FatalErrorCallback that);
4083
v8.team.kasperl727e9952008-09-02 14:56:44 +00004084 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004085 * Set the callback to invoke to check if code generation from
4086 * strings should be allowed.
4087 */
4088 static void SetAllowCodeGenerationFromStringsCallback(
4089 AllowCodeGenerationFromStringsCallback that);
4090
4091 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004092 * Ignore out-of-memory exceptions.
4093 *
4094 * V8 running out of memory is treated as a fatal error by default.
4095 * This means that the fatal error handler is called and that V8 is
4096 * terminated.
4097 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004098 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00004099 * out-of-memory situation as a fatal error. This way, the contexts
4100 * that did not cause the out of memory problem might be able to
4101 * continue execution.
4102 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004103 static void IgnoreOutOfMemoryException();
4104
v8.team.kasperl727e9952008-09-02 14:56:44 +00004105 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004106 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00004107 * fatal errors such as out-of-memory situations.
4108 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004109 static bool IsDead();
4110
4111 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004112 * The following 4 functions are to be used when V8 is built with
4113 * the 'compress_startup_data' flag enabled. In this case, the
4114 * embedder must decompress startup data prior to initializing V8.
4115 *
4116 * This is how interaction with V8 should look like:
4117 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
4118 * v8::StartupData* compressed_data =
4119 * new v8::StartupData[compressed_data_count];
4120 * v8::V8::GetCompressedStartupData(compressed_data);
4121 * ... decompress data (compressed_data can be updated in-place) ...
4122 * v8::V8::SetDecompressedStartupData(compressed_data);
4123 * ... now V8 can be initialized
4124 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004125 *
4126 * A helper class StartupDataDecompressor is provided. It implements
4127 * the protocol of the interaction described above, and can be used in
4128 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004129 */
4130 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
4131 static int GetCompressedStartupDataCount();
4132 static void GetCompressedStartupData(StartupData* compressed_data);
4133 static void SetDecompressedStartupData(StartupData* decompressed_data);
4134
4135 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004136 * Adds a message listener.
4137 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004138 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00004139 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004140 *
4141 * If data is specified, it will be passed to the callback when it is called.
4142 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004143 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004144 static bool AddMessageListener(MessageCallback that,
4145 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146
4147 /**
4148 * Remove all message listeners from the specified callback function.
4149 */
4150 static void RemoveMessageListeners(MessageCallback that);
4151
4152 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004153 * Tells V8 to capture current stack trace when uncaught exception occurs
4154 * and report it to the message listeners. The option is off by default.
4155 */
4156 static void SetCaptureStackTraceForUncaughtExceptions(
4157 bool capture,
4158 int frame_limit = 10,
4159 StackTrace::StackTraceOptions options = StackTrace::kOverview);
4160
4161 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004162 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004163 */
4164 static void SetFlagsFromString(const char* str, int length);
4165
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004166 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004167 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004168 */
4169 static void SetFlagsFromCommandLine(int* argc,
4170 char** argv,
4171 bool remove_flags);
4172
kasper.lund7276f142008-07-30 08:49:36 +00004173 /** Get the version string. */
4174 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004175
4176 /**
4177 * Enables the host application to provide a mechanism for recording
4178 * statistics counters.
4179 */
4180 static void SetCounterFunction(CounterLookupCallback);
4181
4182 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004183 * Enables the host application to provide a mechanism for recording
4184 * histograms. The CreateHistogram function returns a
4185 * histogram which will later be passed to the AddHistogramSample
4186 * function.
4187 */
4188 static void SetCreateHistogramFunction(CreateHistogramCallback);
4189 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
4190
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004191 /** Callback function for reporting failed access checks.*/
4192 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
4193
4194 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004195 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004196 * garbage collection. Allocations are not allowed in the
4197 * callback function, you therefore cannot manipulate objects (set
4198 * or delete properties for example) since it is possible such
4199 * operations will result in the allocation of objects. It is possible
4200 * to specify the GCType filter for your callback. But it is not possible to
4201 * register the same callback function two times with different
4202 * GCType filters.
4203 */
4204 static void AddGCPrologueCallback(
4205 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
4206
4207 /**
4208 * This function removes callback which was installed by
4209 * AddGCPrologueCallback function.
4210 */
4211 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
4212
4213 /**
4214 * The function is deprecated. Please use AddGCPrologueCallback instead.
4215 * Enables the host application to receive a notification before a
4216 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00004217 * callback function, you therefore cannot manipulate objects (set
4218 * or delete properties for example) since it is possible such
4219 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004220 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004221 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222
4223 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004224 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004225 * garbage collection. Allocations are not allowed in the
4226 * callback function, you therefore cannot manipulate objects (set
4227 * or delete properties for example) since it is possible such
4228 * operations will result in the allocation of objects. It is possible
4229 * to specify the GCType filter for your callback. But it is not possible to
4230 * register the same callback function two times with different
4231 * GCType filters.
4232 */
4233 static void AddGCEpilogueCallback(
4234 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
4235
4236 /**
4237 * This function removes callback which was installed by
4238 * AddGCEpilogueCallback function.
4239 */
4240 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
4241
4242 /**
4243 * The function is deprecated. Please use AddGCEpilogueCallback instead.
4244 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00004245 * major garbage collection. Allocations are not allowed in the
4246 * callback function, you therefore cannot manipulate objects (set
4247 * or delete properties for example) since it is possible such
4248 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004249 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004250 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004251
4252 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004253 * Enables the host application to provide a mechanism to be notified
4254 * and perform custom logging when V8 Allocates Executable Memory.
4255 */
4256 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
4257 ObjectSpace space,
4258 AllocationAction action);
4259
4260 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004261 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004262 */
4263 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
4264
4265 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004266 * Adds a callback to notify the host application when a script finished
4267 * running. If a script re-enters the runtime during executing, the
4268 * CallCompletedCallback is only invoked when the outer-most script
4269 * execution ends. Executing scripts inside the callback do not trigger
4270 * further callbacks.
4271 */
4272 static void AddCallCompletedCallback(CallCompletedCallback callback);
4273
4274 /**
4275 * Removes callback that was installed by AddCallCompletedCallback.
4276 */
4277 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
4278
4279 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004280 * Allows the host application to group objects together. If one
4281 * object in the group is alive, all objects in the group are alive.
4282 * After each garbage collection, object groups are removed. It is
4283 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00004284 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004285 * wrapper objects. Object groups for all dependent handles need to
4286 * be provided for kGCTypeMarkSweepCompact collections, for all other
4287 * garbage collection types it is sufficient to provide object groups
4288 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004289 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004290 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004291 // TODO(marja): deprecate AddObjectGroup. Use Isolate::SetObjectGroupId and
4292 // HeapProfiler::SetRetainedObjectInfo instead.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004293 static void AddObjectGroup(Persistent<Value>* objects,
4294 size_t length,
4295 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004296 static void AddObjectGroup(Isolate* isolate,
4297 Persistent<Value>* objects,
4298 size_t length,
4299 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004300
4301 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004302 * Allows the host application to declare implicit references between
4303 * the objects: if |parent| is alive, all |children| are alive too.
4304 * After each garbage collection, all implicit references
4305 * are removed. It is intended to be used in the before-garbage-collection
4306 * callback function.
4307 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004308 // TODO(marja): Deprecate AddImplicitReferences. Use
4309 // Isolate::SetReferenceFromGroup instead.
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004310 static void AddImplicitReferences(Persistent<Object> parent,
4311 Persistent<Value>* children,
4312 size_t length);
4313
4314 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004315 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004316 * initialize from scratch. This function is called implicitly if
4317 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004318 */
4319 static bool Initialize();
4320
kasper.lund7276f142008-07-30 08:49:36 +00004321 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004322 * Allows the host application to provide a callback which can be used
4323 * as a source of entropy for random number generators.
4324 */
4325 static void SetEntropySource(EntropySource source);
4326
4327 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004328 * Allows the host application to provide a callback that allows v8 to
4329 * cooperate with a profiler that rewrites return addresses on stack.
4330 */
4331 static void SetReturnAddressLocationResolver(
4332 ReturnAddressLocationResolver return_address_resolver);
4333
4334 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004335 * Allows the host application to provide the address of a function that's
4336 * invoked on entry to every V8-generated function.
4337 * Note that \p entry_hook is invoked at the very start of each
4338 * generated function.
4339 *
4340 * \param entry_hook a function that will be invoked on entry to every
4341 * V8-generated function.
4342 * \returns true on success on supported platforms, false on failure.
4343 * \note Setting a new entry hook function when one is already active will
4344 * fail.
4345 */
4346 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
4347
4348 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004349 * Allows the host application to provide the address of a function that is
4350 * notified each time code is added, moved or removed.
4351 *
4352 * \param options options for the JIT code event handler.
4353 * \param event_handler the JIT code event handler, which will be invoked
4354 * each time code is added, moved or removed.
4355 * \note \p event_handler won't get notified of existent code.
4356 * \note since code removal notifications are not currently issued, the
4357 * \p event_handler may get notifications of code that overlaps earlier
4358 * code notifications. This happens when code areas are reused, and the
4359 * earlier overlapping code areas should therefore be discarded.
4360 * \note the events passed to \p event_handler and the strings they point to
4361 * are not guaranteed to live past each call. The \p event_handler must
4362 * copy strings and other parameters it needs to keep around.
4363 * \note the set of events declared in JitCodeEvent::EventType is expected to
4364 * grow over time, and the JitCodeEvent structure is expected to accrue
4365 * new members. The \p event_handler function must ignore event codes
4366 * it does not recognize to maintain future compatibility.
4367 */
4368 static void SetJitCodeEventHandler(JitCodeEventOptions options,
4369 JitCodeEventHandler event_handler);
4370
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004371 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
4372 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004373 static intptr_t AdjustAmountOfExternalAllocatedMemory(
4374 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00004375
iposva@chromium.org245aa852009-02-10 00:49:54 +00004376 /**
4377 * Suspends recording of tick samples in the profiler.
4378 * When the V8 profiling mode is enabled (usually via command line
4379 * switches) this function suspends recording of tick samples.
4380 * Profiling ticks are discarded until ResumeProfiler() is called.
4381 *
4382 * See also the --prof and --prof_auto command line switches to
4383 * enable V8 profiling.
4384 */
4385 static void PauseProfiler();
4386
4387 /**
4388 * Resumes recording of tick samples in the profiler.
4389 * See also PauseProfiler().
4390 */
4391 static void ResumeProfiler();
4392
ager@chromium.org41826e72009-03-30 13:30:57 +00004393 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004394 * Return whether profiler is currently paused.
4395 */
4396 static bool IsProfilerPaused();
4397
4398 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004399 * Retrieve the V8 thread id of the calling thread.
4400 *
4401 * The thread id for a thread should only be retrieved after the V8
4402 * lock has been acquired with a Locker object with that thread.
4403 */
4404 static int GetCurrentThreadId();
4405
4406 /**
4407 * Forcefully terminate execution of a JavaScript thread. This can
4408 * be used to terminate long-running scripts.
4409 *
4410 * TerminateExecution should only be called when then V8 lock has
4411 * been acquired with a Locker object. Therefore, in order to be
4412 * able to terminate long-running threads, preemption must be
4413 * enabled to allow the user of TerminateExecution to acquire the
4414 * lock.
4415 *
4416 * The termination is achieved by throwing an exception that is
4417 * uncatchable by JavaScript exception handlers. Termination
4418 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004419 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004420 * exception handler that catches an exception should check if that
4421 * exception is a termination exception and immediately return if
4422 * that is the case. Returning immediately in that case will
4423 * continue the propagation of the termination exception if needed.
4424 *
4425 * The thread id passed to TerminateExecution must have been
4426 * obtained by calling GetCurrentThreadId on the thread in question.
4427 *
4428 * \param thread_id The thread id of the thread to terminate.
4429 */
4430 static void TerminateExecution(int thread_id);
4431
4432 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004433 * Forcefully terminate the current thread of JavaScript execution
4434 * in the given isolate. If no isolate is provided, the default
4435 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004436 *
4437 * This method can be used by any thread even if that thread has not
4438 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004439 *
4440 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004441 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004442 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00004443
4444 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004445 * Is V8 terminating JavaScript execution.
4446 *
4447 * Returns true if JavaScript execution is currently terminating
4448 * because of a call to TerminateExecution. In that case there are
4449 * still JavaScript frames on the stack and the termination
4450 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004451 *
4452 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004453 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004454 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004455
4456 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004457 * Resume execution capability in the given isolate, whose execution
4458 * was previously forcefully terminated using TerminateExecution().
4459 *
4460 * When execution is forcefully terminated using TerminateExecution(),
4461 * the isolate can not resume execution until all JavaScript frames
4462 * have propagated the uncatchable exception which is generated. This
4463 * method allows the program embedding the engine to handle the
4464 * termination event and resume execution capability, even if
4465 * JavaScript frames remain on the stack.
4466 *
4467 * This method can be used by any thread even if that thread has not
4468 * acquired the V8 lock with a Locker object.
4469 *
4470 * \param isolate The isolate in which to resume execution capability.
4471 */
4472 static void CancelTerminateExecution(Isolate* isolate);
4473
4474 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00004475 * Releases any resources used by v8 and stops any utility threads
4476 * that may be running. Note that disposing v8 is permanent, it
4477 * cannot be reinitialized.
4478 *
4479 * It should generally not be necessary to dispose v8 before exiting
4480 * a process, this should happen automatically. It is only necessary
4481 * to use if the process needs the resources taken up by v8.
4482 */
4483 static bool Dispose();
4484
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00004485 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
4486 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004487
4488 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004489 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004490 * heap. GC is not invoked prior to iterating, therefore there is no
4491 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004492 */
4493 static void VisitExternalResources(ExternalResourceVisitor* visitor);
4494
4495 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004496 * Iterates through all the persistent handles in the current isolate's heap
4497 * that have class_ids.
4498 */
4499 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
4500
4501 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004502 * Iterates through all the persistent handles in the current isolate's heap
4503 * that have class_ids and are candidates to be marked as partially dependent
4504 * handles. This will visit handles to young objects created since the last
4505 * garbage collection but is free to visit an arbitrary superset of these
4506 * objects.
4507 */
4508 static void VisitHandlesForPartialDependence(
4509 Isolate* isolate, PersistentHandleVisitor* visitor);
4510
4511 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004512 * Optional notification that the embedder is idle.
4513 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004514 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004515 * Returns true if the embedder should stop calling IdleNotification
4516 * until real work has been done. This indicates that V8 has done
4517 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004518 *
4519 * The hint argument specifies the amount of work to be done in the function
4520 * on scale from 1 to 1000. There is no guarantee that the actual work will
4521 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004522 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004523 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004524
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004525 /**
4526 * Optional notification that the system is running low on memory.
4527 * V8 uses these notifications to attempt to free memory.
4528 */
4529 static void LowMemoryNotification();
4530
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004531 /**
4532 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004533 * these notifications to guide the GC heuristic. Returns the number
4534 * of context disposals - including this one - since the last time
4535 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004536 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004537 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004538
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004539 private:
4540 V8();
4541
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004542 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
4543 internal::Object** handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004544 static void DisposeGlobal(internal::Isolate* isolate,
4545 internal::Object** global_handle);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004546 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004547 static void MakeWeak(internal::Isolate* isolate,
4548 internal::Object** global_handle,
4549 void* data,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004550 RevivableCallback weak_reference_callback,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004551 NearDeathCallback near_death_callback);
4552 static void ClearWeak(internal::Isolate* isolate,
4553 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004554
4555 template <class T> friend class Handle;
4556 template <class T> friend class Local;
4557 template <class T> friend class Persistent;
4558 friend class Context;
4559};
4560
4561
4562/**
4563 * An external exception handler.
4564 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004565class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004566 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004567 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004568 * Creates a new try/catch block and registers it with v8. Note that
4569 * all TryCatch blocks should be stack allocated because the memory
4570 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004571 */
4572 TryCatch();
4573
4574 /**
4575 * Unregisters and deletes this try/catch block.
4576 */
4577 ~TryCatch();
4578
4579 /**
4580 * Returns true if an exception has been caught by this try/catch block.
4581 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004582 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004583
4584 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004585 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004586 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004587 * If CanContinue returns false, the correct action is to perform any C++
4588 * cleanup needed and then return. If CanContinue returns false and
4589 * HasTerminated returns true, it is possible to call
4590 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004591 */
4592 bool CanContinue() const;
4593
4594 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004595 * Returns true if an exception has been caught due to script execution
4596 * being terminated.
4597 *
4598 * There is no JavaScript representation of an execution termination
4599 * exception. Such exceptions are thrown when the TerminateExecution
4600 * methods are called to terminate a long-running script.
4601 *
4602 * If such an exception has been thrown, HasTerminated will return true,
4603 * indicating that it is possible to call CancelTerminateExecution in order
4604 * to continue calling into the engine.
4605 */
4606 bool HasTerminated() const;
4607
4608 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004609 * Throws the exception caught by this TryCatch in a way that avoids
4610 * it being caught again by this same TryCatch. As with ThrowException
4611 * it is illegal to execute any JavaScript operations after calling
4612 * ReThrow; the caller must return immediately to where the exception
4613 * is caught.
4614 */
4615 Handle<Value> ReThrow();
4616
4617 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004618 * Returns the exception caught by this try/catch block. If no exception has
4619 * been caught an empty handle is returned.
4620 *
4621 * The returned handle is valid until this TryCatch block has been destroyed.
4622 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004623 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004624
4625 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004626 * Returns the .stack property of the thrown object. If no .stack
4627 * property is present an empty handle is returned.
4628 */
4629 Local<Value> StackTrace() const;
4630
4631 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004632 * Returns the message associated with this exception. If there is
4633 * no message associated an empty handle is returned.
4634 *
4635 * The returned handle is valid until this TryCatch block has been
4636 * destroyed.
4637 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004638 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004639
4640 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004641 * Clears any exceptions that may have been caught by this try/catch block.
4642 * After this method has been called, HasCaught() will return false.
4643 *
4644 * It is not necessary to clear a try/catch block before using it again; if
4645 * another exception is thrown the previously caught exception will just be
4646 * overwritten. However, it is often a good idea since it makes it easier
4647 * to determine which operation threw a given exception.
4648 */
4649 void Reset();
4650
v8.team.kasperl727e9952008-09-02 14:56:44 +00004651 /**
4652 * Set verbosity of the external exception handler.
4653 *
4654 * By default, exceptions that are caught by an external exception
4655 * handler are not reported. Call SetVerbose with true on an
4656 * external exception handler to have exceptions caught by the
4657 * handler reported as if they were not caught.
4658 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004659 void SetVerbose(bool value);
4660
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004661 /**
4662 * Set whether or not this TryCatch should capture a Message object
4663 * which holds source information about where the exception
4664 * occurred. True by default.
4665 */
4666 void SetCaptureMessage(bool value);
4667
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004668 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004669 // Make it hard to create heap-allocated TryCatch blocks.
4670 TryCatch(const TryCatch&);
4671 void operator=(const TryCatch&);
4672 void* operator new(size_t size);
4673 void operator delete(void*, size_t);
4674
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004675 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004676 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004677 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004678 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004679 bool is_verbose_ : 1;
4680 bool can_continue_ : 1;
4681 bool capture_message_ : 1;
4682 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004683 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004684
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004685 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004686};
4687
4688
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004689// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004690
4691
4692/**
4693 * Ignore
4694 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004695class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004696 public:
4697 ExtensionConfiguration(int name_count, const char* names[])
4698 : name_count_(name_count), names_(names) { }
4699 private:
4700 friend class ImplementationUtilities;
4701 int name_count_;
4702 const char** names_;
4703};
4704
4705
4706/**
4707 * A sandboxed execution context with its own set of built-in objects
4708 * and functions.
4709 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004710class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004711 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004712 /**
4713 * Returns the global proxy object or global object itself for
4714 * detached contexts.
4715 *
4716 * Global proxy object is a thin wrapper whose prototype points to
4717 * actual context's global object with the properties like Object, etc.
4718 * This is done that way for security reasons (for more details see
4719 * https://wiki.mozilla.org/Gecko:SplitWindow).
4720 *
4721 * Please note that changes to global proxy object prototype most probably
4722 * would break VM---v8 expects only global object as a prototype of
4723 * global proxy object.
4724 *
4725 * If DetachGlobal() has been invoked, Global() would return actual global
4726 * object until global is reattached with ReattachGlobal().
4727 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004728 Local<Object> Global();
4729
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004730 /**
4731 * Detaches the global object from its context before
4732 * the global object can be reused to create a new context.
4733 */
4734 void DetachGlobal();
4735
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00004736 /**
4737 * Reattaches a global object to a context. This can be used to
4738 * restore the connection between a global object and a context
4739 * after DetachGlobal has been called.
4740 *
4741 * \param global_object The global object to reattach to the
4742 * context. For this to work, the global object must be the global
4743 * object that was associated with this context before a call to
4744 * DetachGlobal.
4745 */
4746 void ReattachGlobal(Handle<Object> global_object);
4747
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004748 /**
4749 * Creates a new context and returns a handle to the newly allocated
4750 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004751 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004752 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004753 *
4754 * \param extensions An optional extension configuration containing
4755 * the extensions to be installed in the newly created context.
4756 *
4757 * \param global_template An optional object template from which the
4758 * global object for the newly created context will be created.
4759 *
4760 * \param global_object An optional global object to be reused for
4761 * the newly created context. This global object must have been
4762 * created by a previous call to Context::New with the same global
4763 * template. The state of the global object will be completely reset
4764 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004765 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004766 static Local<Context> New(
4767 Isolate* isolate,
4768 ExtensionConfiguration* extensions = NULL,
4769 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4770 Handle<Value> global_object = Handle<Value>());
4771
4772 /** Deprecated. Use Isolate version instead. */
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004773 V8_DEPRECATED(static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004774 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004775 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004776 Handle<Value> global_object = Handle<Value>()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004777
kasper.lund44510672008-07-25 07:37:58 +00004778 /** Returns the last entered context. */
4779 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004780
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004781 // TODO(svenpanne) Actually deprecate this.
4782 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004783 static Local<Context> GetCurrent();
4784
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004785 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004786 * Returns the context of the calling JavaScript code. That is the
4787 * context of the top-most JavaScript frame. If there are no
4788 * JavaScript frames an empty handle is returned.
4789 */
4790 static Local<Context> GetCalling();
4791
4792 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004793 * Sets the security token for the context. To access an object in
4794 * another context, the security tokens must match.
4795 */
4796 void SetSecurityToken(Handle<Value> token);
4797
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004798 /** Restores the security token to the default value. */
4799 void UseDefaultSecurityToken();
4800
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004801 /** Returns the security token of this context.*/
4802 Handle<Value> GetSecurityToken();
4803
v8.team.kasperl727e9952008-09-02 14:56:44 +00004804 /**
4805 * Enter this context. After entering a context, all code compiled
4806 * and run is compiled and run in this context. If another context
4807 * is already entered, this old context is saved so it can be
4808 * restored when the new context is exited.
4809 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004810 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004811
4812 /**
4813 * Exit this context. Exiting the current context restores the
4814 * context that was in place when entering the current context.
4815 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004816 void Exit();
4817
v8.team.kasperl727e9952008-09-02 14:56:44 +00004818 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004819 bool HasOutOfMemoryException();
4820
v8.team.kasperl727e9952008-09-02 14:56:44 +00004821 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004822 static bool InContext();
4823
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004824 /** Returns an isolate associated with a current context. */
4825 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004826
4827 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004828 * Gets the embedder data with the given index, which must have been set by a
4829 * previous call to SetEmbedderData with the same index. Note that index 0
4830 * currently has a special meaning for Chrome's debugger.
4831 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004832 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004833
4834 /**
4835 * Sets the embedder data with the given index, growing the data as
4836 * needed. Note that index 0 currently has a special meaning for Chrome's
4837 * debugger.
4838 */
4839 void SetEmbedderData(int index, Handle<Value> value);
4840
4841 /**
4842 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4843 * index, which must have bees set by a previous call to
4844 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4845 * currently has a special meaning for Chrome's debugger.
4846 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004847 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004848
4849 /**
4850 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4851 * index, growing the data as needed. Note that index 0 currently has a
4852 * special meaning for Chrome's debugger.
4853 */
4854 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004855
4856 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004857 * Control whether code generation from strings is allowed. Calling
4858 * this method with false will disable 'eval' and the 'Function'
4859 * constructor for code running in this context. If 'eval' or the
4860 * 'Function' constructor are used an exception will be thrown.
4861 *
4862 * If code generation from strings is not allowed the
4863 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4864 * set before blocking the call to 'eval' or the 'Function'
4865 * constructor. If that callback returns true, the call will be
4866 * allowed, otherwise an exception will be thrown. If no callback is
4867 * set an exception will be thrown.
4868 */
4869 void AllowCodeGenerationFromStrings(bool allow);
4870
4871 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004872 * Returns true if code generation from strings is allowed for the context.
4873 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4874 */
4875 bool IsCodeGenerationFromStringsAllowed();
4876
4877 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004878 * Sets the error description for the exception that is thrown when
4879 * code generation from strings is not allowed and 'eval' or the 'Function'
4880 * constructor are called.
4881 */
4882 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4883
4884 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004885 * Stack-allocated class which sets the execution context for all
4886 * operations executed within a local scope.
4887 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004888 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004889 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004890 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004891 context_->Enter();
4892 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004893 V8_INLINE(Scope(Isolate* isolate, Persistent<Context>& context)) // NOLINT
4894#ifndef V8_USE_UNSAFE_HANDLES
4895 : context_(Handle<Context>::New(isolate, context)) {
4896#else
4897 : context_(Local<Context>::New(isolate, context)) {
4898#endif
4899 context_->Enter();
4900 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004901 V8_INLINE(~Scope()) { context_->Exit(); }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004902
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004903 private:
4904 Handle<Context> context_;
4905 };
4906
4907 private:
4908 friend class Value;
4909 friend class Script;
4910 friend class Object;
4911 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004912
4913 Local<Value> SlowGetEmbedderData(int index);
4914 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004915};
4916
4917
4918/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004919 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4920 * to use any given V8 isolate, see the comments in the Isolate class. The
4921 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4922 * object pointers obtained from V8 handles while in the particular V8 isolate.
4923 * It is up to the user of V8 to ensure, perhaps with locking, that this
4924 * constraint is not violated. In addition to any other synchronization
4925 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4926 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004927 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004928 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4929 * construction and destruction, the current thread is allowed to use the locked
4930 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4931 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004932 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004933 * Sample usage:
4934* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004935 * ...
4936 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004937 * v8::Locker locker(isolate);
4938 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004939 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004940 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004941 * ...
4942 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00004943 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004944 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004945 * If you wish to stop using V8 in a thread A you can do this either by
4946 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
4947 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004948 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004949 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004950 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004951 * isolate->Exit();
4952 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004953 * ...
4954 * // Code not using V8 goes here while V8 can run in another thread.
4955 * ...
4956 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004957 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004958 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004959 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004960 * The Unlocker object is intended for use in a long-running callback from V8,
4961 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004962 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004963 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
4964 * given thread. This can be useful if you have code that can be called either
4965 * from code that holds the lock or from code that does not. The Unlocker is
4966 * not recursive so you can not have several Unlockers on the stack at once, and
4967 * 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 +00004968 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004969 * An unlocker will unlock several lockers if it has to and reinstate the
4970 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004971 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00004972 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004973 * // V8 not locked.
4974 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004975 * v8::Locker locker(isolate);
4976 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004977 * // V8 locked.
4978 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004979 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004980 * // V8 still locked (2 levels).
4981 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004982 * isolate->Exit();
4983 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004984 * // V8 not locked.
4985 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004986 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004987 * // V8 locked again (2 levels).
4988 * }
4989 * // V8 still locked (1 level).
4990 * }
4991 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00004992 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004993 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004994class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004995 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00004996 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004997 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004998 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004999 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
5000
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005001 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005002 V8_DEPRECATED(Unlocker());
5003
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005004 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00005005 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005006 void Initialize(Isolate* isolate);
5007
lrn@chromium.org1c092762011-05-09 09:42:16 +00005008 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005009};
5010
5011
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005012class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005013 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005014 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005015 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005016 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005017 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
5018
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005019 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005020 V8_DEPRECATED(Locker());
5021
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005022 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005023
5024 /**
5025 * Start preemption.
5026 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005027 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00005028 * that will switch between multiple threads that are in contention
5029 * for the V8 lock.
5030 */
5031 static void StartPreemption(int every_n_ms);
5032
5033 /**
5034 * Stop preemption.
5035 */
5036 static void StopPreemption();
5037
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005038 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005039 * Returns whether or not the locker for a given isolate, is locked by the
5040 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005041 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005042 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005043
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005044 /**
5045 * Returns whether v8::Locker is being used by this V8 instance.
5046 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00005047 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005048
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005049 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005050 void Initialize(Isolate* isolate);
5051
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005052 bool has_lock_;
5053 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005054 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005055
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005056 static bool active_;
5057
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005058 // Disallow copying and assigning.
5059 Locker(const Locker&);
5060 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005061};
5062
5063
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005064/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005065 * A struct for exporting HeapStats data from V8, using "push" model.
5066 */
5067struct HeapStatsUpdate;
5068
5069
5070/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005071 * An interface for exporting data from V8, using "push" model.
5072 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005073class V8EXPORT OutputStream { // NOLINT
5074 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005075 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005076 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005077 };
5078 enum WriteResult {
5079 kContinue = 0,
5080 kAbort = 1
5081 };
5082 virtual ~OutputStream() {}
5083 /** Notify about the end of stream. */
5084 virtual void EndOfStream() = 0;
5085 /** Get preferred output chunk size. Called only once. */
5086 virtual int GetChunkSize() { return 1024; }
5087 /** Get preferred output encoding. Called only once. */
5088 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
5089 /**
5090 * Writes the next chunk of snapshot data into the stream. Writing
5091 * can be stopped by returning kAbort as function result. EndOfStream
5092 * will not be called in case writing was aborted.
5093 */
5094 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005095 /**
5096 * Writes the next chunk of heap stats data into the stream. Writing
5097 * can be stopped by returning kAbort as function result. EndOfStream
5098 * will not be called in case writing was aborted.
5099 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005100 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00005101 return kAbort;
5102 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005103};
5104
5105
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005106/**
5107 * An interface for reporting progress and controlling long-running
5108 * activities.
5109 */
5110class V8EXPORT ActivityControl { // NOLINT
5111 public:
5112 enum ControlOption {
5113 kContinue = 0,
5114 kAbort = 1
5115 };
5116 virtual ~ActivityControl() {}
5117 /**
5118 * Notify about current progress. The activity can be stopped by
5119 * returning kAbort as the callback result.
5120 */
5121 virtual ControlOption ReportProgressValue(int done, int total) = 0;
5122};
5123
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005124
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00005125// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005126
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005127
5128namespace internal {
5129
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00005130const int kApiPointerSize = sizeof(void*); // NOLINT
5131const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005132
5133// Tag information for HeapObject.
5134const int kHeapObjectTag = 1;
5135const int kHeapObjectTagSize = 2;
5136const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
5137
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005138// Tag information for Smi.
5139const int kSmiTag = 0;
5140const int kSmiTagSize = 1;
5141const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
5142
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005143template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005144
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005145template<int kSmiShiftSize>
5146V8_INLINE(internal::Object* IntToSmi(int value)) {
5147 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
5148 intptr_t tagged_value =
5149 (static_cast<intptr_t>(value) << smi_shift_bits) | kSmiTag;
5150 return reinterpret_cast<internal::Object*>(tagged_value);
5151}
5152
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005153// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005154template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005155 static const int kSmiShiftSize = 0;
5156 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005157 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005158 int shift_bits = kSmiTagSize + kSmiShiftSize;
5159 // Throw away top 32 bits and shift down (requires >> to be sign extending).
5160 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
5161 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005162 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5163 return internal::IntToSmi<kSmiShiftSize>(value);
5164 }
5165 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5166 // To be representable as an tagged small integer, the two
5167 // most-significant bits of 'value' must be either 00 or 11 due to
5168 // sign-extension. To check this we add 01 to the two
5169 // most-significant bits, and check if the most-significant bit is 0
5170 //
5171 // CAUTION: The original code below:
5172 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
5173 // may lead to incorrect results according to the C language spec, and
5174 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
5175 // compiler may produce undefined results in case of signed integer
5176 // overflow. The computation must be done w/ unsigned ints.
5177 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
5178 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005179};
5180
5181// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005182template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005183 static const int kSmiShiftSize = 31;
5184 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005185 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005186 int shift_bits = kSmiTagSize + kSmiShiftSize;
5187 // Shift down and throw away top 32 bits.
5188 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
5189 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005190 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5191 return internal::IntToSmi<kSmiShiftSize>(value);
5192 }
5193 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5194 // To be representable as a long smi, the value must be a 32-bit integer.
5195 return (value == static_cast<int32_t>(value));
5196 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005197};
5198
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005199typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
5200const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
5201const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005202
5203/**
5204 * This class exports constants and functionality from within v8 that
5205 * is necessary to implement inline functions in the v8 api. Don't
5206 * depend on functions and constants defined here.
5207 */
5208class Internals {
5209 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005210 // These values match non-compiler-dependent values defined within
5211 // the implementation of v8.
5212 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005213 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005214 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005215
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005216 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00005217 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005218 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005219 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
5220 static const int kContextHeaderSize = 2 * kApiPointerSize;
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005221 static const int kContextEmbedderDataIndex = 64;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005222 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005223 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00005224 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005225 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005226
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005227 static const int kIsolateStateOffset = 0;
5228 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
5229 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
5230 static const int kUndefinedValueRootIndex = 5;
5231 static const int kNullValueRootIndex = 7;
5232 static const int kTrueValueRootIndex = 8;
5233 static const int kFalseValueRootIndex = 9;
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00005234 static const int kEmptyStringRootIndex = 127;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005235
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005236 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
5237 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
5238 static const int kNodeStateMask = 0xf;
5239 static const int kNodeStateIsWeakValue = 2;
5240 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005241 static const int kNodeIsIndependentShift = 4;
5242 static const int kNodeIsPartiallyDependentShift = 5;
5243
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005244 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005245 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005246 static const int kOddballType = 0x83;
5247 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005248
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005249 static const int kUndefinedOddballKind = 5;
5250 static const int kNullOddballKind = 3;
5251
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005252 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005253 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
5254 kHeapObjectTag);
5255 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005256
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005257 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005258 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005259 }
5260
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005261 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5262 return PlatformSmiTagging::IntToSmi(value);
5263 }
5264
5265 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5266 return PlatformSmiTagging::IsValidSmi(value);
5267 }
5268
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005269 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005270 typedef internal::Object O;
5271 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
5272 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
5273 }
5274
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005275 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005276 typedef internal::Object O;
5277 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
5278 }
5279
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005280 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005281 int representation = (instance_type & kFullStringRepresentationMask);
5282 return representation == kExternalTwoByteRepresentationTag;
5283 }
5284
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005285 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005286 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
5287 return *reinterpret_cast<int*>(addr) == 1;
5288 }
5289
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005290 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
5291 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5292 return *addr & (1 << shift);
5293 }
5294
5295 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
5296 bool value, int shift)) {
5297 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5298 uint8_t mask = 1 << shift;
5299 *addr = (*addr & ~mask) | (value << shift);
5300 }
5301
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005302 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
5303 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5304 return *addr & kNodeStateMask;
5305 }
5306
5307 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
5308 uint8_t value)) {
5309 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5310 *addr = (*addr & ~kNodeStateMask) | value;
5311 }
5312
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005313 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005314 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5315 kIsolateEmbedderDataOffset;
5316 *reinterpret_cast<void**>(addr) = data;
5317 }
5318
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005319 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005320 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5321 kIsolateEmbedderDataOffset;
5322 return *reinterpret_cast<void**>(addr);
5323 }
5324
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005325 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
5326 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005327 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
5328 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
5329 }
5330
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005331 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005332 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005333 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
5334 return *reinterpret_cast<T*>(addr);
5335 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005336
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005337 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005338 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005339 typedef internal::Object O;
5340 typedef internal::Internals I;
5341 O* ctx = *reinterpret_cast<O**>(context);
5342 int embedder_data_offset = I::kContextHeaderSize +
5343 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
5344 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
5345 int value_offset =
5346 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
5347 return I::ReadField<T>(embedder_data, value_offset);
5348 }
5349
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005350 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
5351 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
5352 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
5353 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
5354 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
5355 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
5356 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005357};
5358
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005359} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005360
5361
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005362template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005363Local<T>::Local() : Handle<T>() { }
5364
5365
5366template <class T>
5367Local<T> Local<T>::New(Handle<T> that) {
5368 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005369 T* that_ptr = *that;
5370 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5371 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
5372 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5373 reinterpret_cast<internal::HeapObject*>(*p))));
5374 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005375 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
5376}
5377
5378
5379template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005380Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005381 return New(isolate, that.val_);
5382}
5383
5384#ifndef V8_USE_UNSAFE_HANDLES
5385template <class T>
5386Local<T> Local<T>::New(Isolate* isolate, const Persistent<T>& that) {
5387 return New(isolate, that.val_);
5388}
5389
5390template <class T>
5391Handle<T> Handle<T>::New(Isolate* isolate, T* that) {
5392 if (that == NULL) return Handle<T>();
5393 T* that_ptr = that;
5394 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5395 return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5396 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5397}
5398#endif
5399
5400
5401template <class T>
5402Local<T> Local<T>::New(Isolate* isolate, T* that) {
5403 if (that == NULL) return Local<T>();
5404 T* that_ptr = that;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005405 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5406 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5407 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5408}
5409
5410
5411template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005412Persistent<T> Persistent<T>::New(Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005413 return New(Isolate::GetCurrent(), that.val_);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005414}
5415
5416
5417template <class T>
5418Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005419 return New(Isolate::GetCurrent(), that.val_);
5420}
5421
5422#ifndef V8_USE_UNSAFE_HANDLES
5423template <class T>
5424Persistent<T> Persistent<T>::New(Isolate* isolate, Persistent<T> that) {
5425 return New(Isolate::GetCurrent(), that.val_);
5426}
5427#endif
5428
5429template <class T>
5430Persistent<T> Persistent<T>::New(Isolate* isolate, T* that) {
5431 if (that == NULL) return Persistent<T>();
5432 internal::Object** p = reinterpret_cast<internal::Object**>(that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005433 return Persistent<T>(reinterpret_cast<T*>(
5434 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
5435 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005436}
5437
5438
5439template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005440bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005441 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005442}
5443
5444
5445template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005446bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005447 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005448 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005449 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005450 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005451 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005452}
5453
5454
5455template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005456bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005457 return IsNearDeath(Isolate::GetCurrent());
5458}
5459
5460
5461template <class T>
5462bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
5463 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005464 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005465 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005466 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005467 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005468}
5469
5470
5471template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005472bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005473 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005474}
5475
5476
5477template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005478bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005479 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005480 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005481 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005482 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005483 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005484}
5485
5486
5487template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005488void Persistent<T>::Dispose() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005489 Dispose(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005490}
5491
5492
5493template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005494void Persistent<T>::Dispose(Isolate* isolate) {
5495 if (this->IsEmpty()) return;
5496 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005497 reinterpret_cast<internal::Object**>(this->val_));
5498#ifndef V8_USE_UNSAFE_HANDLES
5499 val_ = 0;
5500#endif
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005501}
5502
5503
5504template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005505template <typename S, typename P>
5506void Persistent<T>::MakeWeak(
5507 Isolate* isolate,
5508 P* parameters,
5509 typename WeakReferenceCallbacks<S, P>::Revivable callback) {
5510 TYPE_CHECK(S, T);
5511 typedef typename WeakReferenceCallbacks<Value, void>::Revivable Revivable;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005512 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005513 reinterpret_cast<internal::Object**>(this->val_),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005514 parameters,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005515 reinterpret_cast<Revivable>(callback),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005516 NULL);
5517}
5518
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005519
5520template <class T>
5521template <typename P>
5522void Persistent<T>::MakeWeak(
5523 Isolate* isolate,
5524 P* parameters,
5525 typename WeakReferenceCallbacks<T, P>::Revivable callback) {
5526 MakeWeak<T, P>(isolate, parameters, callback);
5527}
5528
5529
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005530template <class T>
5531void Persistent<T>::MakeWeak(Isolate* isolate,
5532 void* parameters,
5533 NearDeathCallback callback) {
5534 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005535 reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005536 parameters,
5537 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005538 callback);
5539}
5540
5541template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005542void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005543 ClearWeak(Isolate::GetCurrent());
5544}
5545
5546template <class T>
5547void Persistent<T>::ClearWeak(Isolate* isolate) {
5548 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005549 reinterpret_cast<internal::Object**>(this->val_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005550}
5551
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005552template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005553void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005554 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005555}
5556
5557template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005558void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005559 typedef internal::Internals I;
5560 if (this->IsEmpty()) return;
5561 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005562 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005563 true,
5564 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005565}
5566
5567template <class T>
5568void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005569 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005570}
5571
5572template <class T>
5573void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005574 typedef internal::Internals I;
5575 if (this->IsEmpty()) return;
5576 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005577 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005578 true,
5579 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005580}
5581
5582template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005583void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005584 SetWrapperClassId(Isolate::GetCurrent(), class_id);
5585}
5586
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005587
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005588template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005589void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
5590 Dispose(isolate);
5591#ifdef V8_USE_UNSAFE_HANDLES
5592 *this = *New(isolate, other);
5593#else
5594 if (other.IsEmpty()) {
5595 this->val_ = NULL;
5596 return;
5597 }
5598 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5599 this->val_ = reinterpret_cast<T*>(
5600 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5601#endif
5602}
5603
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005604
5605template <class T>
5606T* Persistent<T>::ClearAndLeak() {
5607 T* old;
5608#ifdef V8_USE_UNSAFE_HANDLES
5609 old = **this;
5610 *this = Persistent<T>();
5611#else
5612 old = val_;
5613 val_ = NULL;
5614#endif
5615 return old;
5616}
5617
5618
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005619template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005620void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005621 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005622 if (this->IsEmpty()) return;
5623 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005624 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005625 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5626 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005627}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005628
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005629template <class T>
5630uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005631 return WrapperClassId(Isolate::GetCurrent());
5632}
5633
5634template <class T>
5635uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005636 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005637 if (this->IsEmpty()) return 0;
5638 if (!I::IsInitialized(isolate)) return 0;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005639 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005640 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5641 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005642}
5643
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005644
5645template<typename T>
5646ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
5647
5648template<typename T>
5649void ReturnValue<T>::Set(const Persistent<T>& handle) {
5650 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5651}
5652
5653template<typename T>
5654void ReturnValue<T>::Set(const Handle<T> handle) {
5655 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5656}
5657
5658template<typename T>
5659void ReturnValue<T>::Set(Isolate* isolate, double i) {
5660 Set(Number::New(isolate, i));
5661}
5662
5663template<typename T>
5664void ReturnValue<T>::Set(Isolate* isolate, int32_t i) {
5665 typedef internal::Internals I;
5666 if (V8_LIKELY(I::IsValidSmi(i))) {
5667 *value_ = I::IntToSmi(i);
5668 return;
5669 }
5670 Set(Integer::New(i, isolate));
5671}
5672
5673template<typename T>
5674void ReturnValue<T>::Set(Isolate* isolate, uint32_t i) {
5675 typedef internal::Internals I;
5676 if (V8_LIKELY(I::IsValidSmi(i))) {
5677 *value_ = I::IntToSmi(i);
5678 return;
5679 }
5680 Set(Integer::NewFromUnsigned(i, isolate));
5681}
5682
5683template<typename T>
5684void ReturnValue<T>::Set(Isolate* isolate, bool value) {
5685 if (value) {
5686 SetTrue(isolate);
5687 } else {
5688 SetFalse(isolate);
5689 }
5690}
5691
5692template<typename T>
5693void ReturnValue<T>::SetTrue(Isolate* isolate) {
5694 typedef internal::Internals I;
5695 *value_ = *I::GetRoot(isolate, I::kTrueValueRootIndex);
5696}
5697
5698template<typename T>
5699void ReturnValue<T>::SetFalse(Isolate* isolate) {
5700 typedef internal::Internals I;
5701 *value_ = *I::GetRoot(isolate, I::kFalseValueRootIndex);
5702}
5703
5704template<typename T>
5705void ReturnValue<T>::SetNull(Isolate* isolate) {
5706 typedef internal::Internals I;
5707 *value_ = *I::GetRoot(isolate, I::kNullValueRootIndex);
5708}
5709
5710template<typename T>
5711void ReturnValue<T>::SetUndefined(Isolate* isolate) {
5712 typedef internal::Internals I;
5713 *value_ = *I::GetRoot(isolate, I::kUndefinedValueRootIndex);
5714}
5715
5716
5717template<typename T>
5718FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
5719 internal::Object** values,
5720 int length,
5721 bool is_construct_call)
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005722 : implicit_args_(implicit_args),
5723 values_(values),
5724 length_(length),
5725 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005726
5727
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005728Arguments::Arguments(internal::Object** args,
5729 internal::Object** values,
5730 int length,
5731 bool is_construct_call)
5732 : FunctionCallbackInfo<Value>(args, values, length, is_construct_call) { }
5733
5734
5735template<typename T>
5736Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005737 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
5738 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
5739}
5740
5741
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005742template<typename T>
5743Local<Function> FunctionCallbackInfo<T>::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005744 return Local<Function>(reinterpret_cast<Function*>(
5745 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005746}
5747
5748
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005749template<typename T>
5750Local<Object> FunctionCallbackInfo<T>::This() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005751 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
5752}
5753
5754
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005755template<typename T>
5756Local<Object> FunctionCallbackInfo<T>::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005757 return Local<Object>(reinterpret_cast<Object*>(
5758 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005759}
5760
5761
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005762template<typename T>
5763Local<Value> FunctionCallbackInfo<T>::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005764 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005765}
5766
5767
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005768template<typename T>
5769Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005770 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
5771}
5772
5773
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005774template<typename T>
5775ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
5776 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
5777}
5778
5779
5780template<typename T>
5781bool FunctionCallbackInfo<T>::IsConstructCall() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005782 return is_construct_call_;
5783}
5784
5785
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005786template<typename T>
5787int FunctionCallbackInfo<T>::Length() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005788 return length_;
5789}
5790
5791
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005792template <class T>
5793Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005794 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
5795 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005796 return Local<T>(reinterpret_cast<T*>(after));
5797}
5798
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005799Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005800 return resource_name_;
5801}
5802
5803
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005804Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005805 return resource_line_offset_;
5806}
5807
5808
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005809Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005810 return resource_column_offset_;
5811}
5812
5813
5814Handle<Boolean> Boolean::New(bool value) {
5815 return value ? True() : False();
5816}
5817
5818
5819void Template::Set(const char* name, v8::Handle<Data> value) {
5820 Set(v8::String::New(name), value);
5821}
5822
5823
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005824Local<Value> Object::GetInternalField(int index) {
5825#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005826 typedef internal::Object O;
5827 typedef internal::Internals I;
5828 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005829 // Fast path: If the object is a plain JSObject, which is the common case, we
5830 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005831 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005832 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005833 O* value = I::ReadField<O*>(obj, offset);
5834 O** result = HandleScope::CreateHandle(value);
5835 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005836 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005837#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005838 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005839}
5840
5841
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005842void* Object::GetAlignedPointerFromInternalField(int index) {
5843#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005844 typedef internal::Object O;
5845 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005846 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005847 // Fast path: If the object is a plain JSObject, which is the common case, we
5848 // know where to find the internal fields and can return the value directly.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005849 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005850 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005851 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005852 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005853#endif
5854 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005855}
5856
5857
5858String* String::Cast(v8::Value* value) {
5859#ifdef V8_ENABLE_CHECKS
5860 CheckCast(value);
5861#endif
5862 return static_cast<String*>(value);
5863}
5864
5865
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005866Local<String> String::Empty(Isolate* isolate) {
5867 typedef internal::Object* S;
5868 typedef internal::Internals I;
5869 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005870 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005871 return Local<String>(reinterpret_cast<String*>(slot));
5872}
5873
5874
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00005875Local<String> String::New(const char* data, int length) {
5876 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
5877}
5878
5879
5880Local<String> String::New(const uint16_t* data, int length) {
5881 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
5882}
5883
5884
5885Local<String> String::NewSymbol(const char* data, int length) {
5886 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
5887}
5888
5889
5890Local<String> String::NewUndetectable(const char* data, int length) {
5891 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
5892}
5893
5894
5895Local<String> String::NewUndetectable(const uint16_t* data, int length) {
5896 return NewFromTwoByte(
5897 Isolate::GetCurrent(), data, kUndetectableString, length);
5898}
5899
5900
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005901String::ExternalStringResource* String::GetExternalStringResource() const {
5902 typedef internal::Object O;
5903 typedef internal::Internals I;
5904 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005905 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005906 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005907 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5908 result = reinterpret_cast<String::ExternalStringResource*>(value);
5909 } else {
5910 result = NULL;
5911 }
5912#ifdef V8_ENABLE_CHECKS
5913 VerifyExternalStringResource(result);
5914#endif
5915 return result;
5916}
5917
5918
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005919String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
5920 String::Encoding* encoding_out) const {
5921 typedef internal::Object O;
5922 typedef internal::Internals I;
5923 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
5924 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
5925 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5926 ExternalStringResourceBase* resource = NULL;
5927 if (type == I::kExternalAsciiRepresentationTag ||
5928 type == I::kExternalTwoByteRepresentationTag) {
5929 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5930 resource = static_cast<ExternalStringResourceBase*>(value);
5931 }
5932#ifdef V8_ENABLE_CHECKS
5933 VerifyExternalStringResourceBase(resource, *encoding_out);
5934#endif
5935 return resource;
5936}
5937
5938
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005939bool Value::IsUndefined() const {
5940#ifdef V8_ENABLE_CHECKS
5941 return FullIsUndefined();
5942#else
5943 return QuickIsUndefined();
5944#endif
5945}
5946
5947bool Value::QuickIsUndefined() const {
5948 typedef internal::Object O;
5949 typedef internal::Internals I;
5950 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5951 if (!I::HasHeapObjectTag(obj)) return false;
5952 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5953 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
5954}
5955
5956
5957bool Value::IsNull() const {
5958#ifdef V8_ENABLE_CHECKS
5959 return FullIsNull();
5960#else
5961 return QuickIsNull();
5962#endif
5963}
5964
5965bool Value::QuickIsNull() const {
5966 typedef internal::Object O;
5967 typedef internal::Internals I;
5968 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5969 if (!I::HasHeapObjectTag(obj)) return false;
5970 if (I::GetInstanceType(obj) != I::kOddballType) return false;
5971 return (I::GetOddballKind(obj) == I::kNullOddballKind);
5972}
5973
5974
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005975bool Value::IsString() const {
5976#ifdef V8_ENABLE_CHECKS
5977 return FullIsString();
5978#else
5979 return QuickIsString();
5980#endif
5981}
5982
5983bool Value::QuickIsString() const {
5984 typedef internal::Object O;
5985 typedef internal::Internals I;
5986 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
5987 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005988 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005989}
5990
5991
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00005992Symbol* Symbol::Cast(v8::Value* value) {
5993#ifdef V8_ENABLE_CHECKS
5994 CheckCast(value);
5995#endif
5996 return static_cast<Symbol*>(value);
5997}
5998
5999
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006000Number* Number::Cast(v8::Value* value) {
6001#ifdef V8_ENABLE_CHECKS
6002 CheckCast(value);
6003#endif
6004 return static_cast<Number*>(value);
6005}
6006
6007
6008Integer* Integer::Cast(v8::Value* value) {
6009#ifdef V8_ENABLE_CHECKS
6010 CheckCast(value);
6011#endif
6012 return static_cast<Integer*>(value);
6013}
6014
6015
6016Date* Date::Cast(v8::Value* value) {
6017#ifdef V8_ENABLE_CHECKS
6018 CheckCast(value);
6019#endif
6020 return static_cast<Date*>(value);
6021}
6022
6023
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006024StringObject* StringObject::Cast(v8::Value* value) {
6025#ifdef V8_ENABLE_CHECKS
6026 CheckCast(value);
6027#endif
6028 return static_cast<StringObject*>(value);
6029}
6030
6031
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006032SymbolObject* SymbolObject::Cast(v8::Value* value) {
6033#ifdef V8_ENABLE_CHECKS
6034 CheckCast(value);
6035#endif
6036 return static_cast<SymbolObject*>(value);
6037}
6038
6039
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006040NumberObject* NumberObject::Cast(v8::Value* value) {
6041#ifdef V8_ENABLE_CHECKS
6042 CheckCast(value);
6043#endif
6044 return static_cast<NumberObject*>(value);
6045}
6046
6047
6048BooleanObject* BooleanObject::Cast(v8::Value* value) {
6049#ifdef V8_ENABLE_CHECKS
6050 CheckCast(value);
6051#endif
6052 return static_cast<BooleanObject*>(value);
6053}
6054
6055
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00006056RegExp* RegExp::Cast(v8::Value* value) {
6057#ifdef V8_ENABLE_CHECKS
6058 CheckCast(value);
6059#endif
6060 return static_cast<RegExp*>(value);
6061}
6062
6063
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006064Object* Object::Cast(v8::Value* value) {
6065#ifdef V8_ENABLE_CHECKS
6066 CheckCast(value);
6067#endif
6068 return static_cast<Object*>(value);
6069}
6070
6071
6072Array* Array::Cast(v8::Value* value) {
6073#ifdef V8_ENABLE_CHECKS
6074 CheckCast(value);
6075#endif
6076 return static_cast<Array*>(value);
6077}
6078
6079
danno@chromium.orgca29dd82013-04-26 11:59:48 +00006080ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
6081#ifdef V8_ENABLE_CHECKS
6082 CheckCast(value);
6083#endif
6084 return static_cast<ArrayBuffer*>(value);
6085}
6086
6087
danno@chromium.orgf005df62013-04-30 16:36:45 +00006088TypedArray* TypedArray::Cast(v8::Value* value) {
6089#ifdef V8_ENABLE_CHECKS
6090 CheckCast(value);
6091#endif
6092 return static_cast<TypedArray*>(value);
6093}
6094
6095
6096Uint8Array* Uint8Array::Cast(v8::Value* value) {
6097#ifdef V8_ENABLE_CHECKS
6098 CheckCast(value);
6099#endif
6100 return static_cast<Uint8Array*>(value);
6101}
6102
6103
6104Int8Array* Int8Array::Cast(v8::Value* value) {
6105#ifdef V8_ENABLE_CHECKS
6106 CheckCast(value);
6107#endif
6108 return static_cast<Int8Array*>(value);
6109}
6110
6111
6112Uint16Array* Uint16Array::Cast(v8::Value* value) {
6113#ifdef V8_ENABLE_CHECKS
6114 CheckCast(value);
6115#endif
6116 return static_cast<Uint16Array*>(value);
6117}
6118
6119
6120Int16Array* Int16Array::Cast(v8::Value* value) {
6121#ifdef V8_ENABLE_CHECKS
6122 CheckCast(value);
6123#endif
6124 return static_cast<Int16Array*>(value);
6125}
6126
6127
6128Uint32Array* Uint32Array::Cast(v8::Value* value) {
6129#ifdef V8_ENABLE_CHECKS
6130 CheckCast(value);
6131#endif
6132 return static_cast<Uint32Array*>(value);
6133}
6134
6135
6136Int32Array* Int32Array::Cast(v8::Value* value) {
6137#ifdef V8_ENABLE_CHECKS
6138 CheckCast(value);
6139#endif
6140 return static_cast<Int32Array*>(value);
6141}
6142
6143
6144Float32Array* Float32Array::Cast(v8::Value* value) {
6145#ifdef V8_ENABLE_CHECKS
6146 CheckCast(value);
6147#endif
6148 return static_cast<Float32Array*>(value);
6149}
6150
6151
6152Float64Array* Float64Array::Cast(v8::Value* value) {
6153#ifdef V8_ENABLE_CHECKS
6154 CheckCast(value);
6155#endif
6156 return static_cast<Float64Array*>(value);
6157}
6158
6159
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006160Function* Function::Cast(v8::Value* value) {
6161#ifdef V8_ENABLE_CHECKS
6162 CheckCast(value);
6163#endif
6164 return static_cast<Function*>(value);
6165}
6166
6167
6168External* External::Cast(v8::Value* value) {
6169#ifdef V8_ENABLE_CHECKS
6170 CheckCast(value);
6171#endif
6172 return static_cast<External*>(value);
6173}
6174
6175
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006176template<typename T>
6177Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
6178 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00006179}
6180
6181
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006182template<typename T>
6183Local<Value> PropertyCallbackInfo<T>::Data() const {
6184 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006185}
6186
6187
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006188template<typename T>
6189Local<Object> PropertyCallbackInfo<T>::This() const {
6190 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006191}
6192
6193
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006194template<typename T>
6195Local<Object> PropertyCallbackInfo<T>::Holder() const {
6196 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
6197}
6198
6199
6200template<typename T>
6201ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
6202 return ReturnValue<T>(&args_[kReturnValueIndex]);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006203}
6204
6205
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00006206Handle<Primitive> Undefined(Isolate* isolate) {
6207 typedef internal::Object* S;
6208 typedef internal::Internals I;
6209 if (!I::IsInitialized(isolate)) return Undefined();
6210 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
6211 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6212}
6213
6214
6215Handle<Primitive> Null(Isolate* isolate) {
6216 typedef internal::Object* S;
6217 typedef internal::Internals I;
6218 if (!I::IsInitialized(isolate)) return Null();
6219 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
6220 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6221}
6222
6223
6224Handle<Boolean> True(Isolate* isolate) {
6225 typedef internal::Object* S;
6226 typedef internal::Internals I;
6227 if (!I::IsInitialized(isolate)) return True();
6228 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
6229 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6230}
6231
6232
6233Handle<Boolean> False(Isolate* isolate) {
6234 typedef internal::Object* S;
6235 typedef internal::Internals I;
6236 if (!I::IsInitialized(isolate)) return False();
6237 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
6238 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6239}
6240
6241
6242void Isolate::SetData(void* data) {
6243 typedef internal::Internals I;
6244 I::SetEmbedderData(this, data);
6245}
6246
6247
6248void* Isolate::GetData() {
6249 typedef internal::Internals I;
6250 return I::GetEmbedderData(this);
6251}
6252
6253
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00006254Local<Value> Context::GetEmbedderData(int index) {
6255#ifndef V8_ENABLE_CHECKS
6256 typedef internal::Object O;
6257 typedef internal::Internals I;
6258 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
6259 return Local<Value>(reinterpret_cast<Value*>(result));
6260#else
6261 return SlowGetEmbedderData(index);
6262#endif
6263}
6264
6265
6266void* Context::GetAlignedPointerFromEmbedderData(int index) {
6267#ifndef V8_ENABLE_CHECKS
6268 typedef internal::Internals I;
6269 return I::ReadEmbedderData<void*>(this, index);
6270#else
6271 return SlowGetAlignedPointerFromEmbedderData(index);
6272#endif
6273}
6274
6275
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006276/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00006277 * \example shell.cc
6278 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006279 * command-line and executes them.
6280 */
6281
6282
6283/**
6284 * \example process.cc
6285 */
6286
6287
6288} // namespace v8
6289
6290
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006291#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006292#undef TYPE_CHECK
6293
6294
ager@chromium.org9258b6b2008-09-11 09:11:10 +00006295#endif // V8_H_