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