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