blob: 51040fe1a70f57d992f08d6107e8f0316de42955 [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
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000566#ifdef V8_USE_UNSAFE_HANDLES
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000567 template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000568#ifdef V8_ENABLE_CHECKS
569 // If we're going to perform the type check then we have to check
570 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000572#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 return Persistent<T>(T::Cast(*that));
574 }
575
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000576 template <class S> V8_INLINE(Persistent<S> As()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000577 return Persistent<S>::Cast(*this);
578 }
579
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000580#else
581 template <class S>
582 V8_INLINE(static Persistent<T>& Cast(Persistent<S>& that)) { // NOLINT
583#ifdef V8_ENABLE_CHECKS
584 // If we're going to perform the type check then we have to check
585 // that the handle isn't empty before doing the checked cast.
586 if (!that.IsEmpty()) T::Cast(*that);
587#endif
588 return reinterpret_cast<Persistent<T>&>(that);
589 }
590
591 template <class S> V8_INLINE(Persistent<S>& As()) { // NOLINT
592 return Persistent<S>::Cast(*this);
593 }
594#endif
595
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000596 V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
597
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000599 * Creates a new persistent handle for an existing local or persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000601 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000602 V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000603#ifndef V8_USE_UNSAFE_HANDLES
604 // TODO(dcarney): remove before cutover
605 V8_INLINE(static Persistent<T> New(Isolate* isolate, Persistent<T> that));
606#endif
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000607
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000608#ifndef V8_USE_UNSAFE_HANDLES
609 template <class S> V8_INLINE(
610 bool operator==(const Persistent<S>& that) const) {
611 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
612 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
613 if (a == 0) return b == 0;
614 if (b == 0) return false;
615 return *a == *b;
616 }
617
618 template <class S> V8_INLINE(bool operator==(const Handle<S> that) const) {
619 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
620 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
621 if (a == 0) return b == 0;
622 if (b == 0) return false;
623 return *a == *b;
624 }
625#endif
626
627 V8_INLINE(void Dispose());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628
629 /**
630 * Releases the storage cell referenced by this persistent handle.
631 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000632 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633 * cell remain and IsEmpty will still return false.
634 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000635 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000636 V8_INLINE(void Dispose(Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000638 template<typename S, typename P>
639 V8_INLINE(void MakeWeak(
640 Isolate* isolate,
641 P* parameters,
642 typename WeakReferenceCallbacks<S, P>::Revivable callback));
643
644 template<typename P>
645 V8_INLINE(void MakeWeak(
646 Isolate* isolate,
647 P* parameters,
648 typename WeakReferenceCallbacks<T, P>::Revivable callback));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000649
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 /**
651 * Make the reference to this object weak. When only weak handles
652 * refer to the object, the garbage collector will perform a
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000653 * callback to the given V8::NearDeathCallback function, passing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 * it the object reference and the given parameters.
655 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000656 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000657 V8_INLINE(void MakeWeak(Isolate* isolate,
658 void* parameters,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000659 NearDeathCallback callback));
660
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000661 V8_INLINE(void ClearWeak());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000663 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000664 V8_INLINE(void ClearWeak(Isolate* isolate));
665
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000666 V8_INLINE(void MarkIndependent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667
668 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000669 * Marks the reference to this object independent. Garbage collector is free
670 * to ignore any object groups containing this object. Weak callback for an
671 * independent handle should not assume that it will be preceded by a global
672 * GC prologue callback or followed by a global GC epilogue callback.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000673 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000674 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000675 V8_INLINE(void MarkIndependent(Isolate* isolate));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000676
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000677 V8_INLINE(void MarkPartiallyDependent());
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000678
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000679 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000680 * Marks the reference to this object partially dependent. Partially dependent
681 * handles only depend on other partially dependent handles and these
682 * dependencies are provided through object groups. It provides a way to build
683 * smaller object groups for young objects that represent only a subset of all
684 * external dependencies. This mark is automatically cleared after each
685 * garbage collection.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000686 */
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(void MarkPartiallyDependent(Isolate* isolate));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000689
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000690 V8_INLINE(bool IsIndependent() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000691
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000692 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000693 V8_INLINE(bool IsIndependent(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000694
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000695 V8_INLINE(bool IsNearDeath() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000696
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000697 /** Checks if the handle holds the only reference to an object. */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000698 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000699 V8_INLINE(bool IsNearDeath(Isolate* isolate) const);
700
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000701 V8_INLINE(bool IsWeak() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000703 /** Returns true if the handle's reference is weak. */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000704 // TODO(dcarney): remove before cutover
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000705 V8_INLINE(bool IsWeak(Isolate* isolate) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000707 V8_INLINE(void SetWrapperClassId(uint16_t class_id));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000708
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000709 /**
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000710 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
711 * description in v8-profiler.h for details.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000712 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000713 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000714 V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id));
715
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000716 V8_INLINE(uint16_t WrapperClassId() const);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000717
718 /**
719 * Returns the class ID previously assigned to this handle or 0 if no class ID
720 * was previously assigned.
721 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000722 // TODO(dcarney): remove before cutover
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000723 V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000724
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000725 /**
726 * Disposes the current contents of the handle and replaces it.
727 */
728 V8_INLINE(void Reset(Isolate* isolate, const Handle<T>& other));
729
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000730 /**
731 * Returns the underlying raw pointer and clears the handle. The caller is
732 * responsible of eventually destroying the underlying object (by creating a
733 * Persistent handle which points to it and Disposing it). In the future,
734 * destructing a Persistent will also Dispose it. With this function, the
735 * embedder can let the Persistent go out of scope without it getting
736 * disposed.
737 */
738 V8_INLINE(T* ClearAndLeak());
739
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000740#ifndef V8_USE_UNSAFE_HANDLES
741
742#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000745#endif
746 // TODO(dcarney): make unlinkable before cutover
747 V8_INLINE(Persistent(const Persistent& that)) : val_(that.val_) {}
748 // TODO(dcarney): make unlinkable before cutover
749 V8_INLINE(Persistent& operator=(const Persistent& that)) { // NOLINT
750 this->val_ = that.val_;
751 return *this;
752 }
753
754 public:
755#ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
756
757 private:
758#endif
759 // TODO(dcarney): remove before cutover
760 template <class S> V8_INLINE(Persistent(S* that)) : val_(that) { }
761 // TODO(dcarney): remove before cutover
762 template <class S> V8_INLINE(Persistent(Persistent<S> that))
763 : val_(*that) {
764 TYPE_CHECK(T, S);
765 }
766 // TODO(dcarney): remove before cutover
767 V8_INLINE(T* operator*() const) { return val_; }
768 public:
769#ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW
770
771 private:
772#endif
773 // TODO(dcarney): remove before cutover
774 V8_INLINE(T* operator->() const) { return val_; }
775 public:
776#endif
777
778 private:
779 template<class F>
780 friend class Handle;
781 template<class F>
782 friend class Local;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783 friend class ImplementationUtilities;
784 friend class ObjectTemplate;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000785 friend class Context;
786 friend class InternalHandleHelper;
787 friend class LocalContext;
788
789 V8_INLINE(static Persistent<T> New(Isolate* isolate, T* that));
790
791#ifndef V8_USE_UNSAFE_HANDLES
792 T* val_;
793#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794};
795
796
v8.team.kasperl727e9952008-09-02 14:56:44 +0000797 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 * A stack-allocated class that governs a number of local handles.
799 * After a handle scope has been created, all local handles will be
800 * allocated within that handle scope until either the handle scope is
801 * deleted or another handle scope is created. If there is already a
802 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000803 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804 * new handles will again be allocated in the original handle scope.
805 *
806 * After the handle scope of a local handle has been deleted the
807 * garbage collector will no longer track the object stored in the
808 * handle and may deallocate it. The behavior of accessing a handle
809 * for which the handle scope has been deleted is undefined.
810 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000811class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 public:
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000813 // TODO(svenpanne) Deprecate me when Chrome is fixed!
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000814 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000816 HandleScope(Isolate* isolate);
817
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000818 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819
820 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821 * Closes the handle scope and returns the value as a handle in the
822 * previous scope, which is the new current scope after the call.
823 */
824 template <class T> Local<T> Close(Handle<T> value);
825
826 /**
827 * Counts the number of allocated handles.
828 */
829 static int NumberOfHandles();
830
831 /**
832 * Creates a new handle with the given value.
833 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000834 static internal::Object** CreateHandle(internal::Object* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000835 static internal::Object** CreateHandle(internal::Isolate* isolate,
836 internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000837 // Faster version, uses HeapObject to obtain the current Isolate.
838 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000839
840 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000841 // Make it hard to create heap-allocated or illegal handle scopes by
842 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 HandleScope(const HandleScope&);
844 void operator=(const HandleScope&);
845 void* operator new(size_t size);
846 void operator delete(void*, size_t);
847
ager@chromium.org3811b432009-10-28 14:53:37 +0000848 // This Data class is accessible internally as HandleScopeData through a
849 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000850 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000852 internal::Object** next;
853 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000854 int level;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000855 V8_INLINE(void Initialize()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000857 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 }
859 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000860
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000861 void Initialize(Isolate* isolate);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000862 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000864 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000865 internal::Object** prev_next_;
866 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000868 // Allow for the active closing of HandleScopes which allows to pass a handle
869 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000871 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 friend class ImplementationUtilities;
874};
875
876
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000877// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878
879
880/**
881 * The superclass of values and API object templates.
882 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000883class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 private:
885 Data();
886};
887
888
889/**
890 * Pre-compilation data that can be associated with a script. This
891 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000892 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000893 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000895class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 public:
897 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000898
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000899 /**
900 * Pre-compiles the specified script (context-independent).
901 *
902 * \param input Pointer to UTF-8 script source code.
903 * \param length Length of UTF-8 script source code.
904 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000907 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000908 * Pre-compiles the specified script (context-independent).
909 *
910 * NOTE: Pre-compilation using this method cannot happen on another thread
911 * without using Lockers.
912 *
913 * \param source Script source code.
914 */
915 static ScriptData* PreCompile(Handle<String> source);
916
917 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000918 * Load previous pre-compilation data.
919 *
920 * \param data Pointer to data returned by a call to Data() of a previous
921 * ScriptData. Ownership is not transferred.
922 * \param length Length of data.
923 */
924 static ScriptData* New(const char* data, int length);
925
926 /**
927 * Returns the length of Data().
928 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000930
931 /**
932 * Returns a serialized representation of this ScriptData that can later be
933 * passed to New(). NOTE: Serialized data is platform-dependent.
934 */
935 virtual const char* Data() = 0;
936
937 /**
938 * Returns true if the source code could not be parsed.
939 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000940 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941};
942
943
944/**
945 * The origin, within a file, of a script.
946 */
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000947class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000949 V8_INLINE(ScriptOrigin(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000950 Handle<Value> resource_name,
951 Handle<Integer> resource_line_offset = Handle<Integer>(),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000952 Handle<Integer> resource_column_offset = Handle<Integer>()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 : resource_name_(resource_name),
954 resource_line_offset_(resource_line_offset),
955 resource_column_offset_(resource_column_offset) { }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000956 V8_INLINE(Handle<Value> ResourceName() const);
957 V8_INLINE(Handle<Integer> ResourceLineOffset() const);
958 V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000960 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 Handle<Integer> resource_line_offset_;
962 Handle<Integer> resource_column_offset_;
963};
964
965
966/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000967 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000969class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000972 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000973 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000974 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000975 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000976 * when New() returns
977 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
978 * using pre_data speeds compilation if it's done multiple times.
979 * Owned by caller, no references are kept when New() returns.
980 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000981 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000982 * available to compile event handlers.
983 * \return Compiled script object (context independent; when run it
984 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000986 static Local<Script> New(Handle<String> source,
987 ScriptOrigin* origin = NULL,
988 ScriptData* pre_data = NULL,
989 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990
mads.s.agercbaa0602008-08-14 13:41:48 +0000991 /**
992 * Compiles the specified script using the specified file name
993 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000994 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000995 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000996 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000997 * as the script's origin.
998 * \return Compiled script object (context independent; when run it
999 * will use the currently entered context).
1000 */
1001 static Local<Script> New(Handle<String> source,
1002 Handle<Value> file_name);
1003
1004 /**
1005 * Compiles the specified script (bound to current context).
1006 *
1007 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001008 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +00001009 * when Compile() returns
1010 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1011 * using pre_data speeds compilation if it's done multiple times.
1012 * Owned by caller, no references are kept when Compile() returns.
1013 * \param script_data Arbitrary data associated with script. Using
1014 * this has same effect as calling SetData(), but makes data available
1015 * earlier (i.e. to compile event handlers).
1016 * \return Compiled script object, bound to the context that was active
1017 * when this function was called. When run it will always use this
1018 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +00001019 */
1020 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +00001021 ScriptOrigin* origin = NULL,
1022 ScriptData* pre_data = NULL,
1023 Handle<String> script_data = Handle<String>());
1024
1025 /**
1026 * Compiles the specified script using the specified file name
1027 * object (typically a string) as the script's origin.
1028 *
1029 * \param source Script source code.
1030 * \param file_name File name to use as script's origin
1031 * \param script_data Arbitrary data associated with script. Using
1032 * this has same effect as calling SetData(), but makes data available
1033 * earlier (i.e. to compile event handlers).
1034 * \return Compiled script object, bound to the context that was active
1035 * when this function was called. When run it will always use this
1036 * context.
1037 */
1038 static Local<Script> Compile(Handle<String> source,
1039 Handle<Value> file_name,
1040 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +00001041
v8.team.kasperl727e9952008-09-02 14:56:44 +00001042 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001043 * Runs the script returning the resulting value. If the script is
1044 * context independent (created using ::New) it will be run in the
1045 * currently entered context. If it is context specific (created
1046 * using ::Compile) it will be run in the context in which it was
1047 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001048 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001050
1051 /**
1052 * Returns the script id value.
1053 */
1054 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001055
1056 /**
1057 * Associate an additional data object with the script. This is mainly used
1058 * with the debugger as this data object is only available through the
1059 * debugger API.
1060 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00001061 void SetData(Handle<String> data);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001062
1063 /**
1064 * Returns the name value of one Script.
1065 */
1066 Handle<Value> GetScriptName();
1067
1068 /**
1069 * Returns zero based line number of the code_pos location in the script.
1070 * -1 will be returned if no information available.
1071 */
1072 int GetLineNumber(int code_pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073};
1074
1075
1076/**
1077 * An error message.
1078 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001079class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001081 Local<String> Get() const;
1082 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001084 /**
1085 * Returns the resource name for the script from where the function causing
1086 * the error originates.
1087 */
ager@chromium.org32912102009-01-16 10:38:43 +00001088 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001090 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001091 * Returns the resource data for the script from where the function causing
1092 * the error originates.
1093 */
1094 Handle<Value> GetScriptData() const;
1095
1096 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001097 * Exception stack trace. By default stack traces are not captured for
1098 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1099 * to change this option.
1100 */
1101 Handle<StackTrace> GetStackTrace() const;
1102
1103 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001104 * Returns the number, 1-based, of the line where the error occurred.
1105 */
ager@chromium.org32912102009-01-16 10:38:43 +00001106 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001108 /**
1109 * Returns the index within the script of the first character where
1110 * the error occurred.
1111 */
ager@chromium.org32912102009-01-16 10:38:43 +00001112 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001113
1114 /**
1115 * Returns the index within the script of the last character where
1116 * the error occurred.
1117 */
ager@chromium.org32912102009-01-16 10:38:43 +00001118 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001119
1120 /**
1121 * Returns the index within the line of the first character where
1122 * the error occurred.
1123 */
ager@chromium.org32912102009-01-16 10:38:43 +00001124 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001125
1126 /**
1127 * Returns the index within the line of the last character where
1128 * the error occurred.
1129 */
ager@chromium.org32912102009-01-16 10:38:43 +00001130 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001131
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132 // TODO(1245381): Print to a string instead of on a FILE.
1133 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001134
1135 static const int kNoLineNumberInfo = 0;
1136 static const int kNoColumnInfo = 0;
1137};
1138
1139
1140/**
1141 * Representation of a JavaScript stack trace. The information collected is a
1142 * snapshot of the execution stack and the information remains valid after
1143 * execution continues.
1144 */
1145class V8EXPORT StackTrace {
1146 public:
1147 /**
1148 * Flags that determine what information is placed captured for each
1149 * StackFrame when grabbing the current stack trace.
1150 */
1151 enum StackTraceOptions {
1152 kLineNumber = 1,
1153 kColumnOffset = 1 << 1 | kLineNumber,
1154 kScriptName = 1 << 2,
1155 kFunctionName = 1 << 3,
1156 kIsEval = 1 << 4,
1157 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001158 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001159 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001160 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001161 };
1162
1163 /**
1164 * Returns a StackFrame at a particular index.
1165 */
1166 Local<StackFrame> GetFrame(uint32_t index) const;
1167
1168 /**
1169 * Returns the number of StackFrames.
1170 */
1171 int GetFrameCount() const;
1172
1173 /**
1174 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1175 */
1176 Local<Array> AsArray();
1177
1178 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001179 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001180 *
1181 * \param frame_limit The maximum number of stack frames we want to capture.
1182 * \param options Enumerates the set of things we will capture for each
1183 * StackFrame.
1184 */
1185 static Local<StackTrace> CurrentStackTrace(
1186 int frame_limit,
1187 StackTraceOptions options = kOverview);
1188};
1189
1190
1191/**
1192 * A single JavaScript stack frame.
1193 */
1194class V8EXPORT StackFrame {
1195 public:
1196 /**
1197 * Returns the number, 1-based, of the line for the associate function call.
1198 * This method will return Message::kNoLineNumberInfo if it is unable to
1199 * retrieve the line number, or if kLineNumber was not passed as an option
1200 * when capturing the StackTrace.
1201 */
1202 int GetLineNumber() const;
1203
1204 /**
1205 * Returns the 1-based column offset on the line for the associated function
1206 * call.
1207 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1208 * the column number, or if kColumnOffset was not passed as an option when
1209 * capturing the StackTrace.
1210 */
1211 int GetColumn() const;
1212
1213 /**
1214 * Returns the name of the resource that contains the script for the
1215 * function for this StackFrame.
1216 */
1217 Local<String> GetScriptName() const;
1218
1219 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001220 * Returns the name of the resource that contains the script for the
1221 * function for this StackFrame or sourceURL value if the script name
1222 * is undefined and its source ends with //@ sourceURL=... string.
1223 */
1224 Local<String> GetScriptNameOrSourceURL() const;
1225
1226 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001227 * Returns the name of the function associated with this stack frame.
1228 */
1229 Local<String> GetFunctionName() const;
1230
1231 /**
1232 * Returns whether or not the associated function is compiled via a call to
1233 * eval().
1234 */
1235 bool IsEval() const;
1236
1237 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001238 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001239 * constructor via "new".
1240 */
1241 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242};
1243
1244
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001245// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246
1247
1248/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001249 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001251class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001252 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253 /**
1254 * Returns true if this value is the undefined value. See ECMA-262
1255 * 4.3.10.
1256 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001257 V8_INLINE(bool IsUndefined() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258
1259 /**
1260 * Returns true if this value is the null value. See ECMA-262
1261 * 4.3.11.
1262 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001263 V8_INLINE(bool IsNull() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264
1265 /**
1266 * Returns true if this value is true.
1267 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001268 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269
1270 /**
1271 * Returns true if this value is false.
1272 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001273 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274
1275 /**
1276 * Returns true if this value is an instance of the String type.
1277 * See ECMA-262 8.4.
1278 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001279 V8_INLINE(bool IsString() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280
1281 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001282 * Returns true if this value is a symbol.
1283 * This is an experimental feature.
1284 */
1285 bool IsSymbol() const;
1286
1287 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 * Returns true if this value is a function.
1289 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001290 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291
1292 /**
1293 * Returns true if this value is an array.
1294 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001295 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296
v8.team.kasperl727e9952008-09-02 14:56:44 +00001297 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298 * Returns true if this value is an object.
1299 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001300 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301
v8.team.kasperl727e9952008-09-02 14:56:44 +00001302 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 * Returns true if this value is boolean.
1304 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001305 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001306
v8.team.kasperl727e9952008-09-02 14:56:44 +00001307 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 * Returns true if this value is a number.
1309 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001310 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311
v8.team.kasperl727e9952008-09-02 14:56:44 +00001312 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 * Returns true if this value is external.
1314 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001315 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316
v8.team.kasperl727e9952008-09-02 14:56:44 +00001317 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 * Returns true if this value is a 32-bit signed integer.
1319 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001320 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001322 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001323 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001324 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001325 bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001326
1327 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001328 * Returns true if this value is a Date.
1329 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001330 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001331
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001332 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001333 * Returns true if this value is a Boolean object.
1334 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001335 bool IsBooleanObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001336
1337 /**
1338 * Returns true if this value is a Number object.
1339 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001340 bool IsNumberObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001341
1342 /**
1343 * Returns true if this value is a String object.
1344 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001345 bool IsStringObject() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001346
1347 /**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001348 * Returns true if this value is a Symbol object.
1349 * This is an experimental feature.
1350 */
1351 bool IsSymbolObject() const;
1352
1353 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001354 * Returns true if this value is a NativeError.
1355 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001356 bool IsNativeError() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001357
1358 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001359 * Returns true if this value is a RegExp.
1360 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001361 bool IsRegExp() const;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001362
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001363
1364 /**
1365 * Returns true if this value is an ArrayBuffer.
1366 * This is an experimental feature.
1367 */
1368 bool IsArrayBuffer() const;
1369
1370 /**
1371 * Returns true if this value is one of TypedArrays.
1372 * This is an experimental feature.
1373 */
1374 bool IsTypedArray() const;
1375
1376 /**
1377 * Returns true if this value is an Uint8Array.
1378 * This is an experimental feature.
1379 */
1380 bool IsUint8Array() const;
1381
1382 /**
1383 * Returns true if this value is an Uint8ClampedArray.
1384 * This is an experimental feature.
1385 */
1386 bool IsUint8ClampedArray() const;
1387
1388 /**
1389 * Returns true if this value is an Int8Array.
1390 * This is an experimental feature.
1391 */
1392 bool IsInt8Array() const;
1393
1394 /**
1395 * Returns true if this value is an Uint16Array.
1396 * This is an experimental feature.
1397 */
1398 bool IsUint16Array() const;
1399
1400 /**
1401 * Returns true if this value is an Int16Array.
1402 * This is an experimental feature.
1403 */
1404 bool IsInt16Array() const;
1405
1406 /**
1407 * Returns true if this value is an Uint32Array.
1408 * This is an experimental feature.
1409 */
1410 bool IsUint32Array() const;
1411
1412 /**
1413 * Returns true if this value is an Int32Array.
1414 * This is an experimental feature.
1415 */
1416 bool IsInt32Array() const;
1417
1418 /**
1419 * Returns true if this value is a Float32Array.
1420 * This is an experimental feature.
1421 */
1422 bool IsFloat32Array() const;
1423
1424 /**
1425 * Returns true if this value is a Float64Array.
1426 * This is an experimental feature.
1427 */
1428 bool IsFloat64Array() const;
1429
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001430 Local<Boolean> ToBoolean() const;
1431 Local<Number> ToNumber() const;
1432 Local<String> ToString() const;
1433 Local<String> ToDetailString() const;
1434 Local<Object> ToObject() const;
1435 Local<Integer> ToInteger() const;
1436 Local<Uint32> ToUint32() const;
1437 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438
1439 /**
1440 * Attempts to convert a string to an array index.
1441 * Returns an empty handle if the conversion fails.
1442 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001443 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001445 bool BooleanValue() const;
1446 double NumberValue() const;
1447 int64_t IntegerValue() const;
1448 uint32_t Uint32Value() const;
1449 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450
1451 /** JS == */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001452 bool Equals(Handle<Value> that) const;
1453 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001454
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001455 private:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001456 V8_INLINE(bool QuickIsUndefined() const);
1457 V8_INLINE(bool QuickIsNull() const);
1458 V8_INLINE(bool QuickIsString() const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001459 bool FullIsUndefined() const;
1460 bool FullIsNull() const;
1461 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462};
1463
1464
1465/**
1466 * The superclass of primitive values. See ECMA-262 4.3.2.
1467 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001468class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
1470
1471/**
1472 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1473 * or false value.
1474 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001475class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001477 bool Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001478 V8_INLINE(static Handle<Boolean> New(bool value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479};
1480
1481
1482/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001483 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001485class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001487 enum Encoding {
1488 UNKNOWN_ENCODING = 0x1,
1489 TWO_BYTE_ENCODING = 0x0,
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001490 ASCII_ENCODING = 0x4,
1491 ONE_BYTE_ENCODING = 0x4
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001492 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001493 /**
1494 * Returns the number of characters in this string.
1495 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001496 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497
v8.team.kasperl727e9952008-09-02 14:56:44 +00001498 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001499 * Returns the number of bytes in the UTF-8 encoded
1500 * representation of this string.
1501 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001502 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001503
1504 /**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001505 * This function is no longer useful.
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001506 */
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001507 V8_DEPRECATED(V8_INLINE(bool MayContainNonAscii()) const) { return true; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001508
1509 /**
1510 * Returns whether this string contains only one byte data.
1511 */
1512 bool IsOneByte() const;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001513
1514 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001515 * Write the contents of the string to an external buffer.
1516 * If no arguments are given, expects the buffer to be large
1517 * enough to hold the entire string and NULL terminator. Copies
1518 * the contents of the string and the NULL terminator into the
1519 * buffer.
1520 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001521 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1522 * before the end of the buffer.
1523 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001524 * Copies up to length characters into the output buffer.
1525 * Only null-terminates if there is enough space in the buffer.
1526 *
1527 * \param buffer The buffer into which the string will be copied.
1528 * \param start The starting position within the string at which
1529 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001530 * \param length The number of characters to copy from the string. For
1531 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001532 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001533 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001534 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001535 * \return The number of characters copied to the buffer excluding the null
1536 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001537 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001538 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001539 enum WriteOptions {
1540 NO_OPTIONS = 0,
1541 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001542 NO_NULL_TERMINATION = 2,
1543 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001544 };
1545
lrn@chromium.org34e60782011-09-15 07:25:40 +00001546 // 16-bit character codes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001547 int Write(uint16_t* buffer,
1548 int start = 0,
1549 int length = -1,
1550 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001551 // ASCII characters.
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001552 V8_DEPRECATED(int WriteAscii(char* buffer,
1553 int start = 0,
1554 int length = -1,
1555 int options = NO_OPTIONS) const);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001556 // One byte characters.
1557 int WriteOneByte(uint8_t* buffer,
1558 int start = 0,
1559 int length = -1,
1560 int options = NO_OPTIONS) const;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001561 // UTF-8 encoded characters.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001562 int WriteUtf8(char* buffer,
1563 int length = -1,
1564 int* nchars_ref = NULL,
1565 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566
v8.team.kasperl727e9952008-09-02 14:56:44 +00001567 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001568 * A zero length string.
1569 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001570 static v8::Local<v8::String> Empty();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001571 V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001572
1573 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001574 * Returns true if the string is external
1575 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001576 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577
v8.team.kasperl727e9952008-09-02 14:56:44 +00001578 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001579 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001580 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001581 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001582
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001583 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001584 public:
1585 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001586
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001587 protected:
1588 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001589
1590 /**
1591 * Internally V8 will call this Dispose method when the external string
1592 * resource is no longer needed. The default implementation will use the
1593 * delete operator. This method can be overridden in subclasses to
1594 * control how allocated external string resources are disposed.
1595 */
1596 virtual void Dispose() { delete this; }
1597
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001598 private:
1599 // Disallow copying and assigning.
1600 ExternalStringResourceBase(const ExternalStringResourceBase&);
1601 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001602
1603 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001604 };
1605
v8.team.kasperl727e9952008-09-02 14:56:44 +00001606 /**
1607 * An ExternalStringResource is a wrapper around a two-byte string
1608 * buffer that resides outside V8's heap. Implement an
1609 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001610 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001611 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001612 class V8EXPORT ExternalStringResource
1613 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 public:
1615 /**
1616 * Override the destructor to manage the life cycle of the underlying
1617 * buffer.
1618 */
1619 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001620
1621 /**
1622 * The string data from the underlying buffer.
1623 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001625
1626 /**
1627 * The length of the string. That is, the number of two-byte characters.
1628 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001630
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631 protected:
1632 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633 };
1634
1635 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001636 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001637 * string buffer that resides outside V8's heap. Implement an
1638 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001639 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001640 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001641 * UTF-8, which would require special treatment internally in the
1642 * engine and, in the case of UTF-8, do not allow efficient indexing.
1643 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001644 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001646 class V8EXPORT ExternalAsciiStringResource
1647 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 public:
1649 /**
1650 * Override the destructor to manage the life cycle of the underlying
1651 * buffer.
1652 */
1653 virtual ~ExternalAsciiStringResource() {}
1654 /** The string data from the underlying buffer.*/
1655 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001656 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657 virtual size_t length() const = 0;
1658 protected:
1659 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 };
1661
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001662 typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1663
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001665 * If the string is an external string, return the ExternalStringResourceBase
1666 * regardless of the encoding, otherwise return NULL. The encoding of the
1667 * string is returned in encoding_out.
1668 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001669 V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1670 Encoding* encoding_out) const);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001671
1672 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001673 * Get the ExternalStringResource for an external string. Returns
1674 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001676 V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677
1678 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001679 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001680 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001682 const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001684 V8_INLINE(static String* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001686 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001688 * Allocates a new string from either UTF-8 encoded or ASCII data.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001689 * The second parameter 'length' gives the buffer length. If omitted,
1690 * the function calls 'strlen' to determine the buffer length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001692 V8_INLINE(static Local<String> New(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001694 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001695 /** Allocates a new string from 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001696 V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001698 // TODO(dcarney): deprecate
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001699 /**
1700 * Creates an internalized string (historically called a "symbol",
1701 * not to be confused with ES6 symbols). Returns one if it exists already.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001702 */
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001703 V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
1704
1705 enum NewStringType {
1706 kNormalString, kInternalizedString, kUndetectableString
1707 };
1708
1709 /** Allocates a new string from UTF-8 data.*/
1710 static Local<String> NewFromUtf8(Isolate* isolate,
1711 const char* data,
1712 NewStringType type = kNormalString,
1713 int length = -1);
1714
1715 /** Allocates a new string from Latin-1 data.*/
1716 static Local<String> NewFromOneByte(
1717 Isolate* isolate,
1718 const uint8_t* data,
1719 NewStringType type = kNormalString,
1720 int length = -1);
1721
1722 /** Allocates a new string from UTF-16 data.*/
1723 static Local<String> NewFromTwoByte(
1724 Isolate* isolate,
1725 const uint16_t* data,
1726 NewStringType type = kNormalString,
1727 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728
v8.team.kasperl727e9952008-09-02 14:56:44 +00001729 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001730 * Creates a new string by concatenating the left and the right strings
1731 * passed in as parameters.
1732 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001733 static Local<String> Concat(Handle<String> left, Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001734
1735 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001736 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001737 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001738 * resource will be disposed by calling its Dispose method. The caller of
1739 * this function should not otherwise delete or modify the resource. Neither
1740 * should the underlying buffer be deallocated or modified except through the
1741 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001742 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001743 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001744
ager@chromium.org6f10e412009-02-13 10:11:16 +00001745 /**
1746 * Associate an external string resource with this string by transforming it
1747 * in place so that existing references to this string in the JavaScript heap
1748 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001749 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001750 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001751 * The string is not modified if the operation fails. See NewExternal for
1752 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001753 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001754 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755
v8.team.kasperl727e9952008-09-02 14:56:44 +00001756 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001757 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001758 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001759 * resource will be disposed by calling its Dispose method. The caller of
1760 * this function should not otherwise delete or modify the resource. Neither
1761 * should the underlying buffer be deallocated or modified except through the
1762 * destructor of the external string resource.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001763 */
1764 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001765
ager@chromium.org6f10e412009-02-13 10:11:16 +00001766 /**
1767 * Associate an external string resource with this string by transforming it
1768 * in place so that existing references to this string in the JavaScript heap
1769 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001770 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001771 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001772 * The string is not modified if the operation fails. See NewExternal for
1773 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001774 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001775 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001777 /**
1778 * Returns true if this string can be made external.
1779 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001780 bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001781
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001782 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001783 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001784 V8_INLINE(
1785 static Local<String> NewUndetectable(const char* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001787 // TODO(dcarney): deprecate
lrn@chromium.org34e60782011-09-15 07:25:40 +00001788 /** Creates an undetectable string from the supplied 16-bit character codes.*/
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001789 V8_INLINE(static Local<String> NewUndetectable(
1790 const uint16_t* data, int length = -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791
1792 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001793 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001794 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001795 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001796 * then the length() method returns 0 and the * operator returns
1797 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001798 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001799 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001800 public:
1801 explicit Utf8Value(Handle<v8::Value> obj);
1802 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001803 char* operator*() { return str_; }
1804 const char* operator*() const { return str_; }
1805 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001806 private:
1807 char* str_;
1808 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001809
1810 // Disallow copying and assigning.
1811 Utf8Value(const Utf8Value&);
1812 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001813 };
1814
1815 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001816 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001818 * If conversion to a string fails (eg. due to an exception in the toString()
1819 * method of the object) then the length() method returns 0 and the * operator
1820 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001822 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 public:
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001824 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 explicit AsciiValue(Handle<v8::Value> obj);
1826 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001827 char* operator*() { return str_; }
1828 const char* operator*() const { return str_; }
1829 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830 private:
1831 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001832 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001833
1834 // Disallow copying and assigning.
1835 AsciiValue(const AsciiValue&);
1836 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 };
1838
1839 /**
1840 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001841 * If conversion to a string fails (eg. due to an exception in the toString()
1842 * method of the object) then the length() method returns 0 and the * operator
1843 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001845 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 public:
1847 explicit Value(Handle<v8::Value> obj);
1848 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001849 uint16_t* operator*() { return str_; }
1850 const uint16_t* operator*() const { return str_; }
1851 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852 private:
1853 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001854 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001855
1856 // Disallow copying and assigning.
1857 Value(const Value&);
1858 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001860
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001861 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001862 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1863 Encoding encoding) const;
1864 void VerifyExternalStringResource(ExternalStringResource* val) const;
1865 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866};
1867
1868
1869/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001870 * A JavaScript symbol (ECMA-262 edition 6)
1871 *
1872 * This is an experimental feature. Use at your own risk.
1873 */
1874class V8EXPORT Symbol : public Primitive {
1875 public:
1876 // Returns the print name string of the symbol, or undefined if none.
1877 Local<Value> Name() const;
1878
1879 // Create a symbol without a print name.
1880 static Local<Symbol> New(Isolate* isolate);
1881
1882 // Create a symbol with a print name.
1883 static Local<Symbol> New(Isolate *isolate, const char* data, int length = -1);
1884
1885 V8_INLINE(static Symbol* Cast(v8::Value* obj));
1886 private:
1887 Symbol();
1888 static void CheckCast(v8::Value* obj);
1889};
1890
1891
1892/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001893 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001895class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001897 double Value() const;
1898 static Local<Number> New(double value);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001899 static Local<Number> New(Isolate* isolate, double value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001900 V8_INLINE(static Number* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001901 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001902 Number();
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 signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001910class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001912 static Local<Integer> New(int32_t value);
1913 static Local<Integer> NewFromUnsigned(uint32_t value);
1914 static Local<Integer> New(int32_t value, Isolate*);
1915 static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1916 int64_t Value() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00001917 V8_INLINE(static Integer* Cast(v8::Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001919 Integer();
1920 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921};
1922
1923
1924/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001925 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001927class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001929 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001931 Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932};
1933
1934
1935/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001936 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001938class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001940 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001942 Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943};
1944
1945
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946enum PropertyAttribute {
1947 None = 0,
1948 ReadOnly = 1 << 0,
1949 DontEnum = 1 << 1,
1950 DontDelete = 1 << 2
1951};
1952
ager@chromium.org3811b432009-10-28 14:53:37 +00001953enum ExternalArrayType {
1954 kExternalByteArray = 1,
1955 kExternalUnsignedByteArray,
1956 kExternalShortArray,
1957 kExternalUnsignedShortArray,
1958 kExternalIntArray,
1959 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001960 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001961 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001962 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001963};
1964
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001966 * Accessor[Getter|Setter] are used as callback functions when
1967 * setting|getting a particular property. See Object and ObjectTemplate's
1968 * method SetAccessor.
1969 */
1970typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1971 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001972typedef void (*AccessorGetterCallback)(
1973 Local<String> property,
1974 const PropertyCallbackInfo<Value>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001975
1976
1977typedef void (*AccessorSetter)(Local<String> property,
1978 Local<Value> value,
1979 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00001980typedef void (*AccessorSetterCallback)(
1981 Local<String> property,
1982 Local<Value> value,
1983 const PropertyCallbackInfo<void>& info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001984
1985
1986/**
1987 * Access control specifications.
1988 *
1989 * Some accessors should be accessible across contexts. These
1990 * accessors have an explicit access control parameter which specifies
1991 * the kind of cross-context access that should be allowed.
1992 *
1993 * Additionally, for security, accessors can prohibit overwriting by
1994 * accessors defined in JavaScript. For objects that have such
1995 * accessors either locally or in their prototype chain it is not
1996 * possible to overwrite the accessor by using __defineGetter__ or
1997 * __defineSetter__ from JavaScript code.
1998 */
1999enum AccessControl {
2000 DEFAULT = 0,
2001 ALL_CAN_READ = 1,
2002 ALL_CAN_WRITE = 1 << 1,
2003 PROHIBITS_OVERWRITING = 1 << 2
2004};
2005
2006
2007/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002008 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002010class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002012 bool Set(Handle<Value> key,
2013 Handle<Value> value,
2014 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002015
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002016 bool Set(uint32_t index, Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002017
ager@chromium.orge2902be2009-06-08 12:21:35 +00002018 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002019 // overriding accessors or read-only properties.
2020 //
2021 // Note that if the object has an interceptor the property will be set
2022 // locally, but since the interceptor takes precedence the local property
2023 // will only be returned if the interceptor doesn't return a value.
2024 //
2025 // Note also that this only works for named properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002026 bool ForceSet(Handle<Value> key,
2027 Handle<Value> value,
2028 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002029
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002030 Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002032 Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002033
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002034 /**
2035 * Gets the property attributes of a property which can be None or
2036 * any combination of ReadOnly, DontEnum and DontDelete. Returns
2037 * None when the property doesn't exist.
2038 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002039 PropertyAttribute GetPropertyAttributes(Handle<Value> key);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002040
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002041 bool Has(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002042
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002043 bool Delete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002044
2045 // Delete a property on this object bypassing interceptors and
2046 // ignoring dont-delete attributes.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002047 bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002048
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002049 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002050
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002051 bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002053 // TODO(dcarney): deprecate
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002054 bool SetAccessor(Handle<String> name,
2055 AccessorGetter getter,
2056 AccessorSetter setter = 0,
2057 Handle<Value> data = Handle<Value>(),
2058 AccessControl settings = DEFAULT,
2059 PropertyAttribute attribute = None);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002060 bool SetAccessor(Handle<String> name,
2061 AccessorGetterCallback getter,
2062 AccessorSetterCallback setter = 0,
2063 Handle<Value> data = Handle<Value>(),
2064 AccessControl settings = DEFAULT,
2065 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002066
ulan@chromium.org750145a2013-03-07 15:14:13 +00002067 // This function is not yet stable and should not be used at this time.
2068 bool SetAccessor(Handle<String> name,
2069 Handle<DeclaredAccessorDescriptor> descriptor,
2070 AccessControl settings = DEFAULT,
2071 PropertyAttribute attribute = None);
2072
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002074 * Returns an array containing the names of the enumerable properties
2075 * of this object, including properties from prototype objects. The
2076 * array returned by this method contains the same values as would
2077 * be enumerated by a for-in statement over this object.
2078 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002079 Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002080
2081 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002082 * This function has the same functionality as GetPropertyNames but
2083 * the returned array doesn't contain the names of properties from
2084 * prototype objects.
2085 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002086 Local<Array> GetOwnPropertyNames();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002087
2088 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 * Get the prototype object. This does not skip objects marked to
2090 * be skipped by __proto__ and it does not consult the security
2091 * handler.
2092 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002093 Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094
2095 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002096 * Set the prototype object. This does not skip objects marked to
2097 * be skipped by __proto__ and it does not consult the security
2098 * handler.
2099 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002100 bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00002101
2102 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002103 * Finds an instance of the given function template in the prototype
2104 * chain.
2105 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002106 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00002107
2108 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 * Call builtin Object.prototype.toString on this object.
2110 * This is different from Value::ToString() that may call
2111 * user-defined toString function. This one does not.
2112 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002113 Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002115 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002116 * Returns the function invoked as a constructor for this object.
2117 * May be the null value.
2118 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002119 Local<Value> GetConstructor();
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00002120
2121 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002122 * Returns the name of the function invoked as a constructor for this object.
2123 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002124 Local<String> GetConstructorName();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00002125
kasper.lund212ac232008-07-16 07:07:30 +00002126 /** Gets the number of internal fields for this Object. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002127 int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002128
2129 /** Gets the value from an internal field. */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002130 V8_INLINE(Local<Value> GetInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002131
kasper.lund212ac232008-07-16 07:07:30 +00002132 /** Sets the value in an internal field. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002133 void SetInternalField(int index, Handle<Value> value);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002134
2135 /**
2136 * Gets a 2-byte-aligned native pointer from an internal field. This field
2137 * must have been set by SetAlignedPointerInInternalField, everything else
2138 * leads to undefined behavior.
2139 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002140 V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002141
2142 /**
2143 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2144 * a field, GetAlignedPointerFromInternalField must be used, everything else
2145 * leads to undefined behavior.
2146 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002147 void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149 // Testers for local properties.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002150 bool HasOwnProperty(Handle<String> key);
2151 bool HasRealNamedProperty(Handle<String> key);
2152 bool HasRealIndexedProperty(uint32_t index);
2153 bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154
2155 /**
2156 * If result.IsEmpty() no real property was located in the prototype chain.
2157 * This means interceptors in the prototype chain are not called.
2158 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002159 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00002160
2161 /**
2162 * If result.IsEmpty() no real property was located on the object or
2163 * in the prototype chain.
2164 * This means interceptors in the prototype chain are not called.
2165 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002166 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167
2168 /** Tests for a named lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002169 bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170
kasper.lund212ac232008-07-16 07:07:30 +00002171 /** Tests for an index lookup interceptor.*/
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002172 bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002174 /**
2175 * Turns on access check on the object if the object is an instance of
2176 * a template that has access check callbacks. If an object has no
2177 * access check info, the object cannot be accessed by anyone.
2178 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002179 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00002180
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002181 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002182 * Returns the identity hash for this object. The current implementation
2183 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002184 *
ager@chromium.org9085a012009-05-11 19:22:57 +00002185 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00002186 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002187 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002188 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00002189
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002190 /**
2191 * Access hidden properties on JavaScript objects. These properties are
2192 * hidden from the executing JavaScript and only accessible through the V8
2193 * C++ API. Hidden properties introduced by V8 internally (for example the
2194 * identity hash) are prefixed with "v8::".
2195 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002196 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
2197 Local<Value> GetHiddenValue(Handle<String> key);
2198 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002199
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002200 /**
2201 * Returns true if this is an instance of an api function (one
2202 * created from a function created from a function template) and has
2203 * been modified since it was created. Note that this method is
2204 * conservative and may return true for objects that haven't actually
2205 * been modified.
2206 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002207 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00002208
2209 /**
2210 * Clone this object with a fast but shallow copy. Values will point
2211 * to the same values as the original object.
2212 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002213 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002215 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002216 * Returns the context in which the object was created.
2217 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002218 Local<Context> CreationContext();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002219
2220 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002221 * Set the backing store of the indexed properties to be managed by the
2222 * embedding layer. Access to the indexed properties will follow the rules
2223 * spelled out in CanvasPixelArray.
2224 * Note: The embedding program still owns the data and needs to ensure that
2225 * the backing store is preserved while V8 has a reference.
2226 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002227 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
2228 bool HasIndexedPropertiesInPixelData();
2229 uint8_t* GetIndexedPropertiesPixelData();
2230 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002231
ager@chromium.org3811b432009-10-28 14:53:37 +00002232 /**
2233 * Set the backing store of the indexed properties to be managed by the
2234 * embedding layer. Access to the indexed properties will follow the rules
2235 * spelled out for the CanvasArray subtypes in the WebGL specification.
2236 * Note: The embedding program still owns the data and needs to ensure that
2237 * the backing store is preserved while V8 has a reference.
2238 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002239 void SetIndexedPropertiesToExternalArrayData(void* data,
2240 ExternalArrayType array_type,
2241 int number_of_elements);
2242 bool HasIndexedPropertiesInExternalArrayData();
2243 void* GetIndexedPropertiesExternalArrayData();
2244 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
2245 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00002246
lrn@chromium.org1c092762011-05-09 09:42:16 +00002247 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002248 * Checks whether a callback is set by the
2249 * ObjectTemplate::SetCallAsFunctionHandler method.
2250 * When an Object is callable this method returns true.
2251 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002252 bool IsCallable();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002253
2254 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00002255 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002256 * ObjectTemplate::SetCallAsFunctionHandler method.
2257 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002258 Local<Value> CallAsFunction(Handle<Object> recv,
2259 int argc,
2260 Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002261
2262 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002263 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00002264 * ObjectTemplate::SetCallAsFunctionHandler method.
2265 * Note: This method behaves like the Function::NewInstance method.
2266 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002267 Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
lrn@chromium.org1c092762011-05-09 09:42:16 +00002268
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002269 static Local<Object> New();
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002270 V8_INLINE(static Object* Cast(Value* obj));
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002271
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002273 Object();
2274 static void CheckCast(Value* obj);
2275 Local<Value> SlowGetInternalField(int index);
2276 void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277};
2278
2279
2280/**
2281 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
2282 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002283class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002285 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286
ager@chromium.org3e875802009-06-29 08:26:34 +00002287 /**
2288 * Clones an element at index |index|. Returns an empty
2289 * handle if cloning fails (for any reason).
2290 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002291 Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00002292
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002293 /**
2294 * Creates a JavaScript array with the given length. If the length
2295 * is negative the returned array will have length 0.
2296 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002297 static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002298
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002299 V8_INLINE(static Array* Cast(Value* obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002301 Array();
2302 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303};
2304
2305
2306/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002307 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002309class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002311 Local<Object> NewInstance() const;
2312 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
2313 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
2314 void SetName(Handle<String> name);
2315 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00002316
2317 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002318 * Name inferred from variable or property assignment of this function.
2319 * Used to facilitate debugging and profiling of JavaScript code written
2320 * in an OO style, where many functions are anonymous but are assigned
2321 * to object properties.
2322 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002323 Handle<Value> GetInferredName() const;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002324
2325 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002326 * Returns zero based line number of function body and
2327 * kLineOffsetNotFound if no information available.
2328 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002329 int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002330 /**
2331 * Returns zero based column number of function body and
2332 * kLineOffsetNotFound if no information available.
2333 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002334 int GetScriptColumnNumber() const;
2335 Handle<Value> GetScriptId() const;
2336 ScriptOrigin GetScriptOrigin() const;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002337 V8_INLINE(static Function* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002338 static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00002339
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002341 Function();
2342 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343};
2344
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002345/**
2346 * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
2347 * populates an instance of this class with a pointer to data and byte length.
2348 *
2349 * |ArrayBufferContents| is the owner of its data. When an instance of
2350 * this class is destructed, the |Data| is freed.
2351 *
2352 * This API is experimental and may change significantly.
2353 */
2354class V8EXPORT ArrayBufferContents {
2355 public:
2356 ArrayBufferContents() : data_(NULL), byte_length_(0) {}
2357 ~ArrayBufferContents();
2358
2359 void* Data() const { return data_; }
2360 size_t ByteLength() const { return byte_length_; }
2361
2362 private:
2363 void* data_;
2364 size_t byte_length_;
2365
2366 friend class ArrayBuffer;
2367};
2368
2369#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
2370#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
2371#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372
2373/**
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002374 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
2375 * This API is experimental and may change significantly.
2376 */
2377class V8EXPORT ArrayBuffer : public Object {
2378 public:
2379 /**
2380 * Data length in bytes.
2381 */
2382 size_t ByteLength() const;
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002383
2384 /**
2385 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
2386 * Allocated memory will be owned by a created ArrayBuffer and
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002387 * will be deallocated when it is garbage-collected,
2388 * unless the object is externalized.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002389 */
2390 static Local<ArrayBuffer> New(size_t byte_length);
2391
2392 /**
2393 * Create a new ArrayBuffer over an existing memory block.
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002394 * The created array buffer is immediately in externalized state.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002395 * The memory block will not be reclaimed when a created ArrayBuffer
2396 * is garbage-collected.
2397 */
2398 static Local<ArrayBuffer> New(void* data, size_t byte_length);
2399
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002400 /**
2401 * Returns true if ArrayBuffer is extrenalized, that is, does not
2402 * own its memory block.
2403 */
2404 bool IsExternal() const;
2405
2406 /**
2407 * Pass the ownership of this ArrayBuffer's backing store to
2408 * a given ArrayBufferContents.
2409 */
2410 void Externalize(ArrayBufferContents* contents);
2411
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002412 V8_INLINE(static ArrayBuffer* Cast(Value* obj));
2413
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002414
2415 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
2416
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002417 private:
2418 ArrayBuffer();
2419 static void CheckCast(Value* obj);
2420};
2421
2422
2423/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002424 * A base class for an instance of TypedArray series of constructors
2425 * (ES6 draft 15.13.6).
2426 * This API is experimental and may change significantly.
2427 */
2428class V8EXPORT TypedArray : public Object {
2429 public:
2430 /**
2431 * Returns underlying ArrayBuffer.
2432 */
2433 Local<ArrayBuffer> Buffer();
2434 /**
2435 * Byte offset in |Buffer|
2436 */
2437 size_t ByteOffset();
2438 /**
2439 * Numbe of elements in this typed array.
2440 */
2441 size_t Length();
2442 /**
2443 * Size of typed array in bytes (e.g. for Int16Array, 2*|Length|).
2444 */
2445 size_t ByteLength();
2446 /**
2447 * Base address of typed array.
2448 */
2449 void* BaseAddress();
2450
2451 V8_INLINE(static TypedArray* Cast(Value* obj));
2452
2453 private:
2454 TypedArray();
2455 static void CheckCast(Value* obj);
2456};
2457
2458
2459/**
2460 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
2461 * This API is experimental and may change significantly.
2462 */
2463class V8EXPORT Uint8Array : public TypedArray {
2464 public:
2465 static Local<Uint8Array> New(Handle<ArrayBuffer> array_buffer,
2466 size_t byte_offset, size_t length);
2467 V8_INLINE(static Uint8Array* Cast(Value* obj));
2468
2469 private:
2470 Uint8Array();
2471 static void CheckCast(Value* obj);
2472};
2473
2474
2475/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002476 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
2477 * This API is experimental and may change significantly.
2478 */
2479class V8EXPORT Uint8ClampedArray : public TypedArray {
2480 public:
2481 static Local<Uint8ClampedArray> New(Handle<ArrayBuffer> array_buffer,
2482 size_t byte_offset, size_t length);
2483 V8_INLINE(static Uint8ClampedArray* Cast(Value* obj));
2484
2485 private:
2486 Uint8ClampedArray();
2487 static void CheckCast(Value* obj);
2488};
2489
2490/**
danno@chromium.orgf005df62013-04-30 16:36:45 +00002491 * An instance of Int8Array constructor (ES6 draft 15.13.6).
2492 * This API is experimental and may change significantly.
2493 */
2494class V8EXPORT Int8Array : public TypedArray {
2495 public:
2496 static Local<Int8Array> New(Handle<ArrayBuffer> array_buffer,
2497 size_t byte_offset, size_t length);
2498 V8_INLINE(static Int8Array* Cast(Value* obj));
2499
2500 private:
2501 Int8Array();
2502 static void CheckCast(Value* obj);
2503};
2504
2505
2506/**
2507 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
2508 * This API is experimental and may change significantly.
2509 */
2510class V8EXPORT Uint16Array : public TypedArray {
2511 public:
2512 static Local<Uint16Array> New(Handle<ArrayBuffer> array_buffer,
2513 size_t byte_offset, size_t length);
2514 V8_INLINE(static Uint16Array* Cast(Value* obj));
2515
2516 private:
2517 Uint16Array();
2518 static void CheckCast(Value* obj);
2519};
2520
2521
2522/**
2523 * An instance of Int16Array constructor (ES6 draft 15.13.6).
2524 * This API is experimental and may change significantly.
2525 */
2526class V8EXPORT Int16Array : public TypedArray {
2527 public:
2528 static Local<Int16Array> New(Handle<ArrayBuffer> array_buffer,
2529 size_t byte_offset, size_t length);
2530 V8_INLINE(static Int16Array* Cast(Value* obj));
2531
2532 private:
2533 Int16Array();
2534 static void CheckCast(Value* obj);
2535};
2536
2537
2538/**
2539 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
2540 * This API is experimental and may change significantly.
2541 */
2542class V8EXPORT Uint32Array : public TypedArray {
2543 public:
2544 static Local<Uint32Array> New(Handle<ArrayBuffer> array_buffer,
2545 size_t byte_offset, size_t length);
2546 V8_INLINE(static Uint32Array* Cast(Value* obj));
2547
2548 private:
2549 Uint32Array();
2550 static void CheckCast(Value* obj);
2551};
2552
2553
2554/**
2555 * An instance of Int32Array constructor (ES6 draft 15.13.6).
2556 * This API is experimental and may change significantly.
2557 */
2558class V8EXPORT Int32Array : public TypedArray {
2559 public:
2560 static Local<Int32Array> New(Handle<ArrayBuffer> array_buffer,
2561 size_t byte_offset, size_t length);
2562 V8_INLINE(static Int32Array* Cast(Value* obj));
2563
2564 private:
2565 Int32Array();
2566 static void CheckCast(Value* obj);
2567};
2568
2569
2570/**
2571 * An instance of Float32Array constructor (ES6 draft 15.13.6).
2572 * This API is experimental and may change significantly.
2573 */
2574class V8EXPORT Float32Array : public TypedArray {
2575 public:
2576 static Local<Float32Array> New(Handle<ArrayBuffer> array_buffer,
2577 size_t byte_offset, size_t length);
2578 V8_INLINE(static Float32Array* Cast(Value* obj));
2579
2580 private:
2581 Float32Array();
2582 static void CheckCast(Value* obj);
2583};
2584
2585
2586/**
2587 * An instance of Float64Array constructor (ES6 draft 15.13.6).
2588 * This API is experimental and may change significantly.
2589 */
2590class V8EXPORT Float64Array : public TypedArray {
2591 public:
2592 static Local<Float64Array> New(Handle<ArrayBuffer> array_buffer,
2593 size_t byte_offset, size_t length);
2594 V8_INLINE(static Float64Array* Cast(Value* obj));
2595
2596 private:
2597 Float64Array();
2598 static void CheckCast(Value* obj);
2599};
2600
2601
2602/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002603 * An instance of the built-in Date constructor (ECMA-262, 15.9).
2604 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002605class V8EXPORT Date : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002606 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002607 static Local<Value> New(double time);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002608
2609 /**
2610 * A specialization of Value::NumberValue that is more efficient
2611 * because we know the structure of this object.
2612 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002613 double NumberValue() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002614
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002615 V8_INLINE(static Date* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002616
2617 /**
2618 * Notification that the embedder has changed the time zone,
2619 * daylight savings time, or other date / time configuration
2620 * parameters. V8 keeps a cache of various values used for
2621 * date / time computation. This notification will reset
2622 * those cached values for the current context so that date /
2623 * time configuration changes would be reflected in the Date
2624 * object.
2625 *
2626 * This API should not be called more than needed as it will
2627 * negatively impact the performance of date operations.
2628 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002629 static void DateTimeConfigurationChangeNotification();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002630
2631 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002632 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002633};
2634
2635
2636/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002637 * A Number object (ECMA-262, 4.3.21).
2638 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002639class V8EXPORT NumberObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002640 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002641 static Local<Value> New(double value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002642
2643 /**
2644 * Returns the Number held by the object.
2645 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002646 double NumberValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002647
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002648 V8_INLINE(static NumberObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002649
2650 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002651 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002652};
2653
2654
2655/**
2656 * A Boolean object (ECMA-262, 4.3.15).
2657 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002658class V8EXPORT BooleanObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002659 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002660 static Local<Value> New(bool value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002661
2662 /**
2663 * Returns the Boolean held by the object.
2664 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002665 bool BooleanValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002666
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002667 V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002668
2669 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002670 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002671};
2672
2673
2674/**
2675 * A String object (ECMA-262, 4.3.18).
2676 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002677class V8EXPORT StringObject : public Object {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002678 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002679 static Local<Value> New(Handle<String> value);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002680
2681 /**
2682 * Returns the String held by the object.
2683 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002684 Local<String> StringValue() const;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002685
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002686 V8_INLINE(static StringObject* Cast(v8::Value* obj));
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002687
2688 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002689 static void CheckCast(v8::Value* obj);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002690};
2691
2692
2693/**
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002694 * A Symbol object (ECMA-262 edition 6).
2695 *
2696 * This is an experimental feature. Use at your own risk.
2697 */
2698class V8EXPORT SymbolObject : public Object {
2699 public:
2700 static Local<Value> New(Isolate* isolate, Handle<Symbol> value);
2701
2702 /**
2703 * Returns the Symbol held by the object.
2704 */
2705 Local<Symbol> SymbolValue() const;
2706
2707 V8_INLINE(static SymbolObject* Cast(v8::Value* obj));
2708
2709 private:
2710 static void CheckCast(v8::Value* obj);
2711};
2712
2713
2714/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002715 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
2716 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002717class V8EXPORT RegExp : public Object {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002718 public:
2719 /**
2720 * Regular expression flag bits. They can be or'ed to enable a set
2721 * of flags.
2722 */
2723 enum Flags {
2724 kNone = 0,
2725 kGlobal = 1,
2726 kIgnoreCase = 2,
2727 kMultiline = 4
2728 };
2729
2730 /**
2731 * Creates a regular expression from the given pattern string and
2732 * the flags bit field. May throw a JavaScript exception as
2733 * described in ECMA-262, 15.10.4.1.
2734 *
2735 * For example,
2736 * RegExp::New(v8::String::New("foo"),
2737 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
2738 * is equivalent to evaluating "/foo/gm".
2739 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002740 static Local<RegExp> New(Handle<String> pattern, Flags flags);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002741
2742 /**
2743 * Returns the value of the source property: a string representing
2744 * the regular expression.
2745 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002746 Local<String> GetSource() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002747
2748 /**
2749 * Returns the flags bit field.
2750 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002751 Flags GetFlags() const;
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002752
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002753 V8_INLINE(static RegExp* Cast(v8::Value* obj));
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002754
2755 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002756 static void CheckCast(v8::Value* obj);
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002757};
2758
2759
2760/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00002761 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2762 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002764class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765 public:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002766 static Local<External> New(void* value);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002767 V8_INLINE(static External* Cast(Value* obj));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002768 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002769 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002770 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771};
2772
2773
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002774// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002775
2776
2777/**
2778 * The superclass of object and function templates.
2779 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002780class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002781 public:
2782 /** Adds a property to each instance created by this template.*/
2783 void Set(Handle<String> name, Handle<Data> value,
2784 PropertyAttribute attributes = None);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002785 V8_INLINE(void Set(const char* name, Handle<Data> value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786 private:
2787 Template();
2788
2789 friend class ObjectTemplate;
2790 friend class FunctionTemplate;
2791};
2792
2793
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002794template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002795class ReturnValue {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002796 public:
2797 V8_INLINE(explicit ReturnValue(internal::Object** slot));
2798 // Handle setters
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00002799 template <typename S> V8_INLINE(void Set(const Persistent<S>& handle));
2800 template <typename S> V8_INLINE(void Set(const Handle<S> handle));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002801 // Fast primitive setters
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002802 V8_INLINE(void Set(bool value));
2803 V8_INLINE(void Set(double i));
2804 V8_INLINE(void Set(int32_t i));
2805 V8_INLINE(void Set(uint32_t i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002806 // Fast JS primitive setters
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002807 V8_INLINE(void SetNull());
2808 V8_INLINE(void SetUndefined());
2809 // Convenience getter for Isolate
2810 V8_INLINE(Isolate* GetIsolate());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002811 private:
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002812 internal::Object** value_;
2813};
2814
2815
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816/**
2817 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002818 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819 * including the receiver, the number and values of arguments, and
2820 * the holder of the function.
2821 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002822template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002823class FunctionCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002825 V8_INLINE(int Length() const);
2826 V8_INLINE(Local<Value> operator[](int i) const);
2827 V8_INLINE(Local<Function> Callee() const);
2828 V8_INLINE(Local<Object> This() const);
2829 V8_INLINE(Local<Object> Holder() const);
2830 V8_INLINE(bool IsConstructCall() const);
2831 V8_INLINE(Local<Value> Data() const);
2832 V8_INLINE(Isolate* GetIsolate() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002833 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2834 // This shouldn't be public, but the arm compiler needs it.
2835 static const int kArgsLength = 5;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002836
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002837 protected:
2838 friend class internal::FunctionCallbackArguments;
2839 friend class internal::CustomArguments<FunctionCallbackInfo>;
2840 static const int kReturnValueIndex = 0;
2841 static const int kIsolateIndex = -1;
2842 static const int kDataIndex = -2;
2843 static const int kCalleeIndex = -3;
2844 static const int kHolderIndex = -4;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002845
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002846 V8_INLINE(FunctionCallbackInfo(internal::Object** implicit_args,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002847 internal::Object** values,
2848 int length,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002849 bool is_construct_call));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002850 internal::Object** implicit_args_;
2851 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002852 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002853 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002854};
2855
2856
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002857class V8EXPORT Arguments : public FunctionCallbackInfo<Value> {
2858 private:
2859 friend class internal::FunctionCallbackArguments;
2860 V8_INLINE(Arguments(internal::Object** implicit_args,
2861 internal::Object** values,
2862 int length,
2863 bool is_construct_call));
2864};
2865
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002866/**
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002867 * The information passed to a property callback about the context
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868 * of the property access.
2869 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002870template<typename T>
ulan@chromium.org867c3b02013-05-22 15:42:42 +00002871class PropertyCallbackInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002872 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00002873 V8_INLINE(Isolate* GetIsolate() const);
2874 V8_INLINE(Local<Value> Data() const);
2875 V8_INLINE(Local<Object> This() const);
2876 V8_INLINE(Local<Object> Holder() const);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002877 V8_INLINE(ReturnValue<T> GetReturnValue() const);
2878 // This shouldn't be public, but the arm compiler needs it.
2879 static const int kArgsLength = 5;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002880
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002881 protected:
2882 friend class MacroAssembler;
2883 friend class internal::PropertyCallbackArguments;
2884 friend class internal::CustomArguments<PropertyCallbackInfo>;
2885 static const int kThisIndex = 0;
2886 static const int kHolderIndex = -1;
2887 static const int kDataIndex = -2;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002888 static const int kReturnValueIndex = -3;
2889 static const int kIsolateIndex = -4;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002890
2891 V8_INLINE(PropertyCallbackInfo(internal::Object** args))
2892 : args_(args) { }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002893 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002894};
2895
2896
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002897class V8EXPORT AccessorInfo : public PropertyCallbackInfo<Value> {
2898 private:
2899 friend class internal::PropertyCallbackArguments;
2900 V8_INLINE(AccessorInfo(internal::Object** args))
2901 : PropertyCallbackInfo<Value>(args) { }
2902};
2903
2904
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002905typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002906typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002907
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002909 * NamedProperty[Getter|Setter] are used as interceptors on object.
2910 * See ObjectTemplate::SetNamedPropertyHandler.
2911 */
2912typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2913 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002914typedef void (*NamedPropertyGetterCallback)(
2915 Local<String> property,
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> (*NamedPropertySetter)(Local<String> property,
2924 Local<Value> value,
2925 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002926typedef void (*NamedPropertySetterCallback)(
2927 Local<String> property,
2928 Local<Value> value,
2929 const PropertyCallbackInfo<Value>& info);
2930
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002931
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002932/**
2933 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002934 * The result is an integer encoding property attributes (like v8::None,
2935 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002937typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2938 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002939typedef void (*NamedPropertyQueryCallback)(
2940 Local<String> property,
2941 const PropertyCallbackInfo<Integer>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002942
2943
2944/**
2945 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002946 * The return value is true if the property could be deleted and false
2947 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002948 */
2949typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2950 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002951typedef void (*NamedPropertyDeleterCallback)(
2952 Local<String> property,
2953 const PropertyCallbackInfo<Boolean>& info);
2954
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955
2956/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002957 * Returns an array containing the names of the properties the named
2958 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959 */
2960typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002961typedef void (*NamedPropertyEnumeratorCallback)(
2962 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002963
v8.team.kasperl727e9952008-09-02 14:56:44 +00002964
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002966 * Returns the value of the property if the getter intercepts the
2967 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002968 */
2969typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2970 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002971typedef void (*IndexedPropertyGetterCallback)(
2972 uint32_t index,
2973 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002974
2975
2976/**
2977 * Returns the value if the setter intercepts the request.
2978 * Otherwise, returns an empty handle.
2979 */
2980typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2981 Local<Value> value,
2982 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002983typedef void (*IndexedPropertySetterCallback)(
2984 uint32_t index,
2985 Local<Value> value,
2986 const PropertyCallbackInfo<Value>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002987
2988
2989/**
2990 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002991 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002992 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002993typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2994 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00002995typedef void (*IndexedPropertyQueryCallback)(
2996 uint32_t index,
2997 const PropertyCallbackInfo<Integer>& info);
2998
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002999
3000/**
3001 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003002 * The return value is true if the property could be deleted and false
3003 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003004 */
3005typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
3006 const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003007typedef void (*IndexedPropertyDeleterCallback)(
3008 uint32_t index,
3009 const PropertyCallbackInfo<Boolean>& info);
3010
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011
v8.team.kasperl727e9952008-09-02 14:56:44 +00003012/**
3013 * Returns an array containing the indices of the properties the
3014 * indexed property getter intercepts.
3015 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003016typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003017typedef void (*IndexedPropertyEnumeratorCallback)(
3018 const PropertyCallbackInfo<Array>& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003019
3020
3021/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003022 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023 */
3024enum AccessType {
3025 ACCESS_GET,
3026 ACCESS_SET,
3027 ACCESS_HAS,
3028 ACCESS_DELETE,
3029 ACCESS_KEYS
3030};
3031
v8.team.kasperl727e9952008-09-02 14:56:44 +00003032
3033/**
3034 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003035 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003036 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003037typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003038 Local<Value> key,
3039 AccessType type,
3040 Local<Value> data);
3041
v8.team.kasperl727e9952008-09-02 14:56:44 +00003042
3043/**
3044 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003045 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003046 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00003047typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003048 uint32_t index,
3049 AccessType type,
3050 Local<Value> data);
3051
3052
3053/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003054 * A FunctionTemplate is used to create functions at runtime. There
3055 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003056 * context. The lifetime of the created function is equal to the
3057 * lifetime of the context. So in case the embedder needs to create
3058 * temporary functions that can be collected using Scripts is
3059 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003060 *
3061 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003062 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003063 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003064 * A FunctionTemplate has a corresponding instance template which is
3065 * used to create object instances when the function is used as a
3066 * constructor. Properties added to the instance template are added to
3067 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068 *
3069 * A FunctionTemplate can have a prototype template. The prototype template
3070 * is used to create the prototype object of the function.
3071 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003072 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003073 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003074 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003075 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
3076 * t->Set("func_property", v8::Number::New(1));
3077 *
3078 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
3079 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
3080 * proto_t->Set("proto_const", v8::Number::New(2));
3081 *
3082 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
3083 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
3084 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
3085 * instance_t->Set("instance_property", Number::New(3));
3086 *
3087 * v8::Local<v8::Function> function = t->GetFunction();
3088 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003089 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003090 *
3091 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00003092 * and "instance" for the instance object created above. The function
3093 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003094 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003095 * \code
3096 * func_property in function == true;
3097 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003098 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003099 * function.prototype.proto_method() invokes 'InvokeCallback'
3100 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003102 * instance instanceof function == true;
3103 * instance.instance_accessor calls 'InstanceAccessorCallback'
3104 * instance.instance_property == 3;
3105 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003106 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003107 * A FunctionTemplate can inherit from another one by calling the
3108 * FunctionTemplate::Inherit method. The following graph illustrates
3109 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003110 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003111 * \code
3112 * FunctionTemplate Parent -> Parent() . prototype -> { }
3113 * ^ ^
3114 * | Inherit(Parent) | .__proto__
3115 * | |
3116 * FunctionTemplate Child -> Child() . prototype -> { }
3117 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003118 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003119 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
3120 * object of the Child() function has __proto__ pointing to the
3121 * Parent() function's prototype object. An instance of the Child
3122 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003124 * Let Parent be the FunctionTemplate initialized in the previous
3125 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003126 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003127 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003128 * Local<FunctionTemplate> parent = t;
3129 * Local<FunctionTemplate> child = FunctionTemplate::New();
3130 * child->Inherit(parent);
3131 *
3132 * Local<Function> child_function = child->GetFunction();
3133 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003134 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003136 * The Child function and Child instance will have the following
3137 * properties:
3138 *
3139 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003140 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003141 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00003143 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003145class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003146 public:
3147 /** Creates a function template.*/
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003148 // TODO(dcarney): deprecate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003149 static Local<FunctionTemplate> New(
3150 InvocationCallback callback = 0,
3151 Handle<Value> data = Handle<Value>(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003152 Handle<Signature> signature = Handle<Signature>(),
3153 int length = 0);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003154 static Local<FunctionTemplate> New(
3155 FunctionCallback callback, // TODO(dcarney): add back default param.
3156 Handle<Value> data = Handle<Value>(),
3157 Handle<Signature> signature = Handle<Signature>(),
3158 int length = 0);
3159
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003160 /** Returns the unique function instance in the current execution context.*/
3161 Local<Function> GetFunction();
3162
v8.team.kasperl727e9952008-09-02 14:56:44 +00003163 /**
3164 * Set the call-handler callback for a FunctionTemplate. This
3165 * callback is called whenever the function created from this
3166 * FunctionTemplate is called.
3167 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003168 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169 void SetCallHandler(InvocationCallback callback,
3170 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003171 void SetCallHandler(FunctionCallback callback,
3172 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003173
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00003174 /** Set the predefined length property for the FunctionTemplate. */
3175 void SetLength(int length);
3176
v8.team.kasperl727e9952008-09-02 14:56:44 +00003177 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178 Local<ObjectTemplate> InstanceTemplate();
3179
3180 /** Causes the function template to inherit from a parent function template.*/
3181 void Inherit(Handle<FunctionTemplate> parent);
3182
3183 /**
3184 * A PrototypeTemplate is the template used to create the prototype object
3185 * of the function created by this template.
3186 */
3187 Local<ObjectTemplate> PrototypeTemplate();
3188
v8.team.kasperl727e9952008-09-02 14:56:44 +00003189 /**
3190 * Set the class name of the FunctionTemplate. This is used for
3191 * printing objects created with the function created from the
3192 * FunctionTemplate as its constructor.
3193 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003194 void SetClassName(Handle<String> name);
3195
3196 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003197 * Determines whether the __proto__ accessor ignores instances of
3198 * the function template. If instances of the function template are
3199 * ignored, __proto__ skips all instances and instead returns the
3200 * next object in the prototype chain.
3201 *
3202 * Call with a value of true to make the __proto__ accessor ignore
3203 * instances of the function template. Call with a value of false
3204 * to make the __proto__ accessor not ignore instances of the
3205 * function template. By default, instances of a function template
3206 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207 */
3208 void SetHiddenPrototype(bool value);
3209
3210 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003211 * Sets the ReadOnly flag in the attributes of the 'prototype' property
3212 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00003213 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003214 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00003215
3216 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003217 * Returns true if the given object is an instance of this function
3218 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003219 */
3220 bool HasInstance(Handle<Value> object);
3221
3222 private:
3223 FunctionTemplate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003224 friend class Context;
3225 friend class ObjectTemplate;
3226};
3227
3228
3229/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003230 * An ObjectTemplate is used to create objects at runtime.
3231 *
3232 * Properties added to an ObjectTemplate are added to each object
3233 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003234 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003235class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003236 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003237 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003238 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003239
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003240 /** Creates a new instance of this template.*/
3241 Local<Object> NewInstance();
3242
3243 /**
3244 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003245 *
3246 * Whenever the property with the given name is accessed on objects
3247 * created from this ObjectTemplate the getter and setter callbacks
3248 * are called instead of getting and setting the property directly
3249 * on the JavaScript object.
3250 *
3251 * \param name The name of the property for which an accessor is added.
3252 * \param getter The callback to invoke when getting the property.
3253 * \param setter The callback to invoke when setting the property.
3254 * \param data A piece of data that will be passed to the getter and setter
3255 * callbacks whenever they are invoked.
3256 * \param settings Access control settings for the accessor. This is a bit
3257 * field consisting of one of more of
3258 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
3259 * The default is to not allow cross-context access.
3260 * ALL_CAN_READ means that all cross-context reads are allowed.
3261 * ALL_CAN_WRITE means that all cross-context writes are allowed.
3262 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
3263 * cross-context access.
3264 * \param attribute The attributes of the property for which an accessor
3265 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003266 * \param signature The signature describes valid receivers for the accessor
3267 * and is used to perform implicit instance checks against them. If the
3268 * receiver is incompatible (i.e. is not an instance of the constructor as
3269 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
3270 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003271 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003272 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273 void SetAccessor(Handle<String> name,
3274 AccessorGetter getter,
3275 AccessorSetter setter = 0,
3276 Handle<Value> data = Handle<Value>(),
3277 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003278 PropertyAttribute attribute = None,
3279 Handle<AccessorSignature> signature =
3280 Handle<AccessorSignature>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003281 void SetAccessor(Handle<String> name,
3282 AccessorGetterCallback getter,
3283 AccessorSetterCallback setter = 0,
3284 Handle<Value> data = Handle<Value>(),
3285 AccessControl settings = DEFAULT,
3286 PropertyAttribute attribute = None,
3287 Handle<AccessorSignature> signature =
3288 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003289
ulan@chromium.org750145a2013-03-07 15:14:13 +00003290 // This function is not yet stable and should not be used at this time.
3291 bool SetAccessor(Handle<String> name,
3292 Handle<DeclaredAccessorDescriptor> descriptor,
3293 AccessControl settings = DEFAULT,
3294 PropertyAttribute attribute = None,
3295 Handle<AccessorSignature> signature =
3296 Handle<AccessorSignature>());
3297
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003298 /**
3299 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003300 *
3301 * Whenever a named property is accessed on objects created from
3302 * this object template, the provided callback is invoked instead of
3303 * accessing the property directly on the JavaScript object.
3304 *
3305 * \param getter The callback to invoke when getting a property.
3306 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003307 * \param query The callback to invoke to check if a property is present,
3308 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003309 * \param deleter The callback to invoke when deleting a property.
3310 * \param enumerator The callback to invoke to enumerate all the named
3311 * properties of an object.
3312 * \param data A piece of data that will be passed to the callbacks
3313 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003314 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003315 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003316 void SetNamedPropertyHandler(NamedPropertyGetter getter,
3317 NamedPropertySetter setter = 0,
3318 NamedPropertyQuery query = 0,
3319 NamedPropertyDeleter deleter = 0,
3320 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003321 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003322 void SetNamedPropertyHandler(
3323 NamedPropertyGetterCallback getter,
3324 NamedPropertySetterCallback setter = 0,
3325 NamedPropertyQueryCallback query = 0,
3326 NamedPropertyDeleterCallback deleter = 0,
3327 NamedPropertyEnumeratorCallback enumerator = 0,
3328 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329
3330 /**
3331 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003332 *
3333 * Whenever an indexed property is accessed on objects created from
3334 * this object template, the provided callback is invoked instead of
3335 * accessing the property directly on the JavaScript object.
3336 *
3337 * \param getter The callback to invoke when getting a property.
3338 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003339 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003340 * \param deleter The callback to invoke when deleting a property.
3341 * \param enumerator The callback to invoke to enumerate all the indexed
3342 * properties of an object.
3343 * \param data A piece of data that will be passed to the callbacks
3344 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003345 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003346 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003347 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
3348 IndexedPropertySetter setter = 0,
3349 IndexedPropertyQuery query = 0,
3350 IndexedPropertyDeleter deleter = 0,
3351 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003352 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003353 void SetIndexedPropertyHandler(
3354 IndexedPropertyGetterCallback getter,
3355 IndexedPropertySetterCallback setter = 0,
3356 IndexedPropertyQueryCallback query = 0,
3357 IndexedPropertyDeleterCallback deleter = 0,
3358 IndexedPropertyEnumeratorCallback enumerator = 0,
3359 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00003360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003361 /**
3362 * Sets the callback to be used when calling instances created from
3363 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00003364 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365 * function.
3366 */
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003367 // TODO(dcarney): deprecate
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003368 void SetCallAsFunctionHandler(InvocationCallback callback,
3369 Handle<Value> data = Handle<Value>());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00003370 void SetCallAsFunctionHandler(FunctionCallback callback,
3371 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003372
v8.team.kasperl727e9952008-09-02 14:56:44 +00003373 /**
3374 * Mark object instances of the template as undetectable.
3375 *
3376 * In many ways, undetectable objects behave as though they are not
3377 * there. They behave like 'undefined' in conditionals and when
3378 * printed. However, properties can be accessed and called as on
3379 * normal objects.
3380 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003381 void MarkAsUndetectable();
3382
v8.team.kasperl727e9952008-09-02 14:56:44 +00003383 /**
3384 * Sets access check callbacks on the object template.
3385 *
3386 * When accessing properties on instances of this object template,
3387 * the access check callback will be called to determine whether or
3388 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003389 * The last parameter specifies whether access checks are turned
3390 * on by default on instances. If access checks are off by default,
3391 * they can be turned on on individual instances by calling
3392 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00003393 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
3395 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003396 Handle<Value> data = Handle<Value>(),
3397 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003398
kasper.lund212ac232008-07-16 07:07:30 +00003399 /**
3400 * Gets the number of internal fields for objects generated from
3401 * this template.
3402 */
3403 int InternalFieldCount();
3404
3405 /**
3406 * Sets the number of internal fields for objects generated from
3407 * this template.
3408 */
3409 void SetInternalFieldCount(int value);
3410
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003411 private:
3412 ObjectTemplate();
3413 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
3414 friend class FunctionTemplate;
3415};
3416
3417
3418/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003419 * A Signature specifies which receivers and arguments are valid
3420 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003421 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003422class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003423 public:
3424 static Local<Signature> New(Handle<FunctionTemplate> receiver =
3425 Handle<FunctionTemplate>(),
3426 int argc = 0,
3427 Handle<FunctionTemplate> argv[] = 0);
3428 private:
3429 Signature();
3430};
3431
3432
3433/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003434 * An AccessorSignature specifies which receivers are valid parameters
3435 * to an accessor callback.
3436 */
3437class V8EXPORT AccessorSignature : public Data {
3438 public:
3439 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
3440 Handle<FunctionTemplate>());
3441 private:
3442 AccessorSignature();
3443};
3444
3445
ulan@chromium.org750145a2013-03-07 15:14:13 +00003446class V8EXPORT DeclaredAccessorDescriptor : public Data {
3447 private:
3448 DeclaredAccessorDescriptor();
3449};
3450
3451
3452class V8EXPORT ObjectOperationDescriptor : public Data {
3453 public:
3454 // This function is not yet stable and should not be used at this time.
3455 static Local<RawOperationDescriptor> NewInternalFieldDereference(
3456 Isolate* isolate,
3457 int internal_field);
3458 private:
3459 ObjectOperationDescriptor();
3460};
3461
3462
3463enum DeclaredAccessorDescriptorDataType {
3464 kDescriptorBoolType,
3465 kDescriptorInt8Type, kDescriptorUint8Type,
3466 kDescriptorInt16Type, kDescriptorUint16Type,
3467 kDescriptorInt32Type, kDescriptorUint32Type,
3468 kDescriptorFloatType, kDescriptorDoubleType
3469};
3470
3471
3472class V8EXPORT RawOperationDescriptor : public Data {
3473 public:
3474 Local<DeclaredAccessorDescriptor> NewHandleDereference(Isolate* isolate);
3475 Local<RawOperationDescriptor> NewRawDereference(Isolate* isolate);
3476 Local<RawOperationDescriptor> NewRawShift(Isolate* isolate,
3477 int16_t byte_offset);
3478 Local<DeclaredAccessorDescriptor> NewPointerCompare(Isolate* isolate,
3479 void* compare_value);
3480 Local<DeclaredAccessorDescriptor> NewPrimitiveValue(
3481 Isolate* isolate,
3482 DeclaredAccessorDescriptorDataType data_type,
3483 uint8_t bool_offset = 0);
3484 Local<DeclaredAccessorDescriptor> NewBitmaskCompare8(Isolate* isolate,
3485 uint8_t bitmask,
3486 uint8_t compare_value);
3487 Local<DeclaredAccessorDescriptor> NewBitmaskCompare16(
3488 Isolate* isolate,
3489 uint16_t bitmask,
3490 uint16_t compare_value);
3491 Local<DeclaredAccessorDescriptor> NewBitmaskCompare32(
3492 Isolate* isolate,
3493 uint32_t bitmask,
3494 uint32_t compare_value);
3495
3496 private:
3497 RawOperationDescriptor();
3498};
3499
3500
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003501/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003502 * A utility for determining the type of objects based on the template
3503 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003504 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003505class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003506 public:
3507 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
3508 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
3509 int match(Handle<Value> value);
3510 private:
3511 TypeSwitch();
3512};
3513
3514
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003515// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003516
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003517class V8EXPORT ExternalAsciiStringResourceImpl
3518 : public String::ExternalAsciiStringResource {
3519 public:
3520 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
3521 ExternalAsciiStringResourceImpl(const char* data, size_t length)
3522 : data_(data), length_(length) {}
3523 const char* data() const { return data_; }
3524 size_t length() const { return length_; }
3525
3526 private:
3527 const char* data_;
3528 size_t length_;
3529};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003530
3531/**
3532 * Ignore
3533 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003534class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003535 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003536 // Note that the strings passed into this constructor must live as long
3537 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003538 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003539 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003540 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003541 const char** deps = 0,
3542 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003543 virtual ~Extension() { }
3544 virtual v8::Handle<v8::FunctionTemplate>
3545 GetNativeFunction(v8::Handle<v8::String> name) {
3546 return v8::Handle<v8::FunctionTemplate>();
3547 }
3548
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003549 const char* name() const { return name_; }
3550 size_t source_length() const { return source_length_; }
3551 const String::ExternalAsciiStringResource* source() const {
3552 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553 int dependency_count() { return dep_count_; }
3554 const char** dependencies() { return deps_; }
3555 void set_auto_enable(bool value) { auto_enable_ = value; }
3556 bool auto_enable() { return auto_enable_; }
3557
3558 private:
3559 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003560 size_t source_length_; // expected to initialize before source_
3561 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003562 int dep_count_;
3563 const char** deps_;
3564 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003565
3566 // Disallow copying and assigning.
3567 Extension(const Extension&);
3568 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003569};
3570
3571
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003572void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003573
3574
3575/**
3576 * Ignore
3577 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003578class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003579 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003580 V8_INLINE(DeclareExtension(Extension* extension)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003581 RegisterExtension(extension);
3582 }
3583};
3584
3585
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003586// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587
3588
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003589Handle<Primitive> V8EXPORT Undefined();
3590Handle<Primitive> V8EXPORT Null();
3591Handle<Boolean> V8EXPORT True();
3592Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003594V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
3595V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
3596V8_INLINE(Handle<Boolean> True(Isolate* isolate));
3597V8_INLINE(Handle<Boolean> False(Isolate* isolate));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003598
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003599
3600/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003601 * A set of constraints that specifies the limits of the runtime's memory use.
3602 * You must set the heap size before initializing the VM - the size cannot be
3603 * adjusted after the VM is initialized.
3604 *
3605 * If you are using threads then you should hold the V8::Locker lock while
3606 * setting the stack limit and you must set a non-default stack limit separately
3607 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003609class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003610 public:
3611 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003612 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003613 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003614 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003616 int max_executable_size() { return max_executable_size_; }
3617 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003618 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003619 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003620 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
3621 private:
3622 int max_young_space_size_;
3623 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003624 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003625 uint32_t* stack_limit_;
3626};
3627
3628
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003629bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003630
3631
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003632// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003633
3634
3635typedef void (*FatalErrorCallback)(const char* location, const char* message);
3636
3637
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003638typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003639
3640
3641/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003642 * Schedules an exception to be thrown when returning to JavaScript. When an
3643 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003644 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00003645 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003646 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003647Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003648
3649/**
3650 * Create new error objects by calling the corresponding error object
3651 * constructor with the message.
3652 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003653class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654 public:
3655 static Local<Value> RangeError(Handle<String> message);
3656 static Local<Value> ReferenceError(Handle<String> message);
3657 static Local<Value> SyntaxError(Handle<String> message);
3658 static Local<Value> TypeError(Handle<String> message);
3659 static Local<Value> Error(Handle<String> message);
3660};
3661
3662
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003663// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003664
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003665typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003666
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003667typedef void* (*CreateHistogramCallback)(const char* name,
3668 int min,
3669 int max,
3670 size_t buckets);
3671
3672typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
3673
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003674// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003675 enum ObjectSpace {
3676 kObjectSpaceNewSpace = 1 << 0,
3677 kObjectSpaceOldPointerSpace = 1 << 1,
3678 kObjectSpaceOldDataSpace = 1 << 2,
3679 kObjectSpaceCodeSpace = 1 << 3,
3680 kObjectSpaceMapSpace = 1 << 4,
3681 kObjectSpaceLoSpace = 1 << 5,
3682
3683 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
3684 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
3685 kObjectSpaceLoSpace
3686 };
3687
3688 enum AllocationAction {
3689 kAllocationActionAllocate = 1 << 0,
3690 kAllocationActionFree = 1 << 1,
3691 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
3692 };
3693
3694typedef void (*MemoryAllocationCallback)(ObjectSpace space,
3695 AllocationAction action,
3696 int size);
3697
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003698// --- Leave Script Callback ---
3699typedef void (*CallCompletedCallback)();
3700
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003701// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003702typedef void (*FailedAccessCheckCallback)(Local<Object> target,
3703 AccessType type,
3704 Local<Value> data);
3705
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003706// --- AllowCodeGenerationFromStrings callbacks ---
3707
3708/**
3709 * Callback to check if code generation from strings is allowed. See
3710 * Context::AllowCodeGenerationFromStrings.
3711 */
3712typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
3713
3714// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003715
3716/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003717 * Applications can register callback functions which will be called
3718 * before and after a garbage collection. Allocations are not
3719 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00003720 * objects (set or delete properties for example) since it is possible
3721 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003722 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003723enum GCType {
3724 kGCTypeScavenge = 1 << 0,
3725 kGCTypeMarkSweepCompact = 1 << 1,
3726 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
3727};
3728
3729enum GCCallbackFlags {
3730 kNoGCCallbackFlags = 0,
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003731 kGCCallbackFlagCompacted = 1 << 0,
3732 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003733};
3734
3735typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
3736typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
3737
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003738typedef void (*GCCallback)();
3739
3740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003741/**
ager@chromium.org3811b432009-10-28 14:53:37 +00003742 * Collection of V8 heap information.
3743 *
3744 * Instances of this class can be passed to v8::V8::HeapStatistics to
3745 * get heap statistics from V8.
3746 */
3747class V8EXPORT HeapStatistics {
3748 public:
3749 HeapStatistics();
3750 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003751 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00003752 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003753 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003754 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00003755
3756 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00003757 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00003758 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00003759 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003760 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003761 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00003762
3763 friend class V8;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003764 friend class Isolate;
ager@chromium.org3811b432009-10-28 14:53:37 +00003765};
3766
3767
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003768class RetainedObjectInfo;
3769
ager@chromium.org3811b432009-10-28 14:53:37 +00003770/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003771 * Isolate represents an isolated instance of the V8 engine. V8
3772 * isolates have completely separate states. Objects from one isolate
3773 * must not be used in other isolates. When V8 is initialized a
3774 * default isolate is implicitly created and entered. The embedder
3775 * can create additional isolates and use them in parallel in multiple
3776 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003777 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003778 */
3779class V8EXPORT Isolate {
3780 public:
3781 /**
3782 * Stack-allocated class which sets the isolate for all operations
3783 * executed within a local scope.
3784 */
3785 class V8EXPORT Scope {
3786 public:
3787 explicit Scope(Isolate* isolate) : isolate_(isolate) {
3788 isolate->Enter();
3789 }
3790
3791 ~Scope() { isolate_->Exit(); }
3792
3793 private:
3794 Isolate* const isolate_;
3795
3796 // Prevent copying of Scope objects.
3797 Scope(const Scope&);
3798 Scope& operator=(const Scope&);
3799 };
3800
3801 /**
3802 * Creates a new isolate. Does not change the currently entered
3803 * isolate.
3804 *
3805 * When an isolate is no longer used its resources should be freed
3806 * by calling Dispose(). Using the delete operator is not allowed.
3807 */
3808 static Isolate* New();
3809
3810 /**
3811 * Returns the entered isolate for the current thread or NULL in
3812 * case there is no current isolate.
3813 */
3814 static Isolate* GetCurrent();
3815
3816 /**
3817 * Methods below this point require holding a lock (using Locker) in
3818 * a multi-threaded environment.
3819 */
3820
3821 /**
3822 * Sets this isolate as the entered one for the current thread.
3823 * Saves the previously entered one (if any), so that it can be
3824 * restored when exiting. Re-entering an isolate is allowed.
3825 */
3826 void Enter();
3827
3828 /**
3829 * Exits this isolate by restoring the previously entered one in the
3830 * current thread. The isolate may still stay the same, if it was
3831 * entered more than once.
3832 *
3833 * Requires: this == Isolate::GetCurrent().
3834 */
3835 void Exit();
3836
3837 /**
3838 * Disposes the isolate. The isolate must not be entered by any
3839 * thread to be disposable.
3840 */
3841 void Dispose();
3842
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003843 /**
3844 * Associate embedder-specific data with the isolate
3845 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003846 V8_INLINE(void SetData(void* data));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003847
3848 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00003849 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003850 * Returns NULL if SetData has never been called.
3851 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00003852 V8_INLINE(void* GetData());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003853
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00003854 /**
3855 * Get statistics about the heap memory usage.
3856 */
3857 void GetHeapStatistics(HeapStatistics* heap_statistics);
3858
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003859 /**
3860 * Adjusts the amount of registered external memory. Used to give V8 an
3861 * indication of the amount of externally allocated memory that is kept alive
3862 * by JavaScript objects. V8 uses this to decide when to perform global
3863 * garbage collections. Registering externally allocated memory will trigger
3864 * global garbage collections more often than it would otherwise in an attempt
3865 * to garbage collect the JavaScript objects that keep the externally
3866 * allocated memory alive.
3867 *
3868 * \param change_in_bytes the change in externally allocated memory that is
3869 * kept alive by JavaScript objects.
3870 * \returns the adjusted value.
3871 */
3872 intptr_t AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes);
3873
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003874 /**
3875 * Returns heap profiler for this isolate. Will return NULL until the isolate
3876 * is initialized.
3877 */
3878 HeapProfiler* GetHeapProfiler();
3879
3880 /**
3881 * Returns CPU profiler for this isolate. Will return NULL until the isolate
3882 * is initialized.
3883 */
3884 CpuProfiler* GetCpuProfiler();
3885
mvstanton@chromium.org40ce96b2013-04-09 09:52:22 +00003886 /** Returns the context that is on the top of the stack. */
3887 Local<Context> GetCurrentContext();
3888
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003889 /**
3890 * Allows the host application to group objects together. If one
3891 * object in the group is alive, all objects in the group are alive.
3892 * After each garbage collection, object groups are removed. It is
3893 * intended to be used in the before-garbage-collection callback
3894 * function, for instance to simulate DOM tree connections among JS
3895 * wrapper objects. Object groups for all dependent handles need to
3896 * be provided for kGCTypeMarkSweepCompact collections, for all other
3897 * garbage collection types it is sufficient to provide object groups
3898 * for partially dependent handles only.
3899 */
3900 void SetObjectGroupId(const Persistent<Value>& object,
3901 UniqueId id);
3902
3903 /**
3904 * Allows the host application to declare implicit references from an object
3905 * group to an object. If the objects of the object group are alive, the child
3906 * object is alive too. After each garbage collection, all implicit references
3907 * are removed. It is intended to be used in the before-garbage-collection
3908 * callback function.
3909 */
3910 void SetReferenceFromGroup(UniqueId id,
3911 const Persistent<Value>& child);
3912
3913 /**
3914 * Allows the host application to declare implicit references from an object
3915 * to another object. If the parent object is alive, the child object is alive
3916 * too. After each garbage collection, all implicit references are removed. It
3917 * is intended to be used in the before-garbage-collection callback function.
3918 */
3919 void SetReference(const Persistent<Object>& parent,
3920 const Persistent<Value>& child);
3921
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003922 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003923 Isolate();
3924 Isolate(const Isolate&);
3925 ~Isolate();
3926 Isolate& operator=(const Isolate&);
3927 void* operator new(size_t size);
3928 void operator delete(void*, size_t);
3929};
3930
3931
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003932class V8EXPORT StartupData {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003933 public:
3934 enum CompressionAlgorithm {
3935 kUncompressed,
3936 kBZip2
3937 };
3938
3939 const char* data;
3940 int compressed_size;
3941 int raw_size;
3942};
3943
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003944
3945/**
3946 * A helper class for driving V8 startup data decompression. It is based on
3947 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
3948 * for an embedder to use this class, instead, API functions can be used
3949 * directly.
3950 *
3951 * For an example of the class usage, see the "shell.cc" sample application.
3952 */
3953class V8EXPORT StartupDataDecompressor { // NOLINT
3954 public:
3955 StartupDataDecompressor();
3956 virtual ~StartupDataDecompressor();
3957 int Decompress();
3958
3959 protected:
3960 virtual int DecompressData(char* raw_data,
3961 int* raw_data_size,
3962 const char* compressed_data,
3963 int compressed_data_size) = 0;
3964
3965 private:
3966 char** raw_data;
3967};
3968
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003969
3970/**
3971 * EntropySource is used as a callback function when v8 needs a source
3972 * of entropy.
3973 */
3974typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3975
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003976
3977/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003978 * ReturnAddressLocationResolver is used as a callback function when v8 is
3979 * resolving the location of a return address on the stack. Profilers that
3980 * change the return address on the stack can use this to resolve the stack
3981 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003982 *
3983 * \param return_addr_location points to a location on stack where a machine
3984 * return address resides.
3985 * \returns either return_addr_location, or else a pointer to the profiler's
3986 * copy of the original return address.
3987 *
3988 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00003989 */
3990typedef uintptr_t (*ReturnAddressLocationResolver)(
3991 uintptr_t return_addr_location);
3992
3993
3994/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003995 * FunctionEntryHook is the type of the profile entry hook called at entry to
3996 * any generated function when function-level profiling is enabled.
3997 *
3998 * \param function the address of the function that's being entered.
3999 * \param return_addr_location points to a location on stack where the machine
4000 * return address resides. This can be used to identify the caller of
4001 * \p function, and/or modified to divert execution when \p function exits.
4002 *
4003 * \note the entry hook must not cause garbage collection.
4004 */
4005typedef void (*FunctionEntryHook)(uintptr_t function,
4006 uintptr_t return_addr_location);
4007
4008
4009/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004010 * A JIT code event is issued each time code is added, moved or removed.
4011 *
4012 * \note removal events are not currently issued.
4013 */
4014struct JitCodeEvent {
4015 enum EventType {
4016 CODE_ADDED,
4017 CODE_MOVED,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004018 CODE_REMOVED,
4019 CODE_ADD_LINE_POS_INFO,
4020 CODE_START_LINE_INFO_RECORDING,
4021 CODE_END_LINE_INFO_RECORDING
4022 };
4023 // Definition of the code position type. The "POSITION" type means the place
4024 // in the source code which are of interest when making stack traces to
4025 // pin-point the source location of a stack frame as close as possible.
4026 // The "STATEMENT_POSITION" means the place at the beginning of each
4027 // statement, and is used to indicate possible break locations.
4028 enum PositionType {
4029 POSITION,
4030 STATEMENT_POSITION
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004031 };
4032
4033 // Type of event.
4034 EventType type;
4035 // Start of the instructions.
4036 void* code_start;
4037 // Size of the instructions.
4038 size_t code_len;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004039 // Script info for CODE_ADDED event.
4040 Handle<Script> script;
4041 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
4042 // code line information which is returned from the
4043 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
4044 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
4045 void* user_data;
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004046
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004047 struct name_t {
4048 // Name of the object associated with the code, note that the string is not
4049 // zero-terminated.
4050 const char* str;
4051 // Number of chars in str.
4052 size_t len;
4053 };
4054
4055 struct line_info_t {
4056 // PC offset
4057 size_t offset;
4058 // Code postion
4059 size_t pos;
4060 // The position type.
4061 PositionType position_type;
4062 };
4063
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004064 union {
4065 // Only valid for CODE_ADDED.
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004066 struct name_t name;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004067
4068 // Only valid for CODE_ADD_LINE_POS_INFO
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00004069 struct line_info_t line_info;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00004070
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004071 // New location of instructions. Only valid for CODE_MOVED.
4072 void* new_code_start;
4073 };
4074};
4075
4076/**
4077 * Option flags passed to the SetJitCodeEventHandler function.
4078 */
4079enum JitCodeEventOptions {
4080 kJitCodeEventDefault = 0,
4081 // Generate callbacks for already existent code.
4082 kJitCodeEventEnumExisting = 1
4083};
4084
4085
4086/**
4087 * Callback function passed to SetJitCodeEventHandler.
4088 *
4089 * \param event code add, move or removal event.
4090 */
4091typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
4092
4093
4094/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004095 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004096 */
4097class V8EXPORT ExternalResourceVisitor { // NOLINT
4098 public:
4099 virtual ~ExternalResourceVisitor() {}
4100 virtual void VisitExternalString(Handle<String> string) {}
4101};
4102
4103
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004104/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004105 * Interface for iterating through all the persistent handles in the heap.
4106 */
4107class V8EXPORT PersistentHandleVisitor { // NOLINT
4108 public:
4109 virtual ~PersistentHandleVisitor() {}
4110 virtual void VisitPersistentHandle(Persistent<Value> value,
4111 uint16_t class_id) {}
4112};
4113
4114
4115/**
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004116 * Asserts that no action is performed that could cause a handle's value
4117 * to be modified. Useful when otherwise unsafe handle operations need to
4118 * be performed.
4119 */
4120class V8EXPORT AssertNoGCScope {
4121#ifndef DEBUG
4122 V8_INLINE(AssertNoGCScope(Isolate* isolate)) {}
4123#else
4124 AssertNoGCScope(Isolate* isolate);
4125 ~AssertNoGCScope();
4126 private:
4127 Isolate* isolate_;
4128 bool last_state_;
4129#endif
4130};
4131
4132
4133/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004134 * Container class for static utility functions.
4135 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004136class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004137 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00004138 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004139 static void SetFatalErrorHandler(FatalErrorCallback that);
4140
v8.team.kasperl727e9952008-09-02 14:56:44 +00004141 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004142 * Set the callback to invoke to check if code generation from
4143 * strings should be allowed.
4144 */
4145 static void SetAllowCodeGenerationFromStringsCallback(
4146 AllowCodeGenerationFromStringsCallback that);
4147
4148 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004149 * Ignore out-of-memory exceptions.
4150 *
4151 * V8 running out of memory is treated as a fatal error by default.
4152 * This means that the fatal error handler is called and that V8 is
4153 * terminated.
4154 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004155 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00004156 * out-of-memory situation as a fatal error. This way, the contexts
4157 * that did not cause the out of memory problem might be able to
4158 * continue execution.
4159 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004160 static void IgnoreOutOfMemoryException();
4161
v8.team.kasperl727e9952008-09-02 14:56:44 +00004162 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004163 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00004164 * fatal errors such as out-of-memory situations.
4165 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004166 static bool IsDead();
4167
4168 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004169 * The following 4 functions are to be used when V8 is built with
4170 * the 'compress_startup_data' flag enabled. In this case, the
4171 * embedder must decompress startup data prior to initializing V8.
4172 *
4173 * This is how interaction with V8 should look like:
4174 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
4175 * v8::StartupData* compressed_data =
4176 * new v8::StartupData[compressed_data_count];
4177 * v8::V8::GetCompressedStartupData(compressed_data);
4178 * ... decompress data (compressed_data can be updated in-place) ...
4179 * v8::V8::SetDecompressedStartupData(compressed_data);
4180 * ... now V8 can be initialized
4181 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004182 *
4183 * A helper class StartupDataDecompressor is provided. It implements
4184 * the protocol of the interaction described above, and can be used in
4185 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00004186 */
4187 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
4188 static int GetCompressedStartupDataCount();
4189 static void GetCompressedStartupData(StartupData* compressed_data);
4190 static void SetDecompressedStartupData(StartupData* decompressed_data);
4191
4192 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004193 * Adds a message listener.
4194 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004195 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00004196 * case it will be called more than once for each message.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004197 *
4198 * If data is specified, it will be passed to the callback when it is called.
4199 * Otherwise, the exception object will be passed to the callback instead.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004200 */
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004201 static bool AddMessageListener(MessageCallback that,
4202 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004203
4204 /**
4205 * Remove all message listeners from the specified callback function.
4206 */
4207 static void RemoveMessageListeners(MessageCallback that);
4208
4209 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004210 * Tells V8 to capture current stack trace when uncaught exception occurs
4211 * and report it to the message listeners. The option is off by default.
4212 */
4213 static void SetCaptureStackTraceForUncaughtExceptions(
4214 bool capture,
4215 int frame_limit = 10,
4216 StackTrace::StackTraceOptions options = StackTrace::kOverview);
4217
4218 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004219 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004220 */
4221 static void SetFlagsFromString(const char* str, int length);
4222
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004223 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004224 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004225 */
4226 static void SetFlagsFromCommandLine(int* argc,
4227 char** argv,
4228 bool remove_flags);
4229
kasper.lund7276f142008-07-30 08:49:36 +00004230 /** Get the version string. */
4231 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004232
4233 /**
4234 * Enables the host application to provide a mechanism for recording
4235 * statistics counters.
4236 */
4237 static void SetCounterFunction(CounterLookupCallback);
4238
4239 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004240 * Enables the host application to provide a mechanism for recording
4241 * histograms. The CreateHistogram function returns a
4242 * histogram which will later be passed to the AddHistogramSample
4243 * function.
4244 */
4245 static void SetCreateHistogramFunction(CreateHistogramCallback);
4246 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
4247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004248 /** Callback function for reporting failed access checks.*/
4249 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
4250
4251 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004252 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004253 * garbage collection. Allocations are not allowed in the
4254 * callback function, you therefore cannot manipulate objects (set
4255 * or delete properties for example) since it is possible such
4256 * operations will result in the allocation of objects. It is possible
4257 * to specify the GCType filter for your callback. But it is not possible to
4258 * register the same callback function two times with different
4259 * GCType filters.
4260 */
4261 static void AddGCPrologueCallback(
4262 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
4263
4264 /**
4265 * This function removes callback which was installed by
4266 * AddGCPrologueCallback function.
4267 */
4268 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
4269
4270 /**
4271 * The function is deprecated. Please use AddGCPrologueCallback instead.
4272 * Enables the host application to receive a notification before a
4273 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00004274 * callback function, you therefore cannot manipulate objects (set
4275 * or delete properties for example) since it is possible such
4276 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004277 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004278 V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004279
4280 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004281 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004282 * garbage collection. Allocations are not allowed in the
4283 * callback function, you therefore cannot manipulate objects (set
4284 * or delete properties for example) since it is possible such
4285 * operations will result in the allocation of objects. It is possible
4286 * to specify the GCType filter for your callback. But it is not possible to
4287 * register the same callback function two times with different
4288 * GCType filters.
4289 */
4290 static void AddGCEpilogueCallback(
4291 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
4292
4293 /**
4294 * This function removes callback which was installed by
4295 * AddGCEpilogueCallback function.
4296 */
4297 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
4298
4299 /**
4300 * The function is deprecated. Please use AddGCEpilogueCallback instead.
4301 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00004302 * major garbage collection. Allocations are not allowed in the
4303 * callback function, you therefore cannot manipulate objects (set
4304 * or delete properties for example) since it is possible such
4305 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004306 */
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004307 V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004308
4309 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004310 * Enables the host application to provide a mechanism to be notified
4311 * and perform custom logging when V8 Allocates Executable Memory.
4312 */
4313 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
4314 ObjectSpace space,
4315 AllocationAction action);
4316
4317 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004318 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004319 */
4320 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
4321
4322 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00004323 * Adds a callback to notify the host application when a script finished
4324 * running. If a script re-enters the runtime during executing, the
4325 * CallCompletedCallback is only invoked when the outer-most script
4326 * execution ends. Executing scripts inside the callback do not trigger
4327 * further callbacks.
4328 */
4329 static void AddCallCompletedCallback(CallCompletedCallback callback);
4330
4331 /**
4332 * Removes callback that was installed by AddCallCompletedCallback.
4333 */
4334 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
4335
4336 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004337 * Allows the host application to group objects together. If one
4338 * object in the group is alive, all objects in the group are alive.
4339 * After each garbage collection, object groups are removed. It is
4340 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00004341 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004342 * wrapper objects. Object groups for all dependent handles need to
4343 * be provided for kGCTypeMarkSweepCompact collections, for all other
4344 * garbage collection types it is sufficient to provide object groups
4345 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004346 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004347 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004348 // TODO(marja): deprecate AddObjectGroup. Use Isolate::SetObjectGroupId and
4349 // HeapProfiler::SetRetainedObjectInfo instead.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004350 static void AddObjectGroup(Persistent<Value>* objects,
4351 size_t length,
4352 RetainedObjectInfo* info = NULL);
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004353 static void AddObjectGroup(Isolate* isolate,
4354 Persistent<Value>* objects,
4355 size_t length,
4356 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004357
4358 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004359 * Allows the host application to declare implicit references between
4360 * the objects: if |parent| is alive, all |children| are alive too.
4361 * After each garbage collection, all implicit references
4362 * are removed. It is intended to be used in the before-garbage-collection
4363 * callback function.
4364 */
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004365 // TODO(marja): Deprecate AddImplicitReferences. Use
4366 // Isolate::SetReferenceFromGroup instead.
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004367 static void AddImplicitReferences(Persistent<Object> parent,
4368 Persistent<Value>* children,
4369 size_t length);
4370
4371 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004372 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004373 * initialize from scratch. This function is called implicitly if
4374 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004375 */
4376 static bool Initialize();
4377
kasper.lund7276f142008-07-30 08:49:36 +00004378 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004379 * Allows the host application to provide a callback which can be used
4380 * as a source of entropy for random number generators.
4381 */
4382 static void SetEntropySource(EntropySource source);
4383
4384 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00004385 * Allows the host application to provide a callback that allows v8 to
4386 * cooperate with a profiler that rewrites return addresses on stack.
4387 */
4388 static void SetReturnAddressLocationResolver(
4389 ReturnAddressLocationResolver return_address_resolver);
4390
4391 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004392 * Allows the host application to provide the address of a function that's
4393 * invoked on entry to every V8-generated function.
4394 * Note that \p entry_hook is invoked at the very start of each
4395 * generated function.
4396 *
4397 * \param entry_hook a function that will be invoked on entry to every
4398 * V8-generated function.
4399 * \returns true on success on supported platforms, false on failure.
4400 * \note Setting a new entry hook function when one is already active will
4401 * fail.
4402 */
4403 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
4404
4405 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00004406 * Allows the host application to provide the address of a function that is
4407 * notified each time code is added, moved or removed.
4408 *
4409 * \param options options for the JIT code event handler.
4410 * \param event_handler the JIT code event handler, which will be invoked
4411 * each time code is added, moved or removed.
4412 * \note \p event_handler won't get notified of existent code.
4413 * \note since code removal notifications are not currently issued, the
4414 * \p event_handler may get notifications of code that overlaps earlier
4415 * code notifications. This happens when code areas are reused, and the
4416 * earlier overlapping code areas should therefore be discarded.
4417 * \note the events passed to \p event_handler and the strings they point to
4418 * are not guaranteed to live past each call. The \p event_handler must
4419 * copy strings and other parameters it needs to keep around.
4420 * \note the set of events declared in JitCodeEvent::EventType is expected to
4421 * grow over time, and the JitCodeEvent structure is expected to accrue
4422 * new members. The \p event_handler function must ignore event codes
4423 * it does not recognize to maintain future compatibility.
4424 */
4425 static void SetJitCodeEventHandler(JitCodeEventOptions options,
4426 JitCodeEventHandler event_handler);
4427
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004428 // TODO(svenpanne) Really deprecate me when Chrome is fixed.
4429 /** Deprecated. Use Isolate::AdjustAmountOfExternalAllocatedMemory instead. */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004430 static intptr_t AdjustAmountOfExternalAllocatedMemory(
4431 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00004432
iposva@chromium.org245aa852009-02-10 00:49:54 +00004433 /**
4434 * Suspends recording of tick samples in the profiler.
4435 * When the V8 profiling mode is enabled (usually via command line
4436 * switches) this function suspends recording of tick samples.
4437 * Profiling ticks are discarded until ResumeProfiler() is called.
4438 *
4439 * See also the --prof and --prof_auto command line switches to
4440 * enable V8 profiling.
4441 */
4442 static void PauseProfiler();
4443
4444 /**
4445 * Resumes recording of tick samples in the profiler.
4446 * See also PauseProfiler().
4447 */
4448 static void ResumeProfiler();
4449
ager@chromium.org41826e72009-03-30 13:30:57 +00004450 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004451 * Return whether profiler is currently paused.
4452 */
4453 static bool IsProfilerPaused();
4454
4455 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004456 * Retrieve the V8 thread id of the calling thread.
4457 *
4458 * The thread id for a thread should only be retrieved after the V8
4459 * lock has been acquired with a Locker object with that thread.
4460 */
4461 static int GetCurrentThreadId();
4462
4463 /**
4464 * Forcefully terminate execution of a JavaScript thread. This can
4465 * be used to terminate long-running scripts.
4466 *
4467 * TerminateExecution should only be called when then V8 lock has
4468 * been acquired with a Locker object. Therefore, in order to be
4469 * able to terminate long-running threads, preemption must be
4470 * enabled to allow the user of TerminateExecution to acquire the
4471 * lock.
4472 *
4473 * The termination is achieved by throwing an exception that is
4474 * uncatchable by JavaScript exception handlers. Termination
4475 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004476 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004477 * exception handler that catches an exception should check if that
4478 * exception is a termination exception and immediately return if
4479 * that is the case. Returning immediately in that case will
4480 * continue the propagation of the termination exception if needed.
4481 *
4482 * The thread id passed to TerminateExecution must have been
4483 * obtained by calling GetCurrentThreadId on the thread in question.
4484 *
4485 * \param thread_id The thread id of the thread to terminate.
4486 */
4487 static void TerminateExecution(int thread_id);
4488
4489 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004490 * Forcefully terminate the current thread of JavaScript execution
4491 * in the given isolate. If no isolate is provided, the default
4492 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004493 *
4494 * This method can be used by any thread even if that thread has not
4495 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004496 *
4497 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004498 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004499 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00004500
4501 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004502 * Is V8 terminating JavaScript execution.
4503 *
4504 * Returns true if JavaScript execution is currently terminating
4505 * because of a call to TerminateExecution. In that case there are
4506 * still JavaScript frames on the stack and the termination
4507 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004508 *
4509 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004510 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004511 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00004512
4513 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004514 * Resume execution capability in the given isolate, whose execution
4515 * was previously forcefully terminated using TerminateExecution().
4516 *
4517 * When execution is forcefully terminated using TerminateExecution(),
4518 * the isolate can not resume execution until all JavaScript frames
4519 * have propagated the uncatchable exception which is generated. This
4520 * method allows the program embedding the engine to handle the
4521 * termination event and resume execution capability, even if
4522 * JavaScript frames remain on the stack.
4523 *
4524 * This method can be used by any thread even if that thread has not
4525 * acquired the V8 lock with a Locker object.
4526 *
4527 * \param isolate The isolate in which to resume execution capability.
4528 */
4529 static void CancelTerminateExecution(Isolate* isolate);
4530
4531 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00004532 * Releases any resources used by v8 and stops any utility threads
4533 * that may be running. Note that disposing v8 is permanent, it
4534 * cannot be reinitialized.
4535 *
4536 * It should generally not be necessary to dispose v8 before exiting
4537 * a process, this should happen automatically. It is only necessary
4538 * to use if the process needs the resources taken up by v8.
4539 */
4540 static bool Dispose();
4541
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00004542 /** Deprecated. Use Isolate::GetHeapStatistics instead. */
4543 V8_DEPRECATED(static void GetHeapStatistics(HeapStatistics* heap_statistics));
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004544
4545 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004546 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004547 * heap. GC is not invoked prior to iterating, therefore there is no
4548 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00004549 */
4550 static void VisitExternalResources(ExternalResourceVisitor* visitor);
4551
4552 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004553 * Iterates through all the persistent handles in the current isolate's heap
4554 * that have class_ids.
4555 */
4556 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
4557
4558 /**
yangguo@chromium.org003650e2013-01-24 16:31:08 +00004559 * Iterates through all the persistent handles in the current isolate's heap
4560 * that have class_ids and are candidates to be marked as partially dependent
4561 * handles. This will visit handles to young objects created since the last
4562 * garbage collection but is free to visit an arbitrary superset of these
4563 * objects.
4564 */
4565 static void VisitHandlesForPartialDependence(
4566 Isolate* isolate, PersistentHandleVisitor* visitor);
4567
4568 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004569 * Optional notification that the embedder is idle.
4570 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004571 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004572 * Returns true if the embedder should stop calling IdleNotification
4573 * until real work has been done. This indicates that V8 has done
4574 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004575 *
4576 * The hint argument specifies the amount of work to be done in the function
4577 * on scale from 1 to 1000. There is no guarantee that the actual work will
4578 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004579 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00004580 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00004581
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004582 /**
4583 * Optional notification that the system is running low on memory.
4584 * V8 uses these notifications to attempt to free memory.
4585 */
4586 static void LowMemoryNotification();
4587
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004588 /**
4589 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004590 * these notifications to guide the GC heuristic. Returns the number
4591 * of context disposals - including this one - since the last time
4592 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004593 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004594 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00004595
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004596 private:
4597 V8();
4598
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004599 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
4600 internal::Object** handle);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004601 static void DisposeGlobal(internal::Object** global_handle);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004602 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004603 static void MakeWeak(internal::Isolate* isolate,
4604 internal::Object** global_handle,
4605 void* data,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004606 RevivableCallback weak_reference_callback,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004607 NearDeathCallback near_death_callback);
4608 static void ClearWeak(internal::Isolate* isolate,
4609 internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004610
4611 template <class T> friend class Handle;
4612 template <class T> friend class Local;
4613 template <class T> friend class Persistent;
4614 friend class Context;
4615};
4616
4617
4618/**
4619 * An external exception handler.
4620 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004621class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004622 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004623 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004624 * Creates a new try/catch block and registers it with v8. Note that
4625 * all TryCatch blocks should be stack allocated because the memory
4626 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004627 */
4628 TryCatch();
4629
4630 /**
4631 * Unregisters and deletes this try/catch block.
4632 */
4633 ~TryCatch();
4634
4635 /**
4636 * Returns true if an exception has been caught by this try/catch block.
4637 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004638 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004639
4640 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004641 * For certain types of exceptions, it makes no sense to continue execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004642 *
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004643 * If CanContinue returns false, the correct action is to perform any C++
4644 * cleanup needed and then return. If CanContinue returns false and
4645 * HasTerminated returns true, it is possible to call
4646 * CancelTerminateExecution in order to continue calling into the engine.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00004647 */
4648 bool CanContinue() const;
4649
4650 /**
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004651 * Returns true if an exception has been caught due to script execution
4652 * being terminated.
4653 *
4654 * There is no JavaScript representation of an execution termination
4655 * exception. Such exceptions are thrown when the TerminateExecution
4656 * methods are called to terminate a long-running script.
4657 *
4658 * If such an exception has been thrown, HasTerminated will return true,
4659 * indicating that it is possible to call CancelTerminateExecution in order
4660 * to continue calling into the engine.
4661 */
4662 bool HasTerminated() const;
4663
4664 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004665 * Throws the exception caught by this TryCatch in a way that avoids
4666 * it being caught again by this same TryCatch. As with ThrowException
4667 * it is illegal to execute any JavaScript operations after calling
4668 * ReThrow; the caller must return immediately to where the exception
4669 * is caught.
4670 */
4671 Handle<Value> ReThrow();
4672
4673 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004674 * Returns the exception caught by this try/catch block. If no exception has
4675 * been caught an empty handle is returned.
4676 *
4677 * The returned handle is valid until this TryCatch block has been destroyed.
4678 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004679 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004680
4681 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004682 * Returns the .stack property of the thrown object. If no .stack
4683 * property is present an empty handle is returned.
4684 */
4685 Local<Value> StackTrace() const;
4686
4687 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004688 * Returns the message associated with this exception. If there is
4689 * no message associated an empty handle is returned.
4690 *
4691 * The returned handle is valid until this TryCatch block has been
4692 * destroyed.
4693 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004694 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004695
4696 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004697 * Clears any exceptions that may have been caught by this try/catch block.
4698 * After this method has been called, HasCaught() will return false.
4699 *
4700 * It is not necessary to clear a try/catch block before using it again; if
4701 * another exception is thrown the previously caught exception will just be
4702 * overwritten. However, it is often a good idea since it makes it easier
4703 * to determine which operation threw a given exception.
4704 */
4705 void Reset();
4706
v8.team.kasperl727e9952008-09-02 14:56:44 +00004707 /**
4708 * Set verbosity of the external exception handler.
4709 *
4710 * By default, exceptions that are caught by an external exception
4711 * handler are not reported. Call SetVerbose with true on an
4712 * external exception handler to have exceptions caught by the
4713 * handler reported as if they were not caught.
4714 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004715 void SetVerbose(bool value);
4716
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004717 /**
4718 * Set whether or not this TryCatch should capture a Message object
4719 * which holds source information about where the exception
4720 * occurred. True by default.
4721 */
4722 void SetCaptureMessage(bool value);
4723
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004724 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00004725 // Make it hard to create heap-allocated TryCatch blocks.
4726 TryCatch(const TryCatch&);
4727 void operator=(const TryCatch&);
4728 void* operator new(size_t size);
4729 void operator delete(void*, size_t);
4730
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004731 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004732 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004733 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004734 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00004735 bool is_verbose_ : 1;
4736 bool can_continue_ : 1;
4737 bool capture_message_ : 1;
4738 bool rethrow_ : 1;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004739 bool has_terminated_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004740
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004741 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004742};
4743
4744
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004745// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004746
4747
4748/**
4749 * Ignore
4750 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004751class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004752 public:
4753 ExtensionConfiguration(int name_count, const char* names[])
4754 : name_count_(name_count), names_(names) { }
4755 private:
4756 friend class ImplementationUtilities;
4757 int name_count_;
4758 const char** names_;
4759};
4760
4761
4762/**
4763 * A sandboxed execution context with its own set of built-in objects
4764 * and functions.
4765 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004766class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004767 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00004768 /**
4769 * Returns the global proxy object or global object itself for
4770 * detached contexts.
4771 *
4772 * Global proxy object is a thin wrapper whose prototype points to
4773 * actual context's global object with the properties like Object, etc.
4774 * This is done that way for security reasons (for more details see
4775 * https://wiki.mozilla.org/Gecko:SplitWindow).
4776 *
4777 * Please note that changes to global proxy object prototype most probably
4778 * would break VM---v8 expects only global object as a prototype of
4779 * global proxy object.
4780 *
4781 * If DetachGlobal() has been invoked, Global() would return actual global
4782 * object until global is reattached with ReattachGlobal().
4783 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004784 Local<Object> Global();
4785
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004786 /**
4787 * Detaches the global object from its context before
4788 * the global object can be reused to create a new context.
4789 */
4790 void DetachGlobal();
4791
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00004792 /**
4793 * Reattaches a global object to a context. This can be used to
4794 * restore the connection between a global object and a context
4795 * after DetachGlobal has been called.
4796 *
4797 * \param global_object The global object to reattach to the
4798 * context. For this to work, the global object must be the global
4799 * object that was associated with this context before a call to
4800 * DetachGlobal.
4801 */
4802 void ReattachGlobal(Handle<Object> global_object);
4803
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004804 /**
4805 * Creates a new context and returns a handle to the newly allocated
4806 * context.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004807 *
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004808 * \param isolate The isolate in which to create the context.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004809 *
4810 * \param extensions An optional extension configuration containing
4811 * the extensions to be installed in the newly created context.
4812 *
4813 * \param global_template An optional object template from which the
4814 * global object for the newly created context will be created.
4815 *
4816 * \param global_object An optional global object to be reused for
4817 * the newly created context. This global object must have been
4818 * created by a previous call to Context::New with the same global
4819 * template. The state of the global object will be completely reset
4820 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004821 */
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004822 static Local<Context> New(
4823 Isolate* isolate,
4824 ExtensionConfiguration* extensions = NULL,
4825 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
4826 Handle<Value> global_object = Handle<Value>());
4827
4828 /** Deprecated. Use Isolate version instead. */
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004829 V8_DEPRECATED(static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004830 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00004831 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004832 Handle<Value> global_object = Handle<Value>()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004833
kasper.lund44510672008-07-25 07:37:58 +00004834 /** Returns the last entered context. */
4835 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004836
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004837 // TODO(svenpanne) Actually deprecate this.
4838 /** Deprecated. Use Isolate::GetCurrentContext instead. */
kasper.lund44510672008-07-25 07:37:58 +00004839 static Local<Context> GetCurrent();
4840
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004841 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00004842 * Returns the context of the calling JavaScript code. That is the
4843 * context of the top-most JavaScript frame. If there are no
4844 * JavaScript frames an empty handle is returned.
4845 */
4846 static Local<Context> GetCalling();
4847
4848 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004849 * Sets the security token for the context. To access an object in
4850 * another context, the security tokens must match.
4851 */
4852 void SetSecurityToken(Handle<Value> token);
4853
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004854 /** Restores the security token to the default value. */
4855 void UseDefaultSecurityToken();
4856
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004857 /** Returns the security token of this context.*/
4858 Handle<Value> GetSecurityToken();
4859
v8.team.kasperl727e9952008-09-02 14:56:44 +00004860 /**
4861 * Enter this context. After entering a context, all code compiled
4862 * and run is compiled and run in this context. If another context
4863 * is already entered, this old context is saved so it can be
4864 * restored when the new context is exited.
4865 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004866 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00004867
4868 /**
4869 * Exit this context. Exiting the current context restores the
4870 * context that was in place when entering the current context.
4871 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004872 void Exit();
4873
v8.team.kasperl727e9952008-09-02 14:56:44 +00004874 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004875 bool HasOutOfMemoryException();
4876
v8.team.kasperl727e9952008-09-02 14:56:44 +00004877 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004878 static bool InContext();
4879
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004880 /** Returns an isolate associated with a current context. */
4881 v8::Isolate* GetIsolate();
yangguo@chromium.orge19986e2013-01-16 09:48:20 +00004882
4883 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004884 * Gets the embedder data with the given index, which must have been set by a
4885 * previous call to SetEmbedderData with the same index. Note that index 0
4886 * currently has a special meaning for Chrome's debugger.
4887 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004888 V8_INLINE(Local<Value> GetEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004889
4890 /**
4891 * Sets the embedder data with the given index, growing the data as
4892 * needed. Note that index 0 currently has a special meaning for Chrome's
4893 * debugger.
4894 */
4895 void SetEmbedderData(int index, Handle<Value> value);
4896
4897 /**
4898 * Gets a 2-byte-aligned native pointer from the embedder data with the given
4899 * index, which must have bees set by a previous call to
4900 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
4901 * currently has a special meaning for Chrome's debugger.
4902 */
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004903 V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004904
4905 /**
4906 * Sets a 2-byte-aligned native pointer in the embedder data with the given
4907 * index, growing the data as needed. Note that index 0 currently has a
4908 * special meaning for Chrome's debugger.
4909 */
4910 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00004911
4912 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004913 * Control whether code generation from strings is allowed. Calling
4914 * this method with false will disable 'eval' and the 'Function'
4915 * constructor for code running in this context. If 'eval' or the
4916 * 'Function' constructor are used an exception will be thrown.
4917 *
4918 * If code generation from strings is not allowed the
4919 * V8::AllowCodeGenerationFromStrings callback will be invoked if
4920 * set before blocking the call to 'eval' or the 'Function'
4921 * constructor. If that callback returns true, the call will be
4922 * allowed, otherwise an exception will be thrown. If no callback is
4923 * set an exception will be thrown.
4924 */
4925 void AllowCodeGenerationFromStrings(bool allow);
4926
4927 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00004928 * Returns true if code generation from strings is allowed for the context.
4929 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
4930 */
4931 bool IsCodeGenerationFromStringsAllowed();
4932
4933 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004934 * Sets the error description for the exception that is thrown when
4935 * code generation from strings is not allowed and 'eval' or the 'Function'
4936 * constructor are called.
4937 */
4938 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
4939
4940 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004941 * Stack-allocated class which sets the execution context for all
4942 * operations executed within a local scope.
4943 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004944 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004945 public:
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004946 explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004947 context_->Enter();
4948 }
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00004949 // TODO(dcarney): deprecate
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004950 V8_INLINE(Scope(Isolate* isolate, Persistent<Context>& context)) // NOLINT
4951#ifndef V8_USE_UNSAFE_HANDLES
4952 : context_(Handle<Context>::New(isolate, context)) {
4953#else
4954 : context_(Local<Context>::New(isolate, context)) {
4955#endif
4956 context_->Enter();
4957 }
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00004958 V8_INLINE(~Scope()) { context_->Exit(); }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004959
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004960 private:
4961 Handle<Context> context_;
4962 };
4963
4964 private:
4965 friend class Value;
4966 friend class Script;
4967 friend class Object;
4968 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004969
4970 Local<Value> SlowGetEmbedderData(int index);
4971 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004972};
4973
4974
4975/**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004976 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
4977 * to use any given V8 isolate, see the comments in the Isolate class. The
4978 * definition of 'using a V8 isolate' includes accessing handles or holding onto
4979 * object pointers obtained from V8 handles while in the particular V8 isolate.
4980 * It is up to the user of V8 to ensure, perhaps with locking, that this
4981 * constraint is not violated. In addition to any other synchronization
4982 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
4983 * used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004984 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00004985 * v8::Locker is a scoped lock object. While it's active, i.e. between its
4986 * construction and destruction, the current thread is allowed to use the locked
4987 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
4988 * any time. In other words, the scope of a v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004989 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00004990 * Sample usage:
4991* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004992 * ...
4993 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00004994 * v8::Locker locker(isolate);
4995 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004996 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00004997 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004998 * ...
4999 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00005000 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005001 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005002 * If you wish to stop using V8 in a thread A you can do this either by
5003 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
5004 * object:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005005 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00005006 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005007 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005008 * isolate->Exit();
5009 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005010 * ...
5011 * // Code not using V8 goes here while V8 can run in another thread.
5012 * ...
5013 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005014 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005015 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005016 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005017 * The Unlocker object is intended for use in a long-running callback from V8,
5018 * where you want to release the V8 lock for other threads to use.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005019 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005020 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
5021 * given thread. This can be useful if you have code that can be called either
5022 * from code that holds the lock or from code that does not. The Unlocker is
5023 * not recursive so you can not have several Unlockers on the stack at once, and
5024 * 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 +00005025 *
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005026 * An unlocker will unlock several lockers if it has to and reinstate the
5027 * correct depth of locking on its destruction, e.g.:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005028 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00005029 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005030 * // V8 not locked.
5031 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005032 * v8::Locker locker(isolate);
5033 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005034 * // V8 locked.
5035 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005036 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005037 * // V8 still locked (2 levels).
5038 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005039 * isolate->Exit();
5040 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005041 * // V8 not locked.
5042 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00005043 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005044 * // V8 locked again (2 levels).
5045 * }
5046 * // V8 still locked (1 level).
5047 * }
5048 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00005049 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005050 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005051class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005052 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005053 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005054 * Initialize Unlocker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005055 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005056 V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
5057
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005058 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005059 V8_DEPRECATED(Unlocker());
5060
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005061 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00005062 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005063 void Initialize(Isolate* isolate);
5064
lrn@chromium.org1c092762011-05-09 09:42:16 +00005065 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005066};
5067
5068
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005069class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005070 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00005071 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005072 * Initialize Locker for a given Isolate.
lrn@chromium.org1c092762011-05-09 09:42:16 +00005073 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005074 V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
5075
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005076 /** Deprecated. Use Isolate version instead. */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005077 V8_DEPRECATED(Locker());
5078
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005079 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00005080
5081 /**
5082 * Start preemption.
5083 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005084 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00005085 * that will switch between multiple threads that are in contention
5086 * for the V8 lock.
5087 */
5088 static void StartPreemption(int every_n_ms);
5089
5090 /**
5091 * Stop preemption.
5092 */
5093 static void StopPreemption();
5094
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005095 /**
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005096 * Returns whether or not the locker for a given isolate, is locked by the
5097 * current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005098 */
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005099 static bool IsLocked(Isolate* isolate);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005100
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005101 /**
5102 * Returns whether v8::Locker is being used by this V8 instance.
5103 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00005104 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005106 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005107 void Initialize(Isolate* isolate);
5108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005109 bool has_lock_;
5110 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005111 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005112
ager@chromium.orgddb913d2009-01-27 10:01:48 +00005113 static bool active_;
5114
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005115 // Disallow copying and assigning.
5116 Locker(const Locker&);
5117 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005118};
5119
5120
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005121/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005122 * A struct for exporting HeapStats data from V8, using "push" model.
5123 */
5124struct HeapStatsUpdate;
5125
5126
5127/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005128 * An interface for exporting data from V8, using "push" model.
5129 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005130class V8EXPORT OutputStream { // NOLINT
5131 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005132 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005133 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005134 };
5135 enum WriteResult {
5136 kContinue = 0,
5137 kAbort = 1
5138 };
5139 virtual ~OutputStream() {}
5140 /** Notify about the end of stream. */
5141 virtual void EndOfStream() = 0;
5142 /** Get preferred output chunk size. Called only once. */
5143 virtual int GetChunkSize() { return 1024; }
5144 /** Get preferred output encoding. Called only once. */
5145 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
5146 /**
5147 * Writes the next chunk of snapshot data into the stream. Writing
5148 * can be stopped by returning kAbort as function result. EndOfStream
5149 * will not be called in case writing was aborted.
5150 */
5151 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005152 /**
5153 * Writes the next chunk of heap stats data into the stream. Writing
5154 * can be stopped by returning kAbort as function result. EndOfStream
5155 * will not be called in case writing was aborted.
5156 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005157 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00005158 return kAbort;
5159 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005160};
5161
5162
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005163/**
5164 * An interface for reporting progress and controlling long-running
5165 * activities.
5166 */
5167class V8EXPORT ActivityControl { // NOLINT
5168 public:
5169 enum ControlOption {
5170 kContinue = 0,
5171 kAbort = 1
5172 };
5173 virtual ~ActivityControl() {}
5174 /**
5175 * Notify about current progress. The activity can be stopped by
5176 * returning kAbort as the callback result.
5177 */
5178 virtual ControlOption ReportProgressValue(int done, int total) = 0;
5179};
5180
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005181
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00005182// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005183
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005184
5185namespace internal {
5186
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00005187const int kApiPointerSize = sizeof(void*); // NOLINT
5188const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005189
5190// Tag information for HeapObject.
5191const int kHeapObjectTag = 1;
5192const int kHeapObjectTagSize = 2;
5193const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
5194
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005195// Tag information for Smi.
5196const int kSmiTag = 0;
5197const int kSmiTagSize = 1;
5198const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
5199
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005200template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005201
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005202template<int kSmiShiftSize>
5203V8_INLINE(internal::Object* IntToSmi(int value)) {
5204 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
5205 intptr_t tagged_value =
5206 (static_cast<intptr_t>(value) << smi_shift_bits) | kSmiTag;
5207 return reinterpret_cast<internal::Object*>(tagged_value);
5208}
5209
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005210// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005211template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005212 static const int kSmiShiftSize = 0;
5213 static const int kSmiValueSize = 31;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005214 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005215 int shift_bits = kSmiTagSize + kSmiShiftSize;
5216 // Throw away top 32 bits and shift down (requires >> to be sign extending).
5217 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
5218 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005219 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5220 return internal::IntToSmi<kSmiShiftSize>(value);
5221 }
5222 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5223 // To be representable as an tagged small integer, the two
5224 // most-significant bits of 'value' must be either 00 or 11 due to
5225 // sign-extension. To check this we add 01 to the two
5226 // most-significant bits, and check if the most-significant bit is 0
5227 //
5228 // CAUTION: The original code below:
5229 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
5230 // may lead to incorrect results according to the C language spec, and
5231 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
5232 // compiler may produce undefined results in case of signed integer
5233 // overflow. The computation must be done w/ unsigned ints.
5234 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
5235 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005236};
5237
5238// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005239template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005240 static const int kSmiShiftSize = 31;
5241 static const int kSmiValueSize = 32;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005242 V8_INLINE(static int SmiToInt(internal::Object* value)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005243 int shift_bits = kSmiTagSize + kSmiShiftSize;
5244 // Shift down and throw away top 32 bits.
5245 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
5246 }
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005247 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5248 return internal::IntToSmi<kSmiShiftSize>(value);
5249 }
5250 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5251 // To be representable as a long smi, the value must be a 32-bit integer.
5252 return (value == static_cast<int32_t>(value));
5253 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005254};
5255
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005256typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
5257const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
5258const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005259
5260/**
5261 * This class exports constants and functionality from within v8 that
5262 * is necessary to implement inline functions in the v8 api. Don't
5263 * depend on functions and constants defined here.
5264 */
5265class Internals {
5266 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005267 // These values match non-compiler-dependent values defined within
5268 // the implementation of v8.
5269 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005270 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005271 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005272
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005273 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00005274 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005275 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005276 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
5277 static const int kContextHeaderSize = 2 * kApiPointerSize;
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005278 static const int kContextEmbedderDataIndex = 64;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005279 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005280 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00005281 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005282 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005283
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005284 static const int kIsolateStateOffset = 0;
5285 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
5286 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
5287 static const int kUndefinedValueRootIndex = 5;
5288 static const int kNullValueRootIndex = 7;
5289 static const int kTrueValueRootIndex = 8;
5290 static const int kFalseValueRootIndex = 9;
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005291 static const int kEmptyStringRootIndex = 129;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005292
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005293 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
5294 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
5295 static const int kNodeStateMask = 0xf;
5296 static const int kNodeStateIsWeakValue = 2;
5297 static const int kNodeStateIsNearDeathValue = 4;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005298 static const int kNodeIsIndependentShift = 4;
5299 static const int kNodeIsPartiallyDependentShift = 5;
5300
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005301 static const int kJSObjectType = 0xae;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005302 static const int kFirstNonstringType = 0x80;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005303 static const int kOddballType = 0x83;
5304 static const int kForeignType = 0x86;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005305
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005306 static const int kUndefinedOddballKind = 5;
5307 static const int kNullOddballKind = 3;
5308
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005309 V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005310 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
5311 kHeapObjectTag);
5312 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005313
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005314 V8_INLINE(static int SmiValue(internal::Object* value)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00005315 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005316 }
5317
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005318 V8_INLINE(static internal::Object* IntToSmi(int value)) {
5319 return PlatformSmiTagging::IntToSmi(value);
5320 }
5321
5322 V8_INLINE(static bool IsValidSmi(intptr_t value)) {
5323 return PlatformSmiTagging::IsValidSmi(value);
5324 }
5325
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005326 V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005327 typedef internal::Object O;
5328 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
5329 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
5330 }
5331
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005332 V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005333 typedef internal::Object O;
5334 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
5335 }
5336
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005337 V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005338 int representation = (instance_type & kFullStringRepresentationMask);
5339 return representation == kExternalTwoByteRepresentationTag;
5340 }
5341
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005342 V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005343 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
5344 return *reinterpret_cast<int*>(addr) == 1;
5345 }
5346
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005347 V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
5348 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5349 return *addr & (1 << shift);
5350 }
5351
5352 V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
5353 bool value, int shift)) {
5354 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5355 uint8_t mask = 1 << shift;
5356 *addr = (*addr & ~mask) | (value << shift);
5357 }
5358
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005359 V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
5360 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5361 return *addr & kNodeStateMask;
5362 }
5363
5364 V8_INLINE(static void UpdateNodeState(internal::Object** obj,
5365 uint8_t value)) {
5366 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
5367 *addr = (*addr & ~kNodeStateMask) | value;
5368 }
5369
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005370 V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005371 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5372 kIsolateEmbedderDataOffset;
5373 *reinterpret_cast<void**>(addr) = data;
5374 }
5375
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005376 V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005377 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
5378 kIsolateEmbedderDataOffset;
5379 return *reinterpret_cast<void**>(addr);
5380 }
5381
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005382 V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
5383 int index)) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005384 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
5385 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
5386 }
5387
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005388 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005389 V8_INLINE(static T ReadField(Object* ptr, int offset)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005390 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
5391 return *reinterpret_cast<T*>(addr);
5392 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005393
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005394 template <typename T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005395 V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005396 typedef internal::Object O;
5397 typedef internal::Internals I;
5398 O* ctx = *reinterpret_cast<O**>(context);
5399 int embedder_data_offset = I::kContextHeaderSize +
5400 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
5401 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
5402 int value_offset =
5403 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
5404 return I::ReadField<T>(embedder_data, value_offset);
5405 }
5406
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005407 V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
5408 V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
5409 V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
5410 V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
5411 V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
5412 V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
5413 V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005414};
5415
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00005416} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005417
5418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005419template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005420Local<T>::Local() : Handle<T>() { }
5421
5422
5423template <class T>
5424Local<T> Local<T>::New(Handle<T> that) {
5425 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005426 T* that_ptr = *that;
5427 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5428 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
5429 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5430 reinterpret_cast<internal::HeapObject*>(*p))));
5431 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005432 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
5433}
5434
5435
5436template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005437Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005438 return New(isolate, that.val_);
5439}
5440
5441#ifndef V8_USE_UNSAFE_HANDLES
5442template <class T>
5443Local<T> Local<T>::New(Isolate* isolate, const Persistent<T>& that) {
5444 return New(isolate, that.val_);
5445}
5446
5447template <class T>
5448Handle<T> Handle<T>::New(Isolate* isolate, T* that) {
5449 if (that == NULL) return Handle<T>();
5450 T* that_ptr = that;
5451 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5452 return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5453 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5454}
5455#endif
5456
5457
5458template <class T>
5459Local<T> Local<T>::New(Isolate* isolate, T* that) {
5460 if (that == NULL) return Local<T>();
5461 T* that_ptr = that;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005462 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
5463 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
5464 reinterpret_cast<internal::Isolate*>(isolate), *p)));
5465}
5466
5467
5468template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005469Persistent<T> Persistent<T>::New(Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005470 return New(Isolate::GetCurrent(), that.val_);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005471}
5472
5473
5474template <class T>
5475Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005476 return New(Isolate::GetCurrent(), that.val_);
5477}
5478
5479#ifndef V8_USE_UNSAFE_HANDLES
5480template <class T>
5481Persistent<T> Persistent<T>::New(Isolate* isolate, Persistent<T> that) {
5482 return New(Isolate::GetCurrent(), that.val_);
5483}
5484#endif
5485
5486template <class T>
5487Persistent<T> Persistent<T>::New(Isolate* isolate, T* that) {
5488 if (that == NULL) return Persistent<T>();
5489 internal::Object** p = reinterpret_cast<internal::Object**>(that);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005490 return Persistent<T>(reinterpret_cast<T*>(
5491 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
5492 p)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005493}
5494
5495
5496template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005497bool Persistent<T>::IsIndependent() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005498 return IsIndependent(Isolate::GetCurrent());
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005499}
5500
5501
5502template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005503bool Persistent<T>::IsIndependent(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005504 typedef internal::Internals I;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005505 if (this->IsEmpty()) return false;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005506 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005507 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005508 I::kNodeIsIndependentShift);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005509}
5510
5511
5512template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005513bool Persistent<T>::IsNearDeath() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005514 return IsNearDeath(Isolate::GetCurrent());
5515}
5516
5517
5518template <class T>
5519bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
5520 typedef internal::Internals I;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005521 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005522 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005523 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005524 I::kNodeStateIsNearDeathValue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005525}
5526
5527
5528template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00005529bool Persistent<T>::IsWeak() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005530 return IsWeak(Isolate::GetCurrent());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005531}
5532
5533
5534template <class T>
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005535bool Persistent<T>::IsWeak(Isolate* isolate) const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005536 typedef internal::Internals I;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005537 if (this->IsEmpty()) return false;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005538 if (!I::IsInitialized(isolate)) return false;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005539 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005540 I::kNodeStateIsWeakValue;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005541}
5542
5543
5544template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005545void Persistent<T>::Dispose() {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005546 if (this->IsEmpty()) return;
5547 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
5548#ifndef V8_USE_UNSAFE_HANDLES
5549 val_ = 0;
5550#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005551}
5552
5553
5554template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005555void Persistent<T>::Dispose(Isolate* isolate) {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00005556 Dispose();
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00005557}
5558
5559
5560template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005561template <typename S, typename P>
5562void Persistent<T>::MakeWeak(
5563 Isolate* isolate,
5564 P* parameters,
5565 typename WeakReferenceCallbacks<S, P>::Revivable callback) {
5566 TYPE_CHECK(S, T);
5567 typedef typename WeakReferenceCallbacks<Value, void>::Revivable Revivable;
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005568 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005569 reinterpret_cast<internal::Object**>(this->val_),
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005570 parameters,
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005571 reinterpret_cast<Revivable>(callback),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005572 NULL);
5573}
5574
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005575
5576template <class T>
5577template <typename P>
5578void Persistent<T>::MakeWeak(
5579 Isolate* isolate,
5580 P* parameters,
5581 typename WeakReferenceCallbacks<T, P>::Revivable callback) {
5582 MakeWeak<T, P>(isolate, parameters, callback);
5583}
5584
5585
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005586template <class T>
5587void Persistent<T>::MakeWeak(Isolate* isolate,
5588 void* parameters,
5589 NearDeathCallback callback) {
5590 V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005591 reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005592 parameters,
5593 NULL,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +00005594 callback);
5595}
5596
5597template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005598void Persistent<T>::ClearWeak() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005599 ClearWeak(Isolate::GetCurrent());
5600}
5601
5602template <class T>
5603void Persistent<T>::ClearWeak(Isolate* isolate) {
5604 V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005605 reinterpret_cast<internal::Object**>(this->val_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005606}
5607
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005608template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005609void Persistent<T>::MarkIndependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005610 MarkIndependent(Isolate::GetCurrent());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00005611}
5612
5613template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005614void Persistent<T>::MarkIndependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005615 typedef internal::Internals I;
5616 if (this->IsEmpty()) return;
5617 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005618 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005619 true,
5620 I::kNodeIsIndependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005621}
5622
5623template <class T>
5624void Persistent<T>::MarkPartiallyDependent() {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005625 MarkPartiallyDependent(Isolate::GetCurrent());
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005626}
5627
5628template <class T>
5629void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005630 typedef internal::Internals I;
5631 if (this->IsEmpty()) return;
5632 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005633 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005634 true,
5635 I::kNodeIsPartiallyDependentShift);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00005636}
5637
5638template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005639void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005640 SetWrapperClassId(Isolate::GetCurrent(), class_id);
5641}
5642
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005643
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005644template <class T>
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005645void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
5646 Dispose(isolate);
5647#ifdef V8_USE_UNSAFE_HANDLES
5648 *this = *New(isolate, other);
5649#else
5650 if (other.IsEmpty()) {
5651 this->val_ = NULL;
5652 return;
5653 }
5654 internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
5655 this->val_ = reinterpret_cast<T*>(
5656 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
5657#endif
5658}
5659
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00005660
5661template <class T>
5662T* Persistent<T>::ClearAndLeak() {
5663 T* old;
5664#ifdef V8_USE_UNSAFE_HANDLES
5665 old = **this;
5666 *this = Persistent<T>();
5667#else
5668 old = val_;
5669 val_ = NULL;
5670#endif
5671 return old;
5672}
5673
5674
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005675template <class T>
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005676void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005677 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005678 if (this->IsEmpty()) return;
5679 if (!I::IsInitialized(isolate)) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005680 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005681 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5682 *reinterpret_cast<uint16_t*>(addr) = class_id;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005683}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005684
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005685template <class T>
5686uint16_t Persistent<T>::WrapperClassId() const {
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005687 return WrapperClassId(Isolate::GetCurrent());
5688}
5689
5690template <class T>
5691uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005692 typedef internal::Internals I;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00005693 if (this->IsEmpty()) return 0;
5694 if (!I::IsInitialized(isolate)) return 0;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00005695 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00005696 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
5697 return *reinterpret_cast<uint16_t*>(addr);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00005698}
5699
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005700
5701template<typename T>
5702ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
5703
5704template<typename T>
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00005705template<typename S>
5706void ReturnValue<T>::Set(const Persistent<S>& handle) {
5707 TYPE_CHECK(T, S);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005708 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5709}
5710
5711template<typename T>
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00005712template<typename S>
5713void ReturnValue<T>::Set(const Handle<S> handle) {
5714 TYPE_CHECK(T, S);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005715 *value_ = *reinterpret_cast<internal::Object**>(*handle);
5716}
5717
5718template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005719void ReturnValue<T>::Set(double i) {
5720 Set(Number::New(GetIsolate(), i));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005721}
5722
5723template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005724void ReturnValue<T>::Set(int32_t i) {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005725 typedef internal::Internals I;
5726 if (V8_LIKELY(I::IsValidSmi(i))) {
5727 *value_ = I::IntToSmi(i);
5728 return;
5729 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005730 Set(Integer::New(i, GetIsolate()));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005731}
5732
5733template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005734void ReturnValue<T>::Set(uint32_t i) {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005735 typedef internal::Internals I;
5736 if (V8_LIKELY(I::IsValidSmi(i))) {
5737 *value_ = I::IntToSmi(i);
5738 return;
5739 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005740 Set(Integer::NewFromUnsigned(i, GetIsolate()));
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005741}
5742
5743template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005744void ReturnValue<T>::Set(bool value) {
5745 typedef internal::Internals I;
5746 int root_index;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005747 if (value) {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005748 root_index = I::kTrueValueRootIndex;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005749 } else {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005750 root_index = I::kFalseValueRootIndex;
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005751 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005752 *value_ = *I::GetRoot(GetIsolate(), root_index);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005753}
5754
5755template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005756void ReturnValue<T>::SetNull() {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005757 typedef internal::Internals I;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005758 *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005759}
5760
5761template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005762void ReturnValue<T>::SetUndefined() {
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005763 typedef internal::Internals I;
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005764 *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005765}
5766
5767template<typename T>
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00005768Isolate* ReturnValue<T>::GetIsolate() {
5769 // Isolate is always the pointer below value_ on the stack.
5770 return *reinterpret_cast<Isolate**>(&value_[-1]);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005771}
5772
5773
5774template<typename T>
5775FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
5776 internal::Object** values,
5777 int length,
5778 bool is_construct_call)
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005779 : implicit_args_(implicit_args),
5780 values_(values),
5781 length_(length),
5782 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005783
5784
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005785Arguments::Arguments(internal::Object** args,
5786 internal::Object** values,
5787 int length,
5788 bool is_construct_call)
5789 : FunctionCallbackInfo<Value>(args, values, length, is_construct_call) { }
5790
5791
5792template<typename T>
5793Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005794 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
5795 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
5796}
5797
5798
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005799template<typename T>
5800Local<Function> FunctionCallbackInfo<T>::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005801 return Local<Function>(reinterpret_cast<Function*>(
5802 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005803}
5804
5805
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005806template<typename T>
5807Local<Object> FunctionCallbackInfo<T>::This() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005808 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
5809}
5810
5811
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005812template<typename T>
5813Local<Object> FunctionCallbackInfo<T>::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005814 return Local<Object>(reinterpret_cast<Object*>(
5815 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005816}
5817
5818
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005819template<typename T>
5820Local<Value> FunctionCallbackInfo<T>::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00005821 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005822}
5823
5824
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005825template<typename T>
5826Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00005827 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
5828}
5829
5830
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005831template<typename T>
5832ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
5833 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
5834}
5835
5836
5837template<typename T>
5838bool FunctionCallbackInfo<T>::IsConstructCall() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005839 return is_construct_call_;
5840}
5841
5842
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00005843template<typename T>
5844int FunctionCallbackInfo<T>::Length() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005845 return length_;
5846}
5847
5848
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005849template <class T>
5850Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005851 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
5852 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005853 return Local<T>(reinterpret_cast<T*>(after));
5854}
5855
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005856Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005857 return resource_name_;
5858}
5859
5860
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005861Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005862 return resource_line_offset_;
5863}
5864
5865
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005866Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005867 return resource_column_offset_;
5868}
5869
5870
5871Handle<Boolean> Boolean::New(bool value) {
5872 return value ? True() : False();
5873}
5874
5875
5876void Template::Set(const char* name, v8::Handle<Data> value) {
5877 Set(v8::String::New(name), value);
5878}
5879
5880
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005881Local<Value> Object::GetInternalField(int index) {
5882#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005883 typedef internal::Object O;
5884 typedef internal::Internals I;
5885 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005886 // Fast path: If the object is a plain JSObject, which is the common case, we
5887 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005888 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005889 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005890 O* value = I::ReadField<O*>(obj, offset);
5891 O** result = HandleScope::CreateHandle(value);
5892 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005893 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005894#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005895 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005896}
5897
5898
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005899void* Object::GetAlignedPointerFromInternalField(int index) {
5900#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005901 typedef internal::Object O;
5902 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005903 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005904 // Fast path: If the object is a plain JSObject, which is the common case, we
5905 // know where to find the internal fields and can return the value directly.
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005906 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00005907 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005908 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005909 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00005910#endif
5911 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005912}
5913
5914
5915String* String::Cast(v8::Value* value) {
5916#ifdef V8_ENABLE_CHECKS
5917 CheckCast(value);
5918#endif
5919 return static_cast<String*>(value);
5920}
5921
5922
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005923Local<String> String::Empty(Isolate* isolate) {
5924 typedef internal::Object* S;
5925 typedef internal::Internals I;
5926 if (!I::IsInitialized(isolate)) return Empty();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00005927 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00005928 return Local<String>(reinterpret_cast<String*>(slot));
5929}
5930
5931
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00005932Local<String> String::New(const char* data, int length) {
5933 return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
5934}
5935
5936
5937Local<String> String::New(const uint16_t* data, int length) {
5938 return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
5939}
5940
5941
5942Local<String> String::NewSymbol(const char* data, int length) {
5943 return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
5944}
5945
5946
5947Local<String> String::NewUndetectable(const char* data, int length) {
5948 return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
5949}
5950
5951
5952Local<String> String::NewUndetectable(const uint16_t* data, int length) {
5953 return NewFromTwoByte(
5954 Isolate::GetCurrent(), data, kUndetectableString, length);
5955}
5956
5957
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005958String::ExternalStringResource* String::GetExternalStringResource() const {
5959 typedef internal::Object O;
5960 typedef internal::Internals I;
5961 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005962 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005963 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005964 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5965 result = reinterpret_cast<String::ExternalStringResource*>(value);
5966 } else {
5967 result = NULL;
5968 }
5969#ifdef V8_ENABLE_CHECKS
5970 VerifyExternalStringResource(result);
5971#endif
5972 return result;
5973}
5974
5975
ulan@chromium.org56c14af2012-09-20 12:51:09 +00005976String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
5977 String::Encoding* encoding_out) const {
5978 typedef internal::Object O;
5979 typedef internal::Internals I;
5980 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
5981 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
5982 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5983 ExternalStringResourceBase* resource = NULL;
5984 if (type == I::kExternalAsciiRepresentationTag ||
5985 type == I::kExternalTwoByteRepresentationTag) {
5986 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
5987 resource = static_cast<ExternalStringResourceBase*>(value);
5988 }
5989#ifdef V8_ENABLE_CHECKS
5990 VerifyExternalStringResourceBase(resource, *encoding_out);
5991#endif
5992 return resource;
5993}
5994
5995
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00005996bool Value::IsUndefined() const {
5997#ifdef V8_ENABLE_CHECKS
5998 return FullIsUndefined();
5999#else
6000 return QuickIsUndefined();
6001#endif
6002}
6003
6004bool Value::QuickIsUndefined() const {
6005 typedef internal::Object O;
6006 typedef internal::Internals I;
6007 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6008 if (!I::HasHeapObjectTag(obj)) return false;
6009 if (I::GetInstanceType(obj) != I::kOddballType) return false;
6010 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
6011}
6012
6013
6014bool Value::IsNull() const {
6015#ifdef V8_ENABLE_CHECKS
6016 return FullIsNull();
6017#else
6018 return QuickIsNull();
6019#endif
6020}
6021
6022bool Value::QuickIsNull() const {
6023 typedef internal::Object O;
6024 typedef internal::Internals I;
6025 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6026 if (!I::HasHeapObjectTag(obj)) return false;
6027 if (I::GetInstanceType(obj) != I::kOddballType) return false;
6028 return (I::GetOddballKind(obj) == I::kNullOddballKind);
6029}
6030
6031
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006032bool Value::IsString() const {
6033#ifdef V8_ENABLE_CHECKS
6034 return FullIsString();
6035#else
6036 return QuickIsString();
6037#endif
6038}
6039
6040bool Value::QuickIsString() const {
6041 typedef internal::Object O;
6042 typedef internal::Internals I;
6043 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
6044 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006045 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006046}
6047
6048
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006049Symbol* Symbol::Cast(v8::Value* value) {
6050#ifdef V8_ENABLE_CHECKS
6051 CheckCast(value);
6052#endif
6053 return static_cast<Symbol*>(value);
6054}
6055
6056
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006057Number* Number::Cast(v8::Value* value) {
6058#ifdef V8_ENABLE_CHECKS
6059 CheckCast(value);
6060#endif
6061 return static_cast<Number*>(value);
6062}
6063
6064
6065Integer* Integer::Cast(v8::Value* value) {
6066#ifdef V8_ENABLE_CHECKS
6067 CheckCast(value);
6068#endif
6069 return static_cast<Integer*>(value);
6070}
6071
6072
6073Date* Date::Cast(v8::Value* value) {
6074#ifdef V8_ENABLE_CHECKS
6075 CheckCast(value);
6076#endif
6077 return static_cast<Date*>(value);
6078}
6079
6080
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006081StringObject* StringObject::Cast(v8::Value* value) {
6082#ifdef V8_ENABLE_CHECKS
6083 CheckCast(value);
6084#endif
6085 return static_cast<StringObject*>(value);
6086}
6087
6088
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00006089SymbolObject* SymbolObject::Cast(v8::Value* value) {
6090#ifdef V8_ENABLE_CHECKS
6091 CheckCast(value);
6092#endif
6093 return static_cast<SymbolObject*>(value);
6094}
6095
6096
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006097NumberObject* NumberObject::Cast(v8::Value* value) {
6098#ifdef V8_ENABLE_CHECKS
6099 CheckCast(value);
6100#endif
6101 return static_cast<NumberObject*>(value);
6102}
6103
6104
6105BooleanObject* BooleanObject::Cast(v8::Value* value) {
6106#ifdef V8_ENABLE_CHECKS
6107 CheckCast(value);
6108#endif
6109 return static_cast<BooleanObject*>(value);
6110}
6111
6112
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00006113RegExp* RegExp::Cast(v8::Value* value) {
6114#ifdef V8_ENABLE_CHECKS
6115 CheckCast(value);
6116#endif
6117 return static_cast<RegExp*>(value);
6118}
6119
6120
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006121Object* Object::Cast(v8::Value* value) {
6122#ifdef V8_ENABLE_CHECKS
6123 CheckCast(value);
6124#endif
6125 return static_cast<Object*>(value);
6126}
6127
6128
6129Array* Array::Cast(v8::Value* value) {
6130#ifdef V8_ENABLE_CHECKS
6131 CheckCast(value);
6132#endif
6133 return static_cast<Array*>(value);
6134}
6135
6136
danno@chromium.orgca29dd82013-04-26 11:59:48 +00006137ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
6138#ifdef V8_ENABLE_CHECKS
6139 CheckCast(value);
6140#endif
6141 return static_cast<ArrayBuffer*>(value);
6142}
6143
6144
danno@chromium.orgf005df62013-04-30 16:36:45 +00006145TypedArray* TypedArray::Cast(v8::Value* value) {
6146#ifdef V8_ENABLE_CHECKS
6147 CheckCast(value);
6148#endif
6149 return static_cast<TypedArray*>(value);
6150}
6151
6152
6153Uint8Array* Uint8Array::Cast(v8::Value* value) {
6154#ifdef V8_ENABLE_CHECKS
6155 CheckCast(value);
6156#endif
6157 return static_cast<Uint8Array*>(value);
6158}
6159
6160
6161Int8Array* Int8Array::Cast(v8::Value* value) {
6162#ifdef V8_ENABLE_CHECKS
6163 CheckCast(value);
6164#endif
6165 return static_cast<Int8Array*>(value);
6166}
6167
6168
6169Uint16Array* Uint16Array::Cast(v8::Value* value) {
6170#ifdef V8_ENABLE_CHECKS
6171 CheckCast(value);
6172#endif
6173 return static_cast<Uint16Array*>(value);
6174}
6175
6176
6177Int16Array* Int16Array::Cast(v8::Value* value) {
6178#ifdef V8_ENABLE_CHECKS
6179 CheckCast(value);
6180#endif
6181 return static_cast<Int16Array*>(value);
6182}
6183
6184
6185Uint32Array* Uint32Array::Cast(v8::Value* value) {
6186#ifdef V8_ENABLE_CHECKS
6187 CheckCast(value);
6188#endif
6189 return static_cast<Uint32Array*>(value);
6190}
6191
6192
6193Int32Array* Int32Array::Cast(v8::Value* value) {
6194#ifdef V8_ENABLE_CHECKS
6195 CheckCast(value);
6196#endif
6197 return static_cast<Int32Array*>(value);
6198}
6199
6200
6201Float32Array* Float32Array::Cast(v8::Value* value) {
6202#ifdef V8_ENABLE_CHECKS
6203 CheckCast(value);
6204#endif
6205 return static_cast<Float32Array*>(value);
6206}
6207
6208
6209Float64Array* Float64Array::Cast(v8::Value* value) {
6210#ifdef V8_ENABLE_CHECKS
6211 CheckCast(value);
6212#endif
6213 return static_cast<Float64Array*>(value);
6214}
6215
6216
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006217Function* Function::Cast(v8::Value* value) {
6218#ifdef V8_ENABLE_CHECKS
6219 CheckCast(value);
6220#endif
6221 return static_cast<Function*>(value);
6222}
6223
6224
6225External* External::Cast(v8::Value* value) {
6226#ifdef V8_ENABLE_CHECKS
6227 CheckCast(value);
6228#endif
6229 return static_cast<External*>(value);
6230}
6231
6232
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006233template<typename T>
6234Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
6235 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00006236}
6237
6238
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006239template<typename T>
6240Local<Value> PropertyCallbackInfo<T>::Data() const {
6241 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006242}
6243
6244
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006245template<typename T>
6246Local<Object> PropertyCallbackInfo<T>::This() const {
6247 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006248}
6249
6250
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +00006251template<typename T>
6252Local<Object> PropertyCallbackInfo<T>::Holder() const {
6253 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
6254}
6255
6256
6257template<typename T>
6258ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
6259 return ReturnValue<T>(&args_[kReturnValueIndex]);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006260}
6261
6262
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00006263Handle<Primitive> Undefined(Isolate* isolate) {
6264 typedef internal::Object* S;
6265 typedef internal::Internals I;
6266 if (!I::IsInitialized(isolate)) return Undefined();
6267 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
6268 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6269}
6270
6271
6272Handle<Primitive> Null(Isolate* isolate) {
6273 typedef internal::Object* S;
6274 typedef internal::Internals I;
6275 if (!I::IsInitialized(isolate)) return Null();
6276 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
6277 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
6278}
6279
6280
6281Handle<Boolean> True(Isolate* isolate) {
6282 typedef internal::Object* S;
6283 typedef internal::Internals I;
6284 if (!I::IsInitialized(isolate)) return True();
6285 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
6286 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6287}
6288
6289
6290Handle<Boolean> False(Isolate* isolate) {
6291 typedef internal::Object* S;
6292 typedef internal::Internals I;
6293 if (!I::IsInitialized(isolate)) return False();
6294 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
6295 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
6296}
6297
6298
6299void Isolate::SetData(void* data) {
6300 typedef internal::Internals I;
6301 I::SetEmbedderData(this, data);
6302}
6303
6304
6305void* Isolate::GetData() {
6306 typedef internal::Internals I;
6307 return I::GetEmbedderData(this);
6308}
6309
6310
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00006311Local<Value> Context::GetEmbedderData(int index) {
6312#ifndef V8_ENABLE_CHECKS
6313 typedef internal::Object O;
6314 typedef internal::Internals I;
6315 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
6316 return Local<Value>(reinterpret_cast<Value*>(result));
6317#else
6318 return SlowGetEmbedderData(index);
6319#endif
6320}
6321
6322
6323void* Context::GetAlignedPointerFromEmbedderData(int index) {
6324#ifndef V8_ENABLE_CHECKS
6325 typedef internal::Internals I;
6326 return I::ReadEmbedderData<void*>(this, index);
6327#else
6328 return SlowGetAlignedPointerFromEmbedderData(index);
6329#endif
6330}
6331
6332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006333/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00006334 * \example shell.cc
6335 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006336 * command-line and executes them.
6337 */
6338
6339
6340/**
6341 * \example process.cc
6342 */
6343
6344
6345} // namespace v8
6346
6347
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006348#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006349#undef TYPE_CHECK
6350
6351
ager@chromium.org9258b6b2008-09-11 09:11:10 +00006352#endif // V8_H_