Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2007-2009 the V8 project authors. All rights reserved. |
| 2 | // 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 | |
| 28 | /** \mainpage V8 API Reference Guide |
| 29 | * |
| 30 | * V8 is Google's open source JavaScript engine. |
| 31 | * |
| 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/ |
| 36 | */ |
| 37 | |
| 38 | #ifndef V8_H_ |
| 39 | #define V8_H_ |
| 40 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 41 | #include "v8stdint.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
| 43 | #ifdef _WIN32 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | |
| 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. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | #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 |
| 53 | #endif |
| 54 | |
| 55 | #ifdef BUILDING_V8_SHARED |
| 56 | #define V8EXPORT __declspec(dllexport) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | #elif USING_V8_SHARED |
| 58 | #define V8EXPORT __declspec(dllimport) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | #else |
| 60 | #define V8EXPORT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | #endif // BUILDING_V8_SHARED |
| 62 | |
| 63 | #else // _WIN32 |
| 64 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 65 | // Setup for Linux shared library export. There is no need to distinguish |
| 66 | // between building or using the V8 shared library, but we should not |
| 67 | // export symbols when we are building a static library. |
| 68 | #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED) |
| 69 | #define V8EXPORT __attribute__ ((visibility("default"))) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | #else // defined(__GNUC__) && (__GNUC__ >= 4) |
| 71 | #define V8EXPORT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | #endif // defined(__GNUC__) && (__GNUC__ >= 4) |
| 73 | |
| 74 | #endif // _WIN32 |
| 75 | |
| 76 | /** |
| 77 | * The v8 JavaScript engine. |
| 78 | */ |
| 79 | namespace v8 { |
| 80 | |
| 81 | class Context; |
| 82 | class String; |
| 83 | class Value; |
| 84 | class Utils; |
| 85 | class Number; |
| 86 | class Object; |
| 87 | class Array; |
| 88 | class Int32; |
| 89 | class Uint32; |
| 90 | class External; |
| 91 | class Primitive; |
| 92 | class Boolean; |
| 93 | class Integer; |
| 94 | class Function; |
| 95 | class Date; |
| 96 | class ImplementationUtilities; |
| 97 | class Signature; |
| 98 | template <class T> class Handle; |
| 99 | template <class T> class Local; |
| 100 | template <class T> class Persistent; |
| 101 | class FunctionTemplate; |
| 102 | class ObjectTemplate; |
| 103 | class Data; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 104 | class AccessorInfo; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 105 | class StackTrace; |
| 106 | class StackFrame; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | |
| 108 | namespace internal { |
| 109 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | class Arguments; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 111 | class Object; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 112 | class Heap; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 113 | class HeapObject; |
| 114 | class Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
| 118 | // --- W e a k H a n d l e s |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * A weak reference callback function. |
| 123 | * |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 124 | * This callback should either explicitly invoke Dispose on |object| if |
| 125 | * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak. |
| 126 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 127 | * \param object the weak global object to be reclaimed by the garbage collector |
| 128 | * \param parameter the value passed in when making the weak global object |
| 129 | */ |
| 130 | typedef void (*WeakReferenceCallback)(Persistent<Value> object, |
| 131 | void* parameter); |
| 132 | |
| 133 | |
| 134 | // --- H a n d l e s --- |
| 135 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 136 | #define TYPE_CHECK(T, S) \ |
| 137 | while (false) { \ |
| 138 | *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * An object reference managed by the v8 garbage collector. |
| 143 | * |
| 144 | * All objects returned from v8 have to be tracked by the garbage |
| 145 | * collector so that it knows that the objects are still alive. Also, |
| 146 | * because the garbage collector may move objects, it is unsafe to |
| 147 | * point directly to an object. Instead, all objects are stored in |
| 148 | * handles which are known by the garbage collector and updated |
| 149 | * whenever an object moves. Handles should always be passed by value |
| 150 | * (except in cases like out-parameters) and they should never be |
| 151 | * allocated on the heap. |
| 152 | * |
| 153 | * There are two types of handles: local and persistent handles. |
| 154 | * Local handles are light-weight and transient and typically used in |
| 155 | * local operations. They are managed by HandleScopes. Persistent |
| 156 | * handles can be used when storing objects across several independent |
| 157 | * operations and have to be explicitly deallocated when they're no |
| 158 | * longer used. |
| 159 | * |
| 160 | * It is safe to extract the object stored in the handle by |
| 161 | * dereferencing the handle (for instance, to extract the Object* from |
| 162 | * an Handle<Object>); the value will still be governed by a handle |
| 163 | * behind the scenes and the same rules apply to these values as to |
| 164 | * their handles. |
| 165 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 166 | template <class T> class Handle { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | public: |
| 168 | |
| 169 | /** |
| 170 | * Creates an empty handle. |
| 171 | */ |
| 172 | inline Handle(); |
| 173 | |
| 174 | /** |
| 175 | * Creates a new handle for the specified value. |
| 176 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 177 | inline explicit Handle(T* val) : val_(val) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * Creates a handle for the contents of the specified handle. This |
| 181 | * constructor allows you to pass handles as arguments by value and |
| 182 | * to assign between handles. However, if you try to assign between |
| 183 | * incompatible handles, for instance from a Handle<String> to a |
| 184 | * Handle<Number> it will cause a compiletime error. Assigning |
| 185 | * between compatible handles, for instance assigning a |
| 186 | * Handle<String> to a variable declared as Handle<Value>, is legal |
| 187 | * because String is a subclass of Value. |
| 188 | */ |
| 189 | template <class S> inline Handle(Handle<S> that) |
| 190 | : val_(reinterpret_cast<T*>(*that)) { |
| 191 | /** |
| 192 | * This check fails when trying to convert between incompatible |
| 193 | * handles. For example, converting from a Handle<String> to a |
| 194 | * Handle<Number>. |
| 195 | */ |
| 196 | TYPE_CHECK(T, S); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns true if the handle is empty. |
| 201 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 202 | inline bool IsEmpty() const { return val_ == 0; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 204 | inline T* operator->() const { return val_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 205 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 206 | inline T* operator*() const { return val_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 207 | |
| 208 | /** |
| 209 | * Sets the handle to be empty. IsEmpty() will then return true. |
| 210 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 211 | inline void Clear() { this->val_ = 0; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * Checks whether two handles are the same. |
| 215 | * Returns true if both are empty, or if the objects |
| 216 | * to which they refer are identical. |
| 217 | * The handles' references are not checked. |
| 218 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 219 | template <class S> inline bool operator==(Handle<S> that) const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 220 | internal::Object** a = reinterpret_cast<internal::Object**>(**this); |
| 221 | internal::Object** b = reinterpret_cast<internal::Object**>(*that); |
| 222 | if (a == 0) return b == 0; |
| 223 | if (b == 0) return false; |
| 224 | return *a == *b; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Checks whether two handles are different. |
| 229 | * Returns true if only one of the handles is empty, or if |
| 230 | * the objects to which they refer are different. |
| 231 | * The handles' references are not checked. |
| 232 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 233 | template <class S> inline bool operator!=(Handle<S> that) const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | return !operator==(that); |
| 235 | } |
| 236 | |
| 237 | template <class S> static inline Handle<T> Cast(Handle<S> that) { |
| 238 | #ifdef V8_ENABLE_CHECKS |
| 239 | // If we're going to perform the type check then we have to check |
| 240 | // that the handle isn't empty before doing the checked cast. |
| 241 | if (that.IsEmpty()) return Handle<T>(); |
| 242 | #endif |
| 243 | return Handle<T>(T::Cast(*that)); |
| 244 | } |
| 245 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 246 | template <class S> inline Handle<S> As() { |
| 247 | return Handle<S>::Cast(*this); |
| 248 | } |
| 249 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | private: |
| 251 | T* val_; |
| 252 | }; |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * A light-weight stack-allocated object handle. All operations |
| 257 | * that return objects from within v8 return them in local handles. They |
| 258 | * are created within HandleScopes, and all local handles allocated within a |
| 259 | * handle scope are destroyed when the handle scope is destroyed. Hence it |
| 260 | * is not necessary to explicitly deallocate local handles. |
| 261 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 262 | template <class T> class Local : public Handle<T> { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 263 | public: |
| 264 | inline Local(); |
| 265 | template <class S> inline Local(Local<S> that) |
| 266 | : Handle<T>(reinterpret_cast<T*>(*that)) { |
| 267 | /** |
| 268 | * This check fails when trying to convert between incompatible |
| 269 | * handles. For example, converting from a Handle<String> to a |
| 270 | * Handle<Number>. |
| 271 | */ |
| 272 | TYPE_CHECK(T, S); |
| 273 | } |
| 274 | template <class S> inline Local(S* that) : Handle<T>(that) { } |
| 275 | template <class S> static inline Local<T> Cast(Local<S> that) { |
| 276 | #ifdef V8_ENABLE_CHECKS |
| 277 | // If we're going to perform the type check then we have to check |
| 278 | // that the handle isn't empty before doing the checked cast. |
| 279 | if (that.IsEmpty()) return Local<T>(); |
| 280 | #endif |
| 281 | return Local<T>(T::Cast(*that)); |
| 282 | } |
| 283 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 284 | template <class S> inline Local<S> As() { |
| 285 | return Local<S>::Cast(*this); |
| 286 | } |
| 287 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | /** Create a local handle for the content of another handle. |
| 289 | * The referee is kept alive by the local handle even when |
| 290 | * the original handle is destroyed/disposed. |
| 291 | */ |
| 292 | inline static Local<T> New(Handle<T> that); |
| 293 | }; |
| 294 | |
| 295 | |
| 296 | /** |
| 297 | * An object reference that is independent of any handle scope. Where |
| 298 | * a Local handle only lives as long as the HandleScope in which it was |
| 299 | * allocated, a Persistent handle remains valid until it is explicitly |
| 300 | * disposed. |
| 301 | * |
| 302 | * A persistent handle contains a reference to a storage cell within |
| 303 | * the v8 engine which holds an object value and which is updated by |
| 304 | * the garbage collector whenever the object is moved. A new storage |
| 305 | * cell can be created using Persistent::New and existing handles can |
| 306 | * be disposed using Persistent::Dispose. Since persistent handles |
| 307 | * are passed by value you may have many persistent handle objects |
| 308 | * that point to the same storage cell. For instance, if you pass a |
| 309 | * persistent handle as an argument to a function you will not get two |
| 310 | * different storage cells but rather two references to the same |
| 311 | * storage cell. |
| 312 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 313 | template <class T> class Persistent : public Handle<T> { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 314 | public: |
| 315 | |
| 316 | /** |
| 317 | * Creates an empty persistent handle that doesn't point to any |
| 318 | * storage cell. |
| 319 | */ |
| 320 | inline Persistent(); |
| 321 | |
| 322 | /** |
| 323 | * Creates a persistent handle for the same storage cell as the |
| 324 | * specified handle. This constructor allows you to pass persistent |
| 325 | * handles as arguments by value and to assign between persistent |
| 326 | * handles. However, attempting to assign between incompatible |
| 327 | * persistent handles, for instance from a Persistent<String> to a |
| 328 | * Persistent<Number> will cause a compiletime error. Assigning |
| 329 | * between compatible persistent handles, for instance assigning a |
| 330 | * Persistent<String> to a variable declared as Persistent<Value>, |
| 331 | * is allowed as String is a subclass of Value. |
| 332 | */ |
| 333 | template <class S> inline Persistent(Persistent<S> that) |
| 334 | : Handle<T>(reinterpret_cast<T*>(*that)) { |
| 335 | /** |
| 336 | * This check fails when trying to convert between incompatible |
| 337 | * handles. For example, converting from a Handle<String> to a |
| 338 | * Handle<Number>. |
| 339 | */ |
| 340 | TYPE_CHECK(T, S); |
| 341 | } |
| 342 | |
| 343 | template <class S> inline Persistent(S* that) : Handle<T>(that) { } |
| 344 | |
| 345 | /** |
| 346 | * "Casts" a plain handle which is known to be a persistent handle |
| 347 | * to a persistent handle. |
| 348 | */ |
| 349 | template <class S> explicit inline Persistent(Handle<S> that) |
| 350 | : Handle<T>(*that) { } |
| 351 | |
| 352 | template <class S> static inline Persistent<T> Cast(Persistent<S> that) { |
| 353 | #ifdef V8_ENABLE_CHECKS |
| 354 | // If we're going to perform the type check then we have to check |
| 355 | // that the handle isn't empty before doing the checked cast. |
| 356 | if (that.IsEmpty()) return Persistent<T>(); |
| 357 | #endif |
| 358 | return Persistent<T>(T::Cast(*that)); |
| 359 | } |
| 360 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 361 | template <class S> inline Persistent<S> As() { |
| 362 | return Persistent<S>::Cast(*this); |
| 363 | } |
| 364 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 365 | /** |
| 366 | * Creates a new persistent handle for an existing local or |
| 367 | * persistent handle. |
| 368 | */ |
| 369 | inline static Persistent<T> New(Handle<T> that); |
| 370 | |
| 371 | /** |
| 372 | * Releases the storage cell referenced by this persistent handle. |
| 373 | * Does not remove the reference to the cell from any handles. |
| 374 | * This handle's reference, and any any other references to the storage |
| 375 | * cell remain and IsEmpty will still return false. |
| 376 | */ |
| 377 | inline void Dispose(); |
| 378 | |
| 379 | /** |
| 380 | * Make the reference to this object weak. When only weak handles |
| 381 | * refer to the object, the garbage collector will perform a |
| 382 | * callback to the given V8::WeakReferenceCallback function, passing |
| 383 | * it the object reference and the given parameters. |
| 384 | */ |
| 385 | inline void MakeWeak(void* parameters, WeakReferenceCallback callback); |
| 386 | |
| 387 | /** Clears the weak reference to this object.*/ |
| 388 | inline void ClearWeak(); |
| 389 | |
| 390 | /** |
| 391 | *Checks if the handle holds the only reference to an object. |
| 392 | */ |
| 393 | inline bool IsNearDeath() const; |
| 394 | |
| 395 | /** |
| 396 | * Returns true if the handle's reference is weak. |
| 397 | */ |
| 398 | inline bool IsWeak() const; |
| 399 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 400 | /** |
| 401 | * Assigns a wrapper class ID to the handle. See RetainedObjectInfo |
| 402 | * interface description in v8-profiler.h for details. |
| 403 | */ |
| 404 | inline void SetWrapperClassId(uint16_t class_id); |
| 405 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | private: |
| 407 | friend class ImplementationUtilities; |
| 408 | friend class ObjectTemplate; |
| 409 | }; |
| 410 | |
| 411 | |
| 412 | /** |
| 413 | * A stack-allocated class that governs a number of local handles. |
| 414 | * After a handle scope has been created, all local handles will be |
| 415 | * allocated within that handle scope until either the handle scope is |
| 416 | * deleted or another handle scope is created. If there is already a |
| 417 | * handle scope and a new one is created, all allocations will take |
| 418 | * place in the new handle scope until it is deleted. After that, |
| 419 | * new handles will again be allocated in the original handle scope. |
| 420 | * |
| 421 | * After the handle scope of a local handle has been deleted the |
| 422 | * garbage collector will no longer track the object stored in the |
| 423 | * handle and may deallocate it. The behavior of accessing a handle |
| 424 | * for which the handle scope has been deleted is undefined. |
| 425 | */ |
| 426 | class V8EXPORT HandleScope { |
| 427 | public: |
| 428 | HandleScope(); |
| 429 | |
| 430 | ~HandleScope(); |
| 431 | |
| 432 | /** |
| 433 | * Closes the handle scope and returns the value as a handle in the |
| 434 | * previous scope, which is the new current scope after the call. |
| 435 | */ |
| 436 | template <class T> Local<T> Close(Handle<T> value); |
| 437 | |
| 438 | /** |
| 439 | * Counts the number of allocated handles. |
| 440 | */ |
| 441 | static int NumberOfHandles(); |
| 442 | |
| 443 | /** |
| 444 | * Creates a new handle with the given value. |
| 445 | */ |
| 446 | static internal::Object** CreateHandle(internal::Object* value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 447 | // Faster version, uses HeapObject to obtain the current Isolate. |
| 448 | static internal::Object** CreateHandle(internal::HeapObject* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 449 | |
| 450 | private: |
| 451 | // Make it impossible to create heap-allocated or illegal handle |
| 452 | // scopes by disallowing certain operations. |
| 453 | HandleScope(const HandleScope&); |
| 454 | void operator=(const HandleScope&); |
| 455 | void* operator new(size_t size); |
| 456 | void operator delete(void*, size_t); |
| 457 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 458 | // This Data class is accessible internally as HandleScopeData through a |
| 459 | // typedef in the ImplementationUtilities class. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | class V8EXPORT Data { |
| 461 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 462 | internal::Object** next; |
| 463 | internal::Object** limit; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 464 | int level; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 465 | inline void Initialize() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 466 | next = limit = NULL; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 467 | level = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 468 | } |
| 469 | }; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 470 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 471 | void Leave(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 472 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 473 | internal::Isolate* isolate_; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 474 | internal::Object** prev_next_; |
| 475 | internal::Object** prev_limit_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 476 | |
| 477 | // Allow for the active closing of HandleScopes which allows to pass a handle |
| 478 | // from the HandleScope being closed to the next top most HandleScope. |
| 479 | bool is_closed_; |
| 480 | internal::Object** RawClose(internal::Object** value); |
| 481 | |
| 482 | friend class ImplementationUtilities; |
| 483 | }; |
| 484 | |
| 485 | |
| 486 | // --- S p e c i a l o b j e c t s --- |
| 487 | |
| 488 | |
| 489 | /** |
| 490 | * The superclass of values and API object templates. |
| 491 | */ |
| 492 | class V8EXPORT Data { |
| 493 | private: |
| 494 | Data(); |
| 495 | }; |
| 496 | |
| 497 | |
| 498 | /** |
| 499 | * Pre-compilation data that can be associated with a script. This |
| 500 | * data can be calculated for a script in advance of actually |
| 501 | * compiling it, and can be stored between compilations. When script |
| 502 | * data is given to the compile method compilation will be faster. |
| 503 | */ |
| 504 | class V8EXPORT ScriptData { // NOLINT |
| 505 | public: |
| 506 | virtual ~ScriptData() { } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 507 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 508 | /** |
| 509 | * Pre-compiles the specified script (context-independent). |
| 510 | * |
| 511 | * \param input Pointer to UTF-8 script source code. |
| 512 | * \param length Length of UTF-8 script source code. |
| 513 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 514 | static ScriptData* PreCompile(const char* input, int length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 515 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 516 | /** |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 517 | * Pre-compiles the specified script (context-independent). |
| 518 | * |
| 519 | * NOTE: Pre-compilation using this method cannot happen on another thread |
| 520 | * without using Lockers. |
| 521 | * |
| 522 | * \param source Script source code. |
| 523 | */ |
| 524 | static ScriptData* PreCompile(Handle<String> source); |
| 525 | |
| 526 | /** |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 527 | * Load previous pre-compilation data. |
| 528 | * |
| 529 | * \param data Pointer to data returned by a call to Data() of a previous |
| 530 | * ScriptData. Ownership is not transferred. |
| 531 | * \param length Length of data. |
| 532 | */ |
| 533 | static ScriptData* New(const char* data, int length); |
| 534 | |
| 535 | /** |
| 536 | * Returns the length of Data(). |
| 537 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 538 | virtual int Length() = 0; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 539 | |
| 540 | /** |
| 541 | * Returns a serialized representation of this ScriptData that can later be |
| 542 | * passed to New(). NOTE: Serialized data is platform-dependent. |
| 543 | */ |
| 544 | virtual const char* Data() = 0; |
| 545 | |
| 546 | /** |
| 547 | * Returns true if the source code could not be parsed. |
| 548 | */ |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 549 | virtual bool HasError() = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | |
| 553 | /** |
| 554 | * The origin, within a file, of a script. |
| 555 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 556 | class ScriptOrigin { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 557 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 558 | inline ScriptOrigin( |
| 559 | Handle<Value> resource_name, |
| 560 | Handle<Integer> resource_line_offset = Handle<Integer>(), |
| 561 | Handle<Integer> resource_column_offset = Handle<Integer>()) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 562 | : resource_name_(resource_name), |
| 563 | resource_line_offset_(resource_line_offset), |
| 564 | resource_column_offset_(resource_column_offset) { } |
| 565 | inline Handle<Value> ResourceName() const; |
| 566 | inline Handle<Integer> ResourceLineOffset() const; |
| 567 | inline Handle<Integer> ResourceColumnOffset() const; |
| 568 | private: |
| 569 | Handle<Value> resource_name_; |
| 570 | Handle<Integer> resource_line_offset_; |
| 571 | Handle<Integer> resource_column_offset_; |
| 572 | }; |
| 573 | |
| 574 | |
| 575 | /** |
| 576 | * A compiled JavaScript script. |
| 577 | */ |
| 578 | class V8EXPORT Script { |
| 579 | public: |
| 580 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 581 | /** |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 582 | * Compiles the specified script (context-independent). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 583 | * |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 584 | * \param source Script source code. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 585 | * \param origin Script origin, owned by caller, no references are kept |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 586 | * when New() returns |
| 587 | * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile() |
| 588 | * using pre_data speeds compilation if it's done multiple times. |
| 589 | * Owned by caller, no references are kept when New() returns. |
| 590 | * \param script_data Arbitrary data associated with script. Using |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 591 | * this has same effect as calling SetData(), but allows data to be |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 592 | * available to compile event handlers. |
| 593 | * \return Compiled script object (context independent; when run it |
| 594 | * will use the currently entered context). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 595 | */ |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 596 | static Local<Script> New(Handle<String> source, |
| 597 | ScriptOrigin* origin = NULL, |
| 598 | ScriptData* pre_data = NULL, |
| 599 | Handle<String> script_data = Handle<String>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 600 | |
| 601 | /** |
| 602 | * Compiles the specified script using the specified file name |
| 603 | * object (typically a string) as the script's origin. |
| 604 | * |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 605 | * \param source Script source code. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 606 | * \param file_name file name object (typically a string) to be used |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 607 | * as the script's origin. |
| 608 | * \return Compiled script object (context independent; when run it |
| 609 | * will use the currently entered context). |
| 610 | */ |
| 611 | static Local<Script> New(Handle<String> source, |
| 612 | Handle<Value> file_name); |
| 613 | |
| 614 | /** |
| 615 | * Compiles the specified script (bound to current context). |
| 616 | * |
| 617 | * \param source Script source code. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 618 | * \param origin Script origin, owned by caller, no references are kept |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 619 | * when Compile() returns |
| 620 | * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile() |
| 621 | * using pre_data speeds compilation if it's done multiple times. |
| 622 | * Owned by caller, no references are kept when Compile() returns. |
| 623 | * \param script_data Arbitrary data associated with script. Using |
| 624 | * this has same effect as calling SetData(), but makes data available |
| 625 | * earlier (i.e. to compile event handlers). |
| 626 | * \return Compiled script object, bound to the context that was active |
| 627 | * when this function was called. When run it will always use this |
| 628 | * context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 629 | */ |
| 630 | static Local<Script> Compile(Handle<String> source, |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 631 | ScriptOrigin* origin = NULL, |
| 632 | ScriptData* pre_data = NULL, |
| 633 | Handle<String> script_data = Handle<String>()); |
| 634 | |
| 635 | /** |
| 636 | * Compiles the specified script using the specified file name |
| 637 | * object (typically a string) as the script's origin. |
| 638 | * |
| 639 | * \param source Script source code. |
| 640 | * \param file_name File name to use as script's origin |
| 641 | * \param script_data Arbitrary data associated with script. Using |
| 642 | * this has same effect as calling SetData(), but makes data available |
| 643 | * earlier (i.e. to compile event handlers). |
| 644 | * \return Compiled script object, bound to the context that was active |
| 645 | * when this function was called. When run it will always use this |
| 646 | * context. |
| 647 | */ |
| 648 | static Local<Script> Compile(Handle<String> source, |
| 649 | Handle<Value> file_name, |
| 650 | Handle<String> script_data = Handle<String>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 651 | |
| 652 | /** |
| 653 | * Runs the script returning the resulting value. If the script is |
| 654 | * context independent (created using ::New) it will be run in the |
| 655 | * currently entered context. If it is context specific (created |
| 656 | * using ::Compile) it will be run in the context in which it was |
| 657 | * compiled. |
| 658 | */ |
| 659 | Local<Value> Run(); |
| 660 | |
| 661 | /** |
| 662 | * Returns the script id value. |
| 663 | */ |
| 664 | Local<Value> Id(); |
| 665 | |
| 666 | /** |
| 667 | * Associate an additional data object with the script. This is mainly used |
| 668 | * with the debugger as this data object is only available through the |
| 669 | * debugger API. |
| 670 | */ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 671 | void SetData(Handle<String> data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 672 | }; |
| 673 | |
| 674 | |
| 675 | /** |
| 676 | * An error message. |
| 677 | */ |
| 678 | class V8EXPORT Message { |
| 679 | public: |
| 680 | Local<String> Get() const; |
| 681 | Local<String> GetSourceLine() const; |
| 682 | |
| 683 | /** |
| 684 | * Returns the resource name for the script from where the function causing |
| 685 | * the error originates. |
| 686 | */ |
| 687 | Handle<Value> GetScriptResourceName() const; |
| 688 | |
| 689 | /** |
| 690 | * Returns the resource data for the script from where the function causing |
| 691 | * the error originates. |
| 692 | */ |
| 693 | Handle<Value> GetScriptData() const; |
| 694 | |
| 695 | /** |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 696 | * Exception stack trace. By default stack traces are not captured for |
| 697 | * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows |
| 698 | * to change this option. |
| 699 | */ |
| 700 | Handle<StackTrace> GetStackTrace() const; |
| 701 | |
| 702 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 703 | * Returns the number, 1-based, of the line where the error occurred. |
| 704 | */ |
| 705 | int GetLineNumber() const; |
| 706 | |
| 707 | /** |
| 708 | * Returns the index within the script of the first character where |
| 709 | * the error occurred. |
| 710 | */ |
| 711 | int GetStartPosition() const; |
| 712 | |
| 713 | /** |
| 714 | * Returns the index within the script of the last character where |
| 715 | * the error occurred. |
| 716 | */ |
| 717 | int GetEndPosition() const; |
| 718 | |
| 719 | /** |
| 720 | * Returns the index within the line of the first character where |
| 721 | * the error occurred. |
| 722 | */ |
| 723 | int GetStartColumn() const; |
| 724 | |
| 725 | /** |
| 726 | * Returns the index within the line of the last character where |
| 727 | * the error occurred. |
| 728 | */ |
| 729 | int GetEndColumn() const; |
| 730 | |
| 731 | // TODO(1245381): Print to a string instead of on a FILE. |
| 732 | static void PrintCurrentStackTrace(FILE* out); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 733 | |
| 734 | static const int kNoLineNumberInfo = 0; |
| 735 | static const int kNoColumnInfo = 0; |
| 736 | }; |
| 737 | |
| 738 | |
| 739 | /** |
| 740 | * Representation of a JavaScript stack trace. The information collected is a |
| 741 | * snapshot of the execution stack and the information remains valid after |
| 742 | * execution continues. |
| 743 | */ |
| 744 | class V8EXPORT StackTrace { |
| 745 | public: |
| 746 | /** |
| 747 | * Flags that determine what information is placed captured for each |
| 748 | * StackFrame when grabbing the current stack trace. |
| 749 | */ |
| 750 | enum StackTraceOptions { |
| 751 | kLineNumber = 1, |
| 752 | kColumnOffset = 1 << 1 | kLineNumber, |
| 753 | kScriptName = 1 << 2, |
| 754 | kFunctionName = 1 << 3, |
| 755 | kIsEval = 1 << 4, |
| 756 | kIsConstructor = 1 << 5, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 757 | kScriptNameOrSourceURL = 1 << 6, |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 758 | kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 759 | kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 760 | }; |
| 761 | |
| 762 | /** |
| 763 | * Returns a StackFrame at a particular index. |
| 764 | */ |
| 765 | Local<StackFrame> GetFrame(uint32_t index) const; |
| 766 | |
| 767 | /** |
| 768 | * Returns the number of StackFrames. |
| 769 | */ |
| 770 | int GetFrameCount() const; |
| 771 | |
| 772 | /** |
| 773 | * Returns StackTrace as a v8::Array that contains StackFrame objects. |
| 774 | */ |
| 775 | Local<Array> AsArray(); |
| 776 | |
| 777 | /** |
| 778 | * Grab a snapshot of the the current JavaScript execution stack. |
| 779 | * |
| 780 | * \param frame_limit The maximum number of stack frames we want to capture. |
| 781 | * \param options Enumerates the set of things we will capture for each |
| 782 | * StackFrame. |
| 783 | */ |
| 784 | static Local<StackTrace> CurrentStackTrace( |
| 785 | int frame_limit, |
| 786 | StackTraceOptions options = kOverview); |
| 787 | }; |
| 788 | |
| 789 | |
| 790 | /** |
| 791 | * A single JavaScript stack frame. |
| 792 | */ |
| 793 | class V8EXPORT StackFrame { |
| 794 | public: |
| 795 | /** |
| 796 | * Returns the number, 1-based, of the line for the associate function call. |
| 797 | * This method will return Message::kNoLineNumberInfo if it is unable to |
| 798 | * retrieve the line number, or if kLineNumber was not passed as an option |
| 799 | * when capturing the StackTrace. |
| 800 | */ |
| 801 | int GetLineNumber() const; |
| 802 | |
| 803 | /** |
| 804 | * Returns the 1-based column offset on the line for the associated function |
| 805 | * call. |
| 806 | * This method will return Message::kNoColumnInfo if it is unable to retrieve |
| 807 | * the column number, or if kColumnOffset was not passed as an option when |
| 808 | * capturing the StackTrace. |
| 809 | */ |
| 810 | int GetColumn() const; |
| 811 | |
| 812 | /** |
| 813 | * Returns the name of the resource that contains the script for the |
| 814 | * function for this StackFrame. |
| 815 | */ |
| 816 | Local<String> GetScriptName() const; |
| 817 | |
| 818 | /** |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 819 | * Returns the name of the resource that contains the script for the |
| 820 | * function for this StackFrame or sourceURL value if the script name |
| 821 | * is undefined and its source ends with //@ sourceURL=... string. |
| 822 | */ |
| 823 | Local<String> GetScriptNameOrSourceURL() const; |
| 824 | |
| 825 | /** |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 826 | * Returns the name of the function associated with this stack frame. |
| 827 | */ |
| 828 | Local<String> GetFunctionName() const; |
| 829 | |
| 830 | /** |
| 831 | * Returns whether or not the associated function is compiled via a call to |
| 832 | * eval(). |
| 833 | */ |
| 834 | bool IsEval() const; |
| 835 | |
| 836 | /** |
| 837 | * Returns whther or not the associated function is called as a |
| 838 | * constructor via "new". |
| 839 | */ |
| 840 | bool IsConstructor() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 841 | }; |
| 842 | |
| 843 | |
| 844 | // --- V a l u e --- |
| 845 | |
| 846 | |
| 847 | /** |
| 848 | * The superclass of all JavaScript values and objects. |
| 849 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 850 | class Value : public Data { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 851 | public: |
| 852 | |
| 853 | /** |
| 854 | * Returns true if this value is the undefined value. See ECMA-262 |
| 855 | * 4.3.10. |
| 856 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 857 | V8EXPORT bool IsUndefined() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 858 | |
| 859 | /** |
| 860 | * Returns true if this value is the null value. See ECMA-262 |
| 861 | * 4.3.11. |
| 862 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 863 | V8EXPORT bool IsNull() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 864 | |
| 865 | /** |
| 866 | * Returns true if this value is true. |
| 867 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 868 | V8EXPORT bool IsTrue() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 869 | |
| 870 | /** |
| 871 | * Returns true if this value is false. |
| 872 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 873 | V8EXPORT bool IsFalse() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 874 | |
| 875 | /** |
| 876 | * Returns true if this value is an instance of the String type. |
| 877 | * See ECMA-262 8.4. |
| 878 | */ |
| 879 | inline bool IsString() const; |
| 880 | |
| 881 | /** |
| 882 | * Returns true if this value is a function. |
| 883 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 884 | V8EXPORT bool IsFunction() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 885 | |
| 886 | /** |
| 887 | * Returns true if this value is an array. |
| 888 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 889 | V8EXPORT bool IsArray() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 890 | |
| 891 | /** |
| 892 | * Returns true if this value is an object. |
| 893 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 894 | V8EXPORT bool IsObject() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 895 | |
| 896 | /** |
| 897 | * Returns true if this value is boolean. |
| 898 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 899 | V8EXPORT bool IsBoolean() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 900 | |
| 901 | /** |
| 902 | * Returns true if this value is a number. |
| 903 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 904 | V8EXPORT bool IsNumber() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 905 | |
| 906 | /** |
| 907 | * Returns true if this value is external. |
| 908 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 909 | V8EXPORT bool IsExternal() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 910 | |
| 911 | /** |
| 912 | * Returns true if this value is a 32-bit signed integer. |
| 913 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 914 | V8EXPORT bool IsInt32() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 915 | |
| 916 | /** |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 917 | * Returns true if this value is a 32-bit unsigned integer. |
| 918 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 919 | V8EXPORT bool IsUint32() const; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 920 | |
| 921 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 922 | * Returns true if this value is a Date. |
| 923 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 924 | V8EXPORT bool IsDate() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 925 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 926 | /** |
| 927 | * Returns true if this value is a RegExp. |
| 928 | */ |
| 929 | V8EXPORT bool IsRegExp() const; |
| 930 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 931 | V8EXPORT Local<Boolean> ToBoolean() const; |
| 932 | V8EXPORT Local<Number> ToNumber() const; |
| 933 | V8EXPORT Local<String> ToString() const; |
| 934 | V8EXPORT Local<String> ToDetailString() const; |
| 935 | V8EXPORT Local<Object> ToObject() const; |
| 936 | V8EXPORT Local<Integer> ToInteger() const; |
| 937 | V8EXPORT Local<Uint32> ToUint32() const; |
| 938 | V8EXPORT Local<Int32> ToInt32() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 939 | |
| 940 | /** |
| 941 | * Attempts to convert a string to an array index. |
| 942 | * Returns an empty handle if the conversion fails. |
| 943 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 944 | V8EXPORT Local<Uint32> ToArrayIndex() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 945 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 946 | V8EXPORT bool BooleanValue() const; |
| 947 | V8EXPORT double NumberValue() const; |
| 948 | V8EXPORT int64_t IntegerValue() const; |
| 949 | V8EXPORT uint32_t Uint32Value() const; |
| 950 | V8EXPORT int32_t Int32Value() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 951 | |
| 952 | /** JS == */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 953 | V8EXPORT bool Equals(Handle<Value> that) const; |
| 954 | V8EXPORT bool StrictEquals(Handle<Value> that) const; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 955 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 956 | private: |
| 957 | inline bool QuickIsString() const; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 958 | V8EXPORT bool FullIsString() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 959 | }; |
| 960 | |
| 961 | |
| 962 | /** |
| 963 | * The superclass of primitive values. See ECMA-262 4.3.2. |
| 964 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 965 | class Primitive : public Value { }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 966 | |
| 967 | |
| 968 | /** |
| 969 | * A primitive boolean value (ECMA-262, 4.3.14). Either the true |
| 970 | * or false value. |
| 971 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 972 | class Boolean : public Primitive { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 973 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 974 | V8EXPORT bool Value() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 975 | static inline Handle<Boolean> New(bool value); |
| 976 | }; |
| 977 | |
| 978 | |
| 979 | /** |
| 980 | * A JavaScript string value (ECMA-262, 4.3.17). |
| 981 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 982 | class String : public Primitive { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 983 | public: |
| 984 | |
| 985 | /** |
| 986 | * Returns the number of characters in this string. |
| 987 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 988 | V8EXPORT int Length() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 989 | |
| 990 | /** |
| 991 | * Returns the number of bytes in the UTF-8 encoded |
| 992 | * representation of this string. |
| 993 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 994 | V8EXPORT int Utf8Length() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 995 | |
| 996 | /** |
| 997 | * Write the contents of the string to an external buffer. |
| 998 | * If no arguments are given, expects the buffer to be large |
| 999 | * enough to hold the entire string and NULL terminator. Copies |
| 1000 | * the contents of the string and the NULL terminator into the |
| 1001 | * buffer. |
| 1002 | * |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1003 | * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop |
| 1004 | * before the end of the buffer. |
| 1005 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1006 | * Copies up to length characters into the output buffer. |
| 1007 | * Only null-terminates if there is enough space in the buffer. |
| 1008 | * |
| 1009 | * \param buffer The buffer into which the string will be copied. |
| 1010 | * \param start The starting position within the string at which |
| 1011 | * copying begins. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1012 | * \param length The number of characters to copy from the string. For |
| 1013 | * WriteUtf8 the number of bytes in the buffer. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1014 | * \param nchars_ref The number of characters written, can be NULL. |
| 1015 | * \param hints Various hints that might affect performance of this or |
| 1016 | * subsequent operations. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1017 | * \return The number of characters copied to the buffer excluding the null |
| 1018 | * terminator. For WriteUtf8: The number of bytes copied to the buffer |
| 1019 | * including the null terminator. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1020 | */ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1021 | enum WriteHints { |
| 1022 | NO_HINTS = 0, |
| 1023 | HINT_MANY_WRITES_EXPECTED = 1 |
| 1024 | }; |
| 1025 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1026 | V8EXPORT int Write(uint16_t* buffer, |
| 1027 | int start = 0, |
| 1028 | int length = -1, |
| 1029 | WriteHints hints = NO_HINTS) const; // UTF-16 |
| 1030 | V8EXPORT int WriteAscii(char* buffer, |
| 1031 | int start = 0, |
| 1032 | int length = -1, |
| 1033 | WriteHints hints = NO_HINTS) const; // ASCII |
| 1034 | V8EXPORT int WriteUtf8(char* buffer, |
| 1035 | int length = -1, |
| 1036 | int* nchars_ref = NULL, |
| 1037 | WriteHints hints = NO_HINTS) const; // UTF-8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1038 | |
| 1039 | /** |
| 1040 | * A zero length string. |
| 1041 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1042 | V8EXPORT static v8::Local<v8::String> Empty(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1043 | |
| 1044 | /** |
| 1045 | * Returns true if the string is external |
| 1046 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1047 | V8EXPORT bool IsExternal() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1048 | |
| 1049 | /** |
| 1050 | * Returns true if the string is both external and ascii |
| 1051 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1052 | V8EXPORT bool IsExternalAscii() const; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1053 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1054 | class V8EXPORT ExternalStringResourceBase { // NOLINT |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1055 | public: |
| 1056 | virtual ~ExternalStringResourceBase() {} |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1057 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1058 | protected: |
| 1059 | ExternalStringResourceBase() {} |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1060 | |
| 1061 | /** |
| 1062 | * Internally V8 will call this Dispose method when the external string |
| 1063 | * resource is no longer needed. The default implementation will use the |
| 1064 | * delete operator. This method can be overridden in subclasses to |
| 1065 | * control how allocated external string resources are disposed. |
| 1066 | */ |
| 1067 | virtual void Dispose() { delete this; } |
| 1068 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1069 | private: |
| 1070 | // Disallow copying and assigning. |
| 1071 | ExternalStringResourceBase(const ExternalStringResourceBase&); |
| 1072 | void operator=(const ExternalStringResourceBase&); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1073 | |
| 1074 | friend class v8::internal::Heap; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1075 | }; |
| 1076 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1077 | /** |
| 1078 | * An ExternalStringResource is a wrapper around a two-byte string |
| 1079 | * buffer that resides outside V8's heap. Implement an |
| 1080 | * ExternalStringResource to manage the life cycle of the underlying |
| 1081 | * buffer. Note that the string data must be immutable. |
| 1082 | */ |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1083 | class V8EXPORT ExternalStringResource |
| 1084 | : public ExternalStringResourceBase { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1085 | public: |
| 1086 | /** |
| 1087 | * Override the destructor to manage the life cycle of the underlying |
| 1088 | * buffer. |
| 1089 | */ |
| 1090 | virtual ~ExternalStringResource() {} |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1091 | |
| 1092 | /** |
| 1093 | * The string data from the underlying buffer. |
| 1094 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1095 | virtual const uint16_t* data() const = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1096 | |
| 1097 | /** |
| 1098 | * The length of the string. That is, the number of two-byte characters. |
| 1099 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1100 | virtual size_t length() const = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1101 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1102 | protected: |
| 1103 | ExternalStringResource() {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1104 | }; |
| 1105 | |
| 1106 | /** |
| 1107 | * An ExternalAsciiStringResource is a wrapper around an ascii |
| 1108 | * string buffer that resides outside V8's heap. Implement an |
| 1109 | * ExternalAsciiStringResource to manage the life cycle of the |
| 1110 | * underlying buffer. Note that the string data must be immutable |
| 1111 | * and that the data must be strict 7-bit ASCII, not Latin1 or |
| 1112 | * UTF-8, which would require special treatment internally in the |
| 1113 | * engine and, in the case of UTF-8, do not allow efficient indexing. |
| 1114 | * Use String::New or convert to 16 bit data for non-ASCII. |
| 1115 | */ |
| 1116 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1117 | class V8EXPORT ExternalAsciiStringResource |
| 1118 | : public ExternalStringResourceBase { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1119 | public: |
| 1120 | /** |
| 1121 | * Override the destructor to manage the life cycle of the underlying |
| 1122 | * buffer. |
| 1123 | */ |
| 1124 | virtual ~ExternalAsciiStringResource() {} |
| 1125 | /** The string data from the underlying buffer.*/ |
| 1126 | virtual const char* data() const = 0; |
| 1127 | /** The number of ascii characters in the string.*/ |
| 1128 | virtual size_t length() const = 0; |
| 1129 | protected: |
| 1130 | ExternalAsciiStringResource() {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1131 | }; |
| 1132 | |
| 1133 | /** |
| 1134 | * Get the ExternalStringResource for an external string. Returns |
| 1135 | * NULL if IsExternal() doesn't return true. |
| 1136 | */ |
| 1137 | inline ExternalStringResource* GetExternalStringResource() const; |
| 1138 | |
| 1139 | /** |
| 1140 | * Get the ExternalAsciiStringResource for an external ascii string. |
| 1141 | * Returns NULL if IsExternalAscii() doesn't return true. |
| 1142 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1143 | V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1144 | |
| 1145 | static inline String* Cast(v8::Value* obj); |
| 1146 | |
| 1147 | /** |
| 1148 | * Allocates a new string from either utf-8 encoded or ascii data. |
| 1149 | * The second parameter 'length' gives the buffer length. |
| 1150 | * If the data is utf-8 encoded, the caller must |
| 1151 | * be careful to supply the length parameter. |
| 1152 | * If it is not given, the function calls |
| 1153 | * 'strlen' to determine the buffer length, it might be |
| 1154 | * wrong if 'data' contains a null character. |
| 1155 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1156 | V8EXPORT static Local<String> New(const char* data, int length = -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1157 | |
| 1158 | /** Allocates a new string from utf16 data.*/ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1159 | V8EXPORT static Local<String> New(const uint16_t* data, int length = -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1160 | |
| 1161 | /** Creates a symbol. Returns one if it exists already.*/ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1162 | V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1163 | |
| 1164 | /** |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1165 | * Creates a new string by concatenating the left and the right strings |
| 1166 | * passed in as parameters. |
| 1167 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1168 | V8EXPORT static Local<String> Concat(Handle<String> left, |
| 1169 | Handle<String>right); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1170 | |
| 1171 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1172 | * Creates a new external string using the data defined in the given |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1173 | * resource. When the external string is no longer live on V8's heap the |
| 1174 | * resource will be disposed by calling its Dispose method. The caller of |
| 1175 | * this function should not otherwise delete or modify the resource. Neither |
| 1176 | * should the underlying buffer be deallocated or modified except through the |
| 1177 | * destructor of the external string resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1178 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1179 | V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1180 | |
| 1181 | /** |
| 1182 | * Associate an external string resource with this string by transforming it |
| 1183 | * in place so that existing references to this string in the JavaScript heap |
| 1184 | * will use the external string resource. The external string resource's |
| 1185 | * character contents needs to be equivalent to this string. |
| 1186 | * Returns true if the string has been changed to be an external string. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1187 | * The string is not modified if the operation fails. See NewExternal for |
| 1188 | * information on the lifetime of the resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1189 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1190 | V8EXPORT bool MakeExternal(ExternalStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1191 | |
| 1192 | /** |
| 1193 | * Creates a new external string using the ascii data defined in the given |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1194 | * resource. When the external string is no longer live on V8's heap the |
| 1195 | * resource will be disposed by calling its Dispose method. The caller of |
| 1196 | * this function should not otherwise delete or modify the resource. Neither |
| 1197 | * should the underlying buffer be deallocated or modified except through the |
| 1198 | * destructor of the external string resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1199 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1200 | V8EXPORT static Local<String> NewExternal( |
| 1201 | ExternalAsciiStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1202 | |
| 1203 | /** |
| 1204 | * Associate an external string resource with this string by transforming it |
| 1205 | * in place so that existing references to this string in the JavaScript heap |
| 1206 | * will use the external string resource. The external string resource's |
| 1207 | * character contents needs to be equivalent to this string. |
| 1208 | * Returns true if the string has been changed to be an external string. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1209 | * The string is not modified if the operation fails. See NewExternal for |
| 1210 | * information on the lifetime of the resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1211 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1212 | V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1213 | |
| 1214 | /** |
| 1215 | * Returns true if this string can be made external. |
| 1216 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1217 | V8EXPORT bool CanMakeExternal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1218 | |
| 1219 | /** Creates an undetectable string from the supplied ascii or utf-8 data.*/ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1220 | V8EXPORT static Local<String> NewUndetectable(const char* data, |
| 1221 | int length = -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1222 | |
| 1223 | /** Creates an undetectable string from the supplied utf-16 data.*/ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1224 | V8EXPORT static Local<String> NewUndetectable(const uint16_t* data, |
| 1225 | int length = -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1226 | |
| 1227 | /** |
| 1228 | * Converts an object to a utf8-encoded character array. Useful if |
| 1229 | * you want to print the object. If conversion to a string fails |
| 1230 | * (eg. due to an exception in the toString() method of the object) |
| 1231 | * then the length() method returns 0 and the * operator returns |
| 1232 | * NULL. |
| 1233 | */ |
| 1234 | class V8EXPORT Utf8Value { |
| 1235 | public: |
| 1236 | explicit Utf8Value(Handle<v8::Value> obj); |
| 1237 | ~Utf8Value(); |
| 1238 | char* operator*() { return str_; } |
| 1239 | const char* operator*() const { return str_; } |
| 1240 | int length() const { return length_; } |
| 1241 | private: |
| 1242 | char* str_; |
| 1243 | int length_; |
| 1244 | |
| 1245 | // Disallow copying and assigning. |
| 1246 | Utf8Value(const Utf8Value&); |
| 1247 | void operator=(const Utf8Value&); |
| 1248 | }; |
| 1249 | |
| 1250 | /** |
| 1251 | * Converts an object to an ascii string. |
| 1252 | * Useful if you want to print the object. |
| 1253 | * If conversion to a string fails (eg. due to an exception in the toString() |
| 1254 | * method of the object) then the length() method returns 0 and the * operator |
| 1255 | * returns NULL. |
| 1256 | */ |
| 1257 | class V8EXPORT AsciiValue { |
| 1258 | public: |
| 1259 | explicit AsciiValue(Handle<v8::Value> obj); |
| 1260 | ~AsciiValue(); |
| 1261 | char* operator*() { return str_; } |
| 1262 | const char* operator*() const { return str_; } |
| 1263 | int length() const { return length_; } |
| 1264 | private: |
| 1265 | char* str_; |
| 1266 | int length_; |
| 1267 | |
| 1268 | // Disallow copying and assigning. |
| 1269 | AsciiValue(const AsciiValue&); |
| 1270 | void operator=(const AsciiValue&); |
| 1271 | }; |
| 1272 | |
| 1273 | /** |
| 1274 | * Converts an object to a two-byte string. |
| 1275 | * If conversion to a string fails (eg. due to an exception in the toString() |
| 1276 | * method of the object) then the length() method returns 0 and the * operator |
| 1277 | * returns NULL. |
| 1278 | */ |
| 1279 | class V8EXPORT Value { |
| 1280 | public: |
| 1281 | explicit Value(Handle<v8::Value> obj); |
| 1282 | ~Value(); |
| 1283 | uint16_t* operator*() { return str_; } |
| 1284 | const uint16_t* operator*() const { return str_; } |
| 1285 | int length() const { return length_; } |
| 1286 | private: |
| 1287 | uint16_t* str_; |
| 1288 | int length_; |
| 1289 | |
| 1290 | // Disallow copying and assigning. |
| 1291 | Value(const Value&); |
| 1292 | void operator=(const Value&); |
| 1293 | }; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1294 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1295 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1296 | V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const; |
| 1297 | V8EXPORT static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1298 | }; |
| 1299 | |
| 1300 | |
| 1301 | /** |
| 1302 | * A JavaScript number value (ECMA-262, 4.3.20) |
| 1303 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1304 | class Number : public Primitive { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1305 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1306 | V8EXPORT double Value() const; |
| 1307 | V8EXPORT static Local<Number> New(double value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1308 | static inline Number* Cast(v8::Value* obj); |
| 1309 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1310 | V8EXPORT Number(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1311 | static void CheckCast(v8::Value* obj); |
| 1312 | }; |
| 1313 | |
| 1314 | |
| 1315 | /** |
| 1316 | * A JavaScript value representing a signed integer. |
| 1317 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1318 | class Integer : public Number { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1319 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1320 | V8EXPORT static Local<Integer> New(int32_t value); |
| 1321 | V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value); |
| 1322 | V8EXPORT int64_t Value() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1323 | static inline Integer* Cast(v8::Value* obj); |
| 1324 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1325 | V8EXPORT Integer(); |
| 1326 | V8EXPORT static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1327 | }; |
| 1328 | |
| 1329 | |
| 1330 | /** |
| 1331 | * A JavaScript value representing a 32-bit signed integer. |
| 1332 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1333 | class Int32 : public Integer { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1334 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1335 | V8EXPORT int32_t Value() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1336 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1337 | V8EXPORT Int32(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1338 | }; |
| 1339 | |
| 1340 | |
| 1341 | /** |
| 1342 | * A JavaScript value representing a 32-bit unsigned integer. |
| 1343 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1344 | class Uint32 : public Integer { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1345 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1346 | V8EXPORT uint32_t Value() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1347 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1348 | V8EXPORT Uint32(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1349 | }; |
| 1350 | |
| 1351 | |
| 1352 | /** |
| 1353 | * An instance of the built-in Date constructor (ECMA-262, 15.9). |
| 1354 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1355 | class Date : public Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1356 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1357 | V8EXPORT static Local<Value> New(double time); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1358 | |
| 1359 | /** |
| 1360 | * A specialization of Value::NumberValue that is more efficient |
| 1361 | * because we know the structure of this object. |
| 1362 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1363 | V8EXPORT double NumberValue() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1364 | |
| 1365 | static inline Date* Cast(v8::Value* obj); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1366 | |
| 1367 | /** |
| 1368 | * Notification that the embedder has changed the time zone, |
| 1369 | * daylight savings time, or other date / time configuration |
| 1370 | * parameters. V8 keeps a cache of various values used for |
| 1371 | * date / time computation. This notification will reset |
| 1372 | * those cached values for the current context so that date / |
| 1373 | * time configuration changes would be reflected in the Date |
| 1374 | * object. |
| 1375 | * |
| 1376 | * This API should not be called more than needed as it will |
| 1377 | * negatively impact the performance of date operations. |
| 1378 | */ |
| 1379 | V8EXPORT static void DateTimeConfigurationChangeNotification(); |
| 1380 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1381 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1382 | V8EXPORT static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1383 | }; |
| 1384 | |
| 1385 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1386 | /** |
| 1387 | * An instance of the built-in RegExp constructor (ECMA-262, 15.10). |
| 1388 | */ |
| 1389 | class RegExp : public Value { |
| 1390 | public: |
| 1391 | /** |
| 1392 | * Regular expression flag bits. They can be or'ed to enable a set |
| 1393 | * of flags. |
| 1394 | */ |
| 1395 | enum Flags { |
| 1396 | kNone = 0, |
| 1397 | kGlobal = 1, |
| 1398 | kIgnoreCase = 2, |
| 1399 | kMultiline = 4 |
| 1400 | }; |
| 1401 | |
| 1402 | /** |
| 1403 | * Creates a regular expression from the given pattern string and |
| 1404 | * the flags bit field. May throw a JavaScript exception as |
| 1405 | * described in ECMA-262, 15.10.4.1. |
| 1406 | * |
| 1407 | * For example, |
| 1408 | * RegExp::New(v8::String::New("foo"), |
| 1409 | * static_cast<RegExp::Flags>(kGlobal | kMultiline)) |
| 1410 | * is equivalent to evaluating "/foo/gm". |
| 1411 | */ |
| 1412 | V8EXPORT static Local<RegExp> New(Handle<String> pattern, |
| 1413 | Flags flags); |
| 1414 | |
| 1415 | /** |
| 1416 | * Returns the value of the source property: a string representing |
| 1417 | * the regular expression. |
| 1418 | */ |
| 1419 | V8EXPORT Local<String> GetSource() const; |
| 1420 | |
| 1421 | /** |
| 1422 | * Returns the flags bit field. |
| 1423 | */ |
| 1424 | V8EXPORT Flags GetFlags() const; |
| 1425 | |
| 1426 | static inline RegExp* Cast(v8::Value* obj); |
| 1427 | |
| 1428 | private: |
| 1429 | V8EXPORT static void CheckCast(v8::Value* obj); |
| 1430 | }; |
| 1431 | |
| 1432 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1433 | enum PropertyAttribute { |
| 1434 | None = 0, |
| 1435 | ReadOnly = 1 << 0, |
| 1436 | DontEnum = 1 << 1, |
| 1437 | DontDelete = 1 << 2 |
| 1438 | }; |
| 1439 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1440 | enum ExternalArrayType { |
| 1441 | kExternalByteArray = 1, |
| 1442 | kExternalUnsignedByteArray, |
| 1443 | kExternalShortArray, |
| 1444 | kExternalUnsignedShortArray, |
| 1445 | kExternalIntArray, |
| 1446 | kExternalUnsignedIntArray, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 1447 | kExternalFloatArray, |
| 1448 | kExternalPixelArray |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1449 | }; |
| 1450 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1451 | /** |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1452 | * Accessor[Getter|Setter] are used as callback functions when |
| 1453 | * setting|getting a particular property. See Object and ObjectTemplate's |
| 1454 | * method SetAccessor. |
| 1455 | */ |
| 1456 | typedef Handle<Value> (*AccessorGetter)(Local<String> property, |
| 1457 | const AccessorInfo& info); |
| 1458 | |
| 1459 | |
| 1460 | typedef void (*AccessorSetter)(Local<String> property, |
| 1461 | Local<Value> value, |
| 1462 | const AccessorInfo& info); |
| 1463 | |
| 1464 | |
| 1465 | /** |
| 1466 | * Access control specifications. |
| 1467 | * |
| 1468 | * Some accessors should be accessible across contexts. These |
| 1469 | * accessors have an explicit access control parameter which specifies |
| 1470 | * the kind of cross-context access that should be allowed. |
| 1471 | * |
| 1472 | * Additionally, for security, accessors can prohibit overwriting by |
| 1473 | * accessors defined in JavaScript. For objects that have such |
| 1474 | * accessors either locally or in their prototype chain it is not |
| 1475 | * possible to overwrite the accessor by using __defineGetter__ or |
| 1476 | * __defineSetter__ from JavaScript code. |
| 1477 | */ |
| 1478 | enum AccessControl { |
| 1479 | DEFAULT = 0, |
| 1480 | ALL_CAN_READ = 1, |
| 1481 | ALL_CAN_WRITE = 1 << 1, |
| 1482 | PROHIBITS_OVERWRITING = 1 << 2 |
| 1483 | }; |
| 1484 | |
| 1485 | |
| 1486 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1487 | * A JavaScript object (ECMA-262, 4.3.3) |
| 1488 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1489 | class Object : public Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1490 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1491 | V8EXPORT bool Set(Handle<Value> key, |
| 1492 | Handle<Value> value, |
| 1493 | PropertyAttribute attribs = None); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1494 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1495 | V8EXPORT bool Set(uint32_t index, |
| 1496 | Handle<Value> value); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1497 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1498 | // Sets a local property on this object bypassing interceptors and |
| 1499 | // overriding accessors or read-only properties. |
| 1500 | // |
| 1501 | // Note that if the object has an interceptor the property will be set |
| 1502 | // locally, but since the interceptor takes precedence the local property |
| 1503 | // will only be returned if the interceptor doesn't return a value. |
| 1504 | // |
| 1505 | // Note also that this only works for named properties. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1506 | V8EXPORT bool ForceSet(Handle<Value> key, |
| 1507 | Handle<Value> value, |
| 1508 | PropertyAttribute attribs = None); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1509 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1510 | V8EXPORT Local<Value> Get(Handle<Value> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1511 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1512 | V8EXPORT Local<Value> Get(uint32_t index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1513 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1514 | // TODO(1245389): Replace the type-specific versions of these |
| 1515 | // functions with generic ones that accept a Handle<Value> key. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1516 | V8EXPORT bool Has(Handle<String> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1517 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1518 | V8EXPORT bool Delete(Handle<String> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1519 | |
| 1520 | // Delete a property on this object bypassing interceptors and |
| 1521 | // ignoring dont-delete attributes. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1522 | V8EXPORT bool ForceDelete(Handle<Value> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1523 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1524 | V8EXPORT bool Has(uint32_t index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1525 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1526 | V8EXPORT bool Delete(uint32_t index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1527 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1528 | V8EXPORT bool SetAccessor(Handle<String> name, |
| 1529 | AccessorGetter getter, |
| 1530 | AccessorSetter setter = 0, |
| 1531 | Handle<Value> data = Handle<Value>(), |
| 1532 | AccessControl settings = DEFAULT, |
| 1533 | PropertyAttribute attribute = None); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1534 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1535 | /** |
| 1536 | * Returns an array containing the names of the enumerable properties |
| 1537 | * of this object, including properties from prototype objects. The |
| 1538 | * array returned by this method contains the same values as would |
| 1539 | * be enumerated by a for-in statement over this object. |
| 1540 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1541 | V8EXPORT Local<Array> GetPropertyNames(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1542 | |
| 1543 | /** |
| 1544 | * Get the prototype object. This does not skip objects marked to |
| 1545 | * be skipped by __proto__ and it does not consult the security |
| 1546 | * handler. |
| 1547 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1548 | V8EXPORT Local<Value> GetPrototype(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1549 | |
| 1550 | /** |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1551 | * Set the prototype object. This does not skip objects marked to |
| 1552 | * be skipped by __proto__ and it does not consult the security |
| 1553 | * handler. |
| 1554 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1555 | V8EXPORT bool SetPrototype(Handle<Value> prototype); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1556 | |
| 1557 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1558 | * Finds an instance of the given function template in the prototype |
| 1559 | * chain. |
| 1560 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1561 | V8EXPORT Local<Object> FindInstanceInPrototypeChain( |
| 1562 | Handle<FunctionTemplate> tmpl); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1563 | |
| 1564 | /** |
| 1565 | * Call builtin Object.prototype.toString on this object. |
| 1566 | * This is different from Value::ToString() that may call |
| 1567 | * user-defined toString function. This one does not. |
| 1568 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1569 | V8EXPORT Local<String> ObjectProtoToString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1570 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1571 | /** |
| 1572 | * Returns the name of the function invoked as a constructor for this object. |
| 1573 | */ |
| 1574 | V8EXPORT Local<String> GetConstructorName(); |
| 1575 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1576 | /** Gets the number of internal fields for this Object. */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1577 | V8EXPORT int InternalFieldCount(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1578 | /** Gets the value in an internal field. */ |
| 1579 | inline Local<Value> GetInternalField(int index); |
| 1580 | /** Sets the value in an internal field. */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1581 | V8EXPORT void SetInternalField(int index, Handle<Value> value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1582 | |
| 1583 | /** Gets a native pointer from an internal field. */ |
| 1584 | inline void* GetPointerFromInternalField(int index); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1585 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1586 | /** Sets a native pointer in an internal field. */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1587 | V8EXPORT void SetPointerInInternalField(int index, void* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1588 | |
| 1589 | // Testers for local properties. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1590 | V8EXPORT bool HasRealNamedProperty(Handle<String> key); |
| 1591 | V8EXPORT bool HasRealIndexedProperty(uint32_t index); |
| 1592 | V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1593 | |
| 1594 | /** |
| 1595 | * If result.IsEmpty() no real property was located in the prototype chain. |
| 1596 | * This means interceptors in the prototype chain are not called. |
| 1597 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1598 | V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain( |
| 1599 | Handle<String> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1600 | |
| 1601 | /** |
| 1602 | * If result.IsEmpty() no real property was located on the object or |
| 1603 | * in the prototype chain. |
| 1604 | * This means interceptors in the prototype chain are not called. |
| 1605 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1606 | V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1607 | |
| 1608 | /** Tests for a named lookup interceptor.*/ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1609 | V8EXPORT bool HasNamedLookupInterceptor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1610 | |
| 1611 | /** Tests for an index lookup interceptor.*/ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1612 | V8EXPORT bool HasIndexedLookupInterceptor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1613 | |
| 1614 | /** |
| 1615 | * Turns on access check on the object if the object is an instance of |
| 1616 | * a template that has access check callbacks. If an object has no |
| 1617 | * access check info, the object cannot be accessed by anyone. |
| 1618 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1619 | V8EXPORT void TurnOnAccessCheck(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1620 | |
| 1621 | /** |
| 1622 | * Returns the identity hash for this object. The current implemenation uses |
| 1623 | * a hidden property on the object to store the identity hash. |
| 1624 | * |
| 1625 | * The return value will never be 0. Also, it is not guaranteed to be |
| 1626 | * unique. |
| 1627 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1628 | V8EXPORT int GetIdentityHash(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1629 | |
| 1630 | /** |
| 1631 | * Access hidden properties on JavaScript objects. These properties are |
| 1632 | * hidden from the executing JavaScript and only accessible through the V8 |
| 1633 | * C++ API. Hidden properties introduced by V8 internally (for example the |
| 1634 | * identity hash) are prefixed with "v8::". |
| 1635 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1636 | V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value); |
| 1637 | V8EXPORT Local<Value> GetHiddenValue(Handle<String> key); |
| 1638 | V8EXPORT bool DeleteHiddenValue(Handle<String> key); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1639 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1640 | /** |
| 1641 | * Returns true if this is an instance of an api function (one |
| 1642 | * created from a function created from a function template) and has |
| 1643 | * been modified since it was created. Note that this method is |
| 1644 | * conservative and may return true for objects that haven't actually |
| 1645 | * been modified. |
| 1646 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1647 | V8EXPORT bool IsDirty(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1648 | |
| 1649 | /** |
| 1650 | * Clone this object with a fast but shallow copy. Values will point |
| 1651 | * to the same values as the original object. |
| 1652 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1653 | V8EXPORT Local<Object> Clone(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1654 | |
| 1655 | /** |
| 1656 | * Set the backing store of the indexed properties to be managed by the |
| 1657 | * embedding layer. Access to the indexed properties will follow the rules |
| 1658 | * spelled out in CanvasPixelArray. |
| 1659 | * Note: The embedding program still owns the data and needs to ensure that |
| 1660 | * the backing store is preserved while V8 has a reference. |
| 1661 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1662 | V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 1663 | V8EXPORT bool HasIndexedPropertiesInPixelData(); |
| 1664 | V8EXPORT uint8_t* GetIndexedPropertiesPixelData(); |
| 1665 | V8EXPORT int GetIndexedPropertiesPixelDataLength(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1666 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1667 | /** |
| 1668 | * Set the backing store of the indexed properties to be managed by the |
| 1669 | * embedding layer. Access to the indexed properties will follow the rules |
| 1670 | * spelled out for the CanvasArray subtypes in the WebGL specification. |
| 1671 | * Note: The embedding program still owns the data and needs to ensure that |
| 1672 | * the backing store is preserved while V8 has a reference. |
| 1673 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1674 | V8EXPORT void SetIndexedPropertiesToExternalArrayData( |
| 1675 | void* data, |
| 1676 | ExternalArrayType array_type, |
| 1677 | int number_of_elements); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 1678 | V8EXPORT bool HasIndexedPropertiesInExternalArrayData(); |
| 1679 | V8EXPORT void* GetIndexedPropertiesExternalArrayData(); |
| 1680 | V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType(); |
| 1681 | V8EXPORT int GetIndexedPropertiesExternalArrayDataLength(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1682 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1683 | V8EXPORT static Local<Object> New(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1684 | static inline Object* Cast(Value* obj); |
| 1685 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1686 | V8EXPORT Object(); |
| 1687 | V8EXPORT static void CheckCast(Value* obj); |
| 1688 | V8EXPORT Local<Value> CheckedGetInternalField(int index); |
| 1689 | V8EXPORT void* SlowGetPointerFromInternalField(int index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1690 | |
| 1691 | /** |
| 1692 | * If quick access to the internal field is possible this method |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1693 | * returns the value. Otherwise an empty handle is returned. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1694 | */ |
| 1695 | inline Local<Value> UncheckedGetInternalField(int index); |
| 1696 | }; |
| 1697 | |
| 1698 | |
| 1699 | /** |
| 1700 | * An instance of the built-in array constructor (ECMA-262, 15.4.2). |
| 1701 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1702 | class Array : public Object { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1703 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1704 | V8EXPORT uint32_t Length() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1705 | |
| 1706 | /** |
| 1707 | * Clones an element at index |index|. Returns an empty |
| 1708 | * handle if cloning fails (for any reason). |
| 1709 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1710 | V8EXPORT Local<Object> CloneElementAt(uint32_t index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1711 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 1712 | /** |
| 1713 | * Creates a JavaScript array with the given length. If the length |
| 1714 | * is negative the returned array will have length 0. |
| 1715 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1716 | V8EXPORT static Local<Array> New(int length = 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 1717 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1718 | static inline Array* Cast(Value* obj); |
| 1719 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1720 | V8EXPORT Array(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1721 | static void CheckCast(Value* obj); |
| 1722 | }; |
| 1723 | |
| 1724 | |
| 1725 | /** |
| 1726 | * A JavaScript function object (ECMA-262, 15.3). |
| 1727 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1728 | class Function : public Object { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1729 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1730 | V8EXPORT Local<Object> NewInstance() const; |
| 1731 | V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const; |
| 1732 | V8EXPORT Local<Value> Call(Handle<Object> recv, |
| 1733 | int argc, |
| 1734 | Handle<Value> argv[]); |
| 1735 | V8EXPORT void SetName(Handle<String> name); |
| 1736 | V8EXPORT Handle<Value> GetName() const; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1737 | |
| 1738 | /** |
| 1739 | * Returns zero based line number of function body and |
| 1740 | * kLineOffsetNotFound if no information available. |
| 1741 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1742 | V8EXPORT int GetScriptLineNumber() const; |
| 1743 | V8EXPORT ScriptOrigin GetScriptOrigin() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1744 | static inline Function* Cast(Value* obj); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1745 | V8EXPORT static const int kLineOffsetNotFound; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1746 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1747 | V8EXPORT Function(); |
| 1748 | V8EXPORT static void CheckCast(Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1749 | }; |
| 1750 | |
| 1751 | |
| 1752 | /** |
| 1753 | * A JavaScript value that wraps a C++ void*. This type of value is |
| 1754 | * mainly used to associate C++ data structures with JavaScript |
| 1755 | * objects. |
| 1756 | * |
| 1757 | * The Wrap function V8 will return the most optimal Value object wrapping the |
| 1758 | * C++ void*. The type of the value is not guaranteed to be an External object |
| 1759 | * and no assumptions about its type should be made. To access the wrapped |
| 1760 | * value Unwrap should be used, all other operations on that object will lead |
| 1761 | * to unpredictable results. |
| 1762 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1763 | class External : public Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1764 | public: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1765 | V8EXPORT static Local<Value> Wrap(void* data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1766 | static inline void* Unwrap(Handle<Value> obj); |
| 1767 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1768 | V8EXPORT static Local<External> New(void* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1769 | static inline External* Cast(Value* obj); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1770 | V8EXPORT void* Value() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1771 | private: |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1772 | V8EXPORT External(); |
| 1773 | V8EXPORT static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1774 | static inline void* QuickUnwrap(Handle<v8::Value> obj); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1775 | V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1776 | }; |
| 1777 | |
| 1778 | |
| 1779 | // --- T e m p l a t e s --- |
| 1780 | |
| 1781 | |
| 1782 | /** |
| 1783 | * The superclass of object and function templates. |
| 1784 | */ |
| 1785 | class V8EXPORT Template : public Data { |
| 1786 | public: |
| 1787 | /** Adds a property to each instance created by this template.*/ |
| 1788 | void Set(Handle<String> name, Handle<Data> value, |
| 1789 | PropertyAttribute attributes = None); |
| 1790 | inline void Set(const char* name, Handle<Data> value); |
| 1791 | private: |
| 1792 | Template(); |
| 1793 | |
| 1794 | friend class ObjectTemplate; |
| 1795 | friend class FunctionTemplate; |
| 1796 | }; |
| 1797 | |
| 1798 | |
| 1799 | /** |
| 1800 | * The argument information given to function call callbacks. This |
| 1801 | * class provides access to information about the context of the call, |
| 1802 | * including the receiver, the number and values of arguments, and |
| 1803 | * the holder of the function. |
| 1804 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1805 | class Arguments { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1806 | public: |
| 1807 | inline int Length() const; |
| 1808 | inline Local<Value> operator[](int i) const; |
| 1809 | inline Local<Function> Callee() const; |
| 1810 | inline Local<Object> This() const; |
| 1811 | inline Local<Object> Holder() const; |
| 1812 | inline bool IsConstructCall() const; |
| 1813 | inline Local<Value> Data() const; |
| 1814 | private: |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1815 | static const int kDataIndex = 0; |
| 1816 | static const int kCalleeIndex = -1; |
| 1817 | static const int kHolderIndex = -2; |
| 1818 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1819 | friend class ImplementationUtilities; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1820 | inline Arguments(internal::Object** implicit_args, |
| 1821 | internal::Object** values, |
| 1822 | int length, |
| 1823 | bool is_construct_call); |
| 1824 | internal::Object** implicit_args_; |
| 1825 | internal::Object** values_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1826 | int length_; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1827 | bool is_construct_call_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1828 | }; |
| 1829 | |
| 1830 | |
| 1831 | /** |
| 1832 | * The information passed to an accessor callback about the context |
| 1833 | * of the property access. |
| 1834 | */ |
| 1835 | class V8EXPORT AccessorInfo { |
| 1836 | public: |
| 1837 | inline AccessorInfo(internal::Object** args) |
| 1838 | : args_(args) { } |
| 1839 | inline Local<Value> Data() const; |
| 1840 | inline Local<Object> This() const; |
| 1841 | inline Local<Object> Holder() const; |
| 1842 | private: |
| 1843 | internal::Object** args_; |
| 1844 | }; |
| 1845 | |
| 1846 | |
| 1847 | typedef Handle<Value> (*InvocationCallback)(const Arguments& args); |
| 1848 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1849 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1850 | * NamedProperty[Getter|Setter] are used as interceptors on object. |
| 1851 | * See ObjectTemplate::SetNamedPropertyHandler. |
| 1852 | */ |
| 1853 | typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property, |
| 1854 | const AccessorInfo& info); |
| 1855 | |
| 1856 | |
| 1857 | /** |
| 1858 | * Returns the value if the setter intercepts the request. |
| 1859 | * Otherwise, returns an empty handle. |
| 1860 | */ |
| 1861 | typedef Handle<Value> (*NamedPropertySetter)(Local<String> property, |
| 1862 | Local<Value> value, |
| 1863 | const AccessorInfo& info); |
| 1864 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1865 | /** |
| 1866 | * Returns a non-empty handle if the interceptor intercepts the request. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1867 | * The result is an integer encoding property attributes (like v8::None, |
| 1868 | * v8::DontEnum, etc.) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1869 | */ |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1870 | typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property, |
| 1871 | const AccessorInfo& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1872 | |
| 1873 | |
| 1874 | /** |
| 1875 | * Returns a non-empty handle if the deleter intercepts the request. |
| 1876 | * The return value is true if the property could be deleted and false |
| 1877 | * otherwise. |
| 1878 | */ |
| 1879 | typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property, |
| 1880 | const AccessorInfo& info); |
| 1881 | |
| 1882 | /** |
| 1883 | * Returns an array containing the names of the properties the named |
| 1884 | * property getter intercepts. |
| 1885 | */ |
| 1886 | typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info); |
| 1887 | |
| 1888 | |
| 1889 | /** |
| 1890 | * Returns the value of the property if the getter intercepts the |
| 1891 | * request. Otherwise, returns an empty handle. |
| 1892 | */ |
| 1893 | typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index, |
| 1894 | const AccessorInfo& info); |
| 1895 | |
| 1896 | |
| 1897 | /** |
| 1898 | * Returns the value if the setter intercepts the request. |
| 1899 | * Otherwise, returns an empty handle. |
| 1900 | */ |
| 1901 | typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index, |
| 1902 | Local<Value> value, |
| 1903 | const AccessorInfo& info); |
| 1904 | |
| 1905 | |
| 1906 | /** |
| 1907 | * Returns a non-empty handle if the interceptor intercepts the request. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1908 | * The result is an integer encoding property attributes. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1909 | */ |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1910 | typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1911 | const AccessorInfo& info); |
| 1912 | |
| 1913 | /** |
| 1914 | * Returns a non-empty handle if the deleter intercepts the request. |
| 1915 | * The return value is true if the property could be deleted and false |
| 1916 | * otherwise. |
| 1917 | */ |
| 1918 | typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index, |
| 1919 | const AccessorInfo& info); |
| 1920 | |
| 1921 | /** |
| 1922 | * Returns an array containing the indices of the properties the |
| 1923 | * indexed property getter intercepts. |
| 1924 | */ |
| 1925 | typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info); |
| 1926 | |
| 1927 | |
| 1928 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1929 | * Access type specification. |
| 1930 | */ |
| 1931 | enum AccessType { |
| 1932 | ACCESS_GET, |
| 1933 | ACCESS_SET, |
| 1934 | ACCESS_HAS, |
| 1935 | ACCESS_DELETE, |
| 1936 | ACCESS_KEYS |
| 1937 | }; |
| 1938 | |
| 1939 | |
| 1940 | /** |
| 1941 | * Returns true if cross-context access should be allowed to the named |
| 1942 | * property with the given key on the host object. |
| 1943 | */ |
| 1944 | typedef bool (*NamedSecurityCallback)(Local<Object> host, |
| 1945 | Local<Value> key, |
| 1946 | AccessType type, |
| 1947 | Local<Value> data); |
| 1948 | |
| 1949 | |
| 1950 | /** |
| 1951 | * Returns true if cross-context access should be allowed to the indexed |
| 1952 | * property with the given index on the host object. |
| 1953 | */ |
| 1954 | typedef bool (*IndexedSecurityCallback)(Local<Object> host, |
| 1955 | uint32_t index, |
| 1956 | AccessType type, |
| 1957 | Local<Value> data); |
| 1958 | |
| 1959 | |
| 1960 | /** |
| 1961 | * A FunctionTemplate is used to create functions at runtime. There |
| 1962 | * can only be one function created from a FunctionTemplate in a |
| 1963 | * context. The lifetime of the created function is equal to the |
| 1964 | * lifetime of the context. So in case the embedder needs to create |
| 1965 | * temporary functions that can be collected using Scripts is |
| 1966 | * preferred. |
| 1967 | * |
| 1968 | * A FunctionTemplate can have properties, these properties are added to the |
| 1969 | * function object when it is created. |
| 1970 | * |
| 1971 | * A FunctionTemplate has a corresponding instance template which is |
| 1972 | * used to create object instances when the function is used as a |
| 1973 | * constructor. Properties added to the instance template are added to |
| 1974 | * each object instance. |
| 1975 | * |
| 1976 | * A FunctionTemplate can have a prototype template. The prototype template |
| 1977 | * is used to create the prototype object of the function. |
| 1978 | * |
| 1979 | * The following example shows how to use a FunctionTemplate: |
| 1980 | * |
| 1981 | * \code |
| 1982 | * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 1983 | * t->Set("func_property", v8::Number::New(1)); |
| 1984 | * |
| 1985 | * v8::Local<v8::Template> proto_t = t->PrototypeTemplate(); |
| 1986 | * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback)); |
| 1987 | * proto_t->Set("proto_const", v8::Number::New(2)); |
| 1988 | * |
| 1989 | * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate(); |
| 1990 | * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback); |
| 1991 | * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...); |
| 1992 | * instance_t->Set("instance_property", Number::New(3)); |
| 1993 | * |
| 1994 | * v8::Local<v8::Function> function = t->GetFunction(); |
| 1995 | * v8::Local<v8::Object> instance = function->NewInstance(); |
| 1996 | * \endcode |
| 1997 | * |
| 1998 | * Let's use "function" as the JS variable name of the function object |
| 1999 | * and "instance" for the instance object created above. The function |
| 2000 | * and the instance will have the following properties: |
| 2001 | * |
| 2002 | * \code |
| 2003 | * func_property in function == true; |
| 2004 | * function.func_property == 1; |
| 2005 | * |
| 2006 | * function.prototype.proto_method() invokes 'InvokeCallback' |
| 2007 | * function.prototype.proto_const == 2; |
| 2008 | * |
| 2009 | * instance instanceof function == true; |
| 2010 | * instance.instance_accessor calls 'InstanceAccessorCallback' |
| 2011 | * instance.instance_property == 3; |
| 2012 | * \endcode |
| 2013 | * |
| 2014 | * A FunctionTemplate can inherit from another one by calling the |
| 2015 | * FunctionTemplate::Inherit method. The following graph illustrates |
| 2016 | * the semantics of inheritance: |
| 2017 | * |
| 2018 | * \code |
| 2019 | * FunctionTemplate Parent -> Parent() . prototype -> { } |
| 2020 | * ^ ^ |
| 2021 | * | Inherit(Parent) | .__proto__ |
| 2022 | * | | |
| 2023 | * FunctionTemplate Child -> Child() . prototype -> { } |
| 2024 | * \endcode |
| 2025 | * |
| 2026 | * A FunctionTemplate 'Child' inherits from 'Parent', the prototype |
| 2027 | * object of the Child() function has __proto__ pointing to the |
| 2028 | * Parent() function's prototype object. An instance of the Child |
| 2029 | * function has all properties on Parent's instance templates. |
| 2030 | * |
| 2031 | * Let Parent be the FunctionTemplate initialized in the previous |
| 2032 | * section and create a Child FunctionTemplate by: |
| 2033 | * |
| 2034 | * \code |
| 2035 | * Local<FunctionTemplate> parent = t; |
| 2036 | * Local<FunctionTemplate> child = FunctionTemplate::New(); |
| 2037 | * child->Inherit(parent); |
| 2038 | * |
| 2039 | * Local<Function> child_function = child->GetFunction(); |
| 2040 | * Local<Object> child_instance = child_function->NewInstance(); |
| 2041 | * \endcode |
| 2042 | * |
| 2043 | * The Child function and Child instance will have the following |
| 2044 | * properties: |
| 2045 | * |
| 2046 | * \code |
| 2047 | * child_func.prototype.__proto__ == function.prototype; |
| 2048 | * child_instance.instance_accessor calls 'InstanceAccessorCallback' |
| 2049 | * child_instance.instance_property == 3; |
| 2050 | * \endcode |
| 2051 | */ |
| 2052 | class V8EXPORT FunctionTemplate : public Template { |
| 2053 | public: |
| 2054 | /** Creates a function template.*/ |
| 2055 | static Local<FunctionTemplate> New( |
| 2056 | InvocationCallback callback = 0, |
| 2057 | Handle<Value> data = Handle<Value>(), |
| 2058 | Handle<Signature> signature = Handle<Signature>()); |
| 2059 | /** Returns the unique function instance in the current execution context.*/ |
| 2060 | Local<Function> GetFunction(); |
| 2061 | |
| 2062 | /** |
| 2063 | * Set the call-handler callback for a FunctionTemplate. This |
| 2064 | * callback is called whenever the function created from this |
| 2065 | * FunctionTemplate is called. |
| 2066 | */ |
| 2067 | void SetCallHandler(InvocationCallback callback, |
| 2068 | Handle<Value> data = Handle<Value>()); |
| 2069 | |
| 2070 | /** Get the InstanceTemplate. */ |
| 2071 | Local<ObjectTemplate> InstanceTemplate(); |
| 2072 | |
| 2073 | /** Causes the function template to inherit from a parent function template.*/ |
| 2074 | void Inherit(Handle<FunctionTemplate> parent); |
| 2075 | |
| 2076 | /** |
| 2077 | * A PrototypeTemplate is the template used to create the prototype object |
| 2078 | * of the function created by this template. |
| 2079 | */ |
| 2080 | Local<ObjectTemplate> PrototypeTemplate(); |
| 2081 | |
| 2082 | |
| 2083 | /** |
| 2084 | * Set the class name of the FunctionTemplate. This is used for |
| 2085 | * printing objects created with the function created from the |
| 2086 | * FunctionTemplate as its constructor. |
| 2087 | */ |
| 2088 | void SetClassName(Handle<String> name); |
| 2089 | |
| 2090 | /** |
| 2091 | * Determines whether the __proto__ accessor ignores instances of |
| 2092 | * the function template. If instances of the function template are |
| 2093 | * ignored, __proto__ skips all instances and instead returns the |
| 2094 | * next object in the prototype chain. |
| 2095 | * |
| 2096 | * Call with a value of true to make the __proto__ accessor ignore |
| 2097 | * instances of the function template. Call with a value of false |
| 2098 | * to make the __proto__ accessor not ignore instances of the |
| 2099 | * function template. By default, instances of a function template |
| 2100 | * are not ignored. |
| 2101 | */ |
| 2102 | void SetHiddenPrototype(bool value); |
| 2103 | |
| 2104 | /** |
| 2105 | * Returns true if the given object is an instance of this function |
| 2106 | * template. |
| 2107 | */ |
| 2108 | bool HasInstance(Handle<Value> object); |
| 2109 | |
| 2110 | private: |
| 2111 | FunctionTemplate(); |
| 2112 | void AddInstancePropertyAccessor(Handle<String> name, |
| 2113 | AccessorGetter getter, |
| 2114 | AccessorSetter setter, |
| 2115 | Handle<Value> data, |
| 2116 | AccessControl settings, |
| 2117 | PropertyAttribute attributes); |
| 2118 | void SetNamedInstancePropertyHandler(NamedPropertyGetter getter, |
| 2119 | NamedPropertySetter setter, |
| 2120 | NamedPropertyQuery query, |
| 2121 | NamedPropertyDeleter remover, |
| 2122 | NamedPropertyEnumerator enumerator, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2123 | Handle<Value> data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2124 | void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter, |
| 2125 | IndexedPropertySetter setter, |
| 2126 | IndexedPropertyQuery query, |
| 2127 | IndexedPropertyDeleter remover, |
| 2128 | IndexedPropertyEnumerator enumerator, |
| 2129 | Handle<Value> data); |
| 2130 | void SetInstanceCallAsFunctionHandler(InvocationCallback callback, |
| 2131 | Handle<Value> data); |
| 2132 | |
| 2133 | friend class Context; |
| 2134 | friend class ObjectTemplate; |
| 2135 | }; |
| 2136 | |
| 2137 | |
| 2138 | /** |
| 2139 | * An ObjectTemplate is used to create objects at runtime. |
| 2140 | * |
| 2141 | * Properties added to an ObjectTemplate are added to each object |
| 2142 | * created from the ObjectTemplate. |
| 2143 | */ |
| 2144 | class V8EXPORT ObjectTemplate : public Template { |
| 2145 | public: |
| 2146 | /** Creates an ObjectTemplate. */ |
| 2147 | static Local<ObjectTemplate> New(); |
| 2148 | |
| 2149 | /** Creates a new instance of this template.*/ |
| 2150 | Local<Object> NewInstance(); |
| 2151 | |
| 2152 | /** |
| 2153 | * Sets an accessor on the object template. |
| 2154 | * |
| 2155 | * Whenever the property with the given name is accessed on objects |
| 2156 | * created from this ObjectTemplate the getter and setter callbacks |
| 2157 | * are called instead of getting and setting the property directly |
| 2158 | * on the JavaScript object. |
| 2159 | * |
| 2160 | * \param name The name of the property for which an accessor is added. |
| 2161 | * \param getter The callback to invoke when getting the property. |
| 2162 | * \param setter The callback to invoke when setting the property. |
| 2163 | * \param data A piece of data that will be passed to the getter and setter |
| 2164 | * callbacks whenever they are invoked. |
| 2165 | * \param settings Access control settings for the accessor. This is a bit |
| 2166 | * field consisting of one of more of |
| 2167 | * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2. |
| 2168 | * The default is to not allow cross-context access. |
| 2169 | * ALL_CAN_READ means that all cross-context reads are allowed. |
| 2170 | * ALL_CAN_WRITE means that all cross-context writes are allowed. |
| 2171 | * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all |
| 2172 | * cross-context access. |
| 2173 | * \param attribute The attributes of the property for which an accessor |
| 2174 | * is added. |
| 2175 | */ |
| 2176 | void SetAccessor(Handle<String> name, |
| 2177 | AccessorGetter getter, |
| 2178 | AccessorSetter setter = 0, |
| 2179 | Handle<Value> data = Handle<Value>(), |
| 2180 | AccessControl settings = DEFAULT, |
| 2181 | PropertyAttribute attribute = None); |
| 2182 | |
| 2183 | /** |
| 2184 | * Sets a named property handler on the object template. |
| 2185 | * |
| 2186 | * Whenever a named property is accessed on objects created from |
| 2187 | * this object template, the provided callback is invoked instead of |
| 2188 | * accessing the property directly on the JavaScript object. |
| 2189 | * |
| 2190 | * \param getter The callback to invoke when getting a property. |
| 2191 | * \param setter The callback to invoke when setting a property. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2192 | * \param query The callback to invoke to check if a property is present, |
| 2193 | * and if present, get its attributes. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2194 | * \param deleter The callback to invoke when deleting a property. |
| 2195 | * \param enumerator The callback to invoke to enumerate all the named |
| 2196 | * properties of an object. |
| 2197 | * \param data A piece of data that will be passed to the callbacks |
| 2198 | * whenever they are invoked. |
| 2199 | */ |
| 2200 | void SetNamedPropertyHandler(NamedPropertyGetter getter, |
| 2201 | NamedPropertySetter setter = 0, |
| 2202 | NamedPropertyQuery query = 0, |
| 2203 | NamedPropertyDeleter deleter = 0, |
| 2204 | NamedPropertyEnumerator enumerator = 0, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2205 | Handle<Value> data = Handle<Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2206 | |
| 2207 | /** |
| 2208 | * Sets an indexed property handler on the object template. |
| 2209 | * |
| 2210 | * Whenever an indexed property is accessed on objects created from |
| 2211 | * this object template, the provided callback is invoked instead of |
| 2212 | * accessing the property directly on the JavaScript object. |
| 2213 | * |
| 2214 | * \param getter The callback to invoke when getting a property. |
| 2215 | * \param setter The callback to invoke when setting a property. |
| 2216 | * \param query The callback to invoke to check is an object has a property. |
| 2217 | * \param deleter The callback to invoke when deleting a property. |
| 2218 | * \param enumerator The callback to invoke to enumerate all the indexed |
| 2219 | * properties of an object. |
| 2220 | * \param data A piece of data that will be passed to the callbacks |
| 2221 | * whenever they are invoked. |
| 2222 | */ |
| 2223 | void SetIndexedPropertyHandler(IndexedPropertyGetter getter, |
| 2224 | IndexedPropertySetter setter = 0, |
| 2225 | IndexedPropertyQuery query = 0, |
| 2226 | IndexedPropertyDeleter deleter = 0, |
| 2227 | IndexedPropertyEnumerator enumerator = 0, |
| 2228 | Handle<Value> data = Handle<Value>()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2229 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2230 | /** |
| 2231 | * Sets the callback to be used when calling instances created from |
| 2232 | * this template as a function. If no callback is set, instances |
| 2233 | * behave like normal JavaScript objects that cannot be called as a |
| 2234 | * function. |
| 2235 | */ |
| 2236 | void SetCallAsFunctionHandler(InvocationCallback callback, |
| 2237 | Handle<Value> data = Handle<Value>()); |
| 2238 | |
| 2239 | /** |
| 2240 | * Mark object instances of the template as undetectable. |
| 2241 | * |
| 2242 | * In many ways, undetectable objects behave as though they are not |
| 2243 | * there. They behave like 'undefined' in conditionals and when |
| 2244 | * printed. However, properties can be accessed and called as on |
| 2245 | * normal objects. |
| 2246 | */ |
| 2247 | void MarkAsUndetectable(); |
| 2248 | |
| 2249 | /** |
| 2250 | * Sets access check callbacks on the object template. |
| 2251 | * |
| 2252 | * When accessing properties on instances of this object template, |
| 2253 | * the access check callback will be called to determine whether or |
| 2254 | * not to allow cross-context access to the properties. |
| 2255 | * The last parameter specifies whether access checks are turned |
| 2256 | * on by default on instances. If access checks are off by default, |
| 2257 | * they can be turned on on individual instances by calling |
| 2258 | * Object::TurnOnAccessCheck(). |
| 2259 | */ |
| 2260 | void SetAccessCheckCallbacks(NamedSecurityCallback named_handler, |
| 2261 | IndexedSecurityCallback indexed_handler, |
| 2262 | Handle<Value> data = Handle<Value>(), |
| 2263 | bool turned_on_by_default = true); |
| 2264 | |
| 2265 | /** |
| 2266 | * Gets the number of internal fields for objects generated from |
| 2267 | * this template. |
| 2268 | */ |
| 2269 | int InternalFieldCount(); |
| 2270 | |
| 2271 | /** |
| 2272 | * Sets the number of internal fields for objects generated from |
| 2273 | * this template. |
| 2274 | */ |
| 2275 | void SetInternalFieldCount(int value); |
| 2276 | |
| 2277 | private: |
| 2278 | ObjectTemplate(); |
| 2279 | static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor); |
| 2280 | friend class FunctionTemplate; |
| 2281 | }; |
| 2282 | |
| 2283 | |
| 2284 | /** |
| 2285 | * A Signature specifies which receivers and arguments a function can |
| 2286 | * legally be called with. |
| 2287 | */ |
| 2288 | class V8EXPORT Signature : public Data { |
| 2289 | public: |
| 2290 | static Local<Signature> New(Handle<FunctionTemplate> receiver = |
| 2291 | Handle<FunctionTemplate>(), |
| 2292 | int argc = 0, |
| 2293 | Handle<FunctionTemplate> argv[] = 0); |
| 2294 | private: |
| 2295 | Signature(); |
| 2296 | }; |
| 2297 | |
| 2298 | |
| 2299 | /** |
| 2300 | * A utility for determining the type of objects based on the template |
| 2301 | * they were constructed from. |
| 2302 | */ |
| 2303 | class V8EXPORT TypeSwitch : public Data { |
| 2304 | public: |
| 2305 | static Local<TypeSwitch> New(Handle<FunctionTemplate> type); |
| 2306 | static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]); |
| 2307 | int match(Handle<Value> value); |
| 2308 | private: |
| 2309 | TypeSwitch(); |
| 2310 | }; |
| 2311 | |
| 2312 | |
| 2313 | // --- E x t e n s i o n s --- |
| 2314 | |
| 2315 | |
| 2316 | /** |
| 2317 | * Ignore |
| 2318 | */ |
| 2319 | class V8EXPORT Extension { // NOLINT |
| 2320 | public: |
| 2321 | Extension(const char* name, |
| 2322 | const char* source = 0, |
| 2323 | int dep_count = 0, |
| 2324 | const char** deps = 0); |
| 2325 | virtual ~Extension() { } |
| 2326 | virtual v8::Handle<v8::FunctionTemplate> |
| 2327 | GetNativeFunction(v8::Handle<v8::String> name) { |
| 2328 | return v8::Handle<v8::FunctionTemplate>(); |
| 2329 | } |
| 2330 | |
| 2331 | const char* name() { return name_; } |
| 2332 | const char* source() { return source_; } |
| 2333 | int dependency_count() { return dep_count_; } |
| 2334 | const char** dependencies() { return deps_; } |
| 2335 | void set_auto_enable(bool value) { auto_enable_ = value; } |
| 2336 | bool auto_enable() { return auto_enable_; } |
| 2337 | |
| 2338 | private: |
| 2339 | const char* name_; |
| 2340 | const char* source_; |
| 2341 | int dep_count_; |
| 2342 | const char** deps_; |
| 2343 | bool auto_enable_; |
| 2344 | |
| 2345 | // Disallow copying and assigning. |
| 2346 | Extension(const Extension&); |
| 2347 | void operator=(const Extension&); |
| 2348 | }; |
| 2349 | |
| 2350 | |
| 2351 | void V8EXPORT RegisterExtension(Extension* extension); |
| 2352 | |
| 2353 | |
| 2354 | /** |
| 2355 | * Ignore |
| 2356 | */ |
| 2357 | class V8EXPORT DeclareExtension { |
| 2358 | public: |
| 2359 | inline DeclareExtension(Extension* extension) { |
| 2360 | RegisterExtension(extension); |
| 2361 | } |
| 2362 | }; |
| 2363 | |
| 2364 | |
| 2365 | // --- S t a t i c s --- |
| 2366 | |
| 2367 | |
| 2368 | Handle<Primitive> V8EXPORT Undefined(); |
| 2369 | Handle<Primitive> V8EXPORT Null(); |
| 2370 | Handle<Boolean> V8EXPORT True(); |
| 2371 | Handle<Boolean> V8EXPORT False(); |
| 2372 | |
| 2373 | |
| 2374 | /** |
| 2375 | * A set of constraints that specifies the limits of the runtime's memory use. |
| 2376 | * You must set the heap size before initializing the VM - the size cannot be |
| 2377 | * adjusted after the VM is initialized. |
| 2378 | * |
| 2379 | * If you are using threads then you should hold the V8::Locker lock while |
| 2380 | * setting the stack limit and you must set a non-default stack limit separately |
| 2381 | * for each thread. |
| 2382 | */ |
| 2383 | class V8EXPORT ResourceConstraints { |
| 2384 | public: |
| 2385 | ResourceConstraints(); |
| 2386 | int max_young_space_size() const { return max_young_space_size_; } |
| 2387 | void set_max_young_space_size(int value) { max_young_space_size_ = value; } |
| 2388 | int max_old_space_size() const { return max_old_space_size_; } |
| 2389 | void set_max_old_space_size(int value) { max_old_space_size_ = value; } |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 2390 | int max_executable_size() { return max_executable_size_; } |
| 2391 | void set_max_executable_size(int value) { max_executable_size_ = value; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2392 | uint32_t* stack_limit() const { return stack_limit_; } |
| 2393 | // Sets an address beyond which the VM's stack may not grow. |
| 2394 | void set_stack_limit(uint32_t* value) { stack_limit_ = value; } |
| 2395 | private: |
| 2396 | int max_young_space_size_; |
| 2397 | int max_old_space_size_; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 2398 | int max_executable_size_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2399 | uint32_t* stack_limit_; |
| 2400 | }; |
| 2401 | |
| 2402 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2403 | bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2404 | |
| 2405 | |
| 2406 | // --- E x c e p t i o n s --- |
| 2407 | |
| 2408 | |
| 2409 | typedef void (*FatalErrorCallback)(const char* location, const char* message); |
| 2410 | |
| 2411 | |
| 2412 | typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data); |
| 2413 | |
| 2414 | |
| 2415 | /** |
| 2416 | * Schedules an exception to be thrown when returning to JavaScript. When an |
| 2417 | * exception has been scheduled it is illegal to invoke any JavaScript |
| 2418 | * operation; the caller must return immediately and only after the exception |
| 2419 | * has been handled does it become legal to invoke JavaScript operations. |
| 2420 | */ |
| 2421 | Handle<Value> V8EXPORT ThrowException(Handle<Value> exception); |
| 2422 | |
| 2423 | /** |
| 2424 | * Create new error objects by calling the corresponding error object |
| 2425 | * constructor with the message. |
| 2426 | */ |
| 2427 | class V8EXPORT Exception { |
| 2428 | public: |
| 2429 | static Local<Value> RangeError(Handle<String> message); |
| 2430 | static Local<Value> ReferenceError(Handle<String> message); |
| 2431 | static Local<Value> SyntaxError(Handle<String> message); |
| 2432 | static Local<Value> TypeError(Handle<String> message); |
| 2433 | static Local<Value> Error(Handle<String> message); |
| 2434 | }; |
| 2435 | |
| 2436 | |
| 2437 | // --- C o u n t e r s C a l l b a c k s --- |
| 2438 | |
| 2439 | typedef int* (*CounterLookupCallback)(const char* name); |
| 2440 | |
| 2441 | typedef void* (*CreateHistogramCallback)(const char* name, |
| 2442 | int min, |
| 2443 | int max, |
| 2444 | size_t buckets); |
| 2445 | |
| 2446 | typedef void (*AddHistogramSampleCallback)(void* histogram, int sample); |
| 2447 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 2448 | // --- M e m o r y A l l o c a t i o n C a l l b a c k --- |
| 2449 | enum ObjectSpace { |
| 2450 | kObjectSpaceNewSpace = 1 << 0, |
| 2451 | kObjectSpaceOldPointerSpace = 1 << 1, |
| 2452 | kObjectSpaceOldDataSpace = 1 << 2, |
| 2453 | kObjectSpaceCodeSpace = 1 << 3, |
| 2454 | kObjectSpaceMapSpace = 1 << 4, |
| 2455 | kObjectSpaceLoSpace = 1 << 5, |
| 2456 | |
| 2457 | kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace | |
| 2458 | kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace | |
| 2459 | kObjectSpaceLoSpace |
| 2460 | }; |
| 2461 | |
| 2462 | enum AllocationAction { |
| 2463 | kAllocationActionAllocate = 1 << 0, |
| 2464 | kAllocationActionFree = 1 << 1, |
| 2465 | kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree |
| 2466 | }; |
| 2467 | |
| 2468 | typedef void (*MemoryAllocationCallback)(ObjectSpace space, |
| 2469 | AllocationAction action, |
| 2470 | int size); |
| 2471 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2472 | // --- F a i l e d A c c e s s C h e c k C a l l b a c k --- |
| 2473 | typedef void (*FailedAccessCheckCallback)(Local<Object> target, |
| 2474 | AccessType type, |
| 2475 | Local<Value> data); |
| 2476 | |
| 2477 | // --- G a r b a g e C o l l e c t i o n C a l l b a c k s |
| 2478 | |
| 2479 | /** |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2480 | * Applications can register callback functions which will be called |
| 2481 | * before and after a garbage collection. Allocations are not |
| 2482 | * allowed in the callback functions, you therefore cannot manipulate |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2483 | * objects (set or delete properties for example) since it is possible |
| 2484 | * such operations will result in the allocation of objects. |
| 2485 | */ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2486 | enum GCType { |
| 2487 | kGCTypeScavenge = 1 << 0, |
| 2488 | kGCTypeMarkSweepCompact = 1 << 1, |
| 2489 | kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact |
| 2490 | }; |
| 2491 | |
| 2492 | enum GCCallbackFlags { |
| 2493 | kNoGCCallbackFlags = 0, |
| 2494 | kGCCallbackFlagCompacted = 1 << 0 |
| 2495 | }; |
| 2496 | |
| 2497 | typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags); |
| 2498 | typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags); |
| 2499 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2500 | typedef void (*GCCallback)(); |
| 2501 | |
| 2502 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2503 | /** |
| 2504 | * Profiler modules. |
| 2505 | * |
| 2506 | * In V8, profiler consists of several modules: CPU profiler, and different |
| 2507 | * kinds of heap profiling. Each can be turned on / off independently. |
| 2508 | * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx, |
| 2509 | * modules are enabled only temporarily for making a snapshot of the heap. |
| 2510 | */ |
| 2511 | enum ProfilerModules { |
| 2512 | PROFILER_MODULE_NONE = 0, |
| 2513 | PROFILER_MODULE_CPU = 1, |
| 2514 | PROFILER_MODULE_HEAP_STATS = 1 << 1, |
| 2515 | PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2, |
| 2516 | PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16 |
| 2517 | }; |
| 2518 | |
| 2519 | |
| 2520 | /** |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2521 | * Collection of V8 heap information. |
| 2522 | * |
| 2523 | * Instances of this class can be passed to v8::V8::HeapStatistics to |
| 2524 | * get heap statistics from V8. |
| 2525 | */ |
| 2526 | class V8EXPORT HeapStatistics { |
| 2527 | public: |
| 2528 | HeapStatistics(); |
| 2529 | size_t total_heap_size() { return total_heap_size_; } |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 2530 | size_t total_heap_size_executable() { return total_heap_size_executable_; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2531 | size_t used_heap_size() { return used_heap_size_; } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 2532 | size_t heap_size_limit() { return heap_size_limit_; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2533 | |
| 2534 | private: |
| 2535 | void set_total_heap_size(size_t size) { total_heap_size_ = size; } |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 2536 | void set_total_heap_size_executable(size_t size) { |
| 2537 | total_heap_size_executable_ = size; |
| 2538 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2539 | void set_used_heap_size(size_t size) { used_heap_size_ = size; } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 2540 | void set_heap_size_limit(size_t size) { heap_size_limit_ = size; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2541 | |
| 2542 | size_t total_heap_size_; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 2543 | size_t total_heap_size_executable_; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2544 | size_t used_heap_size_; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 2545 | size_t heap_size_limit_; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2546 | |
| 2547 | friend class V8; |
| 2548 | }; |
| 2549 | |
| 2550 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 2551 | class RetainedObjectInfo; |
| 2552 | |
| 2553 | /** |
| 2554 | * Isolate represents an isolated instance of the V8 engine. V8 |
| 2555 | * isolates have completely separate states. Objects from one isolate |
| 2556 | * must not be used in other isolates. When V8 is initialized a |
| 2557 | * default isolate is implicitly created and entered. The embedder |
| 2558 | * can create additional isolates and use them in parallel in multiple |
| 2559 | * threads. An isolate can be entered by at most one thread at any |
| 2560 | * given time. The Locker/Unlocker API can be used to synchronize. |
| 2561 | */ |
| 2562 | class V8EXPORT Isolate { |
| 2563 | public: |
| 2564 | /** |
| 2565 | * Stack-allocated class which sets the isolate for all operations |
| 2566 | * executed within a local scope. |
| 2567 | */ |
| 2568 | class V8EXPORT Scope { |
| 2569 | public: |
| 2570 | explicit Scope(Isolate* isolate) : isolate_(isolate) { |
| 2571 | isolate->Enter(); |
| 2572 | } |
| 2573 | |
| 2574 | ~Scope() { isolate_->Exit(); } |
| 2575 | |
| 2576 | private: |
| 2577 | Isolate* const isolate_; |
| 2578 | |
| 2579 | // Prevent copying of Scope objects. |
| 2580 | Scope(const Scope&); |
| 2581 | Scope& operator=(const Scope&); |
| 2582 | }; |
| 2583 | |
| 2584 | /** |
| 2585 | * Creates a new isolate. Does not change the currently entered |
| 2586 | * isolate. |
| 2587 | * |
| 2588 | * When an isolate is no longer used its resources should be freed |
| 2589 | * by calling Dispose(). Using the delete operator is not allowed. |
| 2590 | */ |
| 2591 | static Isolate* New(); |
| 2592 | |
| 2593 | /** |
| 2594 | * Returns the entered isolate for the current thread or NULL in |
| 2595 | * case there is no current isolate. |
| 2596 | */ |
| 2597 | static Isolate* GetCurrent(); |
| 2598 | |
| 2599 | /** |
| 2600 | * Methods below this point require holding a lock (using Locker) in |
| 2601 | * a multi-threaded environment. |
| 2602 | */ |
| 2603 | |
| 2604 | /** |
| 2605 | * Sets this isolate as the entered one for the current thread. |
| 2606 | * Saves the previously entered one (if any), so that it can be |
| 2607 | * restored when exiting. Re-entering an isolate is allowed. |
| 2608 | */ |
| 2609 | void Enter(); |
| 2610 | |
| 2611 | /** |
| 2612 | * Exits this isolate by restoring the previously entered one in the |
| 2613 | * current thread. The isolate may still stay the same, if it was |
| 2614 | * entered more than once. |
| 2615 | * |
| 2616 | * Requires: this == Isolate::GetCurrent(). |
| 2617 | */ |
| 2618 | void Exit(); |
| 2619 | |
| 2620 | /** |
| 2621 | * Disposes the isolate. The isolate must not be entered by any |
| 2622 | * thread to be disposable. |
| 2623 | */ |
| 2624 | void Dispose(); |
| 2625 | |
| 2626 | private: |
| 2627 | |
| 2628 | Isolate(); |
| 2629 | Isolate(const Isolate&); |
| 2630 | ~Isolate(); |
| 2631 | Isolate& operator=(const Isolate&); |
| 2632 | void* operator new(size_t size); |
| 2633 | void operator delete(void*, size_t); |
| 2634 | }; |
| 2635 | |
| 2636 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2637 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2638 | * Container class for static utility functions. |
| 2639 | */ |
| 2640 | class V8EXPORT V8 { |
| 2641 | public: |
| 2642 | /** Set the callback to invoke in case of fatal errors. */ |
| 2643 | static void SetFatalErrorHandler(FatalErrorCallback that); |
| 2644 | |
| 2645 | /** |
| 2646 | * Ignore out-of-memory exceptions. |
| 2647 | * |
| 2648 | * V8 running out of memory is treated as a fatal error by default. |
| 2649 | * This means that the fatal error handler is called and that V8 is |
| 2650 | * terminated. |
| 2651 | * |
| 2652 | * IgnoreOutOfMemoryException can be used to not treat a |
| 2653 | * out-of-memory situation as a fatal error. This way, the contexts |
| 2654 | * that did not cause the out of memory problem might be able to |
| 2655 | * continue execution. |
| 2656 | */ |
| 2657 | static void IgnoreOutOfMemoryException(); |
| 2658 | |
| 2659 | /** |
| 2660 | * Check if V8 is dead and therefore unusable. This is the case after |
| 2661 | * fatal errors such as out-of-memory situations. |
| 2662 | */ |
| 2663 | static bool IsDead(); |
| 2664 | |
| 2665 | /** |
| 2666 | * Adds a message listener. |
| 2667 | * |
| 2668 | * The same message listener can be added more than once and it that |
| 2669 | * case it will be called more than once for each message. |
| 2670 | */ |
| 2671 | static bool AddMessageListener(MessageCallback that, |
| 2672 | Handle<Value> data = Handle<Value>()); |
| 2673 | |
| 2674 | /** |
| 2675 | * Remove all message listeners from the specified callback function. |
| 2676 | */ |
| 2677 | static void RemoveMessageListeners(MessageCallback that); |
| 2678 | |
| 2679 | /** |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2680 | * Tells V8 to capture current stack trace when uncaught exception occurs |
| 2681 | * and report it to the message listeners. The option is off by default. |
| 2682 | */ |
| 2683 | static void SetCaptureStackTraceForUncaughtExceptions( |
| 2684 | bool capture, |
| 2685 | int frame_limit = 10, |
| 2686 | StackTrace::StackTraceOptions options = StackTrace::kOverview); |
| 2687 | |
| 2688 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2689 | * Sets V8 flags from a string. |
| 2690 | */ |
| 2691 | static void SetFlagsFromString(const char* str, int length); |
| 2692 | |
| 2693 | /** |
| 2694 | * Sets V8 flags from the command line. |
| 2695 | */ |
| 2696 | static void SetFlagsFromCommandLine(int* argc, |
| 2697 | char** argv, |
| 2698 | bool remove_flags); |
| 2699 | |
| 2700 | /** Get the version string. */ |
| 2701 | static const char* GetVersion(); |
| 2702 | |
| 2703 | /** |
| 2704 | * Enables the host application to provide a mechanism for recording |
| 2705 | * statistics counters. |
| 2706 | */ |
| 2707 | static void SetCounterFunction(CounterLookupCallback); |
| 2708 | |
| 2709 | /** |
| 2710 | * Enables the host application to provide a mechanism for recording |
| 2711 | * histograms. The CreateHistogram function returns a |
| 2712 | * histogram which will later be passed to the AddHistogramSample |
| 2713 | * function. |
| 2714 | */ |
| 2715 | static void SetCreateHistogramFunction(CreateHistogramCallback); |
| 2716 | static void SetAddHistogramSampleFunction(AddHistogramSampleCallback); |
| 2717 | |
| 2718 | /** |
| 2719 | * Enables the computation of a sliding window of states. The sliding |
| 2720 | * window information is recorded in statistics counters. |
| 2721 | */ |
| 2722 | static void EnableSlidingStateWindow(); |
| 2723 | |
| 2724 | /** Callback function for reporting failed access checks.*/ |
| 2725 | static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback); |
| 2726 | |
| 2727 | /** |
| 2728 | * Enables the host application to receive a notification before a |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2729 | * garbage collection. Allocations are not allowed in the |
| 2730 | * callback function, you therefore cannot manipulate objects (set |
| 2731 | * or delete properties for example) since it is possible such |
| 2732 | * operations will result in the allocation of objects. It is possible |
| 2733 | * to specify the GCType filter for your callback. But it is not possible to |
| 2734 | * register the same callback function two times with different |
| 2735 | * GCType filters. |
| 2736 | */ |
| 2737 | static void AddGCPrologueCallback( |
| 2738 | GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll); |
| 2739 | |
| 2740 | /** |
| 2741 | * This function removes callback which was installed by |
| 2742 | * AddGCPrologueCallback function. |
| 2743 | */ |
| 2744 | static void RemoveGCPrologueCallback(GCPrologueCallback callback); |
| 2745 | |
| 2746 | /** |
| 2747 | * The function is deprecated. Please use AddGCPrologueCallback instead. |
| 2748 | * Enables the host application to receive a notification before a |
| 2749 | * garbage collection. Allocations are not allowed in the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2750 | * callback function, you therefore cannot manipulate objects (set |
| 2751 | * or delete properties for example) since it is possible such |
| 2752 | * operations will result in the allocation of objects. |
| 2753 | */ |
| 2754 | static void SetGlobalGCPrologueCallback(GCCallback); |
| 2755 | |
| 2756 | /** |
| 2757 | * Enables the host application to receive a notification after a |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2758 | * garbage collection. Allocations are not allowed in the |
| 2759 | * callback function, you therefore cannot manipulate objects (set |
| 2760 | * or delete properties for example) since it is possible such |
| 2761 | * operations will result in the allocation of objects. It is possible |
| 2762 | * to specify the GCType filter for your callback. But it is not possible to |
| 2763 | * register the same callback function two times with different |
| 2764 | * GCType filters. |
| 2765 | */ |
| 2766 | static void AddGCEpilogueCallback( |
| 2767 | GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll); |
| 2768 | |
| 2769 | /** |
| 2770 | * This function removes callback which was installed by |
| 2771 | * AddGCEpilogueCallback function. |
| 2772 | */ |
| 2773 | static void RemoveGCEpilogueCallback(GCEpilogueCallback callback); |
| 2774 | |
| 2775 | /** |
| 2776 | * The function is deprecated. Please use AddGCEpilogueCallback instead. |
| 2777 | * Enables the host application to receive a notification after a |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2778 | * major garbage collection. Allocations are not allowed in the |
| 2779 | * callback function, you therefore cannot manipulate objects (set |
| 2780 | * or delete properties for example) since it is possible such |
| 2781 | * operations will result in the allocation of objects. |
| 2782 | */ |
| 2783 | static void SetGlobalGCEpilogueCallback(GCCallback); |
| 2784 | |
| 2785 | /** |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 2786 | * Enables the host application to provide a mechanism to be notified |
| 2787 | * and perform custom logging when V8 Allocates Executable Memory. |
| 2788 | */ |
| 2789 | static void AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 2790 | ObjectSpace space, |
| 2791 | AllocationAction action); |
| 2792 | |
| 2793 | /** |
| 2794 | * This function removes callback which was installed by |
| 2795 | * AddMemoryAllocationCallback function. |
| 2796 | */ |
| 2797 | static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback); |
| 2798 | |
| 2799 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2800 | * Allows the host application to group objects together. If one |
| 2801 | * object in the group is alive, all objects in the group are alive. |
| 2802 | * After each garbage collection, object groups are removed. It is |
| 2803 | * intended to be used in the before-garbage-collection callback |
| 2804 | * function, for instance to simulate DOM tree connections among JS |
| 2805 | * wrapper objects. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 2806 | * See v8-profiler.h for RetainedObjectInfo interface description. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2807 | */ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 2808 | static void AddObjectGroup(Persistent<Value>* objects, |
| 2809 | size_t length, |
| 2810 | RetainedObjectInfo* info = NULL); |
| 2811 | |
| 2812 | /** |
| 2813 | * Allows the host application to declare implicit references between |
| 2814 | * the objects: if |parent| is alive, all |children| are alive too. |
| 2815 | * After each garbage collection, all implicit references |
| 2816 | * are removed. It is intended to be used in the before-garbage-collection |
| 2817 | * callback function. |
| 2818 | */ |
| 2819 | static void AddImplicitReferences(Persistent<Object> parent, |
| 2820 | Persistent<Value>* children, |
| 2821 | size_t length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2822 | |
| 2823 | /** |
| 2824 | * Initializes from snapshot if possible. Otherwise, attempts to |
| 2825 | * initialize from scratch. This function is called implicitly if |
| 2826 | * you use the API without calling it first. |
| 2827 | */ |
| 2828 | static bool Initialize(); |
| 2829 | |
| 2830 | /** |
| 2831 | * Adjusts the amount of registered external memory. Used to give |
| 2832 | * V8 an indication of the amount of externally allocated memory |
| 2833 | * that is kept alive by JavaScript objects. V8 uses this to decide |
| 2834 | * when to perform global garbage collections. Registering |
| 2835 | * externally allocated memory will trigger global garbage |
| 2836 | * collections more often than otherwise in an attempt to garbage |
| 2837 | * collect the JavaScript objects keeping the externally allocated |
| 2838 | * memory alive. |
| 2839 | * |
| 2840 | * \param change_in_bytes the change in externally allocated memory |
| 2841 | * that is kept alive by JavaScript objects. |
| 2842 | * \returns the adjusted value. |
| 2843 | */ |
| 2844 | static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes); |
| 2845 | |
| 2846 | /** |
| 2847 | * Suspends recording of tick samples in the profiler. |
| 2848 | * When the V8 profiling mode is enabled (usually via command line |
| 2849 | * switches) this function suspends recording of tick samples. |
| 2850 | * Profiling ticks are discarded until ResumeProfiler() is called. |
| 2851 | * |
| 2852 | * See also the --prof and --prof_auto command line switches to |
| 2853 | * enable V8 profiling. |
| 2854 | */ |
| 2855 | static void PauseProfiler(); |
| 2856 | |
| 2857 | /** |
| 2858 | * Resumes recording of tick samples in the profiler. |
| 2859 | * See also PauseProfiler(). |
| 2860 | */ |
| 2861 | static void ResumeProfiler(); |
| 2862 | |
| 2863 | /** |
| 2864 | * Return whether profiler is currently paused. |
| 2865 | */ |
| 2866 | static bool IsProfilerPaused(); |
| 2867 | |
| 2868 | /** |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2869 | * Resumes specified profiler modules. Can be called several times to |
| 2870 | * mark the opening of a profiler events block with the given tag. |
| 2871 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2872 | * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)". |
| 2873 | * See ProfilerModules enum. |
| 2874 | * |
| 2875 | * \param flags Flags specifying profiler modules. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2876 | * \param tag Profile tag. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2877 | */ |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2878 | static void ResumeProfilerEx(int flags, int tag = 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2879 | |
| 2880 | /** |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2881 | * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes |
| 2882 | * a block of profiler events opened by a call to "ResumeProfilerEx" with the |
| 2883 | * same tag value. There is no need for blocks to be properly nested. |
| 2884 | * The profiler is paused when the last opened block is closed. |
| 2885 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2886 | * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)". |
| 2887 | * See ProfilerModules enum. |
| 2888 | * |
| 2889 | * \param flags Flags specifying profiler modules. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2890 | * \param tag Profile tag. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2891 | */ |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2892 | static void PauseProfilerEx(int flags, int tag = 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2893 | |
| 2894 | /** |
| 2895 | * Returns active (resumed) profiler modules. |
| 2896 | * See ProfilerModules enum. |
| 2897 | * |
| 2898 | * \returns active profiler modules. |
| 2899 | */ |
| 2900 | static int GetActiveProfilerModules(); |
| 2901 | |
| 2902 | /** |
| 2903 | * If logging is performed into a memory buffer (via --logfile=*), allows to |
| 2904 | * retrieve previously written messages. This can be used for retrieving |
| 2905 | * profiler log data in the application. This function is thread-safe. |
| 2906 | * |
| 2907 | * Caller provides a destination buffer that must exist during GetLogLines |
| 2908 | * call. Only whole log lines are copied into the buffer. |
| 2909 | * |
| 2910 | * \param from_pos specified a point in a buffer to read from, 0 is the |
| 2911 | * beginning of a buffer. It is assumed that caller updates its current |
| 2912 | * position using returned size value from the previous call. |
| 2913 | * \param dest_buf destination buffer for log data. |
| 2914 | * \param max_size size of the destination buffer. |
| 2915 | * \returns actual size of log data copied into buffer. |
| 2916 | */ |
| 2917 | static int GetLogLines(int from_pos, char* dest_buf, int max_size); |
| 2918 | |
| 2919 | /** |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2920 | * The minimum allowed size for a log lines buffer. If the size of |
| 2921 | * the buffer given will not be enough to hold a line of the maximum |
| 2922 | * length, an attempt to find a log line end in GetLogLines will |
| 2923 | * fail, and an empty result will be returned. |
| 2924 | */ |
| 2925 | static const int kMinimumSizeForLogLinesBuffer = 2048; |
| 2926 | |
| 2927 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2928 | * Retrieve the V8 thread id of the calling thread. |
| 2929 | * |
| 2930 | * The thread id for a thread should only be retrieved after the V8 |
| 2931 | * lock has been acquired with a Locker object with that thread. |
| 2932 | */ |
| 2933 | static int GetCurrentThreadId(); |
| 2934 | |
| 2935 | /** |
| 2936 | * Forcefully terminate execution of a JavaScript thread. This can |
| 2937 | * be used to terminate long-running scripts. |
| 2938 | * |
| 2939 | * TerminateExecution should only be called when then V8 lock has |
| 2940 | * been acquired with a Locker object. Therefore, in order to be |
| 2941 | * able to terminate long-running threads, preemption must be |
| 2942 | * enabled to allow the user of TerminateExecution to acquire the |
| 2943 | * lock. |
| 2944 | * |
| 2945 | * The termination is achieved by throwing an exception that is |
| 2946 | * uncatchable by JavaScript exception handlers. Termination |
| 2947 | * exceptions act as if they were caught by a C++ TryCatch exception |
| 2948 | * handlers. If forceful termination is used, any C++ TryCatch |
| 2949 | * exception handler that catches an exception should check if that |
| 2950 | * exception is a termination exception and immediately return if |
| 2951 | * that is the case. Returning immediately in that case will |
| 2952 | * continue the propagation of the termination exception if needed. |
| 2953 | * |
| 2954 | * The thread id passed to TerminateExecution must have been |
| 2955 | * obtained by calling GetCurrentThreadId on the thread in question. |
| 2956 | * |
| 2957 | * \param thread_id The thread id of the thread to terminate. |
| 2958 | */ |
| 2959 | static void TerminateExecution(int thread_id); |
| 2960 | |
| 2961 | /** |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 2962 | * Forcefully terminate the current thread of JavaScript execution |
| 2963 | * in the given isolate. If no isolate is provided, the default |
| 2964 | * isolate is used. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2965 | * |
| 2966 | * This method can be used by any thread even if that thread has not |
| 2967 | * acquired the V8 lock with a Locker object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 2968 | * |
| 2969 | * \param isolate The isolate in which to terminate the current JS execution. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2970 | */ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 2971 | static void TerminateExecution(Isolate* isolate = NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2972 | |
| 2973 | /** |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2974 | * Is V8 terminating JavaScript execution. |
| 2975 | * |
| 2976 | * Returns true if JavaScript execution is currently terminating |
| 2977 | * because of a call to TerminateExecution. In that case there are |
| 2978 | * still JavaScript frames on the stack and the termination |
| 2979 | * exception is still active. |
| 2980 | */ |
| 2981 | static bool IsExecutionTerminating(); |
| 2982 | |
| 2983 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2984 | * Releases any resources used by v8 and stops any utility threads |
| 2985 | * that may be running. Note that disposing v8 is permanent, it |
| 2986 | * cannot be reinitialized. |
| 2987 | * |
| 2988 | * It should generally not be necessary to dispose v8 before exiting |
| 2989 | * a process, this should happen automatically. It is only necessary |
| 2990 | * to use if the process needs the resources taken up by v8. |
| 2991 | */ |
| 2992 | static bool Dispose(); |
| 2993 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2994 | /** |
| 2995 | * Get statistics about the heap memory usage. |
| 2996 | */ |
| 2997 | static void GetHeapStatistics(HeapStatistics* heap_statistics); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2998 | |
| 2999 | /** |
| 3000 | * Optional notification that the embedder is idle. |
| 3001 | * V8 uses the notification to reduce memory footprint. |
| 3002 | * This call can be used repeatedly if the embedder remains idle. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3003 | * Returns true if the embedder should stop calling IdleNotification |
| 3004 | * until real work has been done. This indicates that V8 has done |
| 3005 | * as much cleanup as it will be able to do. |
| 3006 | */ |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3007 | static bool IdleNotification(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3008 | |
| 3009 | /** |
| 3010 | * Optional notification that the system is running low on memory. |
| 3011 | * V8 uses these notifications to attempt to free memory. |
| 3012 | */ |
| 3013 | static void LowMemoryNotification(); |
| 3014 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3015 | /** |
| 3016 | * Optional notification that a context has been disposed. V8 uses |
| 3017 | * these notifications to guide the GC heuristic. Returns the number |
| 3018 | * of context disposals - including this one - since the last time |
| 3019 | * V8 had a chance to clean up. |
| 3020 | */ |
| 3021 | static int ContextDisposedNotification(); |
| 3022 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3023 | private: |
| 3024 | V8(); |
| 3025 | |
| 3026 | static internal::Object** GlobalizeReference(internal::Object** handle); |
| 3027 | static void DisposeGlobal(internal::Object** global_handle); |
| 3028 | static void MakeWeak(internal::Object** global_handle, |
| 3029 | void* data, |
| 3030 | WeakReferenceCallback); |
| 3031 | static void ClearWeak(internal::Object** global_handle); |
| 3032 | static bool IsGlobalNearDeath(internal::Object** global_handle); |
| 3033 | static bool IsGlobalWeak(internal::Object** global_handle); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 3034 | static void SetWrapperClassId(internal::Object** global_handle, |
| 3035 | uint16_t class_id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3036 | |
| 3037 | template <class T> friend class Handle; |
| 3038 | template <class T> friend class Local; |
| 3039 | template <class T> friend class Persistent; |
| 3040 | friend class Context; |
| 3041 | }; |
| 3042 | |
| 3043 | |
| 3044 | /** |
| 3045 | * An external exception handler. |
| 3046 | */ |
| 3047 | class V8EXPORT TryCatch { |
| 3048 | public: |
| 3049 | |
| 3050 | /** |
| 3051 | * Creates a new try/catch block and registers it with v8. |
| 3052 | */ |
| 3053 | TryCatch(); |
| 3054 | |
| 3055 | /** |
| 3056 | * Unregisters and deletes this try/catch block. |
| 3057 | */ |
| 3058 | ~TryCatch(); |
| 3059 | |
| 3060 | /** |
| 3061 | * Returns true if an exception has been caught by this try/catch block. |
| 3062 | */ |
| 3063 | bool HasCaught() const; |
| 3064 | |
| 3065 | /** |
| 3066 | * For certain types of exceptions, it makes no sense to continue |
| 3067 | * execution. |
| 3068 | * |
| 3069 | * Currently, the only type of exception that can be caught by a |
| 3070 | * TryCatch handler and for which it does not make sense to continue |
| 3071 | * is termination exception. Such exceptions are thrown when the |
| 3072 | * TerminateExecution methods are called to terminate a long-running |
| 3073 | * script. |
| 3074 | * |
| 3075 | * If CanContinue returns false, the correct action is to perform |
| 3076 | * any C++ cleanup needed and then return. |
| 3077 | */ |
| 3078 | bool CanContinue() const; |
| 3079 | |
| 3080 | /** |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3081 | * Throws the exception caught by this TryCatch in a way that avoids |
| 3082 | * it being caught again by this same TryCatch. As with ThrowException |
| 3083 | * it is illegal to execute any JavaScript operations after calling |
| 3084 | * ReThrow; the caller must return immediately to where the exception |
| 3085 | * is caught. |
| 3086 | */ |
| 3087 | Handle<Value> ReThrow(); |
| 3088 | |
| 3089 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3090 | * Returns the exception caught by this try/catch block. If no exception has |
| 3091 | * been caught an empty handle is returned. |
| 3092 | * |
| 3093 | * The returned handle is valid until this TryCatch block has been destroyed. |
| 3094 | */ |
| 3095 | Local<Value> Exception() const; |
| 3096 | |
| 3097 | /** |
| 3098 | * Returns the .stack property of the thrown object. If no .stack |
| 3099 | * property is present an empty handle is returned. |
| 3100 | */ |
| 3101 | Local<Value> StackTrace() const; |
| 3102 | |
| 3103 | /** |
| 3104 | * Returns the message associated with this exception. If there is |
| 3105 | * no message associated an empty handle is returned. |
| 3106 | * |
| 3107 | * The returned handle is valid until this TryCatch block has been |
| 3108 | * destroyed. |
| 3109 | */ |
| 3110 | Local<v8::Message> Message() const; |
| 3111 | |
| 3112 | /** |
| 3113 | * Clears any exceptions that may have been caught by this try/catch block. |
| 3114 | * After this method has been called, HasCaught() will return false. |
| 3115 | * |
| 3116 | * It is not necessary to clear a try/catch block before using it again; if |
| 3117 | * another exception is thrown the previously caught exception will just be |
| 3118 | * overwritten. However, it is often a good idea since it makes it easier |
| 3119 | * to determine which operation threw a given exception. |
| 3120 | */ |
| 3121 | void Reset(); |
| 3122 | |
| 3123 | /** |
| 3124 | * Set verbosity of the external exception handler. |
| 3125 | * |
| 3126 | * By default, exceptions that are caught by an external exception |
| 3127 | * handler are not reported. Call SetVerbose with true on an |
| 3128 | * external exception handler to have exceptions caught by the |
| 3129 | * handler reported as if they were not caught. |
| 3130 | */ |
| 3131 | void SetVerbose(bool value); |
| 3132 | |
| 3133 | /** |
| 3134 | * Set whether or not this TryCatch should capture a Message object |
| 3135 | * which holds source information about where the exception |
| 3136 | * occurred. True by default. |
| 3137 | */ |
| 3138 | void SetCaptureMessage(bool value); |
| 3139 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3140 | private: |
| 3141 | void* next_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3142 | void* exception_; |
| 3143 | void* message_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3144 | bool is_verbose_ : 1; |
| 3145 | bool can_continue_ : 1; |
| 3146 | bool capture_message_ : 1; |
| 3147 | bool rethrow_ : 1; |
| 3148 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 3149 | friend class v8::internal::Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3150 | }; |
| 3151 | |
| 3152 | |
| 3153 | // --- C o n t e x t --- |
| 3154 | |
| 3155 | |
| 3156 | /** |
| 3157 | * Ignore |
| 3158 | */ |
| 3159 | class V8EXPORT ExtensionConfiguration { |
| 3160 | public: |
| 3161 | ExtensionConfiguration(int name_count, const char* names[]) |
| 3162 | : name_count_(name_count), names_(names) { } |
| 3163 | private: |
| 3164 | friend class ImplementationUtilities; |
| 3165 | int name_count_; |
| 3166 | const char** names_; |
| 3167 | }; |
| 3168 | |
| 3169 | |
| 3170 | /** |
| 3171 | * A sandboxed execution context with its own set of built-in objects |
| 3172 | * and functions. |
| 3173 | */ |
| 3174 | class V8EXPORT Context { |
| 3175 | public: |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3176 | /** |
| 3177 | * Returns the global proxy object or global object itself for |
| 3178 | * detached contexts. |
| 3179 | * |
| 3180 | * Global proxy object is a thin wrapper whose prototype points to |
| 3181 | * actual context's global object with the properties like Object, etc. |
| 3182 | * This is done that way for security reasons (for more details see |
| 3183 | * https://wiki.mozilla.org/Gecko:SplitWindow). |
| 3184 | * |
| 3185 | * Please note that changes to global proxy object prototype most probably |
| 3186 | * would break VM---v8 expects only global object as a prototype of |
| 3187 | * global proxy object. |
| 3188 | * |
| 3189 | * If DetachGlobal() has been invoked, Global() would return actual global |
| 3190 | * object until global is reattached with ReattachGlobal(). |
| 3191 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3192 | Local<Object> Global(); |
| 3193 | |
| 3194 | /** |
| 3195 | * Detaches the global object from its context before |
| 3196 | * the global object can be reused to create a new context. |
| 3197 | */ |
| 3198 | void DetachGlobal(); |
| 3199 | |
Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 3200 | /** |
| 3201 | * Reattaches a global object to a context. This can be used to |
| 3202 | * restore the connection between a global object and a context |
| 3203 | * after DetachGlobal has been called. |
| 3204 | * |
| 3205 | * \param global_object The global object to reattach to the |
| 3206 | * context. For this to work, the global object must be the global |
| 3207 | * object that was associated with this context before a call to |
| 3208 | * DetachGlobal. |
| 3209 | */ |
| 3210 | void ReattachGlobal(Handle<Object> global_object); |
| 3211 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3212 | /** Creates a new context. |
| 3213 | * |
| 3214 | * Returns a persistent handle to the newly allocated context. This |
| 3215 | * persistent handle has to be disposed when the context is no |
| 3216 | * longer used so the context can be garbage collected. |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3217 | * |
| 3218 | * \param extensions An optional extension configuration containing |
| 3219 | * the extensions to be installed in the newly created context. |
| 3220 | * |
| 3221 | * \param global_template An optional object template from which the |
| 3222 | * global object for the newly created context will be created. |
| 3223 | * |
| 3224 | * \param global_object An optional global object to be reused for |
| 3225 | * the newly created context. This global object must have been |
| 3226 | * created by a previous call to Context::New with the same global |
| 3227 | * template. The state of the global object will be completely reset |
| 3228 | * and only object identify will remain. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3229 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3230 | static Persistent<Context> New( |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 3231 | ExtensionConfiguration* extensions = NULL, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3232 | Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(), |
| 3233 | Handle<Value> global_object = Handle<Value>()); |
| 3234 | |
| 3235 | /** Returns the last entered context. */ |
| 3236 | static Local<Context> GetEntered(); |
| 3237 | |
| 3238 | /** Returns the context that is on the top of the stack. */ |
| 3239 | static Local<Context> GetCurrent(); |
| 3240 | |
| 3241 | /** |
| 3242 | * Returns the context of the calling JavaScript code. That is the |
| 3243 | * context of the top-most JavaScript frame. If there are no |
| 3244 | * JavaScript frames an empty handle is returned. |
| 3245 | */ |
| 3246 | static Local<Context> GetCalling(); |
| 3247 | |
| 3248 | /** |
| 3249 | * Sets the security token for the context. To access an object in |
| 3250 | * another context, the security tokens must match. |
| 3251 | */ |
| 3252 | void SetSecurityToken(Handle<Value> token); |
| 3253 | |
| 3254 | /** Restores the security token to the default value. */ |
| 3255 | void UseDefaultSecurityToken(); |
| 3256 | |
| 3257 | /** Returns the security token of this context.*/ |
| 3258 | Handle<Value> GetSecurityToken(); |
| 3259 | |
| 3260 | /** |
| 3261 | * Enter this context. After entering a context, all code compiled |
| 3262 | * and run is compiled and run in this context. If another context |
| 3263 | * is already entered, this old context is saved so it can be |
| 3264 | * restored when the new context is exited. |
| 3265 | */ |
| 3266 | void Enter(); |
| 3267 | |
| 3268 | /** |
| 3269 | * Exit this context. Exiting the current context restores the |
| 3270 | * context that was in place when entering the current context. |
| 3271 | */ |
| 3272 | void Exit(); |
| 3273 | |
| 3274 | /** Returns true if the context has experienced an out of memory situation. */ |
| 3275 | bool HasOutOfMemoryException(); |
| 3276 | |
| 3277 | /** Returns true if V8 has a current context. */ |
| 3278 | static bool InContext(); |
| 3279 | |
| 3280 | /** |
| 3281 | * Associate an additional data object with the context. This is mainly used |
| 3282 | * with the debugger to provide additional information on the context through |
| 3283 | * the debugger API. |
| 3284 | */ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3285 | void SetData(Handle<String> data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3286 | Local<Value> GetData(); |
| 3287 | |
| 3288 | /** |
| 3289 | * Stack-allocated class which sets the execution context for all |
| 3290 | * operations executed within a local scope. |
| 3291 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3292 | class Scope { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3293 | public: |
| 3294 | inline Scope(Handle<Context> context) : context_(context) { |
| 3295 | context_->Enter(); |
| 3296 | } |
| 3297 | inline ~Scope() { context_->Exit(); } |
| 3298 | private: |
| 3299 | Handle<Context> context_; |
| 3300 | }; |
| 3301 | |
| 3302 | private: |
| 3303 | friend class Value; |
| 3304 | friend class Script; |
| 3305 | friend class Object; |
| 3306 | friend class Function; |
| 3307 | }; |
| 3308 | |
| 3309 | |
| 3310 | /** |
| 3311 | * Multiple threads in V8 are allowed, but only one thread at a time |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 3312 | * is allowed to use any given V8 isolate. See Isolate class |
| 3313 | * comments. The definition of 'using V8 isolate' includes |
| 3314 | * accessing handles or holding onto object pointers obtained |
| 3315 | * from V8 handles while in the particular V8 isolate. It is up |
| 3316 | * to the user of V8 to ensure (perhaps with locking) that this |
| 3317 | * constraint is not violated. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3318 | * |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 3319 | * More then one thread and multiple V8 isolates can be used |
| 3320 | * without any locking if each isolate is created and accessed |
| 3321 | * by a single thread only. For example, one thread can use |
| 3322 | * multiple isolates or multiple threads can each create and run |
| 3323 | * their own isolate. |
| 3324 | * |
| 3325 | * If you wish to start using V8 isolate in more then one thread |
| 3326 | * you can do this by constructing a v8::Locker object to guard |
| 3327 | * access to the isolate. After the code using V8 has completed |
| 3328 | * for the current thread you can call the destructor. This can |
| 3329 | * be combined with C++ scope-based construction as follows |
| 3330 | * (assumes the default isolate that is used if not specified as |
| 3331 | * a parameter for the Locker): |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3332 | * |
| 3333 | * \code |
| 3334 | * ... |
| 3335 | * { |
| 3336 | * v8::Locker locker; |
| 3337 | * ... |
| 3338 | * // Code using V8 goes here. |
| 3339 | * ... |
| 3340 | * } // Destructor called here |
| 3341 | * \endcode |
| 3342 | * |
| 3343 | * If you wish to stop using V8 in a thread A you can do this by either |
| 3344 | * by destroying the v8::Locker object as above or by constructing a |
| 3345 | * v8::Unlocker object: |
| 3346 | * |
| 3347 | * \code |
| 3348 | * { |
| 3349 | * v8::Unlocker unlocker; |
| 3350 | * ... |
| 3351 | * // Code not using V8 goes here while V8 can run in another thread. |
| 3352 | * ... |
| 3353 | * } // Destructor called here. |
| 3354 | * \endcode |
| 3355 | * |
| 3356 | * The Unlocker object is intended for use in a long-running callback |
| 3357 | * from V8, where you want to release the V8 lock for other threads to |
| 3358 | * use. |
| 3359 | * |
| 3360 | * The v8::Locker is a recursive lock. That is, you can lock more than |
| 3361 | * once in a given thread. This can be useful if you have code that can |
| 3362 | * be called either from code that holds the lock or from code that does |
| 3363 | * not. The Unlocker is not recursive so you can not have several |
| 3364 | * Unlockers on the stack at once, and you can not use an Unlocker in a |
| 3365 | * thread that is not inside a Locker's scope. |
| 3366 | * |
| 3367 | * An unlocker will unlock several lockers if it has to and reinstate |
| 3368 | * the correct depth of locking on its destruction. eg.: |
| 3369 | * |
| 3370 | * \code |
| 3371 | * // V8 not locked. |
| 3372 | * { |
| 3373 | * v8::Locker locker; |
| 3374 | * // V8 locked. |
| 3375 | * { |
| 3376 | * v8::Locker another_locker; |
| 3377 | * // V8 still locked (2 levels). |
| 3378 | * { |
| 3379 | * v8::Unlocker unlocker; |
| 3380 | * // V8 not locked. |
| 3381 | * } |
| 3382 | * // V8 locked again (2 levels). |
| 3383 | * } |
| 3384 | * // V8 still locked (1 level). |
| 3385 | * } |
| 3386 | * // V8 Now no longer locked. |
| 3387 | * \endcode |
| 3388 | */ |
| 3389 | class V8EXPORT Unlocker { |
| 3390 | public: |
| 3391 | Unlocker(); |
| 3392 | ~Unlocker(); |
| 3393 | }; |
| 3394 | |
| 3395 | |
| 3396 | class V8EXPORT Locker { |
| 3397 | public: |
| 3398 | Locker(); |
| 3399 | ~Locker(); |
| 3400 | |
| 3401 | /** |
| 3402 | * Start preemption. |
| 3403 | * |
| 3404 | * When preemption is started, a timer is fired every n milli seconds |
| 3405 | * that will switch between multiple threads that are in contention |
| 3406 | * for the V8 lock. |
| 3407 | */ |
| 3408 | static void StartPreemption(int every_n_ms); |
| 3409 | |
| 3410 | /** |
| 3411 | * Stop preemption. |
| 3412 | */ |
| 3413 | static void StopPreemption(); |
| 3414 | |
| 3415 | /** |
| 3416 | * Returns whether or not the locker is locked by the current thread. |
| 3417 | */ |
| 3418 | static bool IsLocked(); |
| 3419 | |
| 3420 | /** |
| 3421 | * Returns whether v8::Locker is being used by this V8 instance. |
| 3422 | */ |
| 3423 | static bool IsActive() { return active_; } |
| 3424 | |
| 3425 | private: |
| 3426 | bool has_lock_; |
| 3427 | bool top_level_; |
| 3428 | |
| 3429 | static bool active_; |
| 3430 | |
| 3431 | // Disallow copying and assigning. |
| 3432 | Locker(const Locker&); |
| 3433 | void operator=(const Locker&); |
| 3434 | }; |
| 3435 | |
| 3436 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3437 | /** |
| 3438 | * An interface for exporting data from V8, using "push" model. |
| 3439 | */ |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3440 | class V8EXPORT OutputStream { // NOLINT |
| 3441 | public: |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3442 | enum OutputEncoding { |
| 3443 | kAscii = 0 // 7-bit ASCII. |
| 3444 | }; |
| 3445 | enum WriteResult { |
| 3446 | kContinue = 0, |
| 3447 | kAbort = 1 |
| 3448 | }; |
| 3449 | virtual ~OutputStream() {} |
| 3450 | /** Notify about the end of stream. */ |
| 3451 | virtual void EndOfStream() = 0; |
| 3452 | /** Get preferred output chunk size. Called only once. */ |
| 3453 | virtual int GetChunkSize() { return 1024; } |
| 3454 | /** Get preferred output encoding. Called only once. */ |
| 3455 | virtual OutputEncoding GetOutputEncoding() { return kAscii; } |
| 3456 | /** |
| 3457 | * Writes the next chunk of snapshot data into the stream. Writing |
| 3458 | * can be stopped by returning kAbort as function result. EndOfStream |
| 3459 | * will not be called in case writing was aborted. |
| 3460 | */ |
| 3461 | virtual WriteResult WriteAsciiChunk(char* data, int size) = 0; |
| 3462 | }; |
| 3463 | |
| 3464 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3465 | /** |
| 3466 | * An interface for reporting progress and controlling long-running |
| 3467 | * activities. |
| 3468 | */ |
| 3469 | class V8EXPORT ActivityControl { // NOLINT |
| 3470 | public: |
| 3471 | enum ControlOption { |
| 3472 | kContinue = 0, |
| 3473 | kAbort = 1 |
| 3474 | }; |
| 3475 | virtual ~ActivityControl() {} |
| 3476 | /** |
| 3477 | * Notify about current progress. The activity can be stopped by |
| 3478 | * returning kAbort as the callback result. |
| 3479 | */ |
| 3480 | virtual ControlOption ReportProgressValue(int done, int total) = 0; |
| 3481 | }; |
| 3482 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3483 | |
| 3484 | // --- I m p l e m e n t a t i o n --- |
| 3485 | |
| 3486 | |
| 3487 | namespace internal { |
| 3488 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 3489 | static const int kApiPointerSize = sizeof(void*); // NOLINT |
| 3490 | static const int kApiIntSize = sizeof(int); // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3491 | |
| 3492 | // Tag information for HeapObject. |
| 3493 | const int kHeapObjectTag = 1; |
| 3494 | const int kHeapObjectTagSize = 2; |
| 3495 | const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1; |
| 3496 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3497 | // Tag information for Smi. |
| 3498 | const int kSmiTag = 0; |
| 3499 | const int kSmiTagSize = 1; |
| 3500 | const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1; |
| 3501 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3502 | template <size_t ptr_size> struct SmiTagging; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3503 | |
| 3504 | // Smi constants for 32-bit systems. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3505 | template <> struct SmiTagging<4> { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3506 | static const int kSmiShiftSize = 0; |
| 3507 | static const int kSmiValueSize = 31; |
| 3508 | static inline int SmiToInt(internal::Object* value) { |
| 3509 | int shift_bits = kSmiTagSize + kSmiShiftSize; |
| 3510 | // Throw away top 32 bits and shift down (requires >> to be sign extending). |
| 3511 | return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits; |
| 3512 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3513 | |
| 3514 | // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi |
| 3515 | // with a plain reinterpret_cast. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3516 | static const uintptr_t kEncodablePointerMask = 0x1; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3517 | static const int kPointerToSmiShift = 0; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3518 | }; |
| 3519 | |
| 3520 | // Smi constants for 64-bit systems. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3521 | template <> struct SmiTagging<8> { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3522 | static const int kSmiShiftSize = 31; |
| 3523 | static const int kSmiValueSize = 32; |
| 3524 | static inline int SmiToInt(internal::Object* value) { |
| 3525 | int shift_bits = kSmiTagSize + kSmiShiftSize; |
| 3526 | // Shift down and throw away top 32 bits. |
| 3527 | return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits); |
| 3528 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3529 | |
| 3530 | // To maximize the range of pointers that can be encoded |
| 3531 | // in the available 32 bits, we require them to be 8 bytes aligned. |
| 3532 | // This gives 2 ^ (32 + 3) = 32G address space covered. |
| 3533 | // It might be not enough to cover stack allocated objects on some platforms. |
| 3534 | static const int kPointerAlignment = 3; |
| 3535 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3536 | static const uintptr_t kEncodablePointerMask = |
| 3537 | ~(uintptr_t(0xffffffff) << kPointerAlignment); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3538 | |
| 3539 | static const int kPointerToSmiShift = |
| 3540 | kSmiTagSize + kSmiShiftSize - kPointerAlignment; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3541 | }; |
| 3542 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3543 | typedef SmiTagging<kApiPointerSize> PlatformSmiTagging; |
| 3544 | const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize; |
| 3545 | const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3546 | const uintptr_t kEncodablePointerMask = |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3547 | PlatformSmiTagging::kEncodablePointerMask; |
| 3548 | const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3549 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3550 | template <size_t ptr_size> struct InternalConstants; |
| 3551 | |
| 3552 | // Internal constants for 32-bit systems. |
| 3553 | template <> struct InternalConstants<4> { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 3554 | static const int kStringResourceOffset = 3 * kApiPointerSize; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3555 | }; |
| 3556 | |
| 3557 | // Internal constants for 64-bit systems. |
| 3558 | template <> struct InternalConstants<8> { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 3559 | static const int kStringResourceOffset = 3 * kApiPointerSize; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3560 | }; |
| 3561 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3562 | /** |
| 3563 | * This class exports constants and functionality from within v8 that |
| 3564 | * is necessary to implement inline functions in the v8 api. Don't |
| 3565 | * depend on functions and constants defined here. |
| 3566 | */ |
| 3567 | class Internals { |
| 3568 | public: |
| 3569 | |
| 3570 | // These values match non-compiler-dependent values defined within |
| 3571 | // the implementation of v8. |
| 3572 | static const int kHeapObjectMapOffset = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 3573 | static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3574 | static const int kStringResourceOffset = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 3575 | InternalConstants<kApiPointerSize>::kStringResourceOffset; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3576 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 3577 | static const int kProxyProxyOffset = kApiPointerSize; |
| 3578 | static const int kJSObjectHeaderSize = 3 * kApiPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3579 | static const int kFullStringRepresentationMask = 0x07; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3580 | static const int kExternalTwoByteRepresentationTag = 0x02; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3581 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3582 | static const int kJSObjectType = 0xa0; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3583 | static const int kFirstNonstringType = 0x80; |
| 3584 | static const int kProxyType = 0x85; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3585 | |
| 3586 | static inline bool HasHeapObjectTag(internal::Object* value) { |
| 3587 | return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) == |
| 3588 | kHeapObjectTag); |
| 3589 | } |
| 3590 | |
| 3591 | static inline bool HasSmiTag(internal::Object* value) { |
| 3592 | return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag); |
| 3593 | } |
| 3594 | |
| 3595 | static inline int SmiValue(internal::Object* value) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3596 | return PlatformSmiTagging::SmiToInt(value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
| 3599 | static inline int GetInstanceType(internal::Object* obj) { |
| 3600 | typedef internal::Object O; |
| 3601 | O* map = ReadField<O*>(obj, kHeapObjectMapOffset); |
| 3602 | return ReadField<uint8_t>(map, kMapInstanceTypeOffset); |
| 3603 | } |
| 3604 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3605 | static inline void* GetExternalPointerFromSmi(internal::Object* value) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3606 | const uintptr_t address = reinterpret_cast<uintptr_t>(value); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3607 | return reinterpret_cast<void*>(address >> kPointerToSmiShift); |
| 3608 | } |
| 3609 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3610 | static inline void* GetExternalPointer(internal::Object* obj) { |
| 3611 | if (HasSmiTag(obj)) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3612 | return GetExternalPointerFromSmi(obj); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3613 | } else if (GetInstanceType(obj) == kProxyType) { |
| 3614 | return ReadField<void*>(obj, kProxyProxyOffset); |
| 3615 | } else { |
| 3616 | return NULL; |
| 3617 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | static inline bool IsExternalTwoByteString(int instance_type) { |
| 3621 | int representation = (instance_type & kFullStringRepresentationMask); |
| 3622 | return representation == kExternalTwoByteRepresentationTag; |
| 3623 | } |
| 3624 | |
| 3625 | template <typename T> |
| 3626 | static inline T ReadField(Object* ptr, int offset) { |
| 3627 | uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag; |
| 3628 | return *reinterpret_cast<T*>(addr); |
| 3629 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 3630 | |
| 3631 | static inline bool CanCastToHeapObject(void* o) { return false; } |
| 3632 | static inline bool CanCastToHeapObject(Context* o) { return true; } |
| 3633 | static inline bool CanCastToHeapObject(String* o) { return true; } |
| 3634 | static inline bool CanCastToHeapObject(Object* o) { return true; } |
| 3635 | static inline bool CanCastToHeapObject(Message* o) { return true; } |
| 3636 | static inline bool CanCastToHeapObject(StackTrace* o) { return true; } |
| 3637 | static inline bool CanCastToHeapObject(StackFrame* o) { return true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3638 | }; |
| 3639 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3640 | } // namespace internal |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3641 | |
| 3642 | |
| 3643 | template <class T> |
| 3644 | Handle<T>::Handle() : val_(0) { } |
| 3645 | |
| 3646 | |
| 3647 | template <class T> |
| 3648 | Local<T>::Local() : Handle<T>() { } |
| 3649 | |
| 3650 | |
| 3651 | template <class T> |
| 3652 | Local<T> Local<T>::New(Handle<T> that) { |
| 3653 | if (that.IsEmpty()) return Local<T>(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 3654 | T* that_ptr = *that; |
| 3655 | internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr); |
| 3656 | if (internal::Internals::CanCastToHeapObject(that_ptr)) { |
| 3657 | return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle( |
| 3658 | reinterpret_cast<internal::HeapObject*>(*p)))); |
| 3659 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3660 | return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p))); |
| 3661 | } |
| 3662 | |
| 3663 | |
| 3664 | template <class T> |
| 3665 | Persistent<T> Persistent<T>::New(Handle<T> that) { |
| 3666 | if (that.IsEmpty()) return Persistent<T>(); |
| 3667 | internal::Object** p = reinterpret_cast<internal::Object**>(*that); |
| 3668 | return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p))); |
| 3669 | } |
| 3670 | |
| 3671 | |
| 3672 | template <class T> |
| 3673 | bool Persistent<T>::IsNearDeath() const { |
| 3674 | if (this->IsEmpty()) return false; |
| 3675 | return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this)); |
| 3676 | } |
| 3677 | |
| 3678 | |
| 3679 | template <class T> |
| 3680 | bool Persistent<T>::IsWeak() const { |
| 3681 | if (this->IsEmpty()) return false; |
| 3682 | return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this)); |
| 3683 | } |
| 3684 | |
| 3685 | |
| 3686 | template <class T> |
| 3687 | void Persistent<T>::Dispose() { |
| 3688 | if (this->IsEmpty()) return; |
| 3689 | V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this)); |
| 3690 | } |
| 3691 | |
| 3692 | |
| 3693 | template <class T> |
| 3694 | Persistent<T>::Persistent() : Handle<T>() { } |
| 3695 | |
| 3696 | template <class T> |
| 3697 | void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) { |
| 3698 | V8::MakeWeak(reinterpret_cast<internal::Object**>(**this), |
| 3699 | parameters, |
| 3700 | callback); |
| 3701 | } |
| 3702 | |
| 3703 | template <class T> |
| 3704 | void Persistent<T>::ClearWeak() { |
| 3705 | V8::ClearWeak(reinterpret_cast<internal::Object**>(**this)); |
| 3706 | } |
| 3707 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 3708 | template <class T> |
| 3709 | void Persistent<T>::SetWrapperClassId(uint16_t class_id) { |
| 3710 | V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id); |
| 3711 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3712 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3713 | Arguments::Arguments(internal::Object** implicit_args, |
| 3714 | internal::Object** values, int length, |
| 3715 | bool is_construct_call) |
| 3716 | : implicit_args_(implicit_args), |
| 3717 | values_(values), |
| 3718 | length_(length), |
| 3719 | is_construct_call_(is_construct_call) { } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3720 | |
| 3721 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3722 | Local<Value> Arguments::operator[](int i) const { |
| 3723 | if (i < 0 || length_ <= i) return Local<Value>(*Undefined()); |
| 3724 | return Local<Value>(reinterpret_cast<Value*>(values_ - i)); |
| 3725 | } |
| 3726 | |
| 3727 | |
| 3728 | Local<Function> Arguments::Callee() const { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3729 | return Local<Function>(reinterpret_cast<Function*>( |
| 3730 | &implicit_args_[kCalleeIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | |
| 3734 | Local<Object> Arguments::This() const { |
| 3735 | return Local<Object>(reinterpret_cast<Object*>(values_ + 1)); |
| 3736 | } |
| 3737 | |
| 3738 | |
| 3739 | Local<Object> Arguments::Holder() const { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3740 | return Local<Object>(reinterpret_cast<Object*>( |
| 3741 | &implicit_args_[kHolderIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | |
| 3745 | Local<Value> Arguments::Data() const { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3746 | return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | |
| 3750 | bool Arguments::IsConstructCall() const { |
| 3751 | return is_construct_call_; |
| 3752 | } |
| 3753 | |
| 3754 | |
| 3755 | int Arguments::Length() const { |
| 3756 | return length_; |
| 3757 | } |
| 3758 | |
| 3759 | |
| 3760 | template <class T> |
| 3761 | Local<T> HandleScope::Close(Handle<T> value) { |
| 3762 | internal::Object** before = reinterpret_cast<internal::Object**>(*value); |
| 3763 | internal::Object** after = RawClose(before); |
| 3764 | return Local<T>(reinterpret_cast<T*>(after)); |
| 3765 | } |
| 3766 | |
| 3767 | Handle<Value> ScriptOrigin::ResourceName() const { |
| 3768 | return resource_name_; |
| 3769 | } |
| 3770 | |
| 3771 | |
| 3772 | Handle<Integer> ScriptOrigin::ResourceLineOffset() const { |
| 3773 | return resource_line_offset_; |
| 3774 | } |
| 3775 | |
| 3776 | |
| 3777 | Handle<Integer> ScriptOrigin::ResourceColumnOffset() const { |
| 3778 | return resource_column_offset_; |
| 3779 | } |
| 3780 | |
| 3781 | |
| 3782 | Handle<Boolean> Boolean::New(bool value) { |
| 3783 | return value ? True() : False(); |
| 3784 | } |
| 3785 | |
| 3786 | |
| 3787 | void Template::Set(const char* name, v8::Handle<Data> value) { |
| 3788 | Set(v8::String::New(name), value); |
| 3789 | } |
| 3790 | |
| 3791 | |
| 3792 | Local<Value> Object::GetInternalField(int index) { |
| 3793 | #ifndef V8_ENABLE_CHECKS |
| 3794 | Local<Value> quick_result = UncheckedGetInternalField(index); |
| 3795 | if (!quick_result.IsEmpty()) return quick_result; |
| 3796 | #endif |
| 3797 | return CheckedGetInternalField(index); |
| 3798 | } |
| 3799 | |
| 3800 | |
| 3801 | Local<Value> Object::UncheckedGetInternalField(int index) { |
| 3802 | typedef internal::Object O; |
| 3803 | typedef internal::Internals I; |
| 3804 | O* obj = *reinterpret_cast<O**>(this); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3805 | if (I::GetInstanceType(obj) == I::kJSObjectType) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3806 | // If the object is a plain JSObject, which is the common case, |
| 3807 | // we know where to find the internal fields and can return the |
| 3808 | // value directly. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 3809 | int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3810 | O* value = I::ReadField<O*>(obj, offset); |
| 3811 | O** result = HandleScope::CreateHandle(value); |
| 3812 | return Local<Value>(reinterpret_cast<Value*>(result)); |
| 3813 | } else { |
| 3814 | return Local<Value>(); |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | |
| 3819 | void* External::Unwrap(Handle<v8::Value> obj) { |
| 3820 | #ifdef V8_ENABLE_CHECKS |
| 3821 | return FullUnwrap(obj); |
| 3822 | #else |
| 3823 | return QuickUnwrap(obj); |
| 3824 | #endif |
| 3825 | } |
| 3826 | |
| 3827 | |
| 3828 | void* External::QuickUnwrap(Handle<v8::Value> wrapper) { |
| 3829 | typedef internal::Object O; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3830 | O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3831 | return internal::Internals::GetExternalPointer(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | |
| 3835 | void* Object::GetPointerFromInternalField(int index) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3836 | typedef internal::Object O; |
| 3837 | typedef internal::Internals I; |
| 3838 | |
| 3839 | O* obj = *reinterpret_cast<O**>(this); |
| 3840 | |
| 3841 | if (I::GetInstanceType(obj) == I::kJSObjectType) { |
| 3842 | // If the object is a plain JSObject, which is the common case, |
| 3843 | // we know where to find the internal fields and can return the |
| 3844 | // value directly. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 3845 | int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3846 | O* value = I::ReadField<O*>(obj, offset); |
| 3847 | return I::GetExternalPointer(value); |
| 3848 | } |
| 3849 | |
| 3850 | return SlowGetPointerFromInternalField(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | |
| 3854 | String* String::Cast(v8::Value* value) { |
| 3855 | #ifdef V8_ENABLE_CHECKS |
| 3856 | CheckCast(value); |
| 3857 | #endif |
| 3858 | return static_cast<String*>(value); |
| 3859 | } |
| 3860 | |
| 3861 | |
| 3862 | String::ExternalStringResource* String::GetExternalStringResource() const { |
| 3863 | typedef internal::Object O; |
| 3864 | typedef internal::Internals I; |
| 3865 | O* obj = *reinterpret_cast<O**>(const_cast<String*>(this)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3866 | String::ExternalStringResource* result; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3867 | if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3868 | void* value = I::ReadField<void*>(obj, I::kStringResourceOffset); |
| 3869 | result = reinterpret_cast<String::ExternalStringResource*>(value); |
| 3870 | } else { |
| 3871 | result = NULL; |
| 3872 | } |
| 3873 | #ifdef V8_ENABLE_CHECKS |
| 3874 | VerifyExternalStringResource(result); |
| 3875 | #endif |
| 3876 | return result; |
| 3877 | } |
| 3878 | |
| 3879 | |
| 3880 | bool Value::IsString() const { |
| 3881 | #ifdef V8_ENABLE_CHECKS |
| 3882 | return FullIsString(); |
| 3883 | #else |
| 3884 | return QuickIsString(); |
| 3885 | #endif |
| 3886 | } |
| 3887 | |
| 3888 | bool Value::QuickIsString() const { |
| 3889 | typedef internal::Object O; |
| 3890 | typedef internal::Internals I; |
| 3891 | O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this)); |
| 3892 | if (!I::HasHeapObjectTag(obj)) return false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3893 | return (I::GetInstanceType(obj) < I::kFirstNonstringType); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | |
| 3897 | Number* Number::Cast(v8::Value* value) { |
| 3898 | #ifdef V8_ENABLE_CHECKS |
| 3899 | CheckCast(value); |
| 3900 | #endif |
| 3901 | return static_cast<Number*>(value); |
| 3902 | } |
| 3903 | |
| 3904 | |
| 3905 | Integer* Integer::Cast(v8::Value* value) { |
| 3906 | #ifdef V8_ENABLE_CHECKS |
| 3907 | CheckCast(value); |
| 3908 | #endif |
| 3909 | return static_cast<Integer*>(value); |
| 3910 | } |
| 3911 | |
| 3912 | |
| 3913 | Date* Date::Cast(v8::Value* value) { |
| 3914 | #ifdef V8_ENABLE_CHECKS |
| 3915 | CheckCast(value); |
| 3916 | #endif |
| 3917 | return static_cast<Date*>(value); |
| 3918 | } |
| 3919 | |
| 3920 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3921 | RegExp* RegExp::Cast(v8::Value* value) { |
| 3922 | #ifdef V8_ENABLE_CHECKS |
| 3923 | CheckCast(value); |
| 3924 | #endif |
| 3925 | return static_cast<RegExp*>(value); |
| 3926 | } |
| 3927 | |
| 3928 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3929 | Object* Object::Cast(v8::Value* value) { |
| 3930 | #ifdef V8_ENABLE_CHECKS |
| 3931 | CheckCast(value); |
| 3932 | #endif |
| 3933 | return static_cast<Object*>(value); |
| 3934 | } |
| 3935 | |
| 3936 | |
| 3937 | Array* Array::Cast(v8::Value* value) { |
| 3938 | #ifdef V8_ENABLE_CHECKS |
| 3939 | CheckCast(value); |
| 3940 | #endif |
| 3941 | return static_cast<Array*>(value); |
| 3942 | } |
| 3943 | |
| 3944 | |
| 3945 | Function* Function::Cast(v8::Value* value) { |
| 3946 | #ifdef V8_ENABLE_CHECKS |
| 3947 | CheckCast(value); |
| 3948 | #endif |
| 3949 | return static_cast<Function*>(value); |
| 3950 | } |
| 3951 | |
| 3952 | |
| 3953 | External* External::Cast(v8::Value* value) { |
| 3954 | #ifdef V8_ENABLE_CHECKS |
| 3955 | CheckCast(value); |
| 3956 | #endif |
| 3957 | return static_cast<External*>(value); |
| 3958 | } |
| 3959 | |
| 3960 | |
| 3961 | Local<Value> AccessorInfo::Data() const { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3962 | return Local<Value>(reinterpret_cast<Value*>(&args_[-2])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | |
| 3966 | Local<Object> AccessorInfo::This() const { |
| 3967 | return Local<Object>(reinterpret_cast<Object*>(&args_[0])); |
| 3968 | } |
| 3969 | |
| 3970 | |
| 3971 | Local<Object> AccessorInfo::Holder() const { |
| 3972 | return Local<Object>(reinterpret_cast<Object*>(&args_[-1])); |
| 3973 | } |
| 3974 | |
| 3975 | |
| 3976 | /** |
| 3977 | * \example shell.cc |
| 3978 | * A simple shell that takes a list of expressions on the |
| 3979 | * command-line and executes them. |
| 3980 | */ |
| 3981 | |
| 3982 | |
| 3983 | /** |
| 3984 | * \example process.cc |
| 3985 | */ |
| 3986 | |
| 3987 | |
| 3988 | } // namespace v8 |
| 3989 | |
| 3990 | |
| 3991 | #undef V8EXPORT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3992 | #undef TYPE_CHECK |
| 3993 | |
| 3994 | |
| 3995 | #endif // V8_H_ |